SmartObjectFactory

Factory object to create and manipulate SmartObject-based objects.

SmartObjectFactory class

class smartobject.factory.SmartObjectFactory(object_class=None, **kwargs)

Bases: object

Object factory class for Smart Objects

Initialize Smart Object factory

Parameters:
  • object_class – Object class the factory is for
  • autoload – try auto-loading object if not found
  • autocreate – auto create objects if autoload is True but object not found in storage
  • autosave – auto save objects after creation
  • maxsize – max number of objects loaded (factory becomes LRU cache)
add_index(prop)

Add index property

The factory can index objects by additional index props.

Condition using index props:

  • prop should be either read-only or not changed after first set
  • factory index props should be defined before objectas are added to factory

If object property is set after it was added to factory, it can be reindexed with factory.reindex() method

Parameters:

prop – object prop name or list of prop names

Raises:
  • RuntimeError – if factory already contains objects
  • ValueError – if index already defined
append(obj, load=False, save=None, override=False)

Append object to factory

Same as create(obj=obj)

Parameters:
  • obj – append existing Smart Object
  • load – call obj.load() before append
  • save – call obj.save() before append
  • override – allow overriding existing objects
cleanup_storage(storage_id=None, opts={})

Cleanup object storage

Deletes from the specified storage all stored objects, which are not in factory

Parameters:
  • storage_id – storage id to cleanup or None for default storage
  • opts – passed to storage.cleanup() as kwargs
clear()

Remove all objects in factory

create(opts={}, obj=None, load=False, save=None, override=False)

Create new Smart Object in factory

Parameters:
  • obj – append existing Smart Object
  • load – call obj.load() before append
  • save – call obj.save() before append
  • override – allow overriding existing objects
  • opts – sent to object constructor as kwargs
Raises:
  • RuntimeError – if object with such primary key already exists and
  • override is False
delete(obj)

Delete object and remove it from the factory

Parameters:obj – object or object primary key, required
get(key=None, prop=None, storage_id=None, get_all=False, opts={})

Get Smart Object from factory

Parameters:
  • key – object key. If not specified, dict of all objects is returned
  • prop – object prop (should be indexed). If no prop specified, object is looked up by primary key
  • storage_id – if prop is specified, look up for objects in specified storage, if no objects present in factory. storage should support
  • get_all – always lookup for all objects in storage
  • opts – options for object constructor if loaded from storage
Raises:
  • KeyError – if object with such key doesn’t exist (for primary key)
  • FileNotFoundError, LookupError – if auto-load is on but object not found in storage
Returns:

single object is returned. For another prop: list of objects. The list can be empty

Return type:

For primary key

load(pk=None)

Call load method of the specified object

Parameters:pk – object primary key. If not specified, load() method is called for all objects in factory
load_all(storage_id=None, load_opts={}, override=False, opts={})

Load all objects from specified storage

Parameters:
  • storage_id – storage ID
  • load_opts – dict of kwargs, passed to storage.load_all() method
  • override – allow overriding existing objects
  • **kwargs – passed to object constructor as kwargs
purge()

Purge factory object cache

reindex(obj)

Reindex object, stored in factory

Parameters:obj – object or object primary key, required
remove(obj)

Remove object from the factory

Parameters:obj – object or object primary key, required
save(pk=None, force=False)

Call save method of the specified object

Parameters:pk – object primary key. If not specified, save() method is called for all objects in factory
serialize(pk, *args, **kwargs)

Serialize object

set_prop(pk, *args, **kwargs)

Call set_prop method of the specified object

Parameters:pk – object primary key, required. Other arguments are passed to SmartObject.set_prop as-is
sync(pk=None, force=False)

Call sync method of the specified object

Parameters:pk – object primary key. If not specified, save() method is called for all objects in factory
touch(obj)

Mark object accessed

Parameters:obj – object or object primary key, required

Object auto-loading

By primary key

If autoload=True argument is used on factory creation, the factory will try automatically load Smart Objects from the storage when factory.get() method is called and the object doesn’t exist.

To make this working, the following conditions must be met:

  • the object class should accept empty arguments for constructor (primary key will be set by factory)
  • factory.get() method should be called as getting object by primary key.

If allow_empty=False for the object storage, factory.get() method raises LookupError (for RDBMS storages) or FileNotFoundError (for file-based storages) exceptions in case if storage doesn’t have an object with such PK.

By property

SmartObjectFactory also supports loading objects by property as secondary key. To use this feature:

  • Factory autoload option should be set to True

  • For get method, prop argument should be specified

  • You may also specify storage ID to load missing objects from, if not specified - default storage is used

  • Objects are auto-loaded from the storage in two cases:

    • when object is missing in factory
    • when get_all=True is specified for factory.get() method.

Note

Loading objects by prop is supported only for RDBMS storages

Object cache size limit

By default, SmartObjectFactory stores all created/appended objects in internal object list.

If you limit factory object size list with constructor param maxsize, it will start working as LRU cache: when the new object is created or appended, the factory will delete the least recently accessed objects to make sure the list size is below or equal to maximum.

Note, that when SmartObjectFactory works as LRU cache, you are unable to use factory.cleanup_storage() method (it will raise RuntimeError exception).

Usually, to make object cache work properly, object auto-loading feature should be also enabled.