Skip to content

Python without ifs and buts - an ORM layer for Python objects, inspired by Django

License

Notifications You must be signed in to change notification settings

JamesDougharty/reobject

Repository files navigation

reobject

Build Status PyPI version PyPI codecov

reobject is an ORM layer for your objects. Simply, add a mixin to your Python class and magically assume the ability to track and query objects at runtime!

Installation

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.
  • Chainable operations on querysets.
  • Transactions.
  • [TBA] Attribute indexes for fast lookups.
  • [TBA] Many-to-one class relationships.

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: The idea is not to provide accurate implementations of various patterns, but to demonstrate what reobject is capable of. Pull requests are most welcome.

Why?

  • Your boss asked you to refactor a codebase with years of accumulated code-vomit.
  • Your code is starting to look like a Christmas tree.
  • You want to implement/invent design patterns without the boilerplate.

Contributing

Want to help? For a start, you can contribute to the project by:

  • Using reobject in your projects, proposing new features, or trying it out just for fun.
  • Sending pull requests with recipes cooked using reobject.
  • Trying your hand at some good first bugs.
  • Improving test coverage, and writing documentation.

I even added some Contributing guidelines to make GitHub happy.

About

Python without ifs and buts - an ORM layer for Python objects, inspired by Django

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.7%
  • Shell 0.3%