Skip to content

Commit dae5ee0

Browse files
committed
add __get__ and __set__ methods
1 parent 59e8549 commit dae5ee0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

NumpyDeque/NumpyDeque.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,40 @@ def _reverse_in_place(array):
655655
def __len__(self):
656656
return self.size
657657

658+
def __getitem__(self, index):
659+
return self.queue[index]
660+
661+
def __setitem__(self, index, value):
662+
self.queue[index] = value
663+
664+
def __getattr__(self, name):
665+
"""
666+
Handles unknown attributes by forwarding them to the NumPy.ndarray that holds the deque.
667+
This allows the NumpyDeque to support NumPy attributes such as `shape`, `dtype`, etc.
668+
669+
Parameters:
670+
name (str): The name of the missing attribute.
671+
672+
Returns:
673+
The value of the attribute from `self.deque`.
674+
675+
Raises:
676+
AttributeError: If the attribute does not exist in `self` or `self.deque`.
677+
"""
678+
679+
try:
680+
# Forward any unknown attribute to the NumPy component
681+
attr = getattr(self.deque, name)
682+
if callable(attr):
683+
684+
def method(*args, **kwargs):
685+
return attr(*args, **kwargs)
686+
687+
return method
688+
return attr
689+
except AttributeError:
690+
raise AttributeError(f"'NumpyDeque' object has no attribute '{name}'")
691+
658692
def __repr__(self):
659693
s = repr(self.deque)
660694
s = s[s.find("(") :] # drop "array" from name

0 commit comments

Comments
 (0)