如何从python中将无穷大传递给redis? [英] how can I pass infinity to redis from python?

查看:181
本文介绍了如何从python中将无穷大传递给redis?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用redis-py,并希望将-inf和inf与ZRANGEBYSCORE一起使用.我尝试使用inf的字符串和浮点数来执行此操作,但是这些返回空集.我该怎么办?

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?

编辑

我尝试执行以下命令:

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

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

更新 我的错误是我对zrangebyscore的抽象错误地使用了zrange ... inf的工作原理如下.

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

推荐答案

这是我的代码已通过测试:

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

这篇关于如何从python中将无穷大传递给redis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆