-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcreate_readme.py
63 lines (40 loc) · 1.44 KB
/
create_readme.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
62
63
import re
from itertools import groupby
import yaml
from jinja2 import Template
from utils import map_reduce, denormalize
def nest(seq, keys):
if not keys:
return seq
first_key, *rest_keys = keys
def keyfunc(x): return x[first_key]
grouped = groupby(sorted(seq, key=keyfunc), key=keyfunc)
result = {}
for key, value in grouped:
result[key] = nest(list(value), rest_keys)
return result
def toc_link(link):
return "#" + re.sub(r"[^a-zA-Z-]", "", link.lower().replace(" ", "-"))
def composed(f, g):
return lambda x: f(g(x))
def unique_by_country(l):
return {k['country']: k for k in l}.values()
def remove_duplicate_country_entry(job):
job['geo'] = unique_by_country(job['geo'])
return job
with open("data.yaml", "r") as stream:
try:
DATA = yaml.safe_load(stream)
DENORMALIZED_JOBS = map_reduce(
composed(denormalize, remove_duplicate_country_entry), DATA["jobs"])
NESTED_JOBS = nest(DENORMALIZED_JOBS, ["country", "field"])
FIELDS = NESTED_JOBS.keys()
NESTED_JOBPORTALS = nest(DATA["jobportals"], ["country"])
TEMPLATE = Template(open("template.md").read())
README = TEMPLATE.render(
jobs=NESTED_JOBS, jobportals=NESTED_JOBPORTALS, toc_link=toc_link
)
with open("../README.md", "w") as readme_file:
readme_file.write(README)
except yaml.YAMLError as error:
print(error)