mixins

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

pydantic model mio.models.mixins.ConfigYAMLMixin

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.

Show JSON schema
{
   "title": "ConfigYAMLMixin",
   "description": "Yaml Mixin class that always puts a header consisting of\n\n * `id` - unique identifier for this config\n * `mio_model` - fully-qualified module path to model class\n * `mio_version` - version of mio when this model was created\n\n at the top of the file.",
   "type": "object",
   "properties": {
      "id": {
         "pattern": "[\\w\\-\\/#]+",
         "title": "Id",
         "type": "string"
      },
      "mio_model": {
         "default": null,
         "title": "Mio Model",
         "type": "string"
      },
      "mio_version": {
         "default": "0.6.1.dev10+g42c564f",
         "title": "Mio Version",
         "type": "string"
      }
   },
   "required": [
      "id"
   ]
}

Fields:
Validators:
field id: Annotated[str, FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(pattern='[\\w\\-\\/#]+')])] [Required]
Constraints:
  • pattern = [w-/#]+

field mio_model: Annotated[str, AfterValidator(func=_is_identifier)] = None
Constraints:
  • func = <function _is_identifier at 0x7f28d2eaef20>

Validated by:
field mio_version: str = '0.6.1.dev10+g42c564f'
validator fill_mio_model  »  mio_model

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\\-\\/#]+')])] | T) T

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\\-\\/#]+')])]) T

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) T

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

HEADER_FIELDS: ClassVar[tuple[str]] = ('id', 'mio_model', 'mio_version')
class property config_sources: List[Path]

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

class mio.models.mixins.YAMLMixin

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

classmethod from_yaml(file_path: str | Path) T

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]

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) – The key to peek for

  • 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