Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude obsolete entries from STATES_AND_TERRITORIES #39

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions us/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
TERRITORIES = []
OBSOLETE = []
STATES_AND_TERRITORIES = []
ALL = []

_lookup_cache = {}

Expand Down Expand Up @@ -72,21 +73,23 @@ def load_states():

state = State(**s) # create state object

ALL.append(state)

# create separate lists for obsolete, states, and territories
if state.is_obsolete:
OBSOLETE.append(state)
elif state.is_territory:
TERRITORIES.append(state)
else:
STATES.append(state)

if state.is_contiguous:
STATES_CONTIGUOUS.append(state)
if state.is_continental:
STATES_CONTINENTAL.append(state)

# also create list of all states and territories
STATES_AND_TERRITORIES.append(state)
if state.is_territory:
TERRITORIES.append(state)
else:
STATES.append(state)
if state.is_contiguous:
STATES_CONTIGUOUS.append(state)
if state.is_continental:
STATES_CONTINENTAL.append(state)

# also create list of all states and territories
STATES_AND_TERRITORIES.append(state)

# provide package-level abbreviation access: us.states.MD
globals()[state.abbr] = state
Expand Down Expand Up @@ -128,15 +131,15 @@ def lookup(val, field=None, use_cache=True):
if use_cache and cache_key in _lookup_cache:
return _lookup_cache[cache_key]

for state in STATES_AND_TERRITORIES:
for state in ALL:
if val == getattr(state, field):
_lookup_cache[cache_key] = state
return state


def mapping(from_field, to_field, states=None):
if states is None:
states = STATES_AND_TERRITORIES
states = ALL
return dict((getattr(s, from_field), getattr(s, to_field)) for s in states)


Expand Down