objectfactory
objectfactory is a python package to easily implement the factory design pattern for object creation, serialization, and polymorphism
objectfactory.base
base module
implements abstract base classes for objectfactory
- class objectfactory.base.FieldABC(default=None, key=None, required=False, allow_none=True)
Bases:
ABCabstract base class for serializable field
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- abstract marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.base.SerializableABC
Bases:
ABCabstract base class for serializable object
- abstract deserialize(body: dict)
deserialize model from dictionary
- Parameters:
body – serialized data to load into object
- abstract serialize(include_type: bool = True, use_full_type: bool = True) dict
serialize model to dictionary
- Parameters:
include_type – if true, type information will be included in body
use_full_type – if true, the fully qualified path with be specified in body
- Returns:
serialized object as dict
objectfactory.factory
factory module
implements serializable object factory
- class objectfactory.factory.Factory(name)
Bases:
objectfactory class for registering and creating serializable objects
- create(body: dict, object_type: ~typing.Type[~objectfactory.factory.T] = <class 'objectfactory.serializable.Serializable'>) T
create object from dictionary
- Parameters:
body – serialized object data
object_type – (optional) specified object type
- Raises:
TypeError – if the object is not an instance of the specified type
- Returns:
deserialized object of specified type
- register(serializable: Serializable)
decorator to register class with factory
- Parameters:
serializable – serializable object class
- Returns:
registered class
- objectfactory.factory.create(body: dict, object_type: ~typing.Type[~objectfactory.factory.T] = <class 'objectfactory.serializable.Serializable'>) T
create object from dictionary with the global factory
- Parameters:
body – serialized object data
object_type – (optional) specified object type
- Raises:
TypeError – if the object is not an instance of the specified type
- Returns:
deserialized object of specified type
- objectfactory.factory.register(serializable: Serializable)
decorator to register class with the global factory
- Parameters:
serializable – serializable object class
- Returns:
registered class
objectfactory.field
field module
implements serializable fields
- class objectfactory.field.Boolean(default=None, key=None, required=False, allow_none=True)
Bases:
Fieldserializable field for boolean data
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.field.DateTime(default=None, key=None, date_format=None, required=False, allow_none=True)
Bases:
Fieldserializable field for datetime data
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
date_format – date format to use (defaults to iso)
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.field.Enum(enum, default=None, key=None, required=False, allow_none=True, by_value=False)
Bases:
Fieldserializable field for enumerated data
- Parameters:
enum – enum type for field validation
default – default value for field if unset
key – dictionary key to use for field serialization
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
by_value – whether to serialize by value or by symbol
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.field.Field(default=None, key=None, required=False, allow_none=True)
Bases:
FieldABCbase class for serializable field
this is a class level descriptor for abstracting access to fields of serializable objects
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.field.Float(default=None, key=None, required=False, allow_none=True)
Bases:
Fieldserializable field for float data
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.field.Integer(default=None, key=None, required=False, allow_none=True)
Bases:
Fieldserializable field for integer data
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.field.List(default=None, key=None, field_type=None, required=False, allow_none=True)
Bases:
Fieldfield type for list of serializable objects
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
field_type – specified type for list of nested objects
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.field.Nested(default=None, key=None, field_type=None, required=False, allow_none=True)
Bases:
Fieldfield type for nested serializable object
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
field_type – specified type for nested object
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
- class objectfactory.field.String(default=None, key=None, required=False, allow_none=True)
Bases:
Fieldserializable field for string data
- Parameters:
default – default value for field if unset
key – dictionary key to use for field serialization
required – whether this field is required to deserialize an object
allow_none – whether null should be considered a valid value
- marshmallow()
create generic marshmallow field to do actual serialization
- Returns:
associated marshmallow field
objectfactory.serializable
serializable module
implements base class and metaclass for serializable objects
- class objectfactory.serializable.Meta(name, bases, attributes, schema=None)
Bases:
ABCMetametaclass for serializable classes
this is a metaclass to be used for collecting relevant field information when defining a new serializable class
define a new serializable object class, collect and register all field descriptors, construct marshmallow schema
- Parameters:
name – class name
bases – list of base classes to inherit from
attributes – dictionary of class attributes
schema – (optional) predefined marshmallow schema
- Returns:
newly defined class
- class objectfactory.serializable.Serializable
Bases:
SerializableABCbase class for serializable objects
- deserialize(body: dict)
deserialize model from dictionary
- Parameters:
body – serialized data to load into object
- classmethod from_dict(body: dict)
constructor to set data with dictionary
- Parameters:
body – dictionary
- Returns:
new instance of serializable object
- classmethod from_kwargs(**kwargs)
constructor to set field data by keyword args
- Parameters:
kwargs – keyword arguments by field
- Returns:
new instance of serializable object
- serialize(include_type: bool = True, use_full_type: bool = True) dict
serialize model to dictionary
- Parameters:
include_type – if true, type information will be included in body
use_full_type – if true, the fully qualified path with be specified in body
- Returns:
serialized object as dict