how can I pass infinity to redis from python?

佐手、 提交于 2019-12-10 15:29:36

问题


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:


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'])


来源:https://stackoverflow.com/questions/18477045/how-can-i-pass-infinity-to-redis-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!