Skip to content

Commit 2060a86

Browse files
committed
add ruff
1 parent e1708ba commit 2060a86

6 files changed

Lines changed: 56 additions & 42 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ It is a simple helper for cases when one needs to download data in systems like
77

88
from data import get_data
99
get_data(url="https://raw.githubusercontent.com/ddmms/data-tutorials/main/data/LiFePO4_supercell.cif", filename="LiFePO4_supercell.cif", folder="data")
10-
11-

data_tutorials/data.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1-
# -*- coding: utf-8 -*-
21
# Author; alin m elena, alin@elena.re
32
# Contribs;
43
# Date: 26-07-2024
54
# ©alin m elena, GPL v3 https://www.gnu.org/licenses/gpl-3.0.en.html
5+
"""
6+
Simple module to download a file from a URL and save it in a specific folder.
7+
"""
68

79
from urllib.request import urlretrieve
810
from pathlib import Path
11+
912
# this is only for python 3.9 once done use str| list[str]
1013
from typing import Union
1114

12-
default_url = "https://raw.githubusercontent.com/ddmms/data-tutorials/main/data/"
13-
def download_file(url: str, filename:str, dest: Path) -> None:
14-
save_file = dest/filename
15+
DEFAULT_URL = "https://raw.githubusercontent.com/ddmms/data-tutorials/main/data/"
16+
17+
18+
def download_file(url: str, filename: str, dest: Path) -> None:
19+
save_file = dest / filename
1520
print(f"try to download {filename} from {url} and save it in {save_file}")
16-
path, headers = urlretrieve(url+filename, save_file)
21+
path, _ = urlretrieve(url + filename, save_file)
1722
if path.exists():
1823
print(f"saved in {save_file}")
1924
else:
2025
print(f"{save_file} could not be downloaded, check url.")
2126

22-
def get_data(url: str=default_url, filename: Union[str, list[str]] = "", folder: str="data") -> None:
27+
28+
def get_data(
29+
url: str = DEFAULT_URL, filename: Union[str, list[str]] = "", folder: str = "data"
30+
) -> None:
2331
p = Path(folder)
2432
p.mkdir(parents=True, exist_ok=True)
25-
if isinstance(filename,str):
26-
download_file(url,filename,p)
33+
if isinstance(filename, str):
34+
download_file(url, filename, p)
2735
elif isinstance(filename, list):
2836
for f in filename:
29-
download_file(url,f,p)
37+
download_file(url, f, p)

pyproject.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ name = "data-tutorials"
88
dynamic = ["version"]
99
dependencies = [
1010
"pytest",
11-
"ruff",
11+
"ruff",
1212
]
1313
requires-python = ">=3.9"
1414
authors = [
@@ -45,10 +45,8 @@ pythonpath = [
4545
]
4646

4747
[tool.ruff]
48-
line-length = 100
49-
indent-width = 2
48+
line-length = 88
5049
target-version = "py38"
51-
line-ending = "auto"
5250

5351
[tool.ruff.lint]
5452

release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
version=$1
44

55
git tag -f -a v$version -m "release $version"
6-
git push --tags
6+
git push --tags

tests/test_file.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1-
# -*- coding: utf-8 -*-
21
# Author; alin m elena, alin@elena.re
32
# Contribs;
43
# Date: 26-07-2024
54
# ©alin m elena, GPL v3 https://www.gnu.org/licenses/gpl-3.0.en.html
6-
import pytest
7-
from data_tutorials.data import get_data
8-
import hashlib
5+
6+
"""
7+
Tests various downloads.
8+
"""
9+
910
from pathlib import Path
11+
import hashlib
12+
from data_tutorials.data import get_data
13+
1014

1115
def test_download_file():
12-
get_data(filename="LiFePO4_supercell.cif",folder="data-test")
13-
with open(Path("data-test")/"LiFePO4_supercell.cif", "rb") as fd:
14-
try:
15-
h = hashlib.file_digest(fd, "sha256")
16-
except AttributeError:
17-
h = hashlib.sha256()
18-
h.update(fd.read())
19-
assert h.hexdigest() == "ea9a538dde5bb84b92e9478dbcc078bb560b28a4f5e4b0469d416bff36be272e"
16+
get_data(filename="LiFePO4_supercell.cif", folder="data-test")
17+
with open(Path("data-test") / "LiFePO4_supercell.cif", "rb") as fd:
18+
try:
19+
h = hashlib.file_digest(fd, "sha256")
20+
except AttributeError:
21+
h = hashlib.sha256()
22+
h.update(fd.read())
23+
assert (
24+
h.hexdigest()
25+
== "ea9a538dde5bb84b92e9478dbcc078bb560b28a4f5e4b0469d416bff36be272e"
26+
)
27+
2028

2129
def test_download_files():
22-
files = ["LiFePO4_supercell.cif","h2o.xyz"]
23-
sha256={'LiFePO4_supercell.cif':"ea9a538dde5bb84b92e9478dbcc078bb560b28a4f5e4b0469d416bff36be272e",
24-
'h2o.xyz': "522ab1d36d213e48ab6e08517ec3f648c378d4b9efd257feadc64a1d8f3e66d6"}
25-
get_data(filename=files,folder="data-test")
26-
for f in files:
27-
with open(Path("data-test")/f, "rb") as fd:
28-
try:
29-
h = hashlib.file_digest(fd, "sha256")
30-
except AttributeError:
31-
h = hashlib.sha256()
32-
h.update(fd.read())
33-
assert h.hexdigest() == sha256[f]
30+
files = ["LiFePO4_supercell.cif", "h2o.xyz"]
31+
sha256 = {
32+
"LiFePO4_supercell.cif": "ea9a538dde5bb84b92e9478dbcc078bb560b28a4f5e4b0469d416bff36be272e",
33+
"h2o.xyz": "522ab1d36d213e48ab6e08517ec3f648c378d4b9efd257feadc64a1d8f3e66d6",
34+
}
35+
get_data(filename=files, folder="data-test")
36+
for f in files:
37+
with open(Path("data-test") / f, "rb") as fd:
38+
try:
39+
h = hashlib.file_digest(fd, "sha256")
40+
except AttributeError:
41+
h = hashlib.sha256()
42+
h.update(fd.read())
43+
assert h.hexdigest() == sha256[f]

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
envlist = py3
33

44
[testenv]
5-
deps = pytest
6-
commands = pytest
5+
deps = pytest
6+
commands = pytest

0 commit comments

Comments
 (0)