-
Notifications
You must be signed in to change notification settings - Fork 37
/
census.py
152 lines (124 loc) · 6.43 KB
/
census.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import web
from settings import db
total_pop = ['/TotalPopulation/Total' ]
edu_totals = ['/Population25YearsAndOver/Female',
'/Population25YearsAndOver/Male']
edu_college = [
'/Population25YearsAndOver/Female/AssociateDegree',
'/Population25YearsAndOver/Female/BachelorsDegree',
'/Population25YearsAndOver/Female/DoctorateDegree',
'/Population25YearsAndOver/Female/MastersDegree',
'/Population25YearsAndOver/Female/ProfessionalSchoolDegree',
'/Population25YearsAndOver/Female/SomeCollege1OrMoreYearsNoDegree',
'/Population25YearsAndOver/Female/SomeCollegeLessThan1Year',
'/Population25YearsAndOver/Male/AssociateDegree',
'/Population25YearsAndOver/Male/BachelorsDegree',
'/Population25YearsAndOver/Male/DoctorateDegree',
'/Population25YearsAndOver/Male/MastersDegree',
'/Population25YearsAndOver/Male/SomeCollege1OrMoreYearsNoDegree',
'/Population25YearsAndOver/Male/SomeCollegeLessThan1Year',
]
edu_prof_degree = ['/Population25YearsAndOver/Male/ProfessionalSchoolDegree']
edu_nocollege = [
'/Population25YearsAndOver/Male/NoSchoolingCompleted',
'/Population25YearsAndOver/Male/NurseryTo4thGrade',
'/Population25YearsAndOver/Male/5thAnd6thGrade',
'/Population25YearsAndOver/Male/7thAnd8thGrade',
'/Population25YearsAndOver/Male/9thGrade',
'/Population25YearsAndOver/Male/10thGrade',
'/Population25YearsAndOver/Male/11thGrade',
'/Population25YearsAndOver/Male/12thGradeNoDiploma',
'/Population25YearsAndOver/Male/HighSchoolGraduateincludesEquivalency',
'/Population25YearsAndOver/Female/NoSchoolingCompleted',
'/Population25YearsAndOver/Female/NurseryTo4thGrade',
'/Population25YearsAndOver/Female/5thAnd6thGrade',
'/Population25YearsAndOver/Female/7thAnd8thGrade',
'/Population25YearsAndOver/Female/9thGrade',
'/Population25YearsAndOver/Female/10thGrade',
'/Population25YearsAndOver/Female/11thGrade',
'/Population25YearsAndOver/Female/12thGradeNoDiploma',
'/Population25YearsAndOver/Female/HighSchoolGraduateincludesEquivalency',
]
marital_stat_totals = [ '/Population15YearsAndOver/Male',
'/Population15YearsAndOver/Female' ]
marital_stat_never_married = [ '/Population15YearsAndOver/Male/NeverMarried',
'/Population15YearsAndOver/Female/NeverMarried']
marital_stat_divorced = [ '/Population15YearsAndOver/Male/Divorced',
'/Population15YearsAndOver/Female/Divorced' ]
mil_totals = ['/Population18YearsAndOver/Total']
#mil_total = ['/Population18YearsAndOver/Male','/Population18YearsAndOver/Female']
mil_cur = [ '/Population18YearsAndOver/Male/18To64Years/InArmedForces',
'/Population18YearsAndOver/Male/65YearsAndOver/InArmedForces',
'/Population18YearsAndOver/Female/18To64Years/InArmedForces',
'/Population18YearsAndOver/Female/65YearsAndOver/InArmedForces' ]
mil_vet = [ '/Population18YearsAndOver/Male/18To64Years/Civilian/Veteran',
'/Population18YearsAndOver/Male/65YearsAndOver/Civilian/Veteran',
'/Population18YearsAndOver/Female/18To64Years/Civilian/Veteran',
'/Population18YearsAndOver/Female/65YearsAndOver/Civilian/Veteran' ]
mil_none = [ '/Population18YearsAndOver/Male/18To64Years/Civilian/Nonveteran',
'/Population18YearsAndOver/Male/65YearsAndOver/Civilian/Nonveteran',
'/Population18YearsAndOver/Female/18To64Years/Civilian/Nonveteran',
'/Population18YearsAndOver/Female/65YearsAndOver/Civilian/Nonveteran' ]
born_totals = ['/TotalPopulation/Total']
born_native = ['/TotalPopulation/Native']
born_foreign = ['/TotalPopulation/ForeignBorn']
def query_census(location, hr_keys):
# Use DISTINCT since some hr_keys map to multiple internal_keys (but should
# have same value).
#q = db.select('census', what='SUM(DISTINCT(value))', where=web.sqlors('hr_key=', hr_keys)+' AND location='+web.sqlquote(location))
q = db.query('SELECT SUM(value) FROM (SELECT DISTINCT value, hr_key FROM census WHERE '+web.sqlors('hr_key=', hr_keys)+' AND district_id='+web.sqlquote(location)+') AS foo;')
if not q: return None
return q[0].sum
# This is for the population of 18 years and older.
def mil_service(location):
tot = query_census(location, mil_totals)
cur = query_census(location, mil_cur)
vet = query_census(location, mil_vet)
none = query_census(location, mil_none)
return {'pct_mil_cur': cur / tot,
'pct_mil_vet': vet / tot,
'pct_mil_none': vet / tot,
'mil_total': tot}
# This is for the entire population
def born_locations(location):
tot = query_census(location, born_totals)
native = query_census(location, born_native)
foreign = query_census(location, born_foreign)
return {'pct_born_foreign': foreign / tot,
'pct_born_native': native / tot,
'born_total': tot}
# This is for the population of 15 years and older.
def marital_stat(location):
tot = query_census(location, marital_stat_totals)
never_married = query_census(location, marital_stat_never_married)
divorced = query_census(location, marital_stat_divorced)
return {'pct_never_married': never_married / tot,
'pct_divorced': divorced / tot,
'marital_stat_total': tot}
# This is for the population of 25 years and older.
def education(location):
tot = query_census(location, edu_totals)
some_college = query_census(location, edu_college)
professional = query_census(location, edu_prof_degree)
no_college = query_census(location, edu_nocollege)
return {'pct_some_college': some_college / tot,
'pct_professional': professional / tot,
'pct_no_college': no_college / tot,
'edu_total': tot}
# This is for the entire population
def get_total_pop(location):
tot = query_census(location, total_pop)
return {'total_pop': tot}
if __name__ == "__main__":
from pprint import pprint
states = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA',
'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI',
'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY',
'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT',
'WA', 'WI', 'WV', 'WY']
for state in states:
print state
pprint(education(state))
pprint(marital_stat(state))
pprint(mil_service(state))
pprint(get_total_pop(state))