zea.Config

class zea.Config(dictionary=None, __parent__=None, **kwargs)[source]

Bases: dict

Config class.

This Config class extends a normal dictionary with dot notation access.

Features:
  • from_path method to load a config from a yaml file (local or huggingface hub).

  • save_to_yaml method to save the config to a yaml file.

  • copy method to create a deep copy of the config.

  • Normal dictionary methods such as keys, values, items, pop, update, get.

  • Propose similar attribute names if a non-existing attribute is accessed.

  • Freeze the config object to prevent new attributes from being added.

  • Load config object from yaml file.

  • Logs all accessed attributes such that you can check if all attributes have been accessed.

We took inspiration from the following sources:

But this implementation is superior :)

Initializes a Config object.

Parameters:
  • dictionary (dict, optional) – A dictionary containing key-value pairs to initialize the Config object. Defaults to None.

  • **kwargs – Additional key-value pairs to initialize the Config object. Will override values in the dictionary if they have the same key.

as_dict(func_on_leaves=None)[source]

Convert the config to a normal dictionary (recursively).

Parameters:

func_on_leaves (callable, optional) – Function to apply to each leaf node. The function should take three arguments: the config object, the key, and the value. You can change the key and value inside the function. Defaults to None.

clear()[source]

Clear the config object.

copy()[source]

Deep copy the config object.

This is useful when you want to modify the config object without changing the original. Does not preserve the access history or frozen state!

freeze()[source]

Freeze config object.

This means that no new attributes can be added. Only existing attributes can be modified.

classmethod from_hf(repo_id, path, **kwargs)[source]

Load config object from huggingface hub.

Parameters:
  • repo_id (str) – huggingface hub repo id. For example: “zeahub/configs”

  • path (str) – path to the config file in the repo. For example: “train_config.yaml”

  • **kwargs – additional arguments to pass to the hf_hub_download function. For example, use repo_type=”dataset” to download from a dataset repo, or revision=”main” to download from a specific branch.

Returns:

config object.

Return type:

Config

classmethod from_path(path, loader=<class 'yaml.loader.FullLoader'>, **kwargs)[source]

Load config object from a file path.

Parameters:
  • path (str or Path) – The path to the config file. Can be a string or a Path object. Additionally can be a string with the prefix ‘hf://’, in which case it will be resolved to a huggingface path.

  • loader (yaml.Loader, optional) – YAML loader used after the file is resolved.

  • **kwargs – Additional Hugging Face Hub arguments for hf:// paths, for example repo_type or revision.

Returns:

config object.

Return type:

Config

Example

>>> from zea import Config
>>> config = Config.from_path("hf://zeahub/configs/config_camus.yaml")
classmethod from_yaml(path, **kwargs)[source]

Load config object from yaml file.

fromkeys(keys, value=None)[source]

Returns a config with the specified keys and value

get(key, default=None)[source]

Returns the value of the specified key

items()[source]

Returns a list containing a tuple for each key value pair

keys()[source]

Returns a list containing the config’s keys

pop(key, default=None)[source]

Removes the element with the specified key

popitem()[source]

Removes the last inserted key-value pair

save_to_yaml(path)[source]

Deprecated alias for to_yaml().

serialize()[source]

Return a dict of this config object with all Path objects converted to strings.

setdefault(key, default=None)[source]

Returns the value of the specified key. If the key does not exist: insert the key, with the specified value

to_json()[source]

Return the config as a json string.

to_tensor(keep_as_is=None)[source]

Convert the attributes in the object to keras tensors

to_yaml(path)[source]

Save config contents to a YAML file.

unfreeze()[source]

Unfreeze config object. This means that new attributes can be added.

update(dictionary=None, **kwargs)[source]

Updates the config with the specified key-value pairs

update_recursive(dictionary=None, **kwargs)[source]

Recursively update the config with the provided dictionary and keyword arguments.

If a key corresponds to another Config object, the update_recursive method is called recursively on that object. This makes it possible to update nested Config objects without replacing them.

If a value is a list and the corresponding config value is also a list, each element is updated recursively if it is a Config, otherwise replaced.

Example

>>> from zea import Config
>>> config = Config({"a": 1, "b": {"c": 2, "d": 3}})
>>> config.update_recursive({"a": 4, "b": {"c": 5}})
>>> # Notice how "d" is kept and only "c" is updated.
>>> print(config)
Config({'a': 4, 'b': {'c': 5, 'd': 3}})
Parameters:
  • dictionary (dict | None) – Dictionary to update from.

  • **kwargs – Additional key-value pairs to update.

values()[source]

Returns a list of all the values in the config