Utils Reference¶
Address Parsing¶
utils.parse_address
¶
find_address_fields(config)
¶
Parses which address fields to consider in the input file based on the content of config.yml. Raises an error if neither full_address_field nor street are specified in the config file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
A config object |
required |
Returns dict: A dict of address field names in the input file.
Source code in utils/parse_address.py
parse_address(parser, address)
¶
Given an address string, uses PassyunkParser to return a standardized address, and whether or not the given string is an extant address in Philadelphia. Makes some attempt to normalize alternate spellings of addresses: eg, 123 Mkt will evaluate to 123 MARKET ST
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
A PassyunkParser object |
required | |
address
|
str
|
An address string |
required |
Returns tuple(str, bool, bool): tuple with the standardized address, a boolean value indicating if the string is formatted as an address, and a boolean value indicating if the address is a valid Philadelphia address.
Source code in utils/parse_address.py
is_non_philly(address, address_is_split, zips)
¶
Determines whether an address is in Philadelphia. Handles A string full address or a dict split address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address_is_split
|
bool
|
whether or not the address is the full address, or split |
required |
zips
|
list of Philadelphia zip codes |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
{'is_non_philly': bool, 'is_undefined': bool} |
Source code in utils/parse_address.py
Geocoding¶
utils.ais_lookup
¶
ais_lookup(sess, api_key, address, zip=None, enrichment_fields=[], existing_is_addr=False, existing_is_philly_addr=False, original_address=None, fetch_4326=True, fetch_2272=True)
¶
Given a passyunk-normalized address, looks up whether or not it is in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sess
|
requests Session object
|
A requests library session object |
required |
api_key
|
str
|
An AIS api key |
required |
address
|
str
|
The address to query |
required |
zip
|
str
|
The zip code associated with the address, if present |
None
|
enrichment_fields
|
list
|
The fields to add from AIS |
[]
|
fetch_4326
|
bool
|
Whether to fetch SRID 4326 coordinates (lat/lon) |
True
|
fetch_2272
|
bool
|
Whether to fetch SRID 2272 coordinates (x/y) |
True
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict with standardized address, latitude and longitude, |
dict
|
and user-requested fields. |
Source code in utils/ais_lookup.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
fetch_service_area_enrichment_data(sess, api_key, lat, lon, enrichment_fields)
¶
Looks up latitude and longitude against the AIS API service area endpoint,
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sess
|
Session
|
a requests Session object |
required |
api_key
|
str
|
an AIS API key |
required |
lat
|
str
|
latitude |
required |
lon
|
str
|
longitude |
required |
enrichment_fields
|
list
|
which fields to return from AIS |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary of enrichment data |
Source code in utils/ais_lookup.py
utils.tomtom_lookup
¶
tomtom_lookup(sess, parser, api_key, tomtom_url, philly_zips, address, fallback_addr, fetch_4326=True, fetch_2272=True)
¶
Given a passyunk-normalized address, looks up via TomTom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sess
|
requests Session object
|
A requests library session object |
required |
parser
|
A passyunk parser object, used to normalize output |
required | |
api_key
|
str
|
The AIS API key, also used for TomTom |
required |
tomtom_url
|
str
|
The TomTom URL to use. Present for backwards compatibility with people using API keys that are not valid for the new Mulesoft gateway endpoint. |
required |
philly_zips
|
list
|
A list of philadelphia zips to validate |
required |
address
|
str
|
The address to query |
required |
fallback_addr
|
str
|
The address to return if no match is found |
required |
fetch_4326
|
bool
|
Whether or not to pull coordinates in 4326 |
True
|
fetch_2272
|
bool
|
Whether or not to pull coordinates in 2272 |
True
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict with standardized address, latitude and longitude, returned |
dict
|
from TomTom. |
Source code in utils/tomtom_lookup.py
Infrastructure¶
utils.cache
¶
LRUCache
¶
A cache that stores processed records. The cache automatically evicts old records once it grows past a maximum size. When a record appears in a cache, move it to the end so it doesn't get removed until later.
Source code in utils/cache.py
utils.encoder
¶
detect_file_encoding(file_path)
¶
Source code in utils/encoder.py
recode_to_utf8(src_path, dst_path, src_encoding)
¶
Reincode an input file to account for non-standard characters, line by line.
Source code in utils/encoder.py
utils.rate_limiter
¶
RateLimiter
¶
Thread-safe rate limiter. Polars is multithreaded by default, so rate limitation needs to take this into account when making calls against APIs.
Source code in utils/rate_limiter.py
wait()
¶
Block until another call can be made.