API Autodocs

Core Functions

prefab_classes.prefab(cls=None, *, init=True, repr=True, eq=True, iter=False, match_args=True, kw_only=False, frozen=False)

Generate boilerplate code for dunder methods in a class.

Use as a decorator.

Parameters:
  • cls – Class to convert to a prefab

  • init – generates __init__ if true or __prefab_init__ if false

  • repr – generate __repr__

  • eq – generate __eq__

  • iter – generate __iter__

  • match_args – generate __match_args__

  • kw_only – make all attributes keyword only

  • frozen – Prevent attribute values from being changed once defined (This does not prevent the modification of mutable attributes such as lists)

Returns:

class with __ methods defined

prefab_classes.attribute(*, default=<NOTHING Sentinel Object>, default_factory=<NOTHING Sentinel Object>, init=True, repr=True, compare=True, kw_only=False, exclude_field=False, doc=None, type=<NOTHING Sentinel Object>)

Additional definition for how to generate standard methods for an instance attribute.

Parameters:
  • default – Default value for this attribute

  • default_factory – 0 argument callable to give a default value (for otherwise mutable defaults, eg: list)

  • init – Include this attribute in the __init__ parameters

  • repr – Include this attribute in the class __repr__

  • compare – Include this attribute in the class __eq__

  • kw_only – Make this argument keyword only in init

  • exclude_field – Exclude this field from all magic method generation apart from __init__ and do not include it in PREFAB_FIELDS

  • doc – Parameter documentation for slotted classes

  • type – Type of this attribute (for slotted classes)

Returns:

Attribute generated with these parameters.

prefab_classes.build_prefab(class_name, attributes, *, bases=(), class_dict=None, init=True, repr=True, eq=True, iter=False, match_args=True, kw_only=False, frozen=False)

Dynamically construct a (dynamic) prefab.

Parameters:
  • class_name (str) – name of the resulting prefab class

  • attributes (list[tuple[str, Attribute]]) – list of (name, attribute()) pairs to assign to the class for construction

  • bases (tuple[type, ...]) – Base classes to inherit from

  • class_dict (None | dict[str, object]) – Other values to add to the class dictionary on creation This is the ‘dict’ parameter from ‘type’

  • init – generates __init__ if true or __prefab_init__ if false

  • repr – generate __repr__

  • eq – generate __eq__

  • iter – generate __iter__

  • match_args – generate __match_args__

  • kw_only – make all attributes keyword only

  • frozen – Prevent attribute values from being changed once defined (This does not prevent the modification of mutable attributes such as lists)

Returns:

class with __ methods defined

Helper functions

prefab_classes.funcs.is_prefab(o)

Identifier function, return True if an object is a prefab class or if it is an instance of a prefab class.

The check works by looking for a PREFAB_FIELDS attribute.

Parameters:

o – object for comparison

Returns:

True/False

prefab_classes.funcs.is_prefab_instance(o)

Identifier function, return True if an object is an instance of a prefab class.

The check works by looking for a PREFAB_FIELDS attribute.

Parameters:

o – object for comparison

Returns:

True/False

prefab_classes.funcs.as_dict(inst, *, excludes=None)

Represent the prefab as a dictionary of attribute names stuband values. Exclude any keys listed in excludes

This does not recurse.

Parameters:
  • inst – instance of prefab class

  • excludes (None | tuple[str, ...]) – tuple of field names to exclude from the resulting dict

Returns:

dictionary {attribute_name: attribute_value, …}

Return type:

dict[str, object]

prefab_classes.funcs.to_json(inst, *, excludes=None, dumps_func=None, **kwargs)

Output the instance attributes as JSON.

If no dumps function is given and no kwargs are used a basic encoder will be reused.

Parameters:
  • inst – instance of prefab class

  • excludes (None | tuple[str, ...]) – tuple of attribute names to exclude from json caching used internally requires this to be a tuple and not a list note that these attribute names will be excluded from all prefabs encountered during serialization

  • dumps_func (None | Callable[[...], str]) – function equivalent to stdlib’s json.dumps making it easier to use third party json libraries

  • kwargs – keyword arguments passed directly to dumps_func

Returns:

string of JSON data from the class attributes

Return type:

str