SmartObject base class

Base class, which should be inherited by your objects.

class smartobject.smartobject.SmartObject

Bases: object

Smart Object implementation class

after_load(opts={}, **kwargs)

Called after load method


Is object alive


Apply loaded property map

Can be called only once, otherwise raises RuntimeError


Delete object


Is object deleted or not

load(opts={}, **kwargs)

Load object data from the storage

Calls self.after_load() method after loading

Parameters:opts – passed to storage.load() as kwargs
load_property_map(property_map=None, override=False)

Load Smart Object property map

  • property_map – property map to load. Can be dict (used as-is), file path or empty (class name + .yml is used for the file name)
  • override
prepare_value(prop, value)

Prepare value before setting it to object property


Save object data to storage

Parameters:force – force save even if object is not modified
serialize(mode=None, allow_deleted=False)

Serialize object

  • mode – serialization mode. if not specified, all object properties are serialized
  • allow_deleted – allow serialization of the deleted object
serialize_prop(prop, target=None)

Serialize object property

If method “serialize_{prop}” is defined in class, returns its value
  • prop – object property to serialize
  • target – smartobject.SERIALIZE_SAVE or SERIALIZE_SYNC
set_prop(prop=None, value=None, save=False, sync=True, _allow_readonly=False)

Set object property by prop/value

To set multiple properties at once, specify value as dict

  • prop – object property prop
  • value – object property value
  • save – auto-save object if properties were modified
  • sync – sync object if properties were modified

True if property is set, False if unchanged

  • AttributeError – if no such property or property is read-only
  • ValueError – if property value is invalid or no prop specified
  • TypeError – if object is deleted

Create snapshot of object properties

Snapshot is also saved to internal object variable

Returns:snapshot dict

Restore objct properties from the snapshot

Parameters:snapshot – snapshot dict, if not defined, internal object variable is used
Raises:ValueError – no snapshot data found

Get property value from the storage

May be used in custom getters/setters for the external properties

storage_set(prop, value)

Save property value to the storage

May be used in custom getters/setters for the external properties


Sync object data with synchroizer

Parameters:force – force sync even if object is not modified