Skip to content

Commit 131b997

Browse files
committed
add tqdm
1 parent 29ffd92 commit 131b997

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

data_tutorials/data.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,38 @@
1111

1212
# this is only for python 3.9 once done use str| list[str]
1313
from typing import Union
14+
from tqdm.auto import tqdm
1415

1516
DEFAULT_URL = "https://raw.githubusercontent.com/ddmms/data-tutorials/main/data/"
1617

18+
class TqdmUpTo(tqdm):
19+
"""Alternative Class-based version of the above.
20+
21+
Provides `update_to(n)` which uses `tqdm.update(delta_n)`.
22+
23+
Inspired by [twine#242](https://github.com/pypa/twine/pull/242),
24+
[here](https://github.com/pypa/twine/commit/42e55e06).
25+
"""
26+
27+
def update_to(self, b=1, bsize=1, tsize=None):
28+
"""
29+
b : int, optional
30+
Number of blocks transferred so far [default: 1].
31+
bsize : int, optional
32+
Size of each block (in tqdm units) [default: 1].
33+
tsize : int, optional
34+
Total size (in tqdm units). If [default: None] remains unchanged.
35+
"""
36+
if tsize is not None:
37+
self.total = tsize
38+
return self.update(b * bsize - self.n) # also sets self.n = b * bsize
39+
1740

1841
def download_file(url: str, filename: str, dest: Path) -> None:
1942
save_file = dest / filename
2043
print(f"try to download {filename} from {url} and save it in {save_file}")
21-
path, _ = urlretrieve(url + filename, save_file)
44+
with TqdmUpTo(unit = 'B', unit_scale = True, unit_divisor = 1024, miniters = 1, desc = filename) as t:
45+
path, _ = urlretrieve(url + filename, save_file, reporthook = t.update_to)
2246
if path.exists():
2347
print(f"saved in {save_file}")
2448
else:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dynamic = ["version"]
99
dependencies = [
1010
"pytest",
1111
"ruff",
12+
"tqdm"
1213
]
1314
requires-python = ">=3.9"
1415
authors = [

0 commit comments

Comments
 (0)