Skip to content

Instantly share code, notes, and snippets.

@ptmcg
Last active November 29, 2023 23:36
Show Gist options
  • Save ptmcg/fa26cff820fceb65e94668adac3362eb to your computer and use it in GitHub Desktop.
Save ptmcg/fa26cff820fceb65e94668adac3362eb to your computer and use it in GitHub Desktop.
Wrapping iterators in generators in generators in ...
# remove pesky trailing newlines from iterating over a text file
def denewlining(line_iterator):
for line in line_iterator:
yield line.rstrip("\n\r")
with open(__file__) as infile:
for line in denewlining(infile):
print(f"{line!r} <-- look ma! no trailing newlines!")
print()
# clip to read only 'n' lines
def clipping(n, seq):
for _, line in zip(range(n), seq):
yield line
with open(__file__) as infile:
for i, line in enumerate(clipping(5, denewlining(infile)), start=1):
print(f"{i} {line!r} <-- look ma! no trailing newlines!")
print()
# collapse repeated lines (import repeated intentionally for demonstration)
import itertools
import itertools
def collapse_repeaters(seq):
for line, _ in itertools.groupby(seq):
yield line
with open(__file__) as infile:
for line in collapse_repeaters(denewlining(infile)):
print(f"{line!r} <-- look ma! no trailing newlines!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment