how can I pass infinity to redis from python?

2019-06-23 08:00发布

I'm using redis-py and want to use -inf and inf with ZRANGEBYSCORE. I tried to do this using string and float of inf but those return an empty set. How can I do this?

EDIT

I tried doing the following command:

redis.StrictRedis.ZRANGEBYSCORE("SORTEDSET", "-inf", "inf")  

or

redis.StrictRedis.ZRANGEBYSCORE("SORTEDSET", float("-inf"), float("inf"))

UPDATE My error was that my abstraction for zrangebyscore was using zrange by mistake...inf works as stated below.

1条回答
Summer. ? 凉城
2楼-- · 2019-06-23 08:32

This is my code has been tested:

import unittest

from redis import Redis


class RedisTest(unittest.TestCase):

    def setUp(self):
        self.redis = Redis()

    def test_zrangebyscore(self):
        r = self.redis
        name = 'myset'
        r.zadd(name, 'one', 1)
        r.zadd(name, 'two', 2)
        r.zadd(name, 'three', 3)
        r.zadd(name, 'four', 4)

        self.assertTrue(r.zrangebyscore(name, '-inf', '+inf') == ['one', 'two', 'three', 'four'])
        self.assertTrue(r.zrangebyscore(name, 1, 1) == ['one'])
        self.assertTrue(r.zrangebyscore(name, 1, 2) == ['one', 'two'])
        self.assertTrue(r.zrangebyscore(name, 2, 3) == ['two', 'three'])
        self.assertTrue(r.zrangebyscore(name, '(1', '(2') == [])
        self.assertTrue(r.zrangebyscore(name, '(1', '(3') == ['two'])
查看更多
登录 后发表回答