an ORM layer for Python objects
for the Django-obsessed
Anirudha Bose
Software Engineer, Legalstart
https://github.com/onyb
February 2018 @ Django-Paris
Follow this presentation here: https://slides.com/onyb/reobject
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
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.
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