|
2 | 2 | from io import StringIO |
3 | 3 | from urllib.request import urlopen |
4 | 4 |
|
5 | | -from larray import read_eurostat, Session |
| 5 | +from larray import Session, read_eurostat |
6 | 6 |
|
7 | 7 |
|
8 | | -def remove_chars(s, chars): |
| 8 | +def _remove_chars(s, chars): |
9 | 9 | return s.translate({ord(c): None for c in chars}) |
10 | 10 |
|
11 | 11 |
|
12 | 12 | EUROSTAT_BASEURL = "https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&file=" |
13 | 13 |
|
14 | 14 |
|
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 |
17 | 29 |
|
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 |
30 | 30 |
|
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. |
34 | 33 |
|
35 | 34 | Parameters |
36 | 35 | ---------- |
@@ -75,5 +74,4 @@ def eurostat_get(indicators, drop_markers=True): |
75 | 74 | """ |
76 | 75 | if isinstance(indicators, (tuple, list)): |
77 | 76 | 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