<commit_before># pylint: disable=consider-using-f-string
import typing

from .e164 import mapping
from .e212 import networks
from .errors import InvalidCountry
from .errors import InvalidNetwork
from .errors import InvalidPhone


def phone_country(phone: typing.Union[str, int]) -> str:
    """Return the ISO-3166-1 country code a given phone number belongs to.

    The phone number must be specified in E.164, aka international format.
    The returned country code is of ISO-3166-1 alpha-2 code.
    """

    def traverse_tree(
        key: typing.List[int],
        tree: dict,
    ) -> str:
        next_key, *remaining_keys = key
        next_element: typing.Union[str, typing.Dict[int, str]] = tree[next_key]
        if isinstance(next_element, dict):
            return traverse_tree(remaining_keys, next_element)

        return next_element

    if not isinstance(phone, str):
        phone = str(phone)

    try:
        return traverse_tree(
            [int(digit) for digit in phone if digit.isdigit()],
            mapping,
        )
    except KeyError as ex:
        raise InvalidPhone("Invalid phone {}".format(phone)) from ex


def country_prefixes() -> typing.Dict[str, int]:
    """Return a mapping of country code to calling prefix.

    For countries with multiple prefixes, an arbitrary calling prefix is
    chosen. Many north american country codes just map to 1.
    """

    def transverse(node, path):
        if isinstance(node, dict):
            for key, value in node.items():
                for i in transverse(value, path + str(key)):
                    yield i
        else:
            yield node, 1 if node in ["US", "CA"] else int(path)

    return dict(transverse(mapping, ""))


def country_prefix(country_code: str) -> int:
    """Return the calling prefix for a given country.

    The country must be provided as a ISO-3166-1 alpha-2 country code.
    """
    prefixes = country_prefixes()
    try:
        return prefixes[country_code.upper()]
    except Exception as ex:
        raise InvalidCountry("Invalid country {}".format(country_code)) from ex


def phone_country_prefix(
    phone: typing.Union[str, int],
) -> typing.Tuple[int, str]:  # pylint: disable=inconsistent-return-statements
    """Figure out that calling prefix and country a phone number belongs to.

    The country returned is in the form of a ISO-3166-1 alpha-2 country code.
    """

    def traverse_tree(
        key: typing.List[int],
        tree: dict,
        key_so_far: str = "",
    ) -> typing.Tuple[int, str]:
        next_key, *remaining_keys = key
        next_element: typing.Union[str, typing.Dict[int, str]] = tree[next_key]
        key_so_far = key_so_far + str(next_key)
        if isinstance(next_element, dict):
            return traverse_tree(
                remaining_keys,
                next_element,
                key_so_far,
            )

        return int(key_so_far), next_element

    if not isinstance(phone, str):
        phone = str(phone)

    try:
        return traverse_tree(
            [int(digit) for digit in phone if digit.isdigit()],
            mapping,
        )<commit_msg>Switch lists to generators<commit_after># pylint: disable=consider-using-f-string
import typing

from .e164 import mapping
from .e212 import networks
from .errors import InvalidCountry
from .errors import InvalidNetwork
from .errors import InvalidPhone


def phone_country(phone: typing.Union[str, int]) -> str:
    """Return the ISO-3166-1 country code a given phone number belongs to.

    The phone number must be specified in E.164, aka international format.
    The returned country code is of ISO-3166-1 alpha-2 code.
    """

    def traverse_tree(
        key: typing.Iterable[int],
        tree: dict,
    ) -> str:
        next_key, *remaining_keys = key
        next_element: typing.Union[str, typing.Dict[int, str]] = tree[next_key]
        if isinstance(next_element, dict):
            return traverse_tree(remaining_keys, next_element)

        return next_element

    if not isinstance(phone, str):
        phone = str(phone)

    try:
        return traverse_tree(
            (int(digit) for digit in phone if digit.isdigit()),
            mapping,
        )
    except KeyError as ex:
        raise InvalidPhone("Invalid phone {}".format(phone)) from ex


def country_prefixes() -> typing.Dict[str, int]:
    """Return a mapping of country code to calling prefix.

    For countries with multiple prefixes, an arbitrary calling prefix is
    chosen. Many north american country codes just map to 1.
    """

    def transverse(node, path):
        if isinstance(node, dict):
            for key, value in node.items():
                for i in transverse(value, path + str(key)):
                    yield i
        else:
            yield node, 1 if node in ["US", "CA"] else int(path)

    return dict(transverse(mapping, ""))


def country_prefix(country_code: str) -> int:
    """Return the calling prefix for a given country.

    The country must be provided as a ISO-3166-1 alpha-2 country code.
    """
    prefixes = country_prefixes()
    try:
        return prefixes[country_code.upper()]
    except Exception as ex:
        raise InvalidCountry("Invalid country {}".format(country_code)) from ex


def phone_country_prefix(
    phone: typing.Union[str, int],
) -> typing.Tuple[int, str]:  # pylint: disable=inconsistent-return-statements
    """Figure out that calling prefix and country a phone number belongs to.

    The country returned is in the form of a ISO-3166-1 alpha-2 country code.
    """

    def traverse_tree(
        key: typing.Iterable[int],
        tree: dict,
        key_so_far: str = "",
    ) -> typing.Tuple[int, str]:
        next_key, *remaining_keys = key
        next_element: typing.Union[str, typing.Dict[int, str]] = tree[next_key]
        key_so_far = key_so_far + str(next_key)
        if isinstance(next_element, dict):
            return traverse_tree(
                remaining_keys,
                next_element,
                key_so_far,
            )

        return int(key_so_far), next_element

    if not isinstance(phone, str):
        phone = str(phone)

    try:
        return traverse_tree(
            (int(digit) for digit in phone if digit.isdigit()),
            mapping,
        )
{'fraction_non_alphanumeric': 0.0943784639746635, 'fraction_numerical': 0.010768012668250199, 'mean_word_length': 2.4513661202185792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1325841', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1752, 'n_words': 597}