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

24. May 2010 Announcements · Code · Code improvement · Design · Python
© Amir Salihefendic