Commits

Dan Connolly  committed d1981dd

playing around with places

  • Participants
  • Parent commits 9d2f007

Comments (0)

Files changed (2)

+#   Postal codes alphabetized by postal abbreviation (not by state)
+#   Abbreviation     State
+# tx http://en.wikipedia.org/wiki/U.S._postal_abbreviations
+_States="""   AK           Alaska
+   AL           Alabama
+   AR           Arkansas
+   AZ           Arizona
+   CA           California
+   CO           Colorado
+   CT           Connecticut
+   DE           Delaware
+   FL           Florida
+   GA           Georgia
+   HI           Hawaii
+   IA           Iowa
+   ID           Idaho
+   IL           Illinois
+   IN           Indiana
+   KS           Kansas
+   KY           Kentucky
+   LA           Louisiana
+   MA           Massachusetts
+   MD           Maryland
+   ME           Maine
+   MI           Michigan
+   MN           Minnesota
+   MO           Missouri
+   MS           Mississippi
+   MT           Montana
+   NC           North Carolina
+   ND           North Dakota
+   NE           Nebraska
+   NH           New Hampshire
+   NJ           New Jersey
+   NM           New Mexico
+   NV           Nevada
+   NY           New York
+   OH           Ohio
+   OK           Oklahoma
+   OR           Oregon
+   PA           Pennsylvania
+   RI           Rhode Island
+   SC           South Carolina
+   SD           South Dakota
+   TN           Tennessee
+   TX           Texas
+   UT           Utah
+   VA           Virginia
+   VT           Vermont
+   WA           Washington
+   WI           Wisconsin
+   WV           West Virginia
+   WY           Wyoming"""
+
+States = dict([ln.strip().split(None, 1) for ln in _States.split("\n")])
+
+Cities = (
+    ('IL', ['CHICAGO']),
+    ('KS', ['STANLEY', 'OVERLAND PARK', 'SHAWNEE MISSION', 'MERRIAM', 'CASSODY']),
+    ('MA', ['CAMBRIDGE']),
+    ('MN', ['ALBERT LEA', 'STILLWATER', 'OAKDALE']),
+    ('MO', ['KANSAS CITY']),
+    ('TX', ['AUSTIN']),
+    )
 import trxtsv
 from trxtsv import isoDate, numField
 
+from places import States, Cities
+
 def main(argv):
 
     opts, args = getopt.getopt(argv[1:],
     try:
 	fn, teli, tel, region = telRegion(desc)
     except IndexError:
-	w("<%s>%s</%s>" % (elt, xmldata(desc), elt))
+	try:
+	    fn, locality, region, postcode = citySt(desc)
+	except IndexError:
+	    w("<%s>%s</%s>" % (elt, xmldata(desc), elt))
+	else:
+	    w('<td class="vcard"><b class="fn org">%s</b> ' % xmldata(fn))
+	    w('<span class="adr">' \
+	      '<span class="locality">%s</span> ' \
+	      '<abbr class="region" title="%s">%s</abbr>' % \
+	      (locality, States[region], region))
+	    if postcode:
+		w(' <u class="postal-code">%s</u>' % postcode)
+	    w('</span>')
+	    w('</td>')
     else:
 	w('<td class="vcard"><b class="fn org">%s</b> ' % xmldata(fn))
 	w('<a class="tel" href="tel:%s">%s</a> ' % (teli, tel))
     raise IndexError
 
 
+
+
+def mkregex(state, cities):
+    r"""Make a regex from city/state data
+    assume no regex chars in 
+    >>> mkregex('KS', ['STANLEY', 'OVERLAND PARK', 'SHAWNEE MISSION'])
+    '((?P<locality>STANLEY|OVERLAND PARK|SHAWNEE MISSION))\\s*(?P<region>KS)'
+
+    """
+
+    expc = '(?P<locality>%s)' % ('|'.join([ v for v in cities]), )
+
+    return '(' + expc + (')\s*(?P<region>%s)' % state)
+
+
+PlaceExp = dict([(st, sre.compile(mkregex(st, cities))) \
+		 for st, cities in Cities])
+
+def citySt(desc):
+    """
+    >>> citySt('COLLEGE PARK FAMILY STANLEY KS')
+    ('COLLEGE PARK FAMILY', 'STANLEY', 'KS', None)
+
+    >>> citySt('COMFORT INN@PLAINFIELD, IN')
+    ('COMFORT INN', 'PLAINFIELD', 'IN', None)
+
+    >>> citySt('BP OIL@DUBLIN, OH 43016')
+    ('BP OIL', 'DUBLIN', 'OH', '43016')
+
+    another: 'USPS 1983579556 SHAWNEE MISSI KS'
+    """
+
+    postcode = None
+
+    if '@' in desc:
+	fn, where = desc.split('@')
+
+	# 3 letter code: airport
+	if sre.match(r'[A-Z][A-Z][A-Z]', where):
+	    raise IndexError
+
+	fn = fn.strip()
+	city, st = where.split(',')
+	if st[-1].isdigit() and ' ' in st:
+	    st, postcode = st.split()
+	st = st.strip()
+	return fn, city, st, postcode
+
+    for st, exp in PlaceExp.items():
+	m = exp.search(desc)
+	if m:
+	    return desc[:m.start(0)].strip(), \
+		m.group('locality'), st, postcode
+
+    raise IndexError
+
+
+
 def parity(ymd):
     """
     >>> parity("2005-11-12")