Skip to content

Instantly share code, notes, and snippets.

@st3fan
Created November 25, 2022 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st3fan/f5f839c0d66f76ab16723afb28a2bb6d to your computer and use it in GitHub Desktop.
Save st3fan/f5f839c0d66f76ab16723afb28a2bb6d to your computer and use it in GitHub Desktop.
class _NoPadding: pass
_Chunk_T = TypeVar("_Chunk_T")
def _windowed(iterable: Iterable[_Chunk_T], size: int, step: int = 1, padding: _NoPadding|Any = _NoPadding()) -> Iterable[Iterable[_Chunk_T|None]]:
it = iter(iterable)
chunk: List[_Chunk_T|Any] = []
while True:
# Pop step items from the buffer
if chunk:
for _ in range(step):
chunk.pop(0)
# Fill the buffer
for _ in range(size - len(chunk)):
try:
chunk.append(next(it))
except StopIteration:
# We've hit the end, add padding
if not isinstance(padding, _NoPadding):
if len(chunk) != size:
for _ in range(size - len(chunk)):
chunk.append(padding)
yield chunk
return
# Yield the buffer
yield chunk
for chunk in _windowed([1,2,3,4,5,6,7], 4, 2, padding=0):
print(list(chunk))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment