|
11 | 11 |
|
12 | 12 | # this is only for python 3.9 once done use str| list[str] |
13 | 13 | from typing import Union |
| 14 | +from tqdm.auto import tqdm |
14 | 15 |
|
15 | 16 | DEFAULT_URL = "https://raw.githubusercontent.com/ddmms/data-tutorials/main/data/" |
16 | 17 |
|
| 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 | + |
17 | 40 |
|
18 | 41 | def download_file(url: str, filename: str, dest: Path) -> None: |
19 | 42 | save_file = dest / filename |
20 | 43 | 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) |
22 | 46 | if path.exists(): |
23 | 47 | print(f"saved in {save_file}") |
24 | 48 | else: |
|
0 commit comments