8000 CLN: improved style issues reported by ruff · larray-project/larray_eurostat@b987a06 · GitHub
[go: up one dir, main page]

Skip to content

Commit b987a06

Browse files
committed
CLN: improved style issues reported by ruff
1 parent 7402c3c commit b987a06

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

larray_eurostat/tsv.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,34 @@
22
from io import StringIO
33
from urllib.request import urlopen
44

5-
from larray import read_eurostat, Session
5+
from larray import Session, read_eurostat
66

77

8-
def remove_chars(s, chars):
8+
def _remove_chars(s, chars):
99
return s.translate({ord(c): None for c in chars})
1010

1111

1212
EUROSTAT_BASEURL = "https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&file="
1313

1414

15-
def _get_one(indicator, drop_markers=True):
16-
"""Get one Eurostat indicator and return it as an array"""
15+
def _get_one(indicator, *, drop_markers=True):
16+
"""Get one Eurostat indicator and return it as an array."""
17+
url = f"{EUROSTAT_BASEURL}data/{indicator}.tsv.gz"
18+
with urlopen(url) as f, gzip.open(f, mode='rt') as fgz: # noqa: S310
19+
try:
20+
s = fgz.read()
21+
if drop_markers:
22+
first_line_end = s.index('\n')
23+
# strip markers except on first line
24+
s = s[:first_line_end] + _remove_chars(s[first_line_end:], ' dbefcuipsrzn:')
25+
return read_eurostat(StringIO(s))
26+
except Exception as e:
27+
e.args = (e.args[0] + f"\nCan't open file {f.geturl()}",)
28+
raise
1729

18-
with urlopen(f"{EUROSTAT_BASEURL}data/{indicator}.tsv.gz") as f:
19-
with gzip.open(f, mode='rt') as fgz:
20-
try:
21-
s = fgz.read()
22-
if drop_markers:
23-
first_line_end = s.index('\n')
24-
# strip markers except on first line
25-
s = s[:first_line_end] + remove_chars(s[first_line_end:], ' dbefcuipsrzn:')
26-
return read_eurostat(StringIO(s))
27-
except Exception as e:
28-
e.args = (e.args[0] + f"\nCan't open file {f.geturl()}",)
29-
raise
3030

31-
32-
def eurostat_get(indicators, drop_markers=True):
33-
"""Gets one or several Eurostat indicators and return them as an array or a session.
31+
def eurostat_get(indicators, *, drop_markers=True):
32+
"""Get one or several Eurostat indicators and return them as an array or a session.
3433
3534
Parameters
3635
----------
@@ -75,5 +74,4 @@ def eurostat_get(indicators, drop_markers=True):
7574
"""
7675
if isinstance(indicators, (tuple, list)):
7776
return Session({i: _get_one(i, drop_markers=drop_markers) for i in indicators})
78-
else:
79-
return _get_one(indicators, drop_markers=drop_markers)
77+
return _get_one(indicators, drop_markers=drop_markers)

0 commit comments

Comments
 (0)
0