Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 9, 2024
1 parent b6f3c41 commit 8aede49
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
15 changes: 6 additions & 9 deletions hyperspy/misc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1640,9 +1640,10 @@ def display(obj):
except ImportError:
print(obj)


class TupleSA(tuple):
"""A tuple that can set the attributes of its items
"""
"""A tuple that can set the attributes of its items"""

def __getitem__(self, *args, **kwargs):
item = super().__getitem__(*args, **kwargs)
try:
Expand All @@ -1661,12 +1662,9 @@ def set(self, **kwargs):
then attribute of each item of the tuple will be set to each of the values.
"""
for key, value in kwargs.items():
no_name = [item
for item in self
if not hasattr(item, key)]
no_name = [item for item in self if not hasattr(item, key)]
if no_name:
raise AttributeError(
f"'The items {no_name} have not attribute '{key}'")
raise AttributeError(f"'The items {no_name} have not attribute '{key}'")
else:
if isiterable(value) and not isinstance(value, str):
for item, value_ in zip(self, value):
Expand All @@ -1693,8 +1691,7 @@ def get(self, *args):
values = list()
for item in self:
if not hasattr(item, key):
raise AttributeError(
f"'The item {item} has not attribute '{key}'")
raise AttributeError(f"'The item {item} has not attribute '{key}'")
else:
values.append(getattr(item, key))
output[key] = tuple(values)
Expand Down
4 changes: 3 additions & 1 deletion hyperspy/tests/axes/test_axes_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def test_set_attributes(self):
assert am.navigation_axes[0].units == "nm"
assert am.navigation_axes[1].units == "nm"
with pytest.raises(AttributeError):
am.signal_axes.set(names=("kx", "kx"), offset=(1, 2), scale=3, units="nm^{-1}")
am.signal_axes.set(
names=("kx", "kx"), offset=(1, 2), scale=3, units="nm^{-1}"
)

def test_all_uniform(self):
assert self.am.all_uniform is True
Expand Down
7 changes: 6 additions & 1 deletion hyperspy/tests/misc/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def test_tuple_sa_set_attribute(self):
assert t[1].attribute1 == "value3"
assert t[0].attribute2 == 0
assert t[1].attribute2 == 1

@staticmethod
def test_tuple_sa_set_attribute_error():
t = TupleSA((1, 2, 3))
Expand All @@ -204,7 +205,11 @@ def test_tuple_sa_set_attribute_error():

def test_tuple_sa_get_attribute(self):
t = self.tuple
assert t.get("attribute1", "attribute2") == {"attribute1": ("value1", "value1"), "attribute2": ("value2", "value2")}
assert t.get("attribute1", "attribute2") == {
"attribute1": ("value1", "value1"),
"attribute2": ("value2", "value2"),
}

@staticmethod
def test_tuple_sa_get_attribute_error():
t = TupleSA((1, 2, 3))
Expand Down

0 comments on commit 8aede49

Please sign in to comment.