redis_wrap: Python wrapper for Redis datatypes

redis_wrap implements Redis datatypes so they mimic the datatypes found in Python. The basic idea is to hide the Python Redis API and expose an API that is known for Python programmers.

You can get redis_wrap from PyPi or github:

redis_wrap requires Redis 2.0+ and newest version of redis-py.

redis_wrap should have good perfomance as most operations are lazy and use native Redis API calls, for example:

len(get_list("bears")) # will call redis_client.llen("bears")
'grizzly' in get_hash('bears') # will call redis_client.hexists('bears', 'grizzly')

But is it better?

Let's rewrite some code so it shows the strengths of redis_wrap. The code we will rewrite will be following:

redis_client = redis.Redis()

redis_client.hset('villains', 'riddler', 'Edward Nigma')
assert redis_client.hexists('villains', 'riddler') == True

redis_client.hdel('villains', 'riddler')
assert redis_client.hexists('villains', 'riddler') == False

Using redis_wrap the code will look like this:

villains = redis_wrap.get_hash('villains')

villains['riddler'] = 'Edward Nigma'
assert 'riddler' in villains

del villains['riddler']
assert 'riddler' not in villains

Python programmers should hopefully feel at home, since the wrapper works and looks like a basic Python dictionary...

List example

from redis_wrap import get_list

bears = get_list('bears')
bears.append('grizzly')
assert len(bears) == 1
assert 'grizzly' in bears

Hash example

from redis_wrap import get_hash

villains = get_hash('villains')
assert 'riddler' not in villains

villains['riddler'] = 'Edward Nigma'
assert 'riddler' in villains

assert len(villains.keys()) == 1

del villains['riddler']
assert len(villains) == 0

Set example

from redis_wrap import get_set

fishes = get_set('fishes')
assert 'nemo' not in fishes

fishes.add('nemo')
assert 'nemo' in fishes

for item in fishes:
    assert item == 'nemo'

Related

Announcements · Code · Code improvement · Design · Python 24. May
4 comments so far

Thank you amix! I knew it should be done. ;P

Hey, why not use the collections module to do much of the work? I just added a RedisSet collection to my redis-py fork[1] and was planning on adding more. Feel free to steal if you like it.

[1] http://github.com/japerk/redis...

Jacob:
Was late when I coded the SetFu.__iter__ method ;-)

Amir,

You might also be interested in redish.proxy, which seems to have similar aims to your wrapper, but with more convenience/magic. Set the key, get back a proxy object.

Post a comment
Commenting on this post has expired.
© 2000-2009 amix. Powered by Skeletonz.