|
| 1 | +[](https://github.com/unitedstates/python-us/actions/workflows/pythonpackage.yml) |
| 2 | + |
| 3 | +# US: The Greatest Package in the World |
| 4 | + |
| 5 | +A package for easily working with US and state metadata. |
| 6 | + |
| 7 | +* all US states and territories |
| 8 | +* postal abbreviations |
| 9 | +* Associated Press style abbreviations |
| 10 | +* FIPS codes |
| 11 | +* capitals |
| 12 | +* years of statehood |
| 13 | +* time zones |
| 14 | +* phonetic state name lookup |
| 15 | +* is contiguous or continental |
| 16 | +* URLs to shapefiles for state, census, congressional districts, |
| 17 | + counties, and census tracts |
| 18 | + |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +As per usual: |
| 23 | + |
| 24 | +``` |
| 25 | +pip install us |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## Features |
| 30 | + |
| 31 | +Easy access to state information: |
| 32 | + |
| 33 | +```python |
| 34 | +>>> import us |
| 35 | +>>> us.states.MD |
| 36 | +<State:Maryland> |
| 37 | +>>> us.states.MD.fips |
| 38 | +'24' |
| 39 | +>>> us.states.MD.name |
| 40 | +'Maryland' |
| 41 | +>>> us.states.MD.is_contiguous |
| 42 | +True |
| 43 | +``` |
| 44 | + |
| 45 | +Includes territories too: |
| 46 | + |
| 47 | +```python |
| 48 | +>>> us.states.VI.name |
| 49 | +'Virgin Islands' |
| 50 | +>>> us.states.VI.is_territory |
| 51 | +True |
| 52 | +>>> us.states.MD.is_territory |
| 53 | +False |
| 54 | +``` |
| 55 | + |
| 56 | +List of all (actual) states: |
| 57 | + |
| 58 | +```python |
| 59 | +>>> us.states.STATES |
| 60 | +[<State:Alabama>, <State:Alaska>, <State:Arizona>, <State:Arkansas>, ... |
| 61 | +>>> us.states.TERRITORIES |
| 62 | +[<State:American Samoa>, <State:Guam>, <State:Northern Mariana Islands>, ... |
| 63 | +``` |
| 64 | + |
| 65 | +And the whole shebang, if you want it: |
| 66 | + |
| 67 | +```python |
| 68 | +>>> us.states.STATES_AND_TERRITORIES |
| 69 | +[<State:Alabama>, <State:Alaska>, <State:American Samoa>, ... |
| 70 | +``` |
| 71 | + |
| 72 | +For convenience, `STATES`, `TERRITORIES`, and `STATES_AND_TERRITORIES` can be |
| 73 | +accessed directly from the `us` module: |
| 74 | + |
| 75 | +```python |
| 76 | +>>> us.states.STATES |
| 77 | +[<State:Alabama>, <State:Alaska>, <State:Arizona>, <State:Arkansas>, ... |
| 78 | +>>> us.STATES |
| 79 | +[<State:Alabama>, <State:Alaska>, <State:Arizona>, <State:Arkansas>, ... |
| 80 | +``` |
| 81 | + |
| 82 | +Some states like to be fancy and call themselves commonwealths: |
| 83 | + |
| 84 | +```python |
| 85 | +>>> us.states.COMMONWEALTHS |
| 86 | +[<State:Kentucky>, <State:Massachusetts>, <State:Pennsylvania>, <State:Virginia>] |
| 87 | +``` |
| 88 | + |
| 89 | +There's also a list of obsolete territories: |
| 90 | + |
| 91 | +```python |
| 92 | +>>> us.states.OBSOLETE |
| 93 | +[<State:Dakota>, <State:Orleans>, <State:Philippine Islands>] |
| 94 | +``` |
| 95 | + |
| 96 | +The state lookup method allows matching by FIPS code, abbreviation, and name: |
| 97 | + |
| 98 | +```python |
| 99 | +>>> us.states.lookup('24') |
| 100 | +<State:Maryland> |
| 101 | +>>> us.states.lookup('MD') |
| 102 | +<State:Maryland> |
| 103 | +>>> us.states.lookup('md') |
| 104 | +<State:Maryland> |
| 105 | +>>> us.states.lookup('maryland') |
| 106 | +<State:Maryland> |
| 107 | +``` |
| 108 | + |
| 109 | +Get useful information: |
| 110 | + |
| 111 | +```python |
| 112 | +>>> state = us.states.lookup('maryland') |
| 113 | +>>> state.abbr |
| 114 | +'MD' |
| 115 | +``` |
| 116 | + |
| 117 | + |
| 118 | +And for those days that you just can't remember how to spell Mississippi, |
| 119 | +we've got phonetic name matching too: |
| 120 | + |
| 121 | +```python |
| 122 | +>>> us.states.lookup('misisipi') |
| 123 | +<State:Mississippi> |
| 124 | +``` |
| 125 | + |
| 126 | + |
| 127 | +### Shapefiles |
| 128 | + |
| 129 | +You want shapefiles too? As long as you want 2010 shapefiles, we've gotcha covered. |
| 130 | + |
| 131 | +``` |
| 132 | +>>> urls = us.states.MD.shapefile_urls() |
| 133 | +>>> sorted(urls.keys()) |
| 134 | +['block', 'blockgroup', 'cd', 'county', 'state', 'tract', 'zcta'] |
| 135 | +>>> urls['block'] |
| 136 | +'https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/2010/tl_2010_24_tabblock10.zip' |
| 137 | +``` |
| 138 | + |
| 139 | +The `shapefile_urls()` method on the State object generates shapefile URLs for |
| 140 | +the following regions: |
| 141 | + |
| 142 | +* block |
| 143 | +* blockgroup |
| 144 | +* census tract (tract) |
| 145 | +* congressional district (cd) |
| 146 | +* county |
| 147 | +* state |
| 148 | +* zcta |
| 149 | + |
| 150 | + |
| 151 | +### Mappings |
| 152 | + |
| 153 | +Mappings between various state attributes are a common need. The `mapping()` |
| 154 | +method will generate a lookup between two specified fields. |
| 155 | + |
| 156 | +```python |
| 157 | +>>> us.states.mapping('fips', 'abbr') |
| 158 | +{'01': 'AL', '02': 'AK', '04': 'AZ', '05': 'AR', '06': 'CA', ... |
| 159 | +>>> us.states.mapping('abbr', 'name') |
| 160 | +{'AL': 'Alabama', 'AK': 'Alaska', 'AZ': 'Arizona', 'AR': 'Arkansas', ... |
| 161 | +``` |
| 162 | + |
| 163 | +This method uses `us.STATES_AND_TERRITORIES` as the default list of states |
| 164 | +it will create a mapping for, but this can be overridden by passing an |
| 165 | +additional states argument: |
| 166 | + |
| 167 | +```python |
| 168 | +>>> us.states.mapping('fips', 'abbr', states=[us.states.DC]) |
| 169 | +{'11': 'DC'} |
| 170 | +``` |
| 171 | + |
| 172 | + |
| 173 | +### DC should be granted statehood |
| 174 | + |
| 175 | +Washington, DC does not appear in `us.STATES` or any of the |
| 176 | +related state lists, but is often treated as a state in practice and |
| 177 | +[should be granted statehood anyway](https://statehood.dc.gov/page/about-dc-statehood). DC can be automatically included in these |
| 178 | +lists by setting a `DC_STATEHOOD` environment variable to any truthy value |
| 179 | +before importing this package. |
| 180 | + |
| 181 | +``` |
| 182 | +DC_STATEHOOD=1 |
| 183 | +``` |
| 184 | + |
| 185 | + |
| 186 | +## CLI |
| 187 | + |
| 188 | +When you need to know state information RIGHT AWAY, there's the _states_ script. |
| 189 | + |
| 190 | +``` |
| 191 | +$ states md |
| 192 | + |
| 193 | +*** The great state of Maryland (MD) *** |
| 194 | + |
| 195 | + FIPS code: 24 |
| 196 | + |
| 197 | + other attributes: |
| 198 | + ap_abbr: Md. |
| 199 | + capital: Annapolis |
| 200 | + capital_tz: America/New_York |
| 201 | + is_contiguous: True |
| 202 | + is_continental: True |
| 203 | + is_obsolete: False |
| 204 | + name_metaphone: MRLNT |
| 205 | + statehood_year: 1788 |
| 206 | + time_zones: America/New_York |
| 207 | + |
| 208 | + shapefiles: |
| 209 | + tract: https://www2.census.gov/geo/tiger/TIGER2010/TRACT/2010/tl_2010_24_tract10.zip |
| 210 | + cd: https://www2.census.gov/geo/tiger/TIGER2010/CD/111/tl_2010_24_cd111.zip |
| 211 | + county: https://www2.census.gov/geo/tiger/TIGER2010/COUNTY/2010/tl_2010_24_county10.zip |
| 212 | + state: https://www2.census.gov/geo/tiger/TIGER2010/STATE/2010/tl_2010_24_state10.zip |
| 213 | + zcta: https://www2.census.gov/geo/tiger/TIGER2010/ZCTA5/2010/tl_2010_24_zcta510.zip |
| 214 | + block: https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/2010/tl_2010_24_tabblock10.zip |
| 215 | + blockgroup: https://www2.census.gov/geo/tiger/TIGER2010/BG/2010/tl_2010_24_bg10.zip |
| 216 | +``` |
| 217 | + |
| 218 | +## Running Tests |
| 219 | + |
| 220 | +GitHub Actions are set up to automatically run unit tests against any new |
| 221 | +commits to the repo. To run these tests yourself: |
| 222 | + |
| 223 | +``` |
| 224 | +pip install -e .[dev] |
| 225 | +pytest . |
| 226 | +``` |
| 227 | + |
| 228 | + |
| 229 | +## Changelog |
| 230 | + |
| 231 | +### 3.2.0 |
| 232 | + |
| 233 | +* add support for Python 3.12 |
| 234 | +* drop support for Python 3.6 and 3.7 |
| 235 | +* add birthday attribute to unitedstatesofamerica |
| 236 | +* upgrade to jellyfish 1.x |
| 237 | +* thanks to [Paul Hawk](https://github.com/pauldhawk), [David Gilman](https://github.com/dgilmanAIDENTIFIED), [Sergii Bondarenko](https://github.com/BR0kEN-), and [Pedro Camargo](https://github.com/pedrocamargo) |
| 238 | + |
| 239 | + |
| 240 | +### 3.1.1 |
| 241 | + |
| 242 | +* add support for Python 3.11 |
| 243 | +* upgrade to jellyfish 0.11.2 |
| 244 | + |
| 245 | + |
| 246 | +### 3.0.0 |
| 247 | + |
| 248 | +* upgrade to jellyfish 0.7.2 |
| 249 | +* drop support for Python 2.7 |
| 250 | +* add us.states.COMMONWEALTHS list of states that call themselves commonwealths 🎩 |
| 251 | +* add DC to STATES, STATES_AND_TERRITORIES, STATES_CONTIGUOUS, or STATES_CONTINENTAL when DC_STATEHOOD environment variable is set |
| 252 | +* remove `region` parameter from `shapefile_urls()` method |
| 253 | +* `mapping()` no longer includes obsolete states |
| 254 | +* added type annotations |
| 255 | + |
| 256 | + |
| 257 | +### 2.0.2 |
| 258 | + |
| 259 | +* restore DC in `lookup()` and `mapping()` |
| 260 | + |
| 261 | + |
| 262 | +### 2.0.1 |
| 263 | + |
| 264 | +* fix Python 2.7 tests that ran with Python 3 |
| 265 | +* revert to jellyfish 0.6.1 to support Python 2.7 |
| 266 | + |
| 267 | + |
| 268 | +### 2.0.0 |
| 269 | + |
| 270 | +* add support for Python 3.7 and 3.8 |
| 271 | +* remove support for Python 3.4 and 3.5 |
| 272 | +* remove pickled objects and database in favor of pure Python code |
| 273 | +* upgrade jellyfish to 0.7.2 to fix metaphone bug |
| 274 | +* fixes for IN, KY, ND, and NM timezones |
| 275 | +* set AZ timezone to America/Phoenix |
| 276 | +* obsolete entries are no longer included in STATES_AND_TERRITORIES |
| 277 | +* DC is no longer included in STATES, STATES_AND_TERRITORIES, STATES_CONTIGUOUS, or STATES_CONTINENTAL |
| 278 | + |
| 279 | + |
| 280 | +### 1.0.0 |
| 281 | + |
| 282 | +* full Python 3.6 support |
| 283 | +* use pytest |
| 284 | + |
| 285 | + |
| 286 | +### 0.10.0 |
| 287 | + |
| 288 | +* upgrade jellyfish to 0.5.3 to fix metaphone bug |
| 289 | + |
| 290 | +### 0.9.0 |
| 291 | + |
| 292 | +* add information on whether a state is contiguous and/or continental, |
| 293 | + thanks to [chebee7i](https://github.com/chebee7i) |
| 294 | + |
| 295 | +### 0.8.0 |
| 296 | + |
| 297 | +* add obsolete territories, thanks to [Ben Chartoff](https://github.com/bchartoff) |
| 298 | +* fix packaging error, thanks to [Alexander Kulakov](https://github.com/momyc) |
| 299 | + |
| 300 | + |
| 301 | +### 0.7.1 |
| 302 | + |
| 303 | +* upgrade to jellyfish 0.5.1 to fix metaphone case bug |
| 304 | + |
| 305 | +### 0.7 |
| 306 | + |
| 307 | +* add time zones, thanks to [Paul Tagliamonte](https://github.com/paultag) |
| 308 | +* Python 2.6 and 3.2 compatibility |
| 309 | + |
| 310 | +### 0.6 |
| 311 | + |
| 312 | +* add AP-style state abbreviations |
| 313 | +* use jellyfish instead of Metaphone package |
| 314 | +* update to requests v1.0.4 for tests |
| 315 | +* Python 3.3 compatibility |
| 316 | + |
| 317 | +### 0.5 |
| 318 | + |
| 319 | +* fix state abbreviation for Nebraska |
| 320 | + |
| 321 | +### 0.4 |
| 322 | + |
| 323 | +* add state capitals |
| 324 | +* add years of statehood |
| 325 | + |
| 326 | +### 0.3 |
| 327 | + |
| 328 | +* add mapping method to generate dicts of arbitrary fields |
| 329 | + |
| 330 | +### 0.2 |
| 331 | + |
| 332 | +* add command line script for quick access to state data |
| 333 | + |
| 334 | +### 0.1 |
| 335 | + |
| 336 | +* initial release |
| 337 | +* state names and abbreviations |
| 338 | +* FIPS codes |
| 339 | +* `lookup()` method |
| 340 | +* shapefile URLs for various regions |
0 commit comments