pyrokinetics.factory.Factory#

class pyrokinetics.factory.Factory(super_class=<class 'object'>)[source]#

Bases: object

A variation on the generic ‘factory’ pattern, as defined in the classic ‘Design Patterns’ by the gang of four.

Creates a mapping of keys to types. New types can be ‘registered’ to the factory via a key, and instances of those types can be created by the create method by passing that key. Multiple keys can be registered to the same type. Optionally, the factory can only return types derived from a given super class.

Parameters:

super_class (Type) – Registered classes must be derived from this type.

__init__(super_class=<class 'object'>)[source]#
Parameters:

super_class (Type)

Methods

__init__([super_class])

create(key, *args, **kwargs)

Create a new object of type key, forwarding all arguments.

items()

Dict-like items iterator

register(key, cls)

Register a new type with the factory.

type(key)

Returns type associated with a given key.

create(key, *args, **kwargs)[source]#

Create a new object of type key, forwarding all arguments.

Return type:

Any

Parameters:

key (str)

items()[source]#

Dict-like items iterator

Return type:

Generator[Tuple[str, Type], None, None]

register(key, cls)[source]#

Register a new type with the factory. cls must be derived from the super_class that was passed to the __init__ method.

Return type:

None

Parameters:
type(key)[source]#

Returns type associated with a given key. Raises KeyError if there is no type registered with that key.

Return type:

Type

Parameters:

key (str)