Skip to content

Instantly share code, notes, and snippets.

@ryanpitts
Last active December 16, 2015 22:09
Show Gist options
  • Save ryanpitts/5505166 to your computer and use it in GitHub Desktop.
Save ryanpitts/5505166 to your computer and use it in GitHub Desktop.
from __future__ import division
from math import ceil
from importd import d
d(INSTALLED_APPS=["django.contrib.humanize"])
AGE_GENDER_GROUPS = [
{'0-10': {
'M': 244,
'F': 256,
}},
{'11-19': {
'M': 326,
'F': 410,
}},
{'20-29': {
'M': 550,
'F': 513,
}},
{'30-39': {
'M': 602,
'F': 547,
}},
{'40-49': {
'M': 814,
'F': 843,
}},
{'50-59': {
'M': 754,
'F': 901,
}},
{'60-69': {
'M': 399,
'F': 423,
}},
{'70+': {
'M': 277,
'F': 281,
}},
]
EDUCATIONAL_ATTAINMENT_GROUPS = [
{
'name': 'No school',
'value': 344,
},
{
'name': '< 8th grade',
'value': 105,
},
{
'name': 'No HS diploma',
'value': 264,
},
{
'name': 'HS grad',
'value': 756,
},
{
'name': 'Some college',
'value': 1012,
},
{
'name': 'College degree',
'value': 966,
},
{
'name': 'Advanced degree',
'value': 415,
},
]
MEDIAN_PER_CAPITA_INCOME = {
'geography': {
'name': 'Example County',
'value': 44885,
},
'state': {
'name': 'Washington',
'value': 43878,
},
'nation': {
'name': 'national',
'value': 41560,
},
'time_series': {
'labels': ['2007','2008','2009','2010','2011'],
'values': [45133,44638,42110,42244,44885],
}
}
EMPLOYMENT_COMMUTE_TYPES = {
'drove_alone': {
'name': 'Drove alone',
'commute_time': {
'value': 1154,
'label': '19:14'
},
},
'carpooled': {
'name': 'Carpooled',
'commute_time': {
'value': 1251,
'label': '20:51'
},
},
'public_transit': {
'name': 'Public transit',
'commute_time': {
'value': 1712,
'label': '28:32'
},
},
'walked': {
'name': 'Walked',
'commute_time': {
'value': 887,
'label': '14:47'
},
},
'taxi': {
'name': 'Taxi, motorcycle, bike',
'commute_time': {
'value': 1348,
'label': '22:28'
},
},
'home': {
'name': 'Worked at home',
'commute_time': {
'value': 0,
'label': '0:00'
},
},
}
DIVORCES_LAST_YEAR = {
'male': 387,
'female': 325,
}
MARITAL_STATUS_GROUPS = [
{'Never married': {
'M': 724,
'F': 536,
}},
{'Spouse present': {
'M': 550,
'F': 513,
}},
{'Spouse absent': {
'M': 326,
'F': 410,
}},
{'Separated': {
'M': 602,
'F': 547,
}},
{'Other': {
'M': 184,
'F': 113,
}},
{'Widowed': {
'M': 754,
'F': 901,
}},
{'Divorced': {
'M': 637,
'F': 541,
}},
]
HOUSING_PRICE_GROUPS = [
{
'name': '< $100K',
'value': 344,
},
{
'name': '$100K to $200K',
'value': 564,
},
{
'name': '$200K to $300K',
'value': 1756,
},
{
'name': '$300K to $400K',
'value': 812,
},
{
'name': '$400K to $500K',
'value': 846,
},
{
'name': '> $500K',
'value': 142,
},
]
MEDIAN_HOUSING_VALUES = {
'geography': {
'name': 'Example County',
'value': 224845,
},
'state': {
'name': 'Washington',
'value': 243878,
},
'nation': {
'name': 'national',
'value': 238160,
},
}
VETERAN_SERVICE_GROUPS = [
{
'name': 'Gulf War (2000s)',
'value': 484,
},
{
'name': 'Gulf War (1990s)',
'value': 605,
},
{
'name': 'Vietnam Era',
'value': 634,
},
{
'name': 'Korean War',
'value': 246,
},
{
'name': 'World War II',
'value': 102,
},
]
HEALTH_INSURANCE_VALUES = {
'geography': {
'name': 'Example County',
'value': 22,
},
'state': {
'name': 'Washington',
'value': 15,
},
'nation': {
'name': 'national',
'value': 18,
},
}
HEALTH_INSURANCE_GROUPS = [
{
'name': 'Employer- based',
'value': 1144,
},
{
'name': 'Direct purchase',
'value': 234,
},
{
'name': 'Medicare',
'value': 644,
},
{
'name': 'Medicaid',
'value': 722,
},
{
'name': 'TRICARE / military',
'value': 136,
},
{
'name': 'VA Health Care',
'value': 242,
},
]
LANGUAGE_GROUPS = [
{'English only': {
'N': 244,
'F': 256,
}},
{'Spanish': {
'N': 326,
'F': 410,
}},
{'Other Indo-European': {
'N': 550,
'F': 513,
}},
{'Asian / Pacific Island': {
'N': 602,
'F': 547,
}},
{'Other': {
'N': 277,
'F': 281,
}},
]
def get_ratio(num1, num2):
return round(num1 / num2, 2)*100
MEDIAN_HOUSING_INDEXES = {
'median_against_state': get_ratio(MEDIAN_HOUSING_VALUES['geography']['value'], MEDIAN_HOUSING_VALUES['state']['value']),
'median_against_nation': get_ratio(MEDIAN_HOUSING_VALUES['geography']['value'], MEDIAN_HOUSING_VALUES['nation']['value']),
'geography_against_nation_index': get_ratio(MEDIAN_HOUSING_VALUES['geography']['value'], MEDIAN_HOUSING_VALUES['nation']['value'])/2,
'state_against_nation_index': get_ratio(MEDIAN_HOUSING_VALUES['state']['value'], MEDIAN_HOUSING_VALUES['nation']['value'])/2,
}
HEALTH_INSURANCE_INDEXES = {
'median_against_state': get_ratio(HEALTH_INSURANCE_VALUES['geography']['value'], HEALTH_INSURANCE_VALUES['state']['value']),
'median_against_nation': get_ratio(HEALTH_INSURANCE_VALUES['geography']['value'], HEALTH_INSURANCE_VALUES['nation']['value']),
'geography_against_nation_index': get_ratio(HEALTH_INSURANCE_VALUES['geography']['value'], HEALTH_INSURANCE_VALUES['nation']['value'])/2,
'state_against_nation_index': get_ratio(HEALTH_INSURANCE_VALUES['state']['value'], HEALTH_INSURANCE_VALUES['nation']['value'])/2,
}
def drill(item):
if isinstance(item, int):
yield item
elif isinstance(item, list):
for i in item:
for result in drill(i):
yield result
elif isinstance(item, dict):
for k,v in item.items():
for result in drill(v):
yield result
def get_max_value(nested_dicts):
max_value = max([item for item in drill(nested_dicts)])
return max_value
page_context = {
'age_gender_groups': AGE_GENDER_GROUPS,
'age_gender_max': get_max_value(AGE_GENDER_GROUPS),
'educational_attainment_groups': EDUCATIONAL_ATTAINMENT_GROUPS,
'educational_attainment_max': get_max_value(EDUCATIONAL_ATTAINMENT_GROUPS),
'median_per_capita_income': MEDIAN_PER_CAPITA_INCOME,
'employment_commute_types': EMPLOYMENT_COMMUTE_TYPES,
'marital_status_groups': MARITAL_STATUS_GROUPS,
'marital_status_max': get_max_value(MARITAL_STATUS_GROUPS),
'divorces_last_year': DIVORCES_LAST_YEAR,
'housing_price_groups': HOUSING_PRICE_GROUPS,
'housing_price_group_max': get_max_value(HOUSING_PRICE_GROUPS),
'median_housing_values': MEDIAN_HOUSING_VALUES,
'median_housing_indexes': MEDIAN_HOUSING_INDEXES,
'veteran_service_groups': VETERAN_SERVICE_GROUPS,
'veteran_service_groups_max': get_max_value(VETERAN_SERVICE_GROUPS),
'health_insurance_values': HEALTH_INSURANCE_VALUES,
'health_insurance_indexes': HEALTH_INSURANCE_INDEXES,
'health_insurance_groups': HEALTH_INSURANCE_GROUPS,
'health_insurance_groups_max': get_max_value(HEALTH_INSURANCE_GROUPS),
'language_groups': LANGUAGE_GROUPS,
'language_groups_max': get_max_value(LANGUAGE_GROUPS),
}
@d("/")
def index(request):
return d.HttpResponse("hello world")
@d
def geo_profile(request):
return "geo_profile.html", page_context
@d
def test(request):
return "test.html", page_context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment