This repository was archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathecho.py
55 lines (43 loc) · 1.69 KB
/
echo.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
import dl
import messytables
import xypath
import orm
from header import headerheader
"""Value: dsID, region, indID, period, value, source, is_number
DataSet: dsID, last_updated, last_scraped, name
Indicator: indID, name, units
"""
baseurl = 'http://ec.europa.eu/echo/files/policies/strategy/gna_2012_2013.xls'
dataset = {"dsID": "echo",
"last_updated": None,
"last_scraped": orm.now(),
"name": "ECHO Europa"}
orm.DataSet(**dataset).save()
indicators = [{"indID": "gna-vi",
"name": "GNA Vulnerability Index",
"units": "index"},
{"indID": "gna-ci",
"name": "GNA Crisis Index",
"units": "index"}]
for indicator in indicators:
orm.Indicator(**indicator).save()
value_template = {"dsID": "echo",
"period": 2012,
"source": baseurl,
"is_number": True}
xls_raw = dl.grab(baseurl)
mt = messytables.excel.XLSTableSet(xls_raw).tables[0]
assert mt.name == "GNA Final Index (rank)"
xy = xypath.Table.from_messy(mt)
countries = xy.filter("ISO3").assert_one().fill(xypath.DOWN)
vuln_h = xy.filter("GNA Vulnerability Index (VI)").assert_one()
crisis_h = xy.filter("GNA Crisis Index (CI)").assert_one()
headerheader(xy.filter("ISO3").assert_one(), xypath.DOWN, xypath.RIGHT)
big = {'region': headerheader(xy.filter("ISO3").assert_one(), xypath.DOWN, xypath.RIGHT),
'indID': {'gna-vi': vuln_h.fill(xypath.DOWN),
'gna-ci': crisis_h.fill(xypath.DOWN)}}
for olap_row in xypath.xyzzy.xyzzy(xy, big, valuename="value"):
print olap_row
full_olap = dict(value_template)
full_olap.update(olap_row)
orm.Value(**full_olap).save()