There's a list of associated states, sovereign nations in free association with
the United States under the Compact of Free Association. They are not US
states or territories and are not included in STATES_AND_TERRITORIES or
returned by lookup():
>>> us.states.ASSOCIATED_STATES
[<State:Federated States of Micronesia>, <State:Marshall Islands>, <State:Palau>]
If your lookup values come from user input or other unpredictable sources,
clean_name() can strip them down to something the lookup is more likely to
match. It lowercases the input, removes punctuation, and drops the filler
words the, commonwealth, state, and of:
>>> us.states.clean_name(' The state OF idaho ')
'idaho'
>>> us.states.clean_name('Commonwealth of Virginia')
'virginia'
>>> us.states.lookup(us.states.clean_name('The State of Maryland!'))
<State:Maryland>
clean_name is a standalone helper and lookup does not call it
automatically. Apply it yourself when you want it.
Custom fallback matching
lookup accepts an optional fallback_func that is called when none of the
built-in matching strategies find a state. It receives the original lookup
value and should return a State or None:
>>> def my_fallback(val):
... return us.states.AK if val == 'the big one' else None
>>> us.states.lookup('the big one', fallback_func=my_fallback)
<State:Alaska>
For the common case of matching against the start of a state name, the
included startswith_fallback helper does just that (case-insensitively):
Fallback results are cached separately per fallback function, so a cached
fallback match won't leak into lookups that pass a different fallback (or
no fallback at all).
Shapefiles
You want shapefiles too? As long as you want 2010 shapefiles, we've gotcha covered.
This method uses us.STATES_AND_TERRITORIES as the default list of states
it will create a mapping for, but this can be overridden by passing an
additional states argument:
The enumeration() method dynamically constructs an Enum of states, keyed
by state abbreviation. The value of each member is taken from the attribute
named by value_field, which defaults to name.
>>> States = us.states.enumeration()
>>> States.VA
<States.VA: 'Virginia'>
>>> States.VA.value
'Virginia'
>>> States = us.states.enumeration('fips')
>>> States.CA.value
'06'
Like mapping(), this method uses us.STATES_AND_TERRITORIES as the
default state list, which can be overridden by passing a states argument:
Washington, DC does not appear in us.STATES or any of the
related state lists, but is often treated as a state in practice and
should be granted statehood anyway. DC can be automatically included in these
lists by setting a DC_STATEHOOD environment variable to any truthy value
before importing this package.
DC_STATEHOOD=1
CLI
When you need to know state information RIGHT AWAY, there's the states script.
$ states md
*** The great state of Maryland (MD) ***
FIPS code: 24
other attributes:
ap_abbr: Md.
capital: Annapolis
capital_tz: America/New_York
is_contiguous: True
is_continental: True
is_obsolete: False
name_metaphone: MRLNT
statehood_year: 1788
time_zones: America/New_York
shapefiles:
tract: https://www2.census.gov/geo/tiger/TIGER2010/TRACT/2010/tl_2010_24_tract10.zip
cd: https://www2.census.gov/geo/tiger/TIGER2010/CD/111/tl_2010_24_cd111.zip
county: https://www2.census.gov/geo/tiger/TIGER2010/COUNTY/2010/tl_2010_24_county10.zip
state: https://www2.census.gov/geo/tiger/TIGER2010/STATE/2010/tl_2010_24_state10.zip
zcta: https://www2.census.gov/geo/tiger/TIGER2010/ZCTA5/2010/tl_2010_24_zcta510.zip
block: https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/2010/tl_2010_24_tabblock10.zip
blockgroup: https://www2.census.gov/geo/tiger/TIGER2010/BG/2010/tl_2010_24_bg10.zip
Running Tests
GitHub Actions are set up to automatically run unit tests against any new
commits to the repo. To run these tests yourself: