Skip to content

decorators module

Class and function decorators.


attach_fields function

attach_fields(
    *args,
    on_conflict='raise'
)

Class decorator to attach field properties in a Records class.

Will extract dtype and other relevant information from Records.field_config and map its fields as properties. This behavior can be changed by using config.

Note

Make sure to run attach_fields() after override_field_config().

config should contain fields (keys) and dictionaries (values) with the following keys:

  • attach: Whether to attach the field property. Can be provided as a string to be used as a target attribute name. Defaults to True.
  • defaults: Dictionary with default keyword arguments for Records.map_field(). Defaults to an empty dict.
  • attach_filters: Whether to attach filters based on the field's values. Can be provided as a dict to be used instead of the mapping (filter value -> target filter name). Defaults to False. If True, defaults to mapping in Records.field_config.
  • filter_defaults: Dictionary with default keyword arguments for Records.apply_mask(). Can be provided by target filter name. Defaults to an empty dict.
  • on_conflict: Overrides global on_conflict for both field and filter properties.

Any potential attribute name is prepared by placing underscores between capital letters and converting to the lower case.

If an attribute with the same name already exists in the class but the name is not listed in the field config:

  • it will be overridden if on_conflict is 'override'
  • it will be ignored if on_conflict is 'ignore'
  • an error will be raised if on_conflict is 'raise'

override_field_config function

override_field_config(
    *args,
    merge_configs=True
)

Class decorator to override field configs of all base classes in MRO that subclass Records.

Instead of overriding _field_config class attribute, you can pass config directly to this decorator.

Disable merge_configs to not merge, which will effectively disable field inheritance.