Examples#

Working with Address#

Comparing 2 addresses can be tidious process. Sometimes street component can be 12 main street or 12 main st. That’s why Address instances could be easily compared using ==. Furthermore, in order to increase the comparing result and extracting address components, normalize() can be used.

from send_the_raven.address import Address, Addresses, compare

# Create Address instance manually
addr1 = Address(street="12 Main St", city="Boston", state="MA")
addr2 = Address(street="12 Main Street", city="Boston", state="massachusetts")
assert addr1 == addr2

# Smartly filling Zip Code to import USPS Validation
print(f"Zipcode: {addr1.zip_code}")
addr1.fill_in_zipcode()
print(f"Zipcode: {addr1.zip_code}")

addr2.fill_in_state()
assert addr2.state == "MA"

# Extract address from 1 string
many_address = Addresses(
    [
        {"full_address": "12 park st, lawrence"},
        {"full_address": "12 park street, lawrence"},
    ]
)

many_address.normalize()

print(compare(many_address[0], many_address[1]))

Convert Address into Longitude and Latitude using Geocoder#

One needs to convert 1 million addresses into Longitude and Latitude? It’s Geocoder comes to the rescue.

Address instances could be easily compared using ==. Furthermore, extracting addresss components can be done in parallel using normalize().

from send_the_raven.geoaddress import Geocoder

addresses = [
    {"street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345"},
    {"street": "456 Oak Rd", "city": "Forest", "state": "VT", "zip": "67890"},
]
geocoder = Geocoder(addresses)
geocoder("nominatim", {"user_agent": "my_app"})

for address in geocoder.addresses:
    print(address.latitude, address.longitude)

Validate addresses to USPS using Validator#

USPS API is not the most developer friendly API. Therefore, Validator is here to help.

Address instances could be easily compared using ==. Furthermore, extracting addresss components can be done in parallel using normalize().

from send_the_raven.validator import Validator

addresses = [
    {"street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345"},
    {"street": "456 Oak Rd", "city": "Forest", "state": "VT", "zip": "67890"},
]
validator = Validator(addresses, usps_id="MY_ID")
valid_addresses = validator()
for addr in valid_addresses:
    print(addr.street, addr.city, addr.state, addr.zip_code)