Redis对象-键映射器
项目描述
Redis对象-键映射器
如果您已经在Python中使用了redis,您必须处理redis键。有时,很多的redis键。随着键的数量增加,错误很容易发生,尤其是由于键只是字符串。我创建了ok
,这样我就不必为redis键处理字符串。
这是您如何使用它的
import ok
import redis
class User(ok.Key):
fields = ['timeline', 'followers', 'following']
# Get user mixxorz' timeline
r = redis.StrictRedis()
r.zrevrange(User('mixxorz').timeline, 0, 50)
# ZREVRANGE User:mixxorz:timeline 0 50
管理键变得更加稳健。
安装
从PyPI安装它
$ pip install ok-redis
使用
访问字段。
class User(ok.Key):
fields = ['timeline', 'followers', 'following']
print(User('mixxorz').timeline)
# User:mixxorz:timeline
链式键。
class City(ok.Key):
fields = ['tweets_hll']
class Country(ok.Key):
subkeys = [City]
print(Country('PH').City('Manila').tweets_hll)
# Country:PH:City:Manila:tweets
子键可以是键的绝对或相对路径。
# mod_one.py
class Refer(Key):
fields = ['elements']
# mod_two.py
class Parent(Key):
subkeys = ['..mod_one.Refer']
print(Parent('foo').Refer('bar').elements)
# Parent:foo:Refer:bar:elements
Key实例的字符串表示是键,因此您可以像这样使用它
class User(ok.Key):
pass
r.get(User('mixxorz'))
但是您也可以显式访问键。
User('mixxorz').key
# >>> User:mixxorz
ID是可选的。
class User(ok.Key):
fields = ['rankings']
print(User().rankings)
# User:rankings
ID不一定是字符串
class User(ok.Key)
pass
print(User(123))
# User:123
您可以更改用于键的字符串。
class Facebook(ok.Key):
fields = ['all_posts']
class_key = 'fb'
print(Facebook().all_posts)
# fb:all_posts
许可
MIT