an ORM layer for Python objects
for the Django-obsessed
Software Engineer, Legalstart
February 2018 @ Django-Paris
Follow this presentation here: https://slides.com/onyb/reobject
Dumb Word Processor
class Glyph: def __init__(self, symbol, size, style): self.symbol = symbol self.size = size self.style = style def word_processor(raw_characters): for symbol, size, style in raw_characters: glyph = Glyph(symbol=symbol, size=size, style=style) render(glyph) # Assume this function exists
Flyweight Design Pattern - I
The flyweight design pattern is a way to reuse objects required in large numbers when a simple repeated representation would use an unacceptable amount of memory, and/or perform a very expensive initialization.
Smart Word Processor
using Flyweight design pattern
from reobject.models import Model, Field class Glyph(Model): symbol = Field() size = Field() style = Field() def word_processor(raw_characters): for symbol, size, style in raw_characters: glyph = Glyph.objects.get_or_create( symbol=symbol, size=size, style=style ) render(glyph) # Assume this function exists
from reobject import transactional from reobject.models import Model, Field class Number(Model): value = Field() @transactional def kaboom(self): self.value = '1111' # invalid value self.value += 1 # should rollback num = Number(value=7) try: num.kaboom() except: assert num.value == 7 # Mutation to '1111' rolled back
- Elegant data-model syntax inspired by Django ORM.
- Class-level model fields, out of the box object protocols, pretty reprs.
- Advanced query language and chainable querysets.
- Many-to-one model relationships.
- Swap out attrs with dataclasses in Python 3.7
- Field validation.
- ManyToMany and OneToOne model relationships.
- Attribute indexes for fast lookups.
- Field constraints.
By Anirudha Bose
Premier meetup 2018 @ Django-Paris meetup