Skip to main content

Looks like ORM and stores your data

Project description

Zerorm is a simple wrapper for three amazing packages. This repository is the place where TinyDB, Schematics and Lifter together look like Django ORM.

It’s still work in progress and not everything looks like Django ORM, but it will.

Installation

pip install zerorm

Usage

First create a file with models and database instance attached to every model:

from zerorm import db, models

database = db('db.json')


class Message(models.Model):
    author = models.StringType(required=True)
    author_email = models.EmailType()
    text = models.StringType()
    views = models.IntType(min_value=0)

    class Meta:
        database = database

Now create some objects:

>>> from models import Message
>>>
>>> bob_message = Message(author='Bob',
...                       author_email='bob@example.com',
...                       text='Hello, everyone!')
>>> bob_message
<Message: Message object>
>>> bob_message.save()  # Save object
1
>>>
>>> bob_message.views = 3
>>> bob_message.save()  # Update object
>>>
>>> alice_message = Message.objects.create(author='Alice',
...                                        text='Hi, Bob!',
...                                        views=0)
>>> alice_message
<Message: Message object>

And try to retrieve them via objects

>>> Message.objects.all()
<QuerySet, len() = 2>
>>> list(Message.objects.all())
[<Message: Message object>, <Message: Message object>]
>>>
>>> second_message = Message.objects.get(eid=2)
>>> second_message.author
'Alice'
>>>
>>> Message.objects.filter(views__gte=3)  # Only Bob's message has 3 views
<QuerySet, len() = 1>
>>> list(Message.objects.filter(views__gte=3))
[<Message: Message object>]

You can also redefine model’s __str__ method for better repr just like in Django.

class Message(models.Model):
    ...

    def __str__(self):
        return 'by {}'.format(self.author)
>>> list(Message.objects.all())
[<Message: by Bob>, <Message: by Alice>]

License

MIT. See LICENSE for details.

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

zerorm-0.2.0.tar.gz (3.4 kB view hashes)

Uploaded Source

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