Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Attributes no longer use a faux functional style (#1046)
Browse files Browse the repository at this point in the history
* attributes no longer use this faux functional style

* improved comment

* pointless try/catch removed

* Update NeXus HTML documentation

* Update NeXus HTML documentation

* Update NeXus HTML documentation

---------

Co-authored-by: cow-bot <dm-jenkins-integration@esss.se>
  • Loading branch information
mattclarke and cow-bot authored Nov 23, 2023
1 parent 41aa05c commit 279a730
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
55 changes: 46 additions & 9 deletions nexus_constructor/model/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,46 @@
import numpy as np

from nexus_constructor.common_attrs import CommonKeys
from nexus_constructor.model.helpers import _get_item, _set_item
from nexus_constructor.model.value_type import ValueType, ValueTypes


class Attributes(list):
"""Abstract class used for common functionality between a group and dataset."""
class Attributes:
"""List-like class used for storing attributes."""

def __init__(self):
self._attributes = []

def _find_item_index(self, item_name: str):
for count, element in enumerate(self._attributes):
if element.name == item_name:
return count
return None

def _get_item(self, item_name: str) -> Any:
index = self._find_item_index(item_name)
return self._attributes[index] if index is not None else None

def _set_item(
self,
parent,
item_name: str,
new_value: Any,
):
index = self._find_item_index(item_name)
if index is not None:
self._attributes[index] = new_value
else:
self._attributes.append(new_value)
if hasattr(new_value, "parent_node"):
new_value.parent_node = parent

def set_attribute_value(
self,
attribute_name: str,
attribute_value: Any,
attribute_type: str = ValueTypes.STRING,
):
_set_item(
self,
self._set_item(
self,
attribute_name,
FieldAttribute(
Expand All @@ -31,20 +56,32 @@ def set_attribute_value(

def get_attribute_value(self, attribute_name: str):
if self.contains_attribute(attribute_name):
return _get_item(self, attribute_name).values
return self._get_item(attribute_name).values
return None

def get_attribute(self, attribute_name: str):
if self.contains_attribute(attribute_name):
return _get_item(self, attribute_name)
return self._get_item(attribute_name)
return None

def contains_attribute(self, attribute_name):
result = _get_item(self, attribute_name)
result = self._get_item(attribute_name)
return True if result is not None else False

def as_dict(self, error_collector: List[str]):
return [attribute.as_dict(error_collector) for attribute in self]
return [attribute.as_dict(error_collector) for attribute in self._attributes]

def __getitem__(self, index):
return self._attributes[index]

def __len__(self):
return len(self._attributes)

def __eq__(self, other):
try:
return other._attributes == self._attributes
except AttributeError:
return False


@attr.s(eq=False)
Expand Down
2 changes: 1 addition & 1 deletion nx-class-documentation/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ <h1>User Manual and Reference Documentation<a class="headerlink" href="#user-man
</div>
<hr class="docutils" />
<p class="rubric">Publishing Information</p>
<p>This manual built Nov 22, 2023.</p>
<p>This manual built Nov 23, 2023.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p>This document is available in these formats online:</p>
Expand Down
2 changes: 1 addition & 1 deletion nx-class-documentation/html/searchindex.js

Large diffs are not rendered by default.

0 comments on commit 279a730

Please sign in to comment.