Code Monkey home page Code Monkey logo

redis-python-datastructures's Introduction

If you'd like to be a contributor, send me a note! I'd be more than glad to pass this over to a more active maintainer.

This package exposes Redis backed datastructures which bring together standard Python syntax with persistent in-memory storage. Redis operations are all atomic as well, meaning that a thoughtful approach can use them concurrently on multiple machines.

Utimately, a trivial wrapper around Redis-Py.

Simple Installation

The simplest approach to installation is:

pip install redis
pip install -e git+https://github.com/lethain/Redis-Python-Datastructures.git#egg=redis_ds

Installation for development

For development, you should checkout the repository, and install it into a virtualenv:

# get the code
git clone https://github.com/lethain/Redis-Python-Datastructures.git
cd Redis-Python-Datastructures.git

# create and activate a virtualenv if you don't have one already
virtualenv env
. ./activate

# install it
pip install -r requirements.txt
python setup.py develop

Running tests

With some embarassment, the tests currently run against local Redis using keys prefixed with "test_rds.", and taking great pains not to delete any other keys, so if you have a toy Redis node, the tests will not cause any harm, but you really shouldn't run the tests against a Redis node you care deeply about.

Run them via:

python src/redis_ds/tests.py

The tests really ought to be run against a mocked out version of Redis, but that work hasn't been done yet.

Usage

This section covers how to use this library to interface with Redis.

Dictionary via Redis Strings

Using the entire Redis cluster as a dictionary:

>>> from redis_ds.redis_dict import RedisDict
>>> x = RedisDict()
>>> x
{}
>>> x['a'] = 100
>>> x
{'a': '100'}
>>> x['a']
'100'
>>> x['b']
>>> len(x)
1

Dictionaries via Redis Hashes

Using Redis hashes we can store multiple dictionaries in one Redis server.

>>> from redis_ds.redis_hash_dict import RedisHashDict
>>> x = RedisHashDict("some_hash_key")
>>> x
{}
>>> x['a'] = 100
>>> x
{'a': '100'}
>>> x['a']
'100'
>>> x['b']
>>> len(x)
1

Lists via Redis Lists

We also have a kind-of-sort-off implementation of a list which certainly doesn't have the full flexibility of a Python list, but is persistent, synchronized and sharable.

>>> from redis_ds.redis_list import RedisList
>>> x = RedisList("my-list")
>>> x
RedisList([])
>>> x.append("a")
1
>>> x.append("b")
2
>>> x
RedisList(['a', 'b'])
>>> x[0]
'a'
>>> x.pop()
'b'
>>> x
RedisList(['a'])

It also provides access to blocking versions of pop, which with a little creativity you can use to create a message queue with workers.

>>> x.pop(blocking=True)
'a'

Woohoo.

Sets

Sets are also available thanks to work by @hhuuggoo:

>>> from redis_ds.redis_set import RedisSet
>>> x = RedisSet()
>>> x.add("a")
>>> x.add("a")
>>> x.add("b")
>>> x.add("b")
>>> len(x)
2
>>> 'a' in x
True
>>> 'c' in x
False
>>> x.pop()
'a'
>>> len(x)
1

Serializing Values Stored in Redis

Thanks to work by @hhuuggoo, this library also supports serializing values before storing them in Redis. Each class has a serialized equivalent, for example the above hashmap example becomes:

>>> from redis_ds.redis_hash_dict import PickleRedisHashDict
>>> y = PickleRedisHashDict('some_other_key')
>>> y
{}
>>> y['a'] = {'obj': 'ect'}
>>> y
{'a': {'obj': 'ect'}}
>>> y['a']['obj']
'ect'

The same can be done using JSON instead of Pickle by changing it to:

>>> from redis_ds.redis_hash_dict import JSONRedisHashDict

and so on. The same is true for RedisList which has PickleRedisList and JSONRedisList, and so on.

redis-python-datastructures's People

Contributors

lethain avatar lysol avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.