ros_sugar.config.base_attrs#

Module Contents#

Classes#

BaseAttrs

Implements setattr method to re-use validators at set time

Functions#

explicit_fields

The fields of an attrs config that differ from its class defaults.

API#

ros_sugar.config.base_attrs.explicit_fields(config: Any) Dict#

The fields of an attrs config that differ from its class defaults.

A full asdict cannot tell a value the caller chose from one that is simply the class default. Anything consuming such a dict has to write every key back, which reverts values its consumer set for itself. This keeps only the fields that were actually changed.

Values are also made JSON-safe (numpy arrays become lists). BaseAttrs.from_dict converts them back on the way in.

Takes any attrs instance rather than a BaseAttrs: algorithm configs come from whichever library implements the algorithm, and need not derive from the base class in this package.

NOTE: a field explicitly set to a value equal to its default is indistinguishable from one never set, and is dropped. That is harmless for a plain override, but means it cannot be used to defend a default against a consumer that would otherwise fill the field in.

Parameters:

config (Any) – Any attrs class instance

Returns:

Fields that differ from the defaults

Return type:

dict

class ros_sugar.config.base_attrs.BaseAttrs#

Implements setattr method to re-use validators at set time

asdict(filter: Optional[Callable] = None) Dict#

Convert class to dict.

Return type:

dict

to_dict() Dict#

Convert class to dict.

Return type:

dict

asdict_explicit() Dict#

Convert to dict, keeping only what differs from the class defaults.

See explicit_fields, which this defers to.

Returns:

Fields that differ from the defaults

Return type:

dict

from_dict(dict_obj: Dict) None#

Gets attributes values from given dictionary

Parameters:

dict_obj (Dict) – Dictionary {attribute_name: attribute_value}

Raises:
  • ValueError – If attribute_name in dictionary does not exists in class attributes

  • TypeError – If attribute_value type in dictionary does not correspond to class attribute type

from_file(file_path: str, nested_root_name: Union[str, None] = None, get_common: bool = False) bool#

Update class attributes from yaml, json, or toml

Parameters:
  • file_path – Path to config file (.yaml, .json, .toml)

  • nested_root_name – Nested root name for the config, defaults to None

  • get_common – Whether to get extra config root (for merging), defaults to False

to_json() Union[str, bytes, bytearray]#

Dump to json

Returns:

description

Return type:

str | bytes | bytearray

from_json(json_obj: Union[str, bytes, bytearray]) None#

Gets attributes values from given json

Parameters:

json_obj (str | bytes | bytearray) – Json object

has_attribute(attr_name: str) bool#

Checks if class object has attribute with given name

Parameters:

attr_name (str) – description

Returns:

If object has attribute with given name

Return type:

bool

get_attribute_type(attr_name: str) Optional[type]#

Gets type of given attribute name

Parameters:

attr_name (str) – description

Raises:

AttributeError – If class does not have attribute with given name

Returns:

Attribute type

Return type:

type

update_value(attr_name: str, attr_value: Any) bool#

Updates the value of an attribute in the class

Parameters:
  • attr_name (str) – Attribute name - can be nested name

  • attr_value (Any) – Attribute value

Raises:
  • AttributeError – If class does not contain attribute with given name

  • TypeError – If class attribute with given name if of different type

Returns:

If attribute value is updated

Return type:

bool

classmethod get_fields_info(class_object) Dict[str, Dict[str, Any]]#

Returns a dictionary with metadata about each field in the class.

This includes the field’s name, type annotation, and parsed validator info.

Returns:

A dictionary where keys are field names and values are dicts of metadata.