Created
November 26, 2017 14:09
-
-
Save Cediddi/d202b94b8550176eaddeee19a4df3490 to your computer and use it in GitHub Desktop.
A very simple read n lines implementation, available in all natural, classic and generator flavors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def readnlines(filehandler, n=4): | |
line_groups = [] | |
while True: | |
nlines = [] | |
for i in range(n): | |
line = filehandler.readline() | |
if line: | |
nlines.append(line.strip()) | |
else: | |
if nlines: | |
line_groups.append(nlines) | |
return line_groups | |
else: | |
line_groups.append(nlines) | |
def xreadnlines(filehandler, n=4): | |
line_cache = [] | |
for line in filehandler: | |
line_cache.append(line.strip()) | |
if len(line_cache) == n: | |
yield line_cache | |
line_cache = [] | |
else: | |
yield line_cache |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment