mixins

Mixin classes that are to be used alongside specific models to use composition for functionality and inheritance for semantics.

class mio.models.mixins.ConfigYAMLMixin(*, id: Annotated[str, _PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')], mio_model: Annotated[str, AfterValidator(func=_is_identifier)] = None, mio_version: str = '0.10.1.dev71+g93c49e3')

Yaml Mixin class that always puts a header consisting of

  • id - unique identifier for this config

  • mio_model - fully-qualified module path to model class

  • mio_version - version of mio when this model was created

at the top of the file.

HEADER_FIELDS: ClassVar[tuple[str]] = ('id', 'mio_model', 'mio_version')
classmethod config_models() dict[str, type[ConfigYAMLMixin]]

Map of the name of all defined config subclasses to their class

classmethod config_sources() list[Path]

Directories to search for config files, in order of priority such that earlier sources are preferred over later sources.

classmethod fill_mio_model(v: str | None) Annotated[str, AfterValidator(func=_is_identifier)]

Get name of instantiating model, if not provided

classmethod from_any(source: Path | PathLike[str] | Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])] | Self) Self

Try and instantiate a config model from any supported constructor.

Parameters:

source (ConfigID, Path, PathLike[str]) –

Either

  • the id of a config file in the user configs directory or builtin

  • a relative Path to a config file, relative to the current working directory

  • a relative Path to a config file, relative to the user config directory

  • an absolute Path to a config file

  • an instance of the class to be constructed (returned unchanged)

classmethod from_id(id: Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])]) Self

Instantiate a model from a config id specified in one of the .yaml configs in either the user Config.config_dir or the packaged config dir.

Note

this method does not yet validate that the config matches the model loading it

classmethod from_yaml(file_path: str | Path) Self

Instantiate this class by passing the contents of a yaml file as kwargs

id: Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])]
classmethod iter_configs() Iterator[ConfigYamlHeader]

Yield headers for all configs along with their paths

mio_model: Annotated[str, AfterValidator(func=_is_identifier)]
mio_version: str
model_config = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class mio.models.mixins.ConfigYamlHeader

Generic container for partially-read config header data

id: Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])] | None
mio_model: Annotated[str, AfterValidator(func=_is_identifier)]
mio_version: str
path: NotRequired[Path]
class mio.models.mixins.YAMLMixin

Mixin class that provides from_yaml() and to_yaml() classmethods

classmethod from_yaml(file_path: str | Path) Self

Instantiate this class by passing the contents of a yaml file as kwargs

to_yaml(path: Path | None = None, **kwargs: Any) str

Dump the contents of this class to a yaml file, returning the contents of the dumped string

to_yamls(**kwargs: Any) str

Dump the contents of this class to a yaml string

Parameters:

**kwargs – passed to BaseModel.model_dump()

class mio.models.mixins.YamlDumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)

Dumper that can represent extra types like Paths

represent_path(data: Path) ScalarNode

Represent a path as a string

yaml_representers = {<class 'NoneType'>: <function SafeRepresenter.represent_none>, <class 'bool'>: <function SafeRepresenter.represent_bool>, <class 'bytes'>: <function SafeRepresenter.represent_binary>, <class 'datetime.date'>: <function SafeRepresenter.represent_date>, <class 'datetime.datetime'>: <function SafeRepresenter.represent_datetime>, <class 'dict'>: <function SafeRepresenter.represent_dict>, <class 'float'>: <function SafeRepresenter.represent_float>, <class 'int'>: <function SafeRepresenter.represent_int>, <class 'list'>: <function SafeRepresenter.represent_list>, <class 'pathlib.PosixPath'>: <function YamlDumper.represent_path>, <class 'set'>: <function SafeRepresenter.represent_set>, <class 'str'>: <function SafeRepresenter.represent_str>, <class 'tuple'>: <function SafeRepresenter.represent_list>, None: <function SafeRepresenter.represent_undefined>}
mio.models.mixins.yaml_peek(key: str, path: str | Path, root: bool = True, first: Literal[True] = True) str
mio.models.mixins.yaml_peek(key: str, path: str | Path, root: bool = True, first: Literal[False] = False) list[str]
mio.models.mixins.yaml_peek(key: str, path: str | Path, root: bool = True, first: bool = True) str | list[str]
mio.models.mixins.yaml_peek(key: list[str] | tuple[str] | set[str] | frozenset[str], path: str | Path, root: bool = True, first: bool = True) dict[str, Any]

Peek into a yaml file without parsing the whole file to retrieve the value of a single key.

This function is _not_ designed for robustness to the yaml spec, it is for simple key: value pairs, not fancy shit like multiline strings, tagged values, etc. If you want it to be, then i’m afraid you’ll have to make a PR about it.

Returns a string no matter what the yaml type is so ya have to do your own casting if you want

Parameters:
  • key (str, list[str], tuple[str], set[str) – The key to peek for. If a collection (list, tuple, set-like) of keys is passed, return a dictionary mapping those keys to their values, (or None) if no value is found for that key.

  • path (pathlib.Path , str) – The yaml file to peek into

  • root (bool) – Only find keys at the root of the document (default True ), otherwise find keys at any level of nesting.

  • first (bool) – Only return the first appearance of the key (default). Otherwise return a list of values (not implemented lol)

Returns:

str