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

Uploaded Source

File details

Details for the file zerorm-0.2.0.tar.gz.

File metadata

  • Download URL: zerorm-0.2.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for zerorm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 52c7d71abe913fbe854f3d129ef5734320202b23a3eccbe82c80e6d2a7f25afe
MD5 346fa068e5a6b3c66cc7e26d03a17d14
BLAKE2b-256 0ab7c31cec17ed84772c4a08c2e8b601117e1089e2c1446f80e65ea2b256d4d9

See more details on using hashes here.

Supported by

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