axtreme.eval.object_logging¶
Helpers for unpacking nestes objects for logging purposes.
When running experiments it is useful to record the objects that produced those results for reproducibility. This module provides helper for unpacking nested objects for logging. It default implementations, and supports cusomistion so the right level of granuality can be achieved.
Functions
Default/handy processing configurations. |
|
|
Searches a dictionary for the closest class, and return the value stored. |
Helper function to turn all values in nested dictionaries to str. |
|
|
Like vars() but just returns public items. |
|
Recursively extracts attributes from objects. |
|
Helper that converts all unpack_object to string. |
- axtreme.eval.object_logging.default_config() dict[type, Callable[[Any], dict[str, Any | dict[str, Any | NestedDict]]]] ¶
Default/handy processing configurations.
- axtreme.eval.object_logging.get_closest_class_value(obj: object, dic: dict[type, T]) T | None ¶
Searches a dictionary for the closest class, and return the value stored.
Searches the class heirachy from bottom to top for a matching key in the dictionary. Returns the values stored with by that key.
- Parameters:
obj – Object to find the class for
dic – takes object of that type,
- Returns:
The value in stored in the dictionary for the closest class, or None if no match is found.
- axtreme.eval.object_logging.nested_content_to_str(d: dict[str, Any | dict[str, Any | NestedDict]]) dict[str, str | dict[str, str | NestedStrDict]] ¶
Helper function to turn all values in nested dictionaries to str.
- axtreme.eval.object_logging.public_vars(obj: object) dict[str, Any] ¶
Like vars() but just returns public items.
- axtreme.eval.object_logging.unpack_object(obj: object, custom_unpacking_config: dict[type, Callable[[Any], dict[str, Any | dict[str, Any | NestedDict]]]] | None = None, depth: int = 1) dict[str, Any | dict[str, Any | NestedDict]] ¶
Recursively extracts attributes from objects.
This can be useful for logging the state of an object. It will unpack the public attributes of an object up to ‘depth’. Specific unpacking function can also be provided for attribute object in custom_unpacking_config. If these objects are encounter the unpacking function will be used instead.
- Parameters:
obj – the object to unpack.
custom_unpacking_config –
Overrides default unpacking behaviour for object that subclass the keys.
Keys: Types
Values: Functions that take instance of that type, and produces a custom unpacking.
This unpacking should be of the following format:
depth –
How many levels of objects to unpack.
- Returns:
A nested dictionary.
- Without custom unpacking:
keys: are the public attribute names
values: are the attribute value, or nested dictionary of the object being unpacked.
{“__class__”: RootObjectClass, “attribute_1”: {“__class__”: Foo, “a”: “blah”, “b”: None}, “attribute_2”: “x”}
- With custom unpacking:
keys: Determined by the custom unpacking function if used otherwise as above.
Values: Determined by the custom unpacking function if used otherwise as above.
{“__class__”: RootObjectClass, “attribute_1”: {“custom_unpacking_key1”: “name of class if FOO”, “custom_unpacking_key2”: [1, 2, 3]}, “attribute_2”: “x”}
Todo
depth: How many levels of objects to unpack.
- axtreme.eval.object_logging.unpack_object_str_content(obj: object, custom_unpacking_config: None | dict[type, Callable[[Any], dict[str, Any | dict[str, Any | NestedDict]]]] = None, depth: int = 1) dict[str, str | dict[str, str | NestedStrDict]] ¶
Helper that converts all unpack_object to string.
- Parameters:
obj – the object to unpack.
custom_unpacking_config –
Overrides default unpacking behaviour for object that subclass the keys. If None default_config() is used.
Keys: Types
Values: Functions that take instance of that type, and produces a custom unpacking. This unpacking should be of the following format:
depth –
How many levels of objects to unpack.