Applications (objetto.applications)

An objetto.applications.Application oversees all objetto.bases.BaseObject objects that are meant to work together. It provides different contexts for managing and keeping track of their changes.

Objects that are part of different applications see each other as regular values and can never be part of the same hierarchy.

An application can have root objects defined by objetto.applications.root(), which are always available at the top of the hierarchy, and cannot be parented under other objects.

Application Class

class objetto.applications.ApplicationMeta(name, bases, dct)

Metaclass for objetto.applications.Application.

Inherits from:
Features:
property _roots

Attributes mapped by name.

Return type

dict[str, objetto.applications.ApplicationRoot]

property _root_names

Names mapped by root.

Return type

dict[objetto.applications.ApplicationRoot, str]

class objetto.applications.Application

Application.

Metaclass:
Inherits from:
Features:

When initializing an objetto.objects.BaseObject, you have to pass an objetto.applications.Application as its first parameter.

>>> from objetto import Application, Object

>>> app = Application()
>>> obj = Object(app)  # pass application as first parameter
>>> obj.app is app  # access it through the 'app' property
True
_get_property(prop, **kwargs)

Get property value.

Parameters
Returns

Value.

Raises

TypeError – Invalid parameter type.

_set_property(prop, value)

Set property value.

Parameters
Raises

TypeError – Invalid parameter type.

_delete_property(prop, force=False)

Delete property value.

Parameters
Raises
read_context(snapshot=None)

Read context.

Parameters

snapshot (objetto.applications.ApplicationSnapshot) – Application state snapshot.

Returns

Context manager.

Return type

contextlib.AbstractContextManager

Raises

ValueError – Application mismatch.

write_context()

Write context.

Returns

Context manager.

Return type

contextlib.AbstractContextManager

temporary_context()

Temporary write context.

Returns

Context manager.

Return type

contextlib.AbstractContextManager

take_snapshot()

Take a snapshot of the current application state.

Returns

Application snapshot.

Return type

objetto.applications.ApplicationSnapshot

property is_writing

Whether this application is inside a write context.

Return type

bool

property is_reading

Whether this application is inside a read context.

Return type

bool

Root Descriptor

objetto.applications.root(obj_type, priority=None, **kwargs)

Describes a root object that gets initialized with the application.

>>> from objetto import Application, Object, attribute, root

>>> class MyObject(Object):
...     name = attribute(str)
...
>>> class MyApplication(Application):  # subclass for adding roots
...     root_a = root(MyObject, name="Root A")  # describe root
...     root_b = root(MyObject, name="Root B")  # describe root
...
>>> app = MyApplication()  # instantiate the application
>>> app.root_a  # access a root object
MyObject(name='Root A')
Parameters
  • obj_type (type[objetto.bases.BaseObject]) – Object type.

  • priority (int or None) – Initialization priority.

  • kwargs – Keyword arguments to be passed to the object’s __init__.

Returns

Application root descriptor.

Return type

objetto.applications.ApplicationRoot

Raises

Root Descriptor Class

class objetto.applications.ApplicationRoot(obj_type, priority=None, **kwargs)

Describes a root object that gets initialized with the application.

Note

Prefer using the objetto.applications.root() factory over instantiating objetto.applications.ApplicationRoot directly.

Inherits from:
Parameters
  • obj_type (type[objetto.bases.BaseObject]) – Object type.

  • priority (int or None) – Initialization priority.

  • kwargs – Keyword arguments to be passed to the object’s __init__.

Raises
__get__(instance, owner)

Get attribute value when accessing from valid instance or this descriptor otherwise.

Parameters
Returns

Object instance or this descriptor.

Return type

objetto.bases.BaseObject or objetto.applications.ApplicationRoot

__hash__()

Get hash based on object id.

Returns

Hash based on object id.

Return type

int

__eq__(other)

Compare with another object for identity.

Parameters

other – Another object.

Returns

True if the same object.

Return type

bool

__repr__()

Get representation.

Returns

Representation.

Return type

str

to_dict()

Convert to dictionary.

Returns

Dictionary.

Return type

dict[str, Any]

property obj_type

Object type.

Return type

type[objetto.bases.BaseObject]

property priority

Initialization priority.

Return type

int or None

property kwargs

Keyword arguments to be passed to object’s ‘__init__’.

Application Property Class

class objetto.applications.ApplicationProperty(default_factory=None, module=None)

Dynamic generic application property.

Inherits from:
Parameters

default_factory (str or collections.abc.Callable or None) – Default value factory.

fabricate_default_value(**kwargs)

Fabricate default value.

Parameters

kwargs – Keyword arguments to be passed to the factory.

Returns

Fabricated value.

Raises

ValueError – No default factory.

property default_factory

Default value factory.

Return type

str or collections.abc.Callable or None

property module

Optional module path to use in case partial paths are provided.

Return type

str or None

Application Snapshot Class

class objetto.applications.ApplicationSnapshot(app, storage)

Application snapshot.

Inherits from:
Features:
  • Freezes entire application state at a moment in time.

  • Can be used with an application’s read context to travel back in time.

You can acquire a snapshot by calling the objetto.applications.Application.take_snapshot() method. You can then pass it to a objetto.applications.Application.read_context() in order to temporarily bring the whole application back to the time the snapshot was taken.

>>> from objetto import Application, Object, attribute

>>> class Person(Object):
...     name = attribute(str)
...
>>> app = Application()
>>> obj = Person(app, name="Albert")
>>> obj.name
'Albert'
>>> snapshot = app.take_snapshot()
>>> obj.name = "Einstein"
>>> obj.name
'Einstein'
>>> with app.read_context(snapshot):
...     obj.name
...
'Albert'
>>> obj.name
'Einstein'
property app

Application.

Return type

objetto.applications.Application