Code Kuno

Problems, solutions, and other stuff I've encountered as a Software Guy

    • Edit
    • Delete
    • Autopost

    Introducing redisco - Python Containers and Simple Models for Redis

    I recently started an open source Python project called redisco. From its README:

    Redisco allows you to store objects in Redis. It is inspired by the 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.

    Why?

    Short answer: I like Redis and I want to code in Python.

    A bit longer answer: I’ve been hacking with Redis and Ohm since last month and I found it lighter than using SQL databases or Mongodb right from its installation to actually using it. It’s data structure approach appeals to me. Sets, lists, dicts, sorted set — using them feels more natural as compared to tables and joins. The Ruby gem Ohm provides a nice interface to Redis. I wanted to have something similar in Python. I looked around, I saw Redish which offers some nice abstractions, but after reading through its code looking specifically for model abstraction, I decided it didn’t have what I wanted. And with so much time in my hands, I rolled out my own. (Another reason for starting this project is that I wanted to learn more about and code more in Python.)

    Simple Models

    Redisco allows you to code model classes like you would in Django or AppEngine.

    class Person(models.Model):
        name = models.Attribute(required=True)
        created_at = models.DateTimeField(auto_now_add=True)
        fave_colors = models.ListField(str)

    Saving an object to Redis automatically creates indexes that are used when querying.

    >>> p = Person(name="Ippo", fave_colors=["Red", "White"])
    >>> p.save()
    True

    Objects can be queried via the Model.objects classmethod:

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

    Redisco has a limited support for queries involving ranges - it can only filter fields that are numeric, i.e. DateField, DateTimeField, IntegerField, and FloatField.

    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)))

    Other limitations

    redisco.models does not offer customizable managers and inheritance.

    Abstractions to Python data structures

    Lists, sets, sorted sets, dicts - this sort of things are easily persisted in Redis. But the main reason they are provided by redisco is that they are dependencies of the Model class. Making them available as well seems reasonable.

    >>> from redisco.containers import List
    >>> l = List('alpha')
    >>> l.append('a')
    >>> l.append('b')
    >>> l.append('c')
    >>> 'a' in l
    True

    Installation

    The package is published in PyPI, so you can

    pip install redisco

    Or, you can clone the git repository:

    git clone http://github.com/iamteem/redisco.git

    Prerequisites: Redis 2.0.0 RC1 and the latest version of redis-py on Github.

    Credits

    Much credit goes to the Ohm team, whose work in Ohm is the primary inspiration and basis of the project.

    Notes

    Because redisco is relatively new, consider it alpha software and not production-ready. I’m currently testing it in a simple web toy that stores tweets from Twitter tracked using the tweetstream and using Hashes to store statistics.

    Invitation

    The project is MIT-licensed so anyone can do anything with it. Use it, fork it on Github, read the source code, and let me know your opinions and reactions. Thanks.

    Tags » opensource python redisco
    • 29 May 2010
    • Views
    • 0 Comments
    • Permalink
    • Favorited 0 Times
    • Tweet

    Comments 0 Comments

    Leave a Comment

  • Tim Medina's Posterous

    I'm a guy with a passion for software development and programming. I like exploring emerging software tools and technologies. I consider Ruby as my primary programming language and Python as my favorite. I'm a co-founder of Sinefunc, Inc. - a Ruby shop here in Manila, Philippines. You can view my Google profile here.

  • About Tim Medina

    I'm a guy with a passion for software development and programming. I like exploring emerging software tools and technologies. I consider Ruby as my primary programming language and Python as my favorite. I'm a co-founder of Sinefunc, Inc. - a Ruby shop here in Manila, Philippines. You can view my Google profile here.

  • Subscribe

    Subscribe to this posterous
    Unsubscribe
    Follow this posterous RSS
  • Follow Me

Theme created for Posterous by Obox