Skip to content

Remove public next method in favor of overloading __next__ method #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions haiopy/buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,8 @@ def __iter__(self):
return self

def __next__(self):
"""Next dunder method for iteration"""
"""Next dunder method for iteration."""
self._start()
return self.next()

@abstractmethod
def next(self):
"""Next method which for sub-class specific handling of data."""
raise NotImplementedError()

@property
def is_active(self):
Expand Down Expand Up @@ -206,10 +200,13 @@ def sampling_rate(self):
"""Return the sampling rate."""
return self._sampling_rate

def next(self):
"""Return None"""
def __next__(self):
"""Cast a warning and stop the iteration.
This buffer is empty by design and should not be iterated.
"""
warnings.warn(
"Buffer is empty. Please provide a valid buffer.", UserWarning)
"Buffer is empty. Please provide a valid buffer.",
UserWarning, stacklevel=2)
# The stop method will raise a StopIteration exception which
# will be caught in the device class. Only the warning will be
# visible to users.
Expand Down Expand Up @@ -350,10 +347,11 @@ def _update_data(self):
(*self.data.cshape, self.n_blocks, self.block_size))
self._index = 0

def next(self):
def __next__(self):
"""Return the next audio block as numpy array and increment the block
index.
"""
super().__next__()
if self._index < self._n_blocks:
current = self._index
self._index += 1
Expand Down Expand Up @@ -461,9 +459,10 @@ def _set_block_size(self, block_size):
super()._set_block_size(block_size)
self._phase = 0

def next(self):
def __next__(self):
"""Return the next audio block as numpy array and increases the phase.
"""
super().__next__()
omega = 2 * np.pi * self._frequency
data = self._amplitude * np.sin(
omega*np.arange(self._block_size)/self._sampling_rate+self._phase)
Expand Down Expand Up @@ -590,10 +589,11 @@ def _generate_rng(self, seed):
self._seed = seed
self._rng = np.random.default_rng(seed)

def next(self):
def __next__(self):
"""Return the next audio block as numpy array, and calculates data
with seed_idx if seed is True.
"""
super().__next__()
data = self._rng.standard_normal(self._block_size)
if self._spectrum == "pink":
# Apply Pink Noise Filter
Expand Down