Skip to content

Commit

Permalink
#3 Reorder functions in utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Aug 22, 2021
1 parent 5720517 commit 00cf2d4
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions xarray_accessors/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
__all__ = [
"get_nested_attr",
"set_nested_attr",
"del_nested_attr",
"get_nested_attr",
"has_nested_attr",
"set_nested_attr",
]


Expand All @@ -20,6 +20,30 @@ def __repr__(self):


# runtime functions
def del_nested_attr(obj: Any, names: Sequence[str]) -> None:
"""Remove a nested attribute from the given object.
``del_nested_attr(x, ['y', 'z'])`` is equivalent to ``del x.y.z``.
Args:
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).
"""
if len(names) == 0:
raise ValueError("At least one name must be specified.")

if len(names) == 1:
delattr(obj, names[0])
else:
delattr(get_nested_attr(obj, names[:-1]), names[-1])


def get_nested_attr(obj: Any, names: Sequence[str], default: Any = MISSING) -> Any:
"""Get a nested attribute from the given object.
Expand Down Expand Up @@ -55,39 +79,38 @@ def get_nested_attr(obj: Any, names: Sequence[str], default: Any = MISSING) -> A
return get_nested_attr(attr, names[1:], default)


def set_nested_attr(obj: Any, names: Sequence[str], value: Any) -> None:
"""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``.
def has_nested_attr(obj: Any, names: Sequence[str]) -> bool:
"""Return whether an object has a nested attribute.
Args:
obj: Object to be evaluated.
names: Sequence of attribute names.
values: Value to be set as the nested attribute.
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).
"""
if len(names) == 0:
raise ValueError("At least one name must be specified.")

if len(names) == 1:
setattr(obj, names[0], value)
else:
setattr(get_nested_attr(obj, names[:-1]), names[-1], value)
try:
get_nested_attr(obj, names)
return True
except AttributeError:
return False


def del_nested_attr(obj: Any, names: Sequence[str]) -> None:
"""Remove a nested attribute from the given object.
def set_nested_attr(obj: Any, names: Sequence[str], value: Any) -> None:
"""Set a nested attribute on the given object to the given value.
``del_nested_attr(x, ['y', 'z'])`` is equivalent to ``del x.y.z``.
``set_nested_attr(x, ['y', 'z'], v)`` is equivalent to ``x.y.z = v``.
Args:
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.
Expand All @@ -99,29 +122,6 @@ def del_nested_attr(obj: Any, names: Sequence[str]) -> None:
raise ValueError("At least one name must be specified.")

if len(names) == 1:
delattr(obj, names[0])
setattr(obj, names[0], value)
else:
delattr(get_nested_attr(obj, names[:-1]), names[-1])


def has_nested_attr(obj: Any, names: Sequence[str]) -> bool:
"""Return whether an object has a nested attribute.
Args:
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).
"""
try:
get_nested_attr(obj, names)
return True
except AttributeError:
return False
setattr(get_nested_attr(obj, names[:-1]), names[-1], value)

0 comments on commit 00cf2d4

Please sign in to comment.