-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·64 lines (45 loc) · 1.54 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /usr/bin/env python3
# This doesn't need to run at build time, as the result of the generation is checked into Git.
import pandas as pd
import json
import sys
if len(sys.argv) < 2:
print("Pass the location of the population.csv as first argument.")
exit()
POP_YEAR=2018
with open("data/european-countries.json") as f:
europe_data = f.read()
europe = set(json.loads(europe_data))
population = pd.read_csv(sys.argv[1])
pop_outdata = dict()
for i, row in population.iterrows():
cc = row["Country Code"]
if cc in europe and row["Year"] == POP_YEAR:
if not cc in pop_outdata:
pop_outdata[cc] = dict()
pop_outdata[cc][row["Year"]] = row["Value"]
elm_header = """module Data exposing
( europeanCountries
, population
)
-- This module is autogenerated! Look at generate.py for changing the content.
-- See https://github.com/datasets/population for source of the data.
import Dict
"""
with open("src/Data.elm", "w") as f:
f.write(elm_header)
countries_list = list()
for c in list(europe):
countries_list.append(f"\"{c}\"")
f.write("europeanCountries =\n [ " + ", ".join(countries_list) + " ]")
f.write("\n\n\n")
f.write("population =\n")
f.write(" Dict.fromList [ ")
pop_list = list()
for k, v in pop_outdata.items():
pop_list.append("( \"{cc}\", Dict.fromList [ ( {year}, {pop} ) ] )".format(
cc = k,
year = POP_YEAR,
pop = v[POP_YEAR]
))
f.write(", ".join(pop_list) + " ]\n")