Skip to content

Commit 3eb3c4a

Browse files
committed
doc: update docstring to include method descriptions
1 parent 1babdd8 commit 3eb3c4a

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

NumpyDeque/NumpyDeque.py

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
class NumpyDeque:
5555
"""
56-
A numpy ndarray based double-ended queue (deque) with a maximum size.
56+
A double-ended queue (deque) with a maximum size that is also a `numpy.ndarray` .
5757
The deque has an initial size of zero and grows as values are added.
5858
When the deque has maxsize values and another value is added (put or putleft),
5959
the value on the opposite end is dropped. The object's `deque` attribute
@@ -66,7 +66,19 @@ class NumpyDeque:
6666
`put` operations (adding to the end), or for `putleft` operations (adding to start).
6767
6868
Attributes:
69-
deque (np.ndarray): The current deque, representing the active elements.
69+
deque (np.ndarray): The current deque, representing the active elements.
70+
size (int): Current number of elements in the deque.
71+
maxsize (int): Maximum size of the deque. Once the limit is reached,
72+
adding an element will discard the element on the opposite end.
73+
dtype (numpy.dtype): Data type of the elements stored in the deque.
74+
priority (int): The buffer priority for internal shifting of the deque.
75+
Set to:
76+
"equal" : buffer space gives equal priority to put and putleft
77+
"right" : buffer space gives priority to put
78+
"left" : buffer space gives priority to putleft
79+
"leftonly" : buffer space gives all priority to put (putleft -> internal shift)
80+
"rightonly": buffer space gives all priority to putleft (put -> internal shift)
81+
buffer_size (int): The size of the underlying buffer that holds the deque.
7082
7183
Private Attributes:
7284
_data (np.ndarray): The underlying NumPy array that stores the elements plus buffered space.
@@ -77,6 +89,77 @@ class NumpyDeque:
7789
_i (int): Index in _data that is the first value in the deque.
7890
_j (int): Index plus one in _data that is the last value in the deque.
7991
92+
Constructors:
93+
array(array_like, maxsize=None): Create a NumpyDeque from an existing array-like object.
94+
95+
Methods:
96+
put(value): Adds an element to the right end of the deque.
97+
If the deque is at maxsize, then removes the left most element.
98+
putleft(value): Adds an element to the left end of the deque.
99+
If the deque is at maxsize, then removes the right most element.
100+
101+
putter(iterable): Adds multiple elements to the right of the deque from an iterable.
102+
If the deque is at maxsize, then removes the left most elements.
103+
putterleft(iterable): Adds multiple elements to the left of the deque from an iterable.
104+
If the deque is at maxsize, then removes the right most elements.
105+
106+
pop(): Removes and returns the rightmost element of the deque.
107+
popleft(): Removes and returns the leftmost element of the deque.
108+
remove(value): Removes the first occurrence of the specified value from the deque.
109+
drop(index): Removes and returns the element at the specified index.
110+
clear(): Removes all elements from the deque, leaving it empty.
111+
112+
index(value): Returns the index of the first occurrence of the specified value in the deque.
113+
count(value): Returns the number of occurrences of the specified value in the deque.
114+
sort(): Sorts the order of the elements in the deque in-place.
115+
reverse(): Reverses the order of the elements in the deque in-place.
116+
117+
118+
Examples:
119+
>>> import numpy as np
120+
>>> from NumpyDeque import NumpyDeque
121+
122+
>>> # Create an empty deque with a maximum size of 5
123+
>>> d = NumpyDeque(maxsize=5, dtype=np.int64)
124+
>>> d.put(1)
125+
>>> d.put(2)
126+
>>> print(d)
127+
NumpyDeque([1, 2])
128+
129+
>>> # Add elements until it reaches max size
130+
>>> d.put(3)
131+
>>> d.put(4)
132+
>>> d.put(5)
133+
>>> print(d)
134+
NumpyDeque([1, 2, 3, 4, 5])
135+
>>> # Adding values that exceed maxsize results in the opposite end being dropped
136+
>>> d.put(6) # To to right, so drops the left
137+
>>> print(d)
138+
NumpyDeque([2, 3, 4, 5, 6]) # 1 is discarded
139+
140+
>>> # Adding to the left end of the deque
141+
>>> d.putleft(0) # To to left, so drops the right
142+
>>> print(d)
143+
NumpyDeque([0, 2, 3, 4, 5]) # 6 is discarded from the right end
144+
145+
>>> # Removing elements
146+
>>> rightmost = d.pop()
147+
>>> print(rightmost) # Output: 5
148+
>>> print(d) # NumpyDeque([0, 2, 3, 4])
149+
150+
>>> # Creating a deque from an array
151+
>>> arr = np.array([10, 20, 30, 40])
152+
>>> d = NumpyDeque.array(arr)
153+
>>> print(d)
154+
NumpyDeque([10, 20, 30, 40])
155+
156+
>>> # Using numpy array methods
157+
>>> mean_value = d.mean()
158+
>>> print(mean_value) # Output: 25.0
159+
160+
# Slicing works similar to numpy arrays
161+
>>> print(d[1:3])
162+
NumpyDeque([20, 30])
80163
"""
81164

82165
deque: np.ndarray # The pointer to the current deque.

0 commit comments

Comments
 (0)