Skip to content

Commit

Permalink
feat: use tqdm to display downloading progress;
Browse files Browse the repository at this point in the history
  • Loading branch information
WenjieDu committed Jul 8, 2024
1 parent e6d536e commit 8811422
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tqdm
numpy
scikit-learn
pandas
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extend-ignore =

# basic dependencies
basic =
tqdm
numpy
scikit-learn
pandas
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
packages=find_packages(exclude=["tests"]),
include_package_data=True,
install_requires=[
"tqdm",
"numpy",
"scikit-learn",
"pandas",
Expand Down
26 changes: 24 additions & 2 deletions tsdb/utils/downloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import os
import shutil
import tempfile
import urllib.request
import warnings
from typing import Optional

import requests
from tqdm import tqdm

from .logging import logger
from ..database import DATABASE

Expand Down Expand Up @@ -54,7 +56,27 @@ def _download_and_extract(url: str, saving_path: str) -> Optional[str]:

# download and save the raw dataset
try:
urllib.request.urlretrieve(url, raw_data_saving_path)
with requests.get(url, stream=True) as r:
r.raise_for_status()
chunk_size = 8192
try:
size = int(r.headers["Content-Length"])
except KeyError:
size = None

with tqdm(
unit="B",
unit_scale=True,
unit_divisor=1024,
miniters=1,
desc=f"Downloading {file_name}",
total=size,
) as pbar:
with open(raw_data_saving_path, "wb") as f:
for chunk in r.iter_content(chunk_size=chunk_size):
f.write(chunk)
pbar.update(len(chunk))

except Exception as e:
shutil.rmtree(saving_path, ignore_errors=True)
shutil.rmtree(raw_data_saving_path, ignore_errors=True)
Expand Down

0 comments on commit 8811422

Please sign in to comment.