Skip to main content

Object-Key Mapper for Redis

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ok-redis-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file ok-redis-0.1.1.tar.gz.

File metadata

  • Download URL: ok-redis-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ok-redis-0.1.1.tar.gz
Algorithm Hash digest
SHA256 85ca6f29cfa083c79677b1ddee9c2c1bddd1fc1e12441cc2322a0dfaff58f188
MD5 acb8df6f1b34ed00aadafb72ce3a86ac
BLAKE2b-256 10516d6ae9efbb326ca74c4a25c837ff1c981961b516d0047955b6ef969746a8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page