coma.config.io

Utilities for serializing configs to file.

class Extension(value)[source]

Supported config serialization file extensions:

Value

Meaning

YAML

".yaml"

YML

".yml"

JSON

".json"

maybe_add_ext(file_path: str, ext: coma.config.io.Extension) str[source]

If file_path lacks a file extension, appends ext.

Parameters
Returns

A file path with an extension if one was lacking

is_json_ext(file_path: str) bool[source]

Returns whether file_path has a JSON-like file extension.

Parameters

file_path (str) – Any file path

Returns

Whether file_path has a JSON-like file extension

is_yaml_ext(file_path: str, *, strict: bool = False) bool[source]

Returns whether file_path has a YAML-like file extension.

Parameters
  • file_path (str) – Any file path

  • strict (bool) – Whether to match Extension.YAML exactly or also allow matching against other valid YAML-like file extensions

Returns

Whether file_path has a YAML-like file extension

is_yml_ext(file_path: str, *, strict: bool = False) bool[source]

Returns whether file_path has a YAML-like file extension.

Parameters
  • file_path (str) – Any file path

  • strict (bool) – Whether to match Extension.YML exactly or also allow matching against other valid YAML-like file extensions

Returns

Whether file_path has a YAML-like file extension

is_ext(file_path: str, which: coma.config.io.Extension, *alts: coma.config.io.Extension, strict: bool = False) bool[source]

Returns whether file_path has a file extension from a specific set.

Parameters
  • file_path (str) – Any file path

  • which (coma.config.io.Extension) – The primary file extension to test against

  • *alts (coma.config.io.Extension) – A set of alternative file extensions to test against

  • strict (bool) – Whether to match which exactly or also allow matching against any extensions in alts

Returns

Whether file_path has a file extension from a specific set

load(config: Any, file_path: Optional[str] = None) Any[source]

Initializes a config object and possibly updates its attributes from file.

Initializes a default config object from config using omegaconf. If file_path is not None, attempts to also load a config object from file. If that succeeds, then attempts to update the default config object’s attributes with attributes of the config object loaded from file.

Parameters
  • config (Any) – Any config type or object to create a default config

  • file_path (str) – An optional file path from which default attributes can be updated

Returns

A new config object, possibly updated from file

Raises
  • ValueError – If file_path has an unsupported file extension

  • IOError – If there are issues relating to reading from file_path

  • Others – As may be raised by the underlying omegaconf handler

dump(config: Any, file_path: str, *, resolve: bool = False) None[source]

Serializes a config to file.

Parameters
  • config (Any) – Any valid omegaconf config object to serialize

  • file_path (str) – A file path for serializing config

  • resolve (bool) – Whether the underlying omegaconf handler should resolve variable interpolation in the configuration

Raises
  • ValueError – If file_path has an unsupported file extension

  • IOError – If there are issues relating to writing to file_path

  • Others – As may be raised by the underlying omegaconf handler