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:
Check and store root descriptors.
- property _roots
Attributes mapped by name.
- Return type
- property _root_names
Names mapped by root.
- Return type
- class objetto.applications.Application
Application.
- Metaclass:
- Inherits from:
- Features:
Manages multiple
objetto.bases.BaseObjectunder the same hierarchy.Offers contexts for reading/writing/batch.
Reverts changes when an error occurs.
Manages
objetto.objects.Actionpropagation, internally and externally.
When initializing an
objetto.objects.BaseObject, you have to pass anobjetto.applications.Applicationas 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
prop (objetto.applications.ApplicationProperty) – Application property.
kwargs – Keyword arguments to be passed to the default factory.
- Returns
Value.
- Raises
TypeError – Invalid parameter type.
- _set_property(prop, value)
Set property value.
- Parameters
prop (objetto.applications.ApplicationProperty) – Application property.
value – Value.
- Raises
TypeError – Invalid parameter type.
- _delete_property(prop, force=False)
Delete property value.
- Parameters
prop (objetto.applications.ApplicationProperty) – Application property.
force (bool) – If True, will not fail if has no value set.
- Raises
TypeError – Invalid parameter type.
ValueError – Property has no value.
- read_context(snapshot=None)
Read context.
- Parameters
snapshot (objetto.applications.ApplicationSnapshot) – Application state snapshot.
- Returns
Context manager.
- Return type
- Raises
ValueError – Application mismatch.
- write_context()
Write context.
- Returns
Context manager.
- Return type
- temporary_context()
Temporary write context.
- Returns
Context manager.
- Return type
- take_snapshot()
Take a snapshot of the current application state.
- Returns
Application snapshot.
- Return type
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
- Raises
ValueError – Used reserved keyword argument.
TypeError – Invalid object type.
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 instantiatingobjetto.applications.ApplicationRootdirectly.- 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
ValueError – Used reserved keyword argument.
TypeError – Invalid object type.
- __get__(instance, owner)
Get attribute value when accessing from valid instance or this descriptor otherwise.
- Parameters
instance (objetto.applications.Application or None) – Instance.
owner (type[objetto.applications.Application]) – Owner class.
- Returns
Object instance or this descriptor.
- Return type
objetto.bases.BaseObject or objetto.applications.ApplicationRoot
- __eq__(other)
Compare with another object for identity.
- Parameters
other – Another object.
- Returns
True if the same object.
- Return type
- property obj_type
Object type.
- Return type
- 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
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 aobjetto.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