If your Python code is starting to look like a Christmas tree (damn you indents!), and you care about testability, ReObject can help you! It provides a mixin which your classes can inherit and assume the ability to track and query its objects at runtime!
Code written using ReObject, is significantly easier to maintain and reason about. It encourages you to encapsulate related logic in classes, just like database models. To get a quick introduction to the library, read the Example section below.
Warning: This is an alpha software, and the API is subject to change without notice.
Inherit from the Model class.
from reobject.model import Model
class Book(Model):
def __init__(self, title, authors, price):
self.title = title
self.authors = author
self.price = priceCreate a bunch of objects, but instead of Book(...) use the Book.objects.create(...) syntax.
>>> Book.objects.create(title='The C Programming Language', authors=['Kernighan', 'Ritchie'], price=52)
>>> Book.objects.create(title='The Go Programming Language', authors=['Donovan', 'Kernighan'], price=30)
>>> Book.objects.create(title='The AWK Programming Language', authors=['Aho', 'Kernighan'], price=127)Get all books
>>> Book.objects.all()
[<Book: 140707840041088>, <Book: 140707840125584>, <Book: 140707840083056>]Get the K&R book
>>> Book.objects.get(title='The C Programming Language')
<Book: 140707840083056>Get the titles of all books which cost less than 100 USD, sorted by price.
>>> Book.objects.filter(price__lte=100).order_by('price').values('title')
[{'title': 'The Go Programming Language'}, {'title': 'The C Programming Language'}]Get titles of all 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', 'The AWK Programming Language']Want to help? Please review the contributing guidelines, and take a look at some good first bugs!