Skip to content

Helper

ASCIIColors

Bases: StrEnum

ASCII colors for use in printing colored text to the terminal.

Source code in src\backend\helper.py
class ASCIIColors(StrEnum):
    """ASCII colors for use in printing colored text to the terminal."""

    GREY = "\x1b[38;20m"
    YELLOW = "\x1b[33;20m"
    RED = "\x1b[31;20m"
    BOLD_RED = "\x1b[31;1m"
    RESET = "\x1b[0m"

df_to_file(df)

Write a DataFrame to a unique file.

Parameters:

Name Type Description Default
df DataFrame

the DataFrame to write

required
Source code in src\backend\helper.py
def df_to_file(df: pl.DataFrame):
    """Write a DataFrame to a unique file.

    Args:
        df (pl.DataFrame): the DataFrame to write
    """
    file_path = OUTPUT_DIR / f"{time.time()}_data_frame.csv"
    print(f"Dataframe saved to {file_path.resolve()}")
    df.write_csv(file_path, include_header=True)

is_valid_zipcode(zip)

Check if the given ZIP code is valid based on a local file.

Parameters:

Name Type Description Default
zip int

the ZIP code to check

required

Returns:

Name Type Description
bool bool

if ZIP code is valid

Source code in src\backend\helper.py
def is_valid_zipcode(zip: int) -> bool:
    """Check if the given ZIP code is valid based on a local file.

    Args:
        zip (int): the ZIP code to check

    Returns:
        bool: if ZIP code is valid
    """
    if isinstance(zip, str):
        zip = int(zip)
    return zip in MASTER_DF["ZIP"]

metro_name_to_zip_code_list(msa_name)

Return the constituent ZIP codes for the given Metropolitan Statistical Area.

Parameters:

Name Type Description Default
msa_name str

name of the Metropolitan Statistical Area

required

Returns:

Type Description
list[int]

list[int]: list of ZIP codes found. Is empty if MSA name is invalid

Source code in src\backend\helper.py
def metro_name_to_zip_code_list(msa_name: str) -> list[int]:
    """Return the constituent ZIP codes for the given Metropolitan Statistical Area.

    Args:
        msa_name (str): name of the Metropolitan Statistical Area

    Returns:
        list[int]: list of ZIP codes found. Is empty if MSA name is invalid
    """
    if msa_name == "TEST":
        # return [20814]  # good and small
        # return [22067, 55424]  # nulls in sqft
        return [20015, 20018, 20017]  # nulls in sqft and large

    df = MASTER_DF.select("ZIP", "METRO_NAME", "LSAD")

    return (
        df.filter(
            (pl.col("METRO_NAME").eq(msa_name))
            & (pl.col("LSAD").eq("Metropolitan Statistical Area"))
        )
        .unique()["ZIP"]
        .to_list()
    )

req_get_to_file(request)

Write the contents of a request response to a unique file.

Parameters:

Name Type Description Default
request Response

the request

required

Returns:

Name Type Description
int int

the status code of the request

Source code in src\backend\helper.py
def req_get_to_file(request: requests.Response) -> int:
    """Write the contents of a request response to a unique file.

    Args:
        request (requests.Response): the request

    Returns:
        int: the status code of the request
    """
    with open(OUTPUT_DIR / f"{time.time()}_request.html", "w+", encoding="utf-8") as f:
        f.write(request.text)
    return request.status_code

state_city_to_zip_df(state, city)

Take in a state and city and return the ZIP code constituents of that city.

Parameters:

Name Type Description Default
state str

the state

required
city str

the city

required

Returns:

Type Description
DataFrame

pl.DataFrame: DataFrame of ZIP codes

Source code in src\backend\helper.py
def state_city_to_zip_df(state: str, city: str) -> pl.DataFrame:
    """Take in a state and city and return the ZIP code constituents of that city.

    Args:
        state (str): the state
        city (str): the city

    Returns:
        pl.DataFrame: DataFrame of ZIP codes
    """
    return (
        pl.read_csv("zip_registry.csv")
        .filter((pl.col("state") == state) & (pl.col("city") == city))
        .select("zipcode")
    )

state_county_to_zip_df(state, county)

Take in a state and county and return the ZIP code constituents of that county.

Parameters:

Name Type Description Default
state str

the state

required
county str

the county

required

Returns:

Type Description
DataFrame

pl.DataFrame: DataFrame of ZIP codes

Source code in src\backend\helper.py
def state_county_to_zip_df(state: str, county: str) -> pl.DataFrame:
    """Take in a state and county and return the ZIP code constituents of that county.

    Args:
        state (str): the state
        county (str): the county

    Returns:
        pl.DataFrame: DataFrame of ZIP codes
    """
    return (
        pl.read_csv("zip_registry.csv")
        .filter((pl.col("state") == state) & (pl.col("county") == county))
        .select("zipcode")
    )

zip_to_metro(zip)

Find the Metropolitan Statistical Area name for the specified ZIP code.

Parameters:

Name Type Description Default
zip int

the ZIP code to look up

required

Returns:

Name Type Description
str str

the Metropolitan name. Is empty if the ZIP code is not a part of a Metropolitan Statistical Area

Source code in src\backend\helper.py
def zip_to_metro(zip: int) -> str:
    """Find the Metropolitan Statistical Area name for the specified ZIP code.

    Args:
        zip (int): the ZIP code to look up

    Returns:
        str: the Metropolitan name. Is empty if the ZIP code is not a part of a Metropolitan Statistical Area
    """
    result = MASTER_DF.filter(MASTER_DF["ZIP"] == zip)["METRO_NAME"]

    if len(result) > 0:
        log("Zip has multiple codes. Only giving first one", "debug")
        return result[0]
    else:
        return ""  # should this be none?