Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A Python Library for Simple Models and Containers Persisted in Redis
Python

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
docs
redisco
tests
.gitignore
LICENSE
README.rst
setup.py

README.rst

Redisco

Python Containers and Simple Models for Redis

Description

Redisco allows you to store objects in Redis. It is inspired by Ruby library Ohm and its design and code are loosely based on Ohm and the Django ORM. It is built on top of redis-py. It includes container classes that allow easier access to Redis sets, lists, and sorted sets.

Installation

Redisco requires latest version of redis-py so get it first.

pip install git+http://github.com/andymccurdy/redis-py.git@master#egg=redis-py

Then install redisco.

pip install git+http://github.com/iamteem/redisco.git@master#egg=redisco

Models

Example

from redisco import models
class Person(models.Model):
    name = models.Attribute(required=True)
    created_at = models.DateTimeField(auto_add=True)
    fave_colors = models.ListField(str)

>>> person = Person(name="Conchita")
>>> person.is_valid()
True
>>> person.save()
True
>>> conchita = Person.objects.filter(name='Conchita')[0]
>>> conchita.name
'Conchita'
>>> conchita.created_at
datetime.datetime(2010, 5, 24, 16, 0, 31, 954704)

Model Attributes

Attribute
Stores unicode strings. If used for large bodies of text, turn indexing of this field off by setting indexed=True.
IntegerField
Stores an int. Ints are stringified using unicode() before saving to Redis.
Counter
An IntegerField that can only be accessed via Model.incr and Model.decr.
DateTimeField
Can store a DateTime object. Saved in the Redis store as a float.
DateFieldDate
Can store a Date object. Saved in Redis as a float.
FloatField
Can store floats.
BooleanField
Can store bools. Saved in Redis as 1's and 0's.
ReferenceField
Can reference other redisco model.
ListField
Can store a list of unicode, int, float, as well as other redisco models.

Attribute Options

Options for creating attributes include required, default, indexed, and validator. DateField and DateTimeField have auto_now_add and auto_now options.

Saving and Validating

Calling Model.is_valid will validate the attributes and lists. Model.is_valid is called when the instance is being saved. When there are invalid fields, Model.errors will hold the list of tuples containing the invalid fields and the reason for its invalidity. E.g. [('name', 'required'),('name', 'too short')]

Fields can be validated using the validator argument of the attribute. Just pass a callable that accepts two arguments -- the field name and the value of the attribute. The callable should return a list of errors.

Model.validate will also be called before saving the instance. Override it to validate instances not related to attributes.

Example

def not_me(field_name, value):
    if value == 'Me':
        return ((field_name, 'it is me'),)

class Person(models.Model):
    name = models.Attribute(required=True, validator=not_me)
    age = models.IntegerField()

    def validate(self):
        if self.age and self.age < 21:
            self._errors.append(('age', 'below 21'))

>>> person = Person(name='Me')
>>> person.is_valid()
False
>>> person.errors
[('name', 'it is me')]

Queries

Queries are executed using a manager, accessed via the objects class attribute.

Examples

Person.objects.all()
Person.objects.filter(name='Conchita')
Person.objects.filter(name='Conchita').first()
Person.objects.all().order('name')
Person.objects.filter(fave_colors='Red')

Ranged Queries

Redisco has a limited support for queries involving ranges -- it can only filter fields that are numeric, i.e. DateField, DateTimeField, IntegerField, and FloatField. The zfilter method of the manager is used for these queries.

Person.objects.zfilter(created_at__lt=datetime(2010, 4, 20, 5, 2, 0))
Person.objects.zfilter(created_at__gte=datetime(2010, 4, 20, 5, 2, 0))
Person.objects.zfilter(created_at__in=(datetime(2010, 4, 20, 5, 2, 0), datetime(2010, 5, 1)))

Containers

Redisco has three containers that roughly match Redis's supported data structures: lists, sets, sorted set. Anything done to the container is persisted to Redis.

Sets

>>> import redis
>>> from redisco.containers import Set
>>> s = Set('myset')
>>> s.add('apple')
>>> s.add('orange')
>>> s.members
set(['orange', 'apple'])
>>> t = Set('nset')
>>> t.add('kiwi')
>>> t.add('guava')
>>> t.members
set(['kiwi', 'guava'])
>>> s.update(t)
>>> s.members
set(['kiwi', 'orange', 'guava', 'apple'])

Lists

>>> import redis
>>> from redisco.containers import List
>>> l = List('alpha')
>>> l.append('a')
>>> l.append('b')
>>> l.append('c')
>>> 'a' in l
True
>>> 'd' in l
False
>>> len(l)
3
>>> l.index('b')
1
>>> l.members
['a', 'b', 'c']

Sorted Sets

>>> zset = SortedSet('zset')
>>> zset.members
['d', 'a', 'b', 'c']
>>> 'e' in zset
False
>>> 'a' in zset
True
>>> zset.rank('d')
0
>>> zset.rank('b')
2
>>> zset[1]
'a'
>>> zset.add('f', 200)
>>> zset.members
['d', 'a', 'b', 'c', 'f']
>>> zset.add('d', 99)
>>> zset.members
['a', 'b', 'c', 'd', 'f']
Something went wrong with that request. Please try again.