<commit_before>
from .. import encoding
from ..networks import DEFAULT_NETCODES, NETWORK_NAMES, NETWORKS

DEFAULT_ADDRESS_TYPES = ["address", "pay_to_script"]

def _generate_network_prefixes():
    d = {}
    for n in NETWORKS:
        for prop in "wif address pay_to_script prv32 pub32".split():
            v = getattr(n, prop, None)
            if v:
                if v not in d:
                    d[v] = []
                d[v].append((n, prop))
    return d


NETWORK_PREFIXES = _generate_network_prefixes()


def netcode_and_type_for_data(data, netcodes=NETWORK_NAMES):
    """
    Given some already-decoded raw data from a base58 string,
    return (N, T) where N is the network code ("BTC" or "LTC") and
    T is the data type ("wif", "address", "public_pair", "prv32", "pub32").
    May also raise EncodingError.
    """
    d = {}
    for length in (4, 1):
        for network, the_type in NETWORK_PREFIXES.get(data[:length], []):
            d[network.code] = the_type
    for netcode in netcodes:
        v = d.get(netcode)
        if v:
            return netcode, v

    raise encoding.EncodingError("unknown prefix")


def _check_against(text, expected_type, allowable_netcodes):
    try:
        data = encoding.a2b_hashed_base58(text)
        netcode, the_type = netcode_and_type_for_data(data)
        if the_type in expected_type and netcode in allowable_netcodes:
            return netcode
    except encoding.EncodingError:
        pass
    return None


def is_address_valid(address, allowable_types=DEFAULT_ADDRESS_TYPES, allowable_netcodes=DEFAULT_NETCODES):
    """
    Accept an address, and a list of allowable address types (a subset of "address" and "pay_to_script"),
    and allowable networks (defaulting to just Bitcoin mainnet), return the network that the address is
    a part of, or None if it doesn't validate.
    """<commit_msg>Fix test.<commit_after>
from .. import encoding
from ..networks import DEFAULT_NETCODES, NETWORK_NAMES, NETWORKS

DEFAULT_ADDRESS_TYPES = ["address", "pay_to_script"]

def _generate_network_prefixes():
    d = {}
    for n in NETWORKS:
        for prop in "wif address pay_to_script prv32 pub32".split():
            v = getattr(n, prop, None)
            if v:
                if v not in d:
                    d[v] = []
                d[v].append((n, prop))
    return d


NETWORK_PREFIXES = _generate_network_prefixes()


def netcode_and_type_for_data(data, netcodes=NETWORK_NAMES):
    """
    Given some already-decoded raw data from a base58 string,
    return (N, T) where N is the network code ("BTC" or "LTC") and
    T is the data type ("wif", "address", "public_pair", "prv32", "pub32").
    May also raise EncodingError.
    """
    d = {}
    for length in (4, 1):
        for network, the_type in NETWORK_PREFIXES.get(data[:length], []):
            d[network.code] = the_type
    for netcode in netcodes:
        v = d.get(netcode)
        if v:
            return netcode, v

    raise encoding.EncodingError("unknown prefix")


def _check_against(text, expected_type, allowable_netcodes):
    try:
        data = encoding.a2b_hashed_base58(text)
        netcode, the_type = netcode_and_type_for_data(data, netcodes=allowable_netcodes)
        if the_type in expected_type and netcode in allowable_netcodes:
            return netcode
    except encoding.EncodingError:
        pass
    return None


def is_address_valid(address, allowable_types=DEFAULT_ADDRESS_TYPES, allowable_netcodes=DEFAULT_NETCODES):
    """
    Accept an address, and a list of allowable address types (a subset of "address" and "pay_to_script"),
    and allowable networks (defaulting to just Bitcoin mainnet), return the network that the address is
    a part of, or None if it doesn't validate.
    """
{'fraction_non_alphanumeric': 0.09148766905330151, 'fraction_numerical': 0.007955449482895784, 'mean_word_length': 2.8647540983606556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4295901', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1172, 'n_words': 364}