Giter Site home page Giter Site logo

python-jsonrepo's Introduction

jsonrepo

Build Status Coverage Status Maintainability Code Health


Jsonrepo proposes a simple repository system for json serializable objects. It can run on various storage backends such as Redis and DynamoDB.

A record in the repository is uniquely identified by a key and optionally a sort key.

Jsonrepo was initially thought with the purpose of using a date as sort key so that we can build, for example, a repository of messages for users. A record will be then identified by the user id and a datetime. The future will tell us if there is a need to be really more generic or on the contrary to be more specific.

This documentation is very minimalist and a full documentation will be available soon. The testing suite is even more minimalist and it will be improved to cover 100% of features.

Installation

pip install python-jsonrepo

Usage

In process memory

import datetime
from jsonrepo.record import NamedtupleRecord
from jsonrepo.repository import Repository

fields = ['title', 'content']


class Message(namedtuple('Message', fields),
              NamedtupleRecord):
    """
    Example of namedtuple based record
    """
    def __new__(cls, **kwargs):
        default = {f: None for f in fields}
        default.update(kwargs)
        return super(Message, cls).__new__(cls, **default)


class MessagesRepository(Repository):
    klass = Message


# make a singleton for our repository
my_repository = MessagesRepository(backend='dict', prefix='messages')
msg1 = Message(title='Message1',
               content='and this is the content')
msg2 = Message(title='Message2',
               content='and this is the content')
now1 = datetime.datetime.utcnow().isoformat()[:-3]
my_repository.save('user-messages',
                   now1, msg1)
now2 = datetime.datetime.utcnow().isoformat()[:-3]
my_repository.save('user-messages',
                   now2, msg2)
record2 = my_repository.latest('user-messages')
record1 = my_repository.get('user-messages', now1)
records1 = my_repository.history('user-messages')
records2 = my_repository.history('user-messages', _from=now1)
records3 = my_repository.history('user-messages', _to=now2, _desc=False)
now3 = datetime.datetime.utcnow().isoformat()[:-3]
records4 = my_repository.history('user-messages', _from=now1, _to=now3)

Redis

REDIS_HOST, REDIS_PORT and REDIS_DB environment variables will be used to define access to a Redis server.

class MessagesRepository(Repository):
    klass = Message

my_repository = MessagesRepository(backend='redis', prefix='messages')

DynamoDB

Amazon AWS must configured. The prefix value points at a table name on DynamoDB service of Amazon AWS. Names of key and sort_key must configured.

class MessagesRepository(Repository):
    klass = Message
    key = 'key'
    sort_key = 'date'

my_repository = MessagesRepository(backend='dynamodb', prefix='messages')

python-jsonrepo's People

Contributors

romaryd avatar

Watchers

 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.