xarray_accessors.utils module

xarray_accessors.utils.del_nested_attr(obj: Any, names: Sequence[str]) → None[source]

Remove a nested attribute from the given object.

del_nested_attr(x, ['y', 'z']) is equivalent to del x.y.z.

Parameters
  • obj – Object to be evaluated.

  • names – Sequence of attribute names.

Raises
  • AttributeError – Raised if the nested attribute does not exist.

  • ValueError – Raised if names is an invalid object (e.g., a string, an empty list or tuple).

xarray_accessors.utils.get_nested_attr(obj: Any, names: Sequence[str], default: Any = <MISSING>) → Any[source]

Get a nested attribute from the given object.

get_nested_attr(x, ['y', 'z']) is equivalent to x.y.z.

Parameters
  • obj – Object to be evaluated.

  • names – Sequence of attribute names.

  • default – Default value. It is returned if the nested attribute does not exist.

Returns

Nested attribute of an object.

Raises
  • AttributeError – Raised if the nested attribute does not exist and default is not specified.

  • ValueError – Raised if names is an invalid object (e.g., a string, an empty list or tuple).

xarray_accessors.utils.has_nested_attr(obj: Any, names: Sequence[str]) → bool[source]

Return whether an object has a nested attribute.

Parameters
  • obj – Object to be evaluated.

  • names – Sequence of attribute names.

Returns

True if the object has the nested attribute. False otherwise.

Raises
  • AttributeError – Raised if the nested attribute does not exist.

  • ValueError – Raised if names is an invalid object (e.g., a string, an empty list or tuple).

xarray_accessors.utils.set_nested_attr(obj: Any, names: Sequence[str], value: Any) → None[source]

Set a nested attribute on the given object to the given value.

set_nested_attr(x, ['y', 'z'], v) is equivalent to x.y.z = v.

Parameters
  • obj – Object to be evaluated.

  • names – Sequence of attribute names.

  • values – Value to be set as the nested attribute.

Raises
  • AttributeError – Raised if the nested attribute does not exist.

  • ValueError – Raised if names is an invalid object (e.g., a string, an empty list or tuple).