Last active
September 29, 2023 07:06
-
-
Save LexSong/cdfd719b9e29fb7f5065732fd1af8cbe to your computer and use it in GitHub Desktop.
tqdm_file_progress_bar
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 io | |
| from tqdm import tqdm | |
| def get_file_end_position(fileobj): | |
| current_pos = fileobj.tell() | |
| end = fileobj.seek(0, io.SEEK_END) | |
| fileobj.seek(current_pos) | |
| return end | |
| def _tqdm_file_progress_bar(iterable, fileobj, *args, **kwargs): | |
| total = get_file_end_position(fileobj) | |
| with tqdm(total=total, *args, **kwargs) as progress: | |
| for x in iterable: | |
| progress.n = fileobj.tell() | |
| progress.update(0) | |
| yield x | |
| def tqdm_file_progress_bar(iterable, fileobj, *args, **kwargs): | |
| if fileobj.seekable(): | |
| return _tqdm_file_progress_bar(iterable, fileobj, *args, **kwargs) | |
| else: | |
| return tqdm(iterable, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment