hifis_surveyval.models.mixins package

Submodules

hifis_surveyval.models.mixins.mixins module

This module provides mixins for model classes with certain properties.

They are designed to co-operate with other mixins and forwards unused initialization arguments down to other mixins in the inheritance order.

class hifis_surveyval.models.mixins.mixins.HasID(object_id: str, parent_id: Optional[str] = None, *args, **kwargs)[source]

Bases: hifis_surveyval.models.mixins.uses_settings.UsesSettings

This is the abstract superclass for all objects that carry an ID.

The ID is expected to be a string (or be convertible into such and to be unique among all identifiable objects.

IDs are separated by a HIERARCHY_SEPARATOR and the part after the last separator forms the so-called “short ID”. If no hierarchical parent_id is given, the short ID and the full ID are the same.

__init__(object_id: str, parent_id: Optional[str] = None, *args, **kwargs) None[source]

Create a new identifiable object with a given ID.

The class will track all known IDs to prevent duplicates. A full ID is formed by merging the parent’s full ID (if it exists) and the object’s ID.

Args:
object_id:

A string serving as an ID to the object. It must be neither None nor empty.

parent_id:

(Optional, Default=None) The full ID of another identifiable object that forms the hierarchical parent of this one. Used to generate the full ID.

*args:

Will be forwarded to other mixins in the initialization order.

**kwargs:

Will be forwarded to other mixins in the initialization order.

Raises:
ValueError:

Signals either a duplicate or invalid object_id

property full_id: str

Get the full ID of the object (includes parent_id IDs).

Returns:

The string identifying the object with respect to any other HasID

known_ids: Set[str] = {}
property short_id: str

Get the short ID of this object (without parent_id IDs).

Returns:

The string identifying this object with respect to its siblings

class hifis_surveyval.models.mixins.mixins.HasLabel(label: str, *args, **kwargs)[source]

Bases: abc.ABC

This mixin provides a label property.

This is used as a shorthand for objects with more complex descriptions that do not fit nicely in some places (e.g. as labels for graph axis).

YAML_TOKEN = 'label'

The token used in metadata YAML files to identify labels.

__init__(label: str, *args, **kwargs)[source]

Initialize a labelled object.

Args:
label:

The label to be given to the object.

*args:

Will be forwarded to other mixins in the initialization order.

**kwargs:

Will be forwarded to other mixins in the initialization order.

property label: str

Get the current label of the object.

Returns:

The current object label.

relabel(new_label: str) None[source]

Set a new label for this object.

If the new labels string representation is empty, nothing will be changed.

Args:
new_label:

The new label to be used for the object. If required, the input will be cast to string before processing.

class hifis_surveyval.models.mixins.mixins.HasMandatory(is_mandatory: bool, *args, **kwargs)[source]

Bases: abc.ABC

This mixin provides functionality for optional mandatory indicators.

Model elements may require something to be present (e.g. an answer) directly or indirectly as a child of this object.

YAML_TOKEN = 'mandatory'

The token used in metadata YAML files to indicate mandatory-ness.

__init__(is_mandatory: bool, *args, **kwargs)[source]

Initialize an object with mandatory-ness indicator.

Args:
is_mandatory:

Whether this object has mandatory elements or not.

*args:

Will be forwarded to other mixins in the initialization order.

**kwargs:

Will be forwarded to other mixins in the initialization order.

property is_mandatory: bool

Check whether this question is marked as mandatory.

Mandatory questions are expected to be answered by participants.

Returns:

True, if the question was marked as mandatory in the metadata, False otherwise

class hifis_surveyval.models.mixins.mixins.HasText(translations: hifis_surveyval.models.translated.Translated, *args, **kwargs)[source]

Bases: abc.ABC

This mixin provides a text property.

This is used as a more detailed description of the object, e.g. a verbatim question text. These texts may be translated, so when accessing them, providing a language is often required.

YAML_TOKEN = 'text'

The token used in metadata YAML files to identify labels.

__init__(translations: hifis_surveyval.models.translated.Translated, *args, **kwargs) None[source]

Initialize an object with a translatable description.

Args:
translations:

The possible translations of the description.

*args:

Will be forwarded to other mixins in the initialization order.

**kwargs:

Will be forwarded to other mixins in the initialization order.

text(language_code: str) str[source]

Get the description text in a specific language.

Args:
language_code:

The IETF code for the language.

Returns:

The translated description. if available.

Raises:
KeyError:

If no translation for the requested language (with or without region code) can be found.

hifis_surveyval.models.mixins.uses_settings module

This module contains the base class for all objects that carry a unique ID.

IDs are composed of multiple parts interjected by a hierarchy separator.

class hifis_surveyval.models.mixins.uses_settings.UsesSettings(settings: hifis_surveyval.core.settings.Settings, *args, **kwargs)[source]

Bases: abc.ABC

This is a mixin for objects that need access to the settings to function.

It caches a reference to the settings instance.

__init__(settings: hifis_surveyval.core.settings.Settings, *args, **kwargs)[source]

Initialize an object that uses the application settings.

Args:
settings:

The applications settings object.

*args:

Will be forwarded to other mixins in the initialization order.

**kwargs:

Will be forwarded to other mixins in the initialization order.

hifis_surveyval.models.mixins.yaml_constructable module

Provides an abstract class for classes constructed from YAML-Dictionaries.

All of these inheriting classes should define a Schema to dictate the required structure of the YAML to be parsed.

class hifis_surveyval.models.mixins.yaml_constructable.YamlConstructable[source]

Bases: abc.ABC

An abstract class for classes that can be instantiated from a YamlDict.

It defines a catchall schema that accepts everything that looks like a valid YAML mapping.

classmethod from_yaml_dictionary(yaml: Dict[str, Union[str, List[Union[str, Dict[str, Union[str, YamlList, YamlDict]]]], Dict[str, Union[str, List[Union[str, YamlDict]], YamlDict]]]], **kwargs) hifis_surveyval.models.mixins.yaml_constructable.YamlConstructable[source]

Instantiate an object of this class from a given YamlDict.

The given YAML will be validated against the schema defined for the class. If the class did not define a schema on its own (although it should) a catchall schema is provided.

Args:
yaml:

A dictionary as received from the YAML parser containing the data required to create a new instance of the inheriting class

Returns:

A new instance of the overriding subclass

schema: schema.Schema = Schema({<class 'str'>: Or(<class 'str'>, <class 'list'>, <class 'dict'>)})

The default schema of a YamlConstructable validates everything which is formatted like a YamlDictionary

Module contents

Provides mixins used in the data models.