Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I pass infinity to redis from python?

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.

like image 570
postelrich Avatar asked Aug 27 '13 23:08

postelrich


People also ask

How do I know if Redis is connected in Python?

If you want to test redis connection once at startup, use the ping() command. The command ping() checks the connection and if invalid will raise an exception.


1 Answers

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'])
like image 112
zihen Avatar answered Oct 05 '22 14:10

zihen