-
Notifications
You must be signed in to change notification settings - Fork 10
/
manage.py
92 lines (72 loc) · 2.83 KB
/
manage.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from colombia import create_app, models, core, factories
from flask.ext.script import Manager, Shell
import random
app = create_app()
manager = Manager(app)
def _make_context():
from colombia import dataset_tools, datasets
return dict(app=app, db=core.db, models=models, factories=factories, dataset_tools=dataset_tools, datasets=datasets)
manager.add_command("shell", Shell(make_context=_make_context))
@manager.option("-n", help="Number of dummy things")
def dummy(n=10):
"""Generate dummy data."""
if not app.debug:
raise Exception("Unsafe to generate dummy data while not in DEBUG.")
# Generate a set of products and departments.
departments = []
for x in range(0, 6):
departments.append(factories.Location(level="department"))
sections = []
for x in range(0, 4):
sections.append(factories.HSProduct(level="section"))
core.db.session.commit()
two_digits = []
for x in range(0, 8):
parent_id = random.choice(sections).id
two_digits.append(factories.HSProduct(level="2digit",
parent_id=parent_id))
core.db.session.commit()
products = []
for x in range(0, 20):
parent_id = random.choice(two_digits).id
products.append(factories.HSProduct(level="4digit",
parent_id=parent_id))
core.db.session.commit()
# Generate what products exist in which departments and by how much
for d in departments:
if random.random() < 0.2:
# This place focuses on a few products
for x in range(4):
factories.DepartmentProductYear(
department=d,
product=random.choice(products),
year=2008
)
else:
# This place is a diverse economy
for x in range(20):
factories.DepartmentProductYear(
department=d,
product=random.choice(products),
year=2008
)
# Permute data for the following years according to this year.
for d in departments:
dpys = models.DepartmentProductYear\
.query.filter_by(department=d).all()
for year in range(2009, 2013):
if random.random() < 0.1:
delta = random.random() - 0.5
else:
delta = 5 * (random.random() - 0.5)
for dpy in dpys:
factories.DepartmentProductYear(
department=dpy.department,
product=dpy.product,
year=year,
import_value=dpy.import_value * delta,
export_value=dpy.export_value * delta
)
core.db.session.commit()
if __name__ == "__main__":
manager.run()