diff --git a/.gitignore b/.gitignore index bee8a64..c18dd8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -__pycache__ +__pycache__/ diff --git a/population.py b/population.py index ce8376d..72784e0 100644 --- a/population.py +++ b/population.py @@ -1,16 +1,37 @@ +from util.webapi import get_json, parse_query +import numpy as np + + class Person: - def __init__(self, age, income, gender): - self.age = age - self.income = income - self.gender = gender - self.household = 0 + def __init__(self, age, income, gender): + self.age = age + self.income = income + self.gender = gender + self.household = 0 + class UrbanActor: - def __init__(self, person): - self.demographic = {} - self.demographic.update(person.__dict__) + def __init__(self, person): + self.demographic = {} + self.demographic.update(person.__dict__) + def generate(n, config): - print("-- GENERATE POPULATION --") - p = Person(23, 50000, 'male') - ua = UrbanActor(p) + print("-- GENERATE POPULATION --") + p = Person(23, 50000, 'male') + ua = UrbanActor(p) + + +if __name__ == "__main__": + # cols = ["PWGTP", "COW", "AGEP"] + # url = "https://api.census.gov/data/2018/acs/acs5/pums" + # query = { + # "get": cols, + # "ucgid": "7950000US4500103" + # } + + url = "https://api.census.gov/data/2018/acs/acs1/pums?get=PWGTP,POWPUMA,AGEP,JWRIP,SEX,JWAP,JWDP&ucgid=7950000US3703001,7950000US3703002" + res = get_json(url, {}) + + data = np.array(res)[1:] + print(data[:, 0].astype(int).sum()) diff --git a/util/webapi.py b/util/webapi.py new file mode 100644 index 0000000..465c4ad --- /dev/null +++ b/util/webapi.py @@ -0,0 +1,31 @@ +import json +import requests +from urllib.parse import urlencode + +headers = { + "Content-Type": "application/json" +} + + +def parse_query(query): + """ Convert all lists to strings within a given query dict. """ + + ret = {} + for k, v in query.items(): + if isinstance(v, list): + ret[k] = ",".join(v) + else: + ret[k] = v + return ret + + +def get_json(url, query=None): + """ Perform a GET request and return the resulting JSON payload as an object. """ + + res = requests.get(url, params=query, headers=headers) + if res.status_code == 200: + return json.loads(res.content.decode("utf-8")) + else: + print("Response code:", res.status_code) + print(res.content) + return None