Skip to content

Commit 0809e37

Browse files
committed
Switch to more flexible tqmd progress bar
1 parent 524d05d commit 0809e37

File tree

6 files changed

+48
-44
lines changed

6 files changed

+48
-44
lines changed

caltechdata_api/download_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import requests, argparse
2-
from tqdm import tnrange, tqdm_notebook
2+
from tqdm.auto import tqdm
33

44

55
def download_url(doi, media_type=None):
@@ -31,7 +31,7 @@ def download_file(doi, fname=None, media_type=None):
3131
else:
3232
with open(fname, "wb") as f:
3333
total_length = int(r.headers.get("content-length"))
34-
pbar = tnrange(int(total_length / 1024), unit="B")
34+
pbar = tqdm(total=int(total_length / 1024), unit="B")
3535
for chunk in r.iter_content(chunk_size=1024):
3636
if chunk:
3737
pbar.update()

example_download_and_upload.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 4,
5+
"execution_count": 1,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -18,22 +18,22 @@
1818
{
1919
"data": {
2020
"application/vnd.jupyter.widget-view+json": {
21-
"model_id": "7e6735a34e5c4448bbeb72920ed7b963",
21+
"model_id": "f2aac6cbb1ef448f8899b204ffb1dc2c",
2222
"version_major": 2,
2323
"version_minor": 0
2424
},
2525
"text/plain": [
26-
"HBox(children=(IntProgress(value=0, max=665896), HTML(value='')))"
26+
"HBox(children=(HTML(value=''), FloatProgress(value=0.0, max=15990.0), HTML(value='')))"
2727
]
2828
},
2929
"metadata": {},
3030
"output_type": "display_data"
3131
}
3232
],
3333
"source": [
34-
"#By default will download to file named 10.22002-D1.1945\n",
34+
"#By default will download to file named 10.22002-D1.1098\n",
3535
"#Can provide filename of interest using fname option\n",
36-
"filen = download_file('10.22002/D1.1945')"
36+
"filen = download_file('10.22002/D1.1098')"
3737
]
3838
},
3939
{
@@ -88,9 +88,9 @@
8888
"name": "python",
8989
"nbconvert_exporter": "python",
9090
"pygments_lexer": "ipython3",
91-
"version": "3.7.4"
91+
"version": "3.8.5"
9292
}
9393
},
9494
"nbformat": 4,
95-
"nbformat_minor": 2
95+
"nbformat_minor": 4
9696
}

tests/10.22002-D1.1098

15.6 MB
Binary file not shown.

tests/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
def load_json_path(path):
2222
"""Helper method for loading a JSON example file from a path."""
2323
path_base = dirname(__file__)
24-
with io.open(join(path_base, path), encoding='utf-8') as file:
24+
with io.open(join(path_base, path), encoding="utf-8") as file:
2525
content = file.read()
2626
return json.loads(content)
2727

28+
2829
def write_json_path(path, metadata):
2930
"""Helper method for writing a JSON example file to a path."""
3031
path_base = dirname(__file__)
3132
path_full = join(path_base, path)
3233
print(path_full)
3334
print(metadata)
34-
with io.open(path_full, 'w', encoding='utf-8') as file:
35+
with io.open(path_full, "w", encoding="utf-8") as file:
3536
json.dump(metadata, file)
36-

tests/test_conversion.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,47 @@
1818
from helpers import load_json_path, write_json_path
1919

2020
CALTECHDATA_FILES = [
21-
'data/caltechdata/210.json',
22-
'data/caltechdata/266.json',
23-
'data/caltechdata/267.json',
24-
'data/caltechdata/268.json',
25-
'data/caltechdata/283.json',
26-
'data/caltechdata/293.json',
27-
'data/caltechdata/301.json',
28-
'data/caltechdata/970.json',
29-
'data/caltechdata/1171.json',
30-
'data/caltechdata/1235.json',
31-
'data/caltechdata/1250.json',
32-
'data/caltechdata/1259.json',
33-
'data/caltechdata/1300.json',
21+
"data/caltechdata/210.json",
22+
"data/caltechdata/266.json",
23+
"data/caltechdata/267.json",
24+
"data/caltechdata/268.json",
25+
"data/caltechdata/283.json",
26+
"data/caltechdata/293.json",
27+
"data/caltechdata/301.json",
28+
"data/caltechdata/970.json",
29+
"data/caltechdata/1171.json",
30+
"data/caltechdata/1235.json",
31+
"data/caltechdata/1250.json",
32+
"data/caltechdata/1259.json",
33+
"data/caltechdata/1300.json",
3434
]
3535

3636
DATACITE4_FILES = [
37-
'data/datacite4/210.json',
38-
'data/datacite4/266.json',
39-
'data/datacite4/267.json',
40-
'data/datacite4/268.json',
41-
'data/datacite4/283.json',
42-
'data/datacite4/293.json',
43-
'data/datacite4/301.json',
44-
'data/datacite4/970.json',
45-
'data/datacite4/1171.json',
46-
'data/datacite4/1235.json',
47-
'data/datacite4/1250.json',
48-
'data/datacite4/1259.json',
49-
'data/datacite4/1300.json',
37+
"data/datacite4/210.json",
38+
"data/datacite4/266.json",
39+
"data/datacite4/267.json",
40+
"data/datacite4/268.json",
41+
"data/datacite4/283.json",
42+
"data/datacite4/293.json",
43+
"data/datacite4/301.json",
44+
"data/datacite4/970.json",
45+
"data/datacite4/1171.json",
46+
"data/datacite4/1235.json",
47+
"data/datacite4/1250.json",
48+
"data/datacite4/1259.json",
49+
"data/datacite4/1300.json",
5050
]
5151

5252

53-
@pytest.mark.parametrize('example_caltechdata', CALTECHDATA_FILES)
53+
@pytest.mark.parametrize("example_caltechdata", CALTECHDATA_FILES)
5454
def test_example_json_validates(example_caltechdata):
5555
"""Test the example file validates against the JSON schema after
5656
decustomizing to DataCite."""
5757
example_json = load_json_path(example_caltechdata)
5858
datacite4 = decustomize_schema(example_json)
5959
validator4.validate(datacite4)
6060
example_json = load_json_path(example_caltechdata)
61-
datacite43 = decustomize_schema(example_json,schema="43")
61+
datacite43 = decustomize_schema(example_json, schema="43")
6262
validator43.validate(datacite43)
63-
outname = example_caltechdata.split('/')[-1]
63+
outname = example_caltechdata.split("/")[-1]
6464
write_json_path(f"data/datacite43/{outname}", datacite43)
65-

tests/test_download.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414

1515
from caltechdata_api import download_url, download_file
1616

17+
1718
def test_download():
1819
"""Test that downloads from the DataCite Media API work."""
19-
example_doi = '10.22002/D1.1945'
20-
expected_url = 'https://data.caltech.edu/tindfiles/serve/0cedc067-1ff0-4fc9-871a-2869f3a4957d/'
20+
example_doi = "10.22002/D1.1098"
21+
expected_url = (
22+
"https://data.caltech.edu/tindfiles/serve/293d37c5-73f2-4016-bcd5-76cf353ff9d8/"
23+
)
2124
assert expected_url == download_url(example_doi)
25+
filen = download_file(example_doi)
26+
assert filen == "10.22002-D1.1098"

0 commit comments

Comments
 (0)