reobject

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

Demo time!

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

Transactions

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

Features

  • 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.
     
  • Transactions.
     
  • Many-to-one model relationships.

Next steps

  • Swap out attrs with dataclasses in Python 3.7
     
  • Field validation.
     
  • ManyToMany and OneToOne model relationships.
     
  • Attribute indexes for fast lookups.
     
  • Field constraints.
     
  • Signals.

Thank you!

reobject

By Anirudha Bose

reobject

Premier meetup 2018 @ Django-Paris meetup

  • 199

More from Anirudha Bose