Python without ifs and buts


Keywords
orm, python, object-oriented-programming, django, design-patterns, django-orm, models
License
Apache-2.0
Install
pip install reobject==0.9a7

Documentation

reobject

Build Status PyPI version PyPI codecov

reobject is an ORM layer for your objects. It allows you to track and query objects at runtime using a familiar query langauge inspired by Django ORM.

Note: reobject is NOT a database ORM. It keeps track of regular objects in the memory.

This is highly experimental code, and not safe for production.

Installation

reobject supports Python 3 only.

pip install reobject

Example usage

from reobject.models import Model, Field

class Book(Model):
    title = Field()
    authors = Field()
    price = Field()

>>> # Create a bunch of objects
>>> Book(title='The C Programming Language', authors=['Kernighan', 'Ritchie'], price=52)
>>> Book(title='The Go Programming Language', authors=['Donovan', 'Kernighan'], price=30)

>>> Book.objects.all()  # All books
[Book(title='The C Programming Language', authors=['Kernighan', 'Ritchie'], price=52),
 Book(title='The Go Programming Language', authors=['Donovan', 'Kernighan'], price=30)]

>>> Book.objects.filter(price__lt=50).values('title')  # Titles of books priced under $50
[{'title': 'The Go Programming Language'}, {'title': 'The C Programming Language'}]

>>> # Titles of books co-authored by Brian Kernighan
>>> Book.objects.filter(authors__contains='Kernighan').values_list('title', flat=True)
['The Go Programming Language', 'The C Programming Language']

Features

  • Elegant data-model syntax inspired by Django ORM.
  • Class-level model fields, out of the box object protocols, pretty reprs; powered by attrs.
  • Advanced query language and chainable querysets. Read the QuerySet API docs.
  • Transactions. See example.
  • Many-to-one model relationships. See example
  • [TBA] Attribute indexes for fast lookups.

Crunching Design Patterns

Pattern Description Pure Python reobject
Flyweight Reuse existing instances of objects with identical state Link Link
Memento Transactional rollback of an object to a previous state in case of an exception Link Link
Prototype Create clones of a prototype without instantiation Link Link
Singleton Restrict a class to provide only a single instance Link Link
Facade Encapsulate a complex subsystem within a single interface object Link Link
Flux Event-driven state management inspired by Facebook Flux Link Link

Note: Some of the examples above may be inaccurate. The idea is to demonstrate what reobject is capable of. Pull requests are most welcome.

Contributing

Want to help? You can contribute to the project by:

  • Using reobject in your projects, finding bugs, and proposing new features.
  • Sending pull requests with recipes built using reobject.
  • Trying your hand at some good first bugs.
  • Improving test coverage, and writing documentation.