def parse_state_row(row): tds = row.findall('td') postal = tds[0].text_content() state = statestyle.get(postal) ths = row.findall('th') a = ths[0].find('a') href = a.attrib['href'] return (state.fips.zfill(2), urljoin(BASE_URL, href))
def build_url(row): place_name = trim_place_name(row['NAME']) state = row['USPS'] state_name = statestyle.get(state).name path = u"%s, %s" % (quote_plus(place_name.encode('utf-8')), state_name) path = path.replace(' ','_') path = path.replace('+','_') return urljoin(BASE_URL,path)
def parse_state_row(row): tds = row.findall('td') postal = tds[0].text_content() state = statestyle.get(postal) ths = row.findall('th') a = ths[0].find('a') href = a.attrib['href'] return (state.fips.zfill(2), urljoin(BASE_URL,href))
def test_attr(self): obj = statestyle.get("CA") self.assertEqual(obj.postal, 'CA') self.assertEqual(obj.fips, '6') self.assertEqual(obj.name, 'California') self.assertEqual(obj.type, 'state') self.assertEqual(obj.ap, 'Calif.') self.assertEqual(obj.stateface, 'E')
def test_dict(self): o = statestyle.get("IA") self.assertEqual(o["ap"], o.ap) with self.assertRaises(AttributeError): o["foobar"] self.assertDictEqual(o.__dict__, o.to_dict()) self.assertDictEqual(dict(o), o.to_dict()) self.assertDictEqual(dict(o), o.__dict__)
def build_url(row): place_name = trim_place_name(row['NAME']) state = row['USPS'] state_name = statestyle.get(state).name path = u"%s, %s" % (quote_plus(place_name.encode('utf-8')), state_name) path = path.replace(' ', '_') path = path.replace('+', '_') return urljoin(BASE_URL, path)
def match_property_from_description(place, pattern, state=False): if place.description: match = pattern.search(place.description) if match: prop = match.group(0) if state: prop = statestyle.get(prop).name return prop return None
def test_attr(self): obj = statestyle.get("CA") self.assertEqual(obj.postal, "CA") self.assertEqual(obj.fips, "6") self.assertEqual(obj.name, "California") self.assertEqual(obj.type, "state") self.assertEqual(obj.ap, "Calif.") self.assertEqual(obj.stateface, "E") obj.__repr__() obj.__str__() obj.__unicode__()
def test_attr(self): obj = statestyle.get("CA") self.assertEqual(obj.postal, 'CA') self.assertEqual(obj.fips, '6') self.assertEqual(obj.name, 'California') self.assertEqual(obj.type, 'state') self.assertEqual(obj.ap, 'Calif.') self.assertEqual(obj.stateface, 'E') obj.__repr__() obj.__str__() obj.__unicode__()
def parse_cd_file(): writer = unicodecsv.writer(open('cd_wiki_data.csv', 'w')) writer.writerow(['full_geoid', 'wiki_url']) response = s.urlopen(CD_LIST) doc = fromstring(response) for h2 in doc.findall('.//h2')[2:59]: for span in h2.find_class('mw-headline'): if span.text_content() in NOT_STATES: break state = statestyle.get(span.text_content()) if state.name not in NON_VOTING: ul = span.getparent().getnext().getnext().getnext() print_output(state, ul)
def parse_district(td): url = parse_url(td) text = td.text_content() state_regex = re.search("(?<!=\\d)\\D+", text) state = state_regex.group() if 'At-large' in text: state = re.sub(" At-large", "", state) cd_num = 0 else: cd_regex = re.search("(\\b\\d+)", text) cd_num = cd_regex.group() if 'Incorrect' in text: stateobj = statestyle.get('California') cd_num = 8 url = "https://en.wikipedia.org/wiki/California%27s_8th_congressional_district" else: stateobj = statestyle.get(state) if stateobj.name in OUTLIERS: cd_num = 98 geoid = str(stateobj.fips).zfill(2) + str(cd_num).zfill(2) print geoid + ',' + url return (geoid, url)
def state_postal(value): """ Converts a state's name, or FIPS to its postal abbreviation Example usage: >> ap_state("California") 'Calif.' """ try: return statestyle.get(value).postal except: return value
def handle(self, *args, **kwds): csv_file = open('image/unemployment.csv', 'rU') data = list(csv.reader(csv_file)) csv_file.close() for i in data[1:]: state = statestyle.get(i[0]) print 'Loading: %s' % state.name State.objects.create( name=state.name, postal=state.postal, unemployment_rate=float(i[1]) / 100, svg_path=us_states.paths.get(state.postal), color=self.get_color(float(i[1]) / 100), ) print 'Finished'
def stateface(value): """ Converts a state's name, postal abbreviation or FIPS to ProPublica's stateface font code. Example usage: >> stateface("California") 'E' Documentation: http://propublica.github.com/stateface/ """ try: return statestyle.get(value).stateface except: return value
def ap_state(value, failure_string=None): """ Converts a state's name, postal abbreviation or FIPS to A.P. style. Example usage: >> ap_state("California") 'Calif.' """ try: return statestyle.get(value).ap except: if failure_string: return failure_string else: return value
def clean_state(self, state): try: obj = statestyle.get(state) return obj.postal except ValueError: return ''
def stateface(self): a = statestyle.get(self.state) return a.stateface
def test_ap(self): for i in self.good_ap: statestyle.get(i) for i in self.bad_ap: self.assertRaises(ValueError, statestyle.get, i)