Skip to content

Commit

Permalink
Remove use of args and kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcr committed Mar 9, 2024
1 parent 9f55174 commit ca1003d
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 114 deletions.
13 changes: 8 additions & 5 deletions rolling/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ class Apply(RollingObject):
[5, 6, 3, 1]]
"""
def __init__(self, iterable, window_size, window_type="fixed", operation=sum):
super().__init__(iterable, window_size, window_type)
self._operation = operation

def _init_fixed(self, iterable, window_size, operation=sum, **kwargs):
def _init_fixed(self):
self._buffer = deque([None])
self._buffer.extend(islice(self._iterator, window_size - 1))
self._operation = operation
self._buffer.extend(islice(self._iterator, self.window_size - 1))

def _init_variable(self, iterable, window_size, operation=sum, **kwargs):
def _init_variable(self):
self._buffer = deque()
self._operation = operation

_init_indexed = _init_variable

@property
def current_value(self):
Expand Down
4 changes: 2 additions & 2 deletions rolling/apply_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def __init__(self, iterable_1, iterable_2, window_size, function, window_type="f
self._function = function
super().__init__(iterable_1, iterable_2, window_size=window_size, window_type=window_type)

def _init_fixed(self, **kwargs):
def _init_fixed(self):
pairs = zip(self._iterator_1, self._iterator_2)
for item_1, item_2 in islice(pairs, self.window_size-1):
self._buffer_1.append(item_1)
self._buffer_2.append(item_2)

def _init_variable(self, **kwargs):
def _init_variable(self):
pass # no action required

@property
Expand Down
8 changes: 5 additions & 3 deletions rolling/arithmetic/nunique.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ class Nunique(RollingObject):
"""

def _init_fixed(self, iterable, window_size, **kwargs):
head = islice(self._iterator, window_size - 1)
def _init_fixed(self):
head = islice(self._iterator, self.window_size - 1)
self._buffer = deque(head)
# append a dummy value that is removed when next() is called
self._buffer.appendleft("dummy_value")
self._counter = Counter(self._buffer)

def _init_variable(self, iterable, window_size, **kwargs):
def _init_variable(self):
self._buffer = deque()
self._counter = Counter()

_init_indexed = _init_variable

def _update_window(self, new):
# remove oldest value before appending new to buffer
self._remove_old()
Expand Down
8 changes: 5 additions & 3 deletions rolling/arithmetic/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class Product(RollingObject):
"""

def _init_fixed(self, iterable, window_size, **kwargs):
head = islice(self._iterator, window_size - 1)
def _init_fixed(self):
head = islice(self._iterator, self.window_size - 1)
self._buffer = deque(head)
self._zero_count = 0

Expand All @@ -56,11 +56,13 @@ def _init_fixed(self, iterable, window_size, **kwargs):
self._buffer.appendleft(1)
self._product = prod

def _init_variable(self, iterable, window_size, **kwargs):
def _init_variable(self):
self._buffer = deque()
self._zero_count = 0
self._product = 1

_init_indexed = _init_variable

def _update_window(self, new):
old = self._buffer.popleft()
self._buffer.append(new)
Expand Down
10 changes: 6 additions & 4 deletions rolling/arithmetic/sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ class Sum(RollingObject):
[13, 11, 15]
"""
def _init_fixed(self, iterable, window_size, **kwargs):
head = islice(self._iterator, window_size - 1)
self._buffer = deque(head, maxlen=window_size)
def _init_fixed(self):
head = islice(self._iterator, self.window_size - 1)
self._buffer = deque(head, maxlen=self.window_size)
self._buffer.appendleft(0)
self._sum = sum(self._buffer)

def _init_variable(self, iterable, window_size, **kwargs):
def _init_variable(self):
self._buffer = deque()
self._sum = 0

_init_indexed = _init_variable

def _update_window(self, new):
self._sum += new - self._buffer.popleft()
self._buffer.append(new)
Expand Down
15 changes: 8 additions & 7 deletions rolling/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ class RollingObject(Iterator):
"""

def __init__(self, iterable, window_size, window_type="fixed", **kwargs):
def __init__(self, iterable, window_size, window_type="fixed"):
self.window_type = window_type
self.window_size = _validate_window_size(window_size, window_type)
self._iterator = iter(iterable)
self._filled = self.window_type == "fixed"

if window_type == "fixed":
self._init_fixed(iterable, window_size, **kwargs)
self._init_fixed()

elif window_type == "variable":
self._init_variable(iterable, window_size, **kwargs)
self._init_variable()

elif window_type == "indexed":
# Keep track of all indexes that we encounter. Assumes that all
# values we encounter will be stored in the same order. If not,
# the subtype will need to implement its own _next_indexed() method.
self.index_buffer = deque()
self._init_indexed(iterable, window_size, **kwargs)
self._init_indexed()

else:
raise ValueError(f"Unknown window_type '{window_type}'")
Expand Down Expand Up @@ -149,7 +149,7 @@ def _obs(self):
"""
Return the number of observations in the window
"""
pass
return 0

@abc.abstractmethod
def _init_fixed(self):
Expand All @@ -165,13 +165,14 @@ def _init_variable(self):
"""
pass

def _init_indexed(self, *args, **kwargs):
@abc.abstractmethod
def _init_indexed(self):
"""
Intialise as an indexed window.
In most cases this is the same as initialising a variable-size window.
"""
return self._init_variable(*args, **kwargs)
pass

@abc.abstractmethod
def _remove_old(self):
Expand Down
16 changes: 8 additions & 8 deletions rolling/base_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ class RollingPairwise(Iterator):
Baseclass for rolling iterators over two iterables.
"""
def __init__(self, iterable_1, iterable_2, window_size, window_type="fixed", **kwargs):
def __init__(self, iterable_1, iterable_2, window_size, window_type="fixed"):
self.window_type = window_type
self.window_size = _validate_window_size(window_size)
self._iterator_1 = iter(iterable_1)
self._iterator_2 = iter(iterable_2)
self._filled = self.window_type == "fixed"

if window_type == "fixed":
self._init_fixed(**kwargs)
self._init_fixed()

elif window_type == "variable":
self._init_variable(**kwargs)
self._init_variable()

else:
raise ValueError(f"Unknown window_type '{window_type}'")
Expand Down Expand Up @@ -80,17 +80,17 @@ def _obs(self):
"""
Return the number of observations in the window
"""
pass
return 0

@abc.abstractmethod
def _init_fixed(self, **kwargs):
def _init_fixed(self):
"""
Intialise as a fixed-size window
"""
pass

@abc.abstractmethod
def _init_variable(self, **kwargs):
def _init_variable(self):
"""
Intialise as a variable-size window
"""
Expand All @@ -104,14 +104,14 @@ def _remove_old(self):
pass

@abc.abstractmethod
def _add_new(self, new):
def _add_new(self, new_1, new_2):
"""
Add a new value to the window, increasing window size by 1
"""
pass

@abc.abstractmethod
def _update_window(self, new):
def _update_window(self, new_1, new_2):
"""
Add a new value to the window and remove the oldest value from the window
"""
Expand Down
12 changes: 6 additions & 6 deletions rolling/entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ def __init__(self, iterable, window_size, base=2, reference_distribution=None):
self._log = _get_log_func(base)
super().__init__(iterable, window_size)

def _init_fixed(self, iterable, window_size, **kwargs):
def _init_fixed(self):
self._entropy = 0.0
self._summands = {}

head = islice(self._iterator, window_size - 1)
self._buffer = deque(head, maxlen=window_size)
head = islice(self._iterator, self.window_size - 1)
self._buffer = deque(head, maxlen=self.window_size)

counts = Counter(self._buffer)

Expand All @@ -104,7 +104,7 @@ def _init_fixed(self, iterable, window_size, **kwargs):
self._buffer.appendleft(object)
self._summands[object] = (1, 0)

def _init_variable(self, iterable, window_size, **kwargs):
def _init_variable(self):
raise NotImplementedError("Entropy not implemented for variable windows")

def _update_window(self, new):
Expand Down Expand Up @@ -147,11 +147,11 @@ def _obs(self):
return len(self._buffer)

# Required, but unused as variable windows not supported
def _add_new(self, new):
def _add_new(self):
pass

def _remove_old(self):
pass

def _init_indexed(self, *args, **kwargs):
def _init_indexed(self):
raise NotImplementedError("window_type='indexed'")
6 changes: 3 additions & 3 deletions rolling/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ def __init__(
self._mod = mod
super().__init__(iterable, window_size, window_type)

def _init_fixed(self, *args, **kwargs):
def _init_fixed(self):
self._buffer = deque([0])
for val in islice(self._iterator, self.window_size - 1):
self._add_new(val)

def _init_variable(self, *args, **kwargs):
def _init_variable(self):
self._buffer = deque()

def _add_new(self, new):
Expand All @@ -103,5 +103,5 @@ def current_value(self):
def _obs(self):
return len(self._buffer)

def _init_indexed(self, *args, **kwargs):
def _init_indexed(self):
raise NotImplementedError("window_type='indexed'")
8 changes: 4 additions & 4 deletions rolling/logical/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class All(RollingObject):
"""

def _init_fixed(self, iterable, window_size, **kwargs):
def _init_fixed(self):
self._i = -1
self._window_obs = 1
self._last_false = -1
for new in islice(self._iterator, window_size - 1):
for new in islice(self._iterator, self.window_size - 1):
self._add_new(new)

def _init_variable(self, iterable, window_size, **kwargs):
def _init_variable(self):
self._i = -1
self._window_obs = 0
self._last_false = -1
Expand All @@ -73,6 +73,6 @@ def _obs(self):
def current_value(self):
return self._i - self._window_obs >= self._last_false

def _init_indexed(self, *args, **kwargs):
def _init_indexed(self):
raise NotImplementedError("window_type='indexed'")

8 changes: 4 additions & 4 deletions rolling/logical/any.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class Any(RollingObject):
"""

def _init_fixed(self, iterable, window_size, **kwargs):
def _init_fixed(self):
self._i = -1
self._window_obs = 1
self._last_true = -1
for new in islice(self._iterator, window_size - 1):
for new in islice(self._iterator, self.window_size - 1):
self._add_new(new)

def _init_variable(self, iterable, window_size, **kwargs):
def _init_variable(self):
self._i = -1
self._window_obs = 0
self._last_true = -1
Expand All @@ -73,5 +73,5 @@ def _obs(self):
def current_value(self):
return self._i - self._window_obs < self._last_true

def _init_indexed(self, *args, **kwargs):
def _init_indexed(self):
raise NotImplementedError("window_type='indexed'")
Loading

0 comments on commit ca1003d

Please sign in to comment.