pyneric.meta

The pyneric.meta module contains helpers for metaclassing.

class pyneric.meta.MetadataBehaviour(metadata_attr='__metadata__', propagate_attr='__propagate__', base_override_attr='__base_overrides__', validate_prefix='validate_', storage_attr='__metadata__', storage_class=<class 'dict'>, metadata_getter='_get_metadata', validate_transforms=False)[source]

Bases: object

The behaviour of a Metaclass.

This behaviour defines how the metadata in a Metaclass is defined and managed. The default behaviour should work for most needs unless there are conflicting attributes or methods. Another reason to use non-default behaviour would be to apply Metaclass to an existing metadata management paradigm.

metadata_attr

The name of a class attribute that may contain metadata.

The attribute may be a sequence of attribute names or a mapping of metadata values keyed by attribute name. The attribute names become identifiers of user metadata, as opposed to internally managed metadata such as propagate_attr and storage_attr.

propagate_attr

The attribute name that contains the propagated attribute names.

This is the name of an internally-managed metadata attribute that can be used to specify which user metadata attributes are also accessible from an instance of the metaclass.

If this is None, then no propagation occurs.

base_override_attr

The attribute name that contains a mapping of overridden values.

This is the name of an internally-managed metadata attribute that can be used to specify user metadata values that should override values obtained from base metaclasses. If the metadata value is specified directly in the derived metaclass, then this has no effect.

If this is None, then no metadata from bases are overridden.

storage_attr

The name of the attribute in which metadata is stored.

This is the name of a metadata behaviour attribute that is used to store the metadata in the metaclass.

This value is required because the metadata must be stored and accessed from somewhere.

storage_class

The class used to contain metadata.

The class specified must be able to accept user metadata arguments via its constructor and provide access to metadata values. If this is not a Mapping, then the metadata values must be accessible as attributes.

This value is required because the metadata must be stored and accessed somehow.

metadata_getter

The name of the class method used to retrieve metadata values.

This applies to both Metaclass instances and classes built with them.

storage_is_mapping

Return whether the storage_class is a Mapping.

define_property_if_not_descriptor(dict, attr)[source]

Define a property in dict if it is not already a data descriptor.

Parameters:
  • dict (dict) – The mapping in which to define the property.
  • attr (str) – The key in the mapping to which to set the property.
get_behavioural_data(cls)[source]

Return a three-tuple containing a class’s behavioural data.

Parameters:cls – The class from which to get the behavioural data.
Returns:Three-tuple whose elements are metadata, propagated attributes, and base-override attributes.
Return type:tuple
get_class_metadata(cls, bases, dict)[source]

Return the metadata defined in a class definition.

Parameters:
  • cls (class) – The class being defined.
  • bases (tuple) – The base classes from the class definition.
  • dict (dict) – The attribute dictionary from the class definition.
Returns:

All (user and internally-managed) metadata from the class (including those inherited from applicable base metaclasses)

Return type:

dict

get_metadata(cls, attr=None)[source]

Return metadata from the given class.

Parameters:
  • cls (class) – The class from which to retrieve metadata.
  • attr (str or None) – The attribute identifying which metadata to return.
Returns:

A shallow copy of all metadata if attr is None; otherwise, the metadata value identified by attr.

prepare_new(cls, bases, dict)[source]

Prepare dict prior to calling __new__().

This sets values in dict that must be set during class creation to manage the metadata properly.

class pyneric.meta.Metaclass[source]

Bases: type

A metaclass for managing metadata.

Metadata management can be customized by specifying a different MetadataBehaviour instance in the __metadata_behaviour__ attribute.

The following applies when default metadata behaviour is used. Metadata may be defined with in the __metadata__ attribute in the metaclass definition, which may be an iterable of attribute names or a mapping of values keys by attribute name. These defined metadata attributes can be set via attributes in the class definition and accessed via class attributes. Metadata may be propagated (as properties) to instances by specifying their attribute names in the __propagate__ attribute when access to metadata from the class’s instances is desired. The __base_overrides__ attribute may be set to a mapping of metadata values keyed by attribute name to automatically set those metadata values in derived metaclasses when it has not defined the metadata value itself.