Skip to content

Instantly share code, notes, and snippets.

@myersjustinc
Created November 4, 2011 22:00
Show Gist options
  • Save myersjustinc/1340601 to your computer and use it in GitHub Desktop.
Save myersjustinc/1340601 to your computer and use it in GitHub Desktop.
Parse saved FactFinder2 queries using web.py and ep.io (updates now at https://github.com/myersjustinc/american_linkfinder)

AFF saved query converter

(NOTE: This Gist is no longer being maintained. See https://github.com/myersjustinc/american_linkfinder for further updates.)

This converter takes a saved query file from American FactFinder and generates a deep link URL corresponding to that query.

There are two versions of the converter:

  • One is a small Web application using the web.py framework.
  • The other is a command-line Python script.

Command-line script

The command-line script (convert_aff.py) just takes the filename of a .aff query file as its sole argument and prints a URL to standard output.

Web application

The Web application (deep_link.py) takes a POST-ed .aff query file and outputs a page with the corresponding URL in a link.

It also includes a basic requirements.txt (i.e., pip freeze) file and a configuration file for ep.io hosting (epio.ini).

Questions?

Email: justin at justinmyers dot net

#!/usr/bin/env python
import sys
from xml.etree.ElementTree import parse
def main(input_filename):
new_url_base = 'http://factfinder2.census.gov/bkmk/table/1.0/en'
document = parse(input_filename)
product = document.find('product')
url = '/'.join([
new_url_base,
product.attrib['program-id'],
product.attrib['dataset-id'],
product.attrib['table-id']
])
geo_ids = []
code_types = {}
selection = document.find('selection')
if selection is not None and len(selection):
for dimension in selection.findall('dimension'):
dimension_type = dimension.attrib['type']
if dimension_type == 'geo':
for cat_id in dimension.findall('cat-id'):
geo_ids.append(cat_id.text)
else:
codes = []
if dimension_type in code_types:
codes = code_types[dimension_type]
for cat_id in dimension.findall('cat-id'):
codes.append(cat_id.text)
code_types[dimension_type] = codes
if geo_ids:
url = '%s/%s' % (url, '|'.join(geo_ids))
if code_types:
for code_type in code_types.keys():
url = '%s/%s~%s' % (url, code_type, '|'.join(code_types[code_type]))
print url
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Usage: %s foo.aff" % sys.argv[0]
else:
main(sys.argv[1])
[wsgi]
requirements = requirements.txt
entrypoint = deep_link:application
https://github.com/webpy/webpy/tarball/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment