Coder Social home page Coder Social logo

python-redis-nest's Introduction

"Nested" Redis keys for Python (redis-nest)

Nested, or "Object Oriented" keys for Redis. Inspired by Ruby Nest.

Please visit the project wiki pages for more information.

License

MIT License. See the LICENSE file for details.

Status

  • Tested with Python 2.7 and redis 2.4.9.
  • redis-nest is currently in an early alpha release. There's more work to be done, but I anticipate no changes to the basic model.

Description

(Much of this section is based on the Ruby Nest README file.)

redis-nest is a simple wrapper around the Python's redis package. It allows you to create and operate on namespaced keys when using Redis.

For example, if you are operating on Redis keys like these:

>>> import redis
>>> r = redis.Redis()
>>> r.sadd("event:3:attendees", "Robert")
1
>>> r.sadd("event:3:attendees", "Albert")
1
>>> r.smembers("event:3:attendees")
set(['Robert', 'Albert'])

You can use redis-nest to operate on keys in a more natural and consistent way:

>>> x=Nest('event')
>>> x[3]['attendees'].sadd('Albert')
1
>>> x[3]['attendees'].sadd('Robert')
1
>>> x[3]['attendees'].smembers()
set(['Robert', 'Albert'])

Just for fun, here's the redis-cli MONITOR output for the above snippet:

1337441916.748159 "SADD" "event:3:attendees" "Albert"
1337441916.748857 "SADD" "event:3:attendees" "Robert"
1337441916.749591 "SMEMBERS" "event:3:attendees"

Usage

Redis Nest is modeled after the Ruby Nest library, and it's easy to use. You create a 'namespace' which is used as the base Redis key, then you can add subkeys under that namespace, which are represented as a concatenation of colon-separated subkeys based on the array path.

To create a new namespace named 'foo':

>>> ns = Nest("foo")
>>> ns
'foo'

You can then invoke public Redis instance methods on the namespace:

>>> ns.set(1)
True
>>> ns.get()
'1'

Which is equivalent to the following Redis methods:

>>> import redis
>>> r = redis.Redis()
>>> r.set('foo', 1)
True
>>> r.get('foo')
'1'    

To create a nested sub-key 'bar' inside the namespace 'foo':

>>> ns["bar"]
'foo:bar'

You can nest as deeply as you wish:

>>> ns["bar"]["baz"]["qux"]
'foo:bar:baz:qux'

You can use other objects as a key, as long the object's string representation is a valid Redis key string:

>>> ns["bar"][42]
"foo:bar:42"

NOTE: Slices and Ellipsis list notation are not supported and will result in an exception.

You can specify your own Redis library instance:

import redis
>>> r = redis.Redis(host="192.168.0.101") 
>>> x = redis_nest.Nest("foo", redis=r)

Installation and Requirements

Requires the redis python library.

Requires Python 2.6 or higher. Python 3 is not supported at this time.

For now, just checkout the repo and add it to your PYTHONPATH.
A PyPi installable package is forthcoming.

More to come...

python-redis-nest's People

Contributors

inactivist avatar

Stargazers

 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.