Giter Club home page Giter Club logo

ok-redis's Introduction

ok Latest version

Build status Python versions Monthly downloads Software license

Object-Key Mapper for Redis

If you’ve used redis on python, you’ve had to deal with redis keys. Sometimes, lots of redis keys. With so many keys, it’s easy to make mistakes, especially since keys are just strings. I built ok so that I didn’t have to work with strings for redis keys.

Here’s how you use it:

import ok
import redis


class User(ok.Key):
    fields = ['timeline', 'followers', 'following']


# Get user mixxorz' timeline
r = redis.StrictRedis()
r.zrevrange(User('mixxorz').timeline, 0, 50)
# ZREVRANGE User:mixxorz:timeline 0 50

Managing your keys just became a lot less fragile.

Installation

Install it from pypi

$ pip install ok-redis

Usage

Access fields.

class User(ok.Key):
    fields = ['timeline', 'followers', 'following']


print(User('mixxorz').timeline)
# User:mixxorz:timeline

Chain keys.

class City(ok.Key):
    fields = ['tweets_hll']


class Country(ok.Key):
    subkeys = [City]


print(Country('PH').City('Manila').tweets_hll)
# Country:PH:City:Manila:tweets

Subkeys can be an absolute or relative path to a key.

# mod_one.py
class Refer(Key):
    fields = ['elements']


# mod_two.py
class Parent(Key):
    subkeys = ['..mod_one.Refer']


print(Parent('foo').Refer('bar').elements)
# Parent:foo:Refer:bar:elements

The string representation of a Key instance is the key, so you can use it like this:

class User(ok.Key):
    pass


r.get(User('mixxorz'))

But you can also access the key explicitly.

User('mixxorz').key
# >>> User:mixxorz

IDs are optional.

class User(ok.Key):
    fields = ['rankings']


print(User().rankings)
# User:rankings

IDs don't have to be strings

class User(ok.Key)
    pass

print(User(123))
# User:123

You can change the string used for the key.

class Facebook(ok.Key):
    fields = ['all_posts']
    class_key = 'fb'


print(Facebook().all_posts)
# fb:all_posts

License

MIT

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.