aboutsummaryrefslogtreecommitdiffstats
path: root/administrative-codes
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2025-04-17 12:23:38 +0200
committerGuilhem Moulin <guilhem@fripost.org>2025-04-19 05:14:48 +0200
commit2abf2297aabb355b72c6ae9e0aaf350f7a6cbe9d (patch)
treedd1a157dc6e08a96fdb99d79a6cf2e43047f3650 /administrative-codes
parent4bcf4d8a3229c89813cbf3c05f4ef14cc80202d9 (diff)
Add type hints and refactor a bit to please pylint.
Diffstat (limited to 'administrative-codes')
-rwxr-xr-xadministrative-codes/csv2json9
-rwxr-xr-xadministrative-codes/update21
2 files changed, 19 insertions, 11 deletions
diff --git a/administrative-codes/csv2json b/administrative-codes/csv2json
index 7c22666..6dd6ad7 100755
--- a/administrative-codes/csv2json
+++ b/administrative-codes/csv2json
@@ -18,6 +18,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#----------------------------------------------------------------------
+# pylint: disable=missing-module-docstring
+
import sys
import csv
from pathlib import Path
@@ -25,13 +27,14 @@ import json
basedir = Path(sys.argv[0]).parent
data = {}
-def readCSV(path):
- with open(path, mode='r', newline='') as fp:
+def readCSV(pathname): # pylint: disable=invalid-name
+ """Read CSV"""
+ with open(pathname, mode='r', newline='', encoding='utf-8') as fp:
reader = csv.DictReader(fp, delimiter='\t', dialect='unix')
for row in reader:
code = row['Code']
if code in data:
- raise Exception(f'Duplicate code {code}')
+ raise RuntimeError(f'Duplicate code {code}')
data[code] = row['Name']
# The source (SCB) lists all codes in same file: they differ only in
diff --git a/administrative-codes/update b/administrative-codes/update
index 855f73b..fa6a6da 100755
--- a/administrative-codes/update
+++ b/administrative-codes/update
@@ -18,6 +18,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#----------------------------------------------------------------------
+# pylint: disable=missing-module-docstring
+
import re
import sys
import csv
@@ -30,13 +32,15 @@ import xlrd
#
# Unfortunately SCB doesn't provide a CSV, so we download their xls file and produce our own.
# https://www.scb.se/hitta-statistik/regional-statistik-och-kartor/regionala-indelningar/lan-och-kommuner/lan-och-kommuner-i-kodnummerordning/
-r = requests.get('https://www.scb.se/contentassets/7a89e48960f741e08918e489ea36354a/kommunlankod_2024.xls')
+r = requests.get('https://www.scb.se/contentassets/7a89e48960f741e08918e489ea36354a/'
+ 'kommunlankod_2024.xls',
+ timeout=30)
r.raise_for_status()
if 'content-type' not in r.headers:
- raise Exception('Missing Content-Type from response headers')
-if r.headers['content-type'] not in ['application/vnd.ms-excel', 'application/octet-stream']:
- raise Exception(f"Unsupported Content-Type: {r.headers['content-type']}")
+ raise RuntimeError('Missing Content-Type from response headers')
+if r.headers['content-type'] not in ('application/vnd.ms-excel', 'application/octet-stream'):
+ raise RuntimeError(f"Unsupported Content-Type: {r.headers['content-type']}")
xls = xlrd.open_workbook(file_contents=r.content)
sheets = xls.sheet_names()
@@ -71,15 +75,16 @@ for i in range(sheet.nrows):
counties.append(row)
basedir = Path(sys.argv[0]).parent
-def writeCSV(filename, data):
+def writeCSV(filename, data): # pylint: disable=invalid-name
+ """Write CSV file."""
fieldnames = ['Code', 'Name']
path = basedir.joinpath(filename).with_suffix('.csv')
- with path.open(mode='w', newline='') as fp:
+ with path.open(mode='w', newline='', encoding='utf-8') as fp:
writer = csv.DictWriter(fp, fieldnames=fieldnames, delimiter='\t',
quoting=csv.QUOTE_MINIMAL, dialect='unix')
writer.writeheader()
- for row in data:
- writer.writerow(row)
+ for row2 in data:
+ writer.writerow(row2)
print(f'Wrote {len(data)} rows in {path}', file=sys.stderr)
writeCSV('counties', counties)