Last active
August 29, 2015 14:12
-
-
Save cdunklau/20cab47619d052745a58 to your computer and use it in GitHub Desktop.
Progress-reporting line reader
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
| import os | |
| class ProgressLineReader(object): | |
| def __init__(self, filepath, mode='r'): | |
| self.path = filepath | |
| self.total_size = os.stat(filepath).st_size | |
| self.already_read = 0 | |
| self.fobj = open(filepath, mode) | |
| def __iter__(self): | |
| for line in self.fobj: | |
| self.already_read += len(line) | |
| yield line | |
| def __enter__(self): | |
| return self | |
| def __exit__(self, exc_type, exc_value, traceback): | |
| self.close() | |
| @property | |
| def progress(self): | |
| """Percentage read""" | |
| return 100.0 * self.total_size / self.already_read | |
| def close(self): | |
| self.fobj.close() |
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
| import csv | |
| from progress import ProgressLineReader | |
| with ProgressLineReader('path/to/file.csv') as progressreader: | |
| for row in csv.reader(progressreader): | |
| print 'Got row: {0}'.format(row) | |
| print 'Read {0.already_read} of {0.total_size}, {0.progress} %'.format(progressreader) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment