Skip to content

Commit

Permalink
Improving testing and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
whubsch committed Jul 26, 2024
1 parent db6031e commit 38330e6
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 16 deletions.
40 changes: 39 additions & 1 deletion src/atlus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
"""`atlus` is a Python package to convert raw address strings into the OSM format.
"""`atlus` is a Python package to convert raw address and phone number strings into the OSM format.
It's designed to be used with US and Canadian phone numbers and addresses.
```python
>> import atlus
>> atlus.abbrs("St. Francis")
# "Saint Francis"
>> atlus.get_address("789 Oak Dr, Smallville California, 98765")[0]
# {"addr:housenumber": "789", "addr:street": "Oak Drive:, "addr:city": "Smallville", "addr:state": "CA", "addr:postcode": "98765"}
>> atlus.get_phone("(202) 900-9019")
# "+1 202-900-9019"
```
"""

# SPDX-FileCopyrightText: 2024-present Will <[email protected]>
#
# SPDX-License-Identifier: MIT

from .atlus import (
get_address,
get_phone,
abbrs,
get_title,
mc_replace,
us_replace,
ord_replace,
clean,
)
from . import atlus
from . import resources

__all__ = [
"get_address",
"get_phone",
"abbrs",
"get_title",
"mc_replace",
"us_replace",
"ord_replace",
"clean",
"atlus",
"resources",
]
22 changes: 14 additions & 8 deletions src/atlus/atlus.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Functions and tools to process the raw address strings."""

from collections import Counter
from typing import OrderedDict, Union, List, Dict, Tuple
from typing import Union, List, Dict, Tuple
import usaddress
import regex
from .resources import (
Expand Down Expand Up @@ -362,21 +362,27 @@ def collapse_list(seq: list) -> list:

def get_address(
address_string: str,
) -> Tuple[OrderedDict[str, Union[str, int]], List[Union[str, None]]]:
) -> Tuple[Dict[str, Union[str, int]], List[Union[str, None]]]:
"""Process address strings.
```python
>> get_address("345 MAPLE RD, COUNTRYSIDE, PA 24680-0198")
# {"addr:housenumber": "345", "addr:street": "Maple Road", "addr:city": "Countryside", "addr:state": "PA", "addr:postcode": "24680-0198"}
>> get_address("777 Strawberry St.")
# {"addr:housenumber": "777", "addr:street": "Strawberry Street",}
>> get_address("345 MAPLE RD, COUNTRYSIDE, PA 24680-0198")[0]
# {"addr:housenumber": "345", "addr:street": "Maple Road",
"addr:city": "Countryside", "addr:state": "PA", "addr:postcode": "24680-0198"}
>> get_address("777 Strawberry St.")[0]
# {"addr:housenumber": "777", "addr:street": "Strawberry Street"}
>> address = get_address("222 NW Pineapple Ave Suite A Unit B")
>> address[0]
# {"addr:housenumber": "222", "addr:street": "Northwest Pineapple Avenue"}
>> address[1]
# ["addr:unit"]
```
Args:
address_string (str): The address string to process.
Returns:
Tuple[OrderedDict[str, Union[str, int]], List[Union[str, None]]]:
Tuple[Dict[str, Union[str, int]], List[Union[str, None]]]:
The processed address string and the removed fields.
"""
address_string = clean(address_string)
Expand Down Expand Up @@ -431,7 +437,7 @@ def get_address(
r"\1", cleaned["addr:postcode"]
).replace(" ", "-")

return cleaned, removed
return dict(cleaned), removed


def get_phone(phone: str) -> str:
Expand Down
46 changes: 46 additions & 0 deletions src/atlus/objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Define objects for parsing fields."""

from pydantic import BaseModel, Field


class Address(BaseModel):
"""Define address parsing fields."""

addr_housenumber: int | str | None = Field(
alias="addr:housenumber",
description="The house number that is included in the address.",
examples=[200, "1200-29"],
default=None,
)
addr_street: str | None = Field(
alias="addr:street",
description="The street that the address is located on.",
examples=["North Spring Street"],
default=None,
)
addr_unit: str | None = Field(
alias="addr:unit",
description="The unit number or letter that is included in the address.",
examples=["B"],
default=None,
)
addr_city: str | None = Field(
alias="addr:city",
description="The city that the address is located in.",
examples=["Los Angeles"],
default=None,
)
addr_state: str | None = Field(
alias="addr:state",
pattern=r"^[A-Z]{2}$",
description="The state or territory of the address.",
examples=["CA"],
default=None,
)
addr_postcode: str | None = Field(
alias="addr:postcode",
pattern=r"^\d{5}(?:\-\d{4})?$",
description="The postal code of the address.",
examples=["90012", "90012-4801"],
default=None,
)
41 changes: 34 additions & 7 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,40 @@ def test_complex_data_types():
assert collapse_list([1, "1", 1, "1"]) == [1, "1"]


def test_get_address():
"""Test cases for get address"""
assert get_address("345 MAPLE RD, COUNTRYSIDE, PA 24680-0198")[0] == {
"addr:housenumber": "345",
"addr:street": "Maple Road",
"addr:city": "Countryside",
"addr:state": "PA",
"addr:postcode": "24680-0198",
}
assert get_address("777 Strawberry St.")[0] == {
"addr:housenumber": "777",
"addr:street": "Strawberry Street",
}


def test_get_address_removed():
"""Test cases for get address"""
add = get_address("222 NW Pineapple Ave Suite A Unit B, Beachville, SC 75309")
assert add[0] == {
"addr:housenumber": "222",
"addr:street": "Northwest Pineapple Avenue",
"addr:city": "Beachville",
"addr:state": "SC",
"addr:postcode": "75309",
}
assert add[1] == ["addr:unit"]
# add = get_address("158 S. Thomas Court 30008 90210")
# assert add[0] == {
# "addr:housenumber": "158",
# "addr:street": "South Thomas Court",
# }
# assert add[1] == ["addr:postcode"]


def test_valid_phone_number_1():
"""Test cases for valid phone numbers"""
assert get_phone("2029009019") == "+1 202-900-9019"
Expand Down Expand Up @@ -232,10 +266,3 @@ def test_invalid_phone_number_4():
"""Test cases for blank phone numbers"""
with pytest.raises(ValueError, match="Invalid phone number: "):
get_phone("")


# def test_cap_match():
# assert cap_match(regex.match("(\w+)", "test")) == "TEST"


# Add more tests for other functions in the file

0 comments on commit 38330e6

Please sign in to comment.