<commit_before>from decimal import Decimal

from eth_utils import decode_hex

from eth_abi.utils.padding import (
    zpad32,
    zpad32_right,
    fpad32,
)


def make_word(description: str) -> bytes:
    r"""
    Converts a "description" of a 32-byte word into a 32-byte word.  A
    description is either a hex representation of a byte string (e.g.
    'deafbeef') or a hex representation along with a fill directive
    'leftpadchar<' or '>rightpadchar'.

    Examples:
    >>> # Left padding examples
    >>> assert make_word('0<deadbeef') == zpad32(b'\xde\xad\xbe\xef')
    >>> assert make_word('f<deadbeef') == fpad32(b'\xde\xad\xbe\xef')
    >>> assert make_word('deadbeef') == zpad32(b'\xde\xad\xbe\xef')
    >>> # Right padding examples
    >>> assert make_word('deadbeef>0') == zpad32_right(b'\xde\xad\xbe\xef')
    """
    if '<' in description:
        fill_char, hex_str = description.split('<')
        return decode_hex(hex_str.rjust(64, fill_char))

    if '>' in description:
        hex_str, fill_char = description.split('>')
        return decode_hex(hex_str.ljust(64, fill_char))

    return decode_hex(description.rjust(64, '0'))


def words(*descriptions: str) -> bytes:
    r"""
    Converts multiple word descriptions into 32-byte words joined into a single
    byte string.

    Examples:
    >>> assert words('1') == zpad32(b'\x01')
    >>> assert words('1', '2f>0') == zpad32(b'\x01') + zpad32_right(b'\x2f')
    """
    return b''.join(make_word(d) for d in descriptions)


CORRECT_TUPLE_ENCODINGS = [
    # (type string, python value, byte string)
    (
        '(bytes32,bytes32)',
        (zpad32_right(b'a'), zpad32_right(b'b')),
        words('61>0', '62>0'),
    ),
    (
        '(bytes32[])',
        ((zpad32_right(b'a'), zpad32_right(b'b')),),
        words('20', '2', '61>0', '62>0'),
    ),
    (
        '(uint32,uint32)',
        (6, 1),
        words('6', '1'),
    ),
    (
        '(uint32,uint32)',
        (2 ** 32 - 1, 2 ** 32 - 1),
        words('ffffffff', 'ffffffff'),
    ),
    (
        '(address,uint32,bytes32,int32)',
        ('0x82a978b3f5962a5b0957d9ee9eef472ee55b42f1', 1, zpad32_right(b'stupid pink animal'), 0),
        words('82a978b3f5962a5b0957d9ee9eef472ee55b42f1', '1', '7374757069642070696e6b20616e696d616c>0', '0'),
    ),
    (
        '(uint256,bytes)',
        (0, b''),
        words('0', '40', '0', '0'),
    ),
]

CORRECT_SINGLE_ENCODINGS = CORRECT_TUPLE_ENCODINGS + [
    # encode_single/decode_single accept tuple of type components
    (('uint', '256', []), 2 ** 256 - 1, words('f<f')),

    #####
    # (type string, python value, byte string)
    #####

    # uint<M>
    ('uint8', 255, words('ff')),
    ('uint8', 21, words('15')),
    ('uint8', 1, words('1')),
    ('uint256', 2 ** 256 - 1, words('f<f')),
    ('uint256', 2 ** 256 - 100, words('f<9c')),<commit_msg>Add more tuple tests<commit_after>from decimal import Decimal

from eth_utils import decode_hex

from eth_abi.utils.padding import (
    zpad32,
    zpad32_right,
    fpad32,
)


def make_word(description: str) -> bytes:
    r"""
    Converts a "description" of a 32-byte word into a 32-byte word.  A
    description is either a hex representation of a byte string (e.g.
    'deafbeef') or a hex representation along with a fill directive
    'leftpadchar<' or '>rightpadchar'.

    Examples:
    >>> # Left padding examples
    >>> assert make_word('0<deadbeef') == zpad32(b'\xde\xad\xbe\xef')
    >>> assert make_word('f<deadbeef') == fpad32(b'\xde\xad\xbe\xef')
    >>> assert make_word('deadbeef') == zpad32(b'\xde\xad\xbe\xef')
    >>> # Right padding examples
    >>> assert make_word('deadbeef>0') == zpad32_right(b'\xde\xad\xbe\xef')
    """
    if '<' in description:
        fill_char, hex_str = description.split('<')
        return decode_hex(hex_str.rjust(64, fill_char))

    if '>' in description:
        hex_str, fill_char = description.split('>')
        return decode_hex(hex_str.ljust(64, fill_char))

    return decode_hex(description.rjust(64, '0'))


def words(*descriptions: str) -> bytes:
    r"""
    Converts multiple word descriptions into 32-byte words joined into a single
    byte string.

    Examples:
    >>> assert words('1') == zpad32(b'\x01')
    >>> assert words('1', '2f>0') == zpad32(b'\x01') + zpad32_right(b'\x2f')
    """
    return b''.join(make_word(d) for d in descriptions)


CORRECT_TUPLE_ENCODINGS = [
    # (type string, python value, byte string)
    (
        '(bytes32,bytes32)',
        (zpad32_right(b'a'), zpad32_right(b'b')),
        words('61>0', '62>0'),
    ),
    (
        '(bytes32[])',
        ((zpad32_right(b'a'), zpad32_right(b'b')),),
        words('20', '2', '61>0', '62>0'),
    ),
    (
        '(uint32,uint32)',
        (6, 1),
        words('6', '1'),
    ),
    (
        '(uint32,uint32)',
        (2 ** 32 - 1, 2 ** 32 - 1),
        words('ffffffff', 'ffffffff'),
    ),
    (
        '(address,uint32,bytes32,int32)',
        ('0x82a978b3f5962a5b0957d9ee9eef472ee55b42f1', 1, zpad32_right(b'stupid pink animal'), 0),
        words('82a978b3f5962a5b0957d9ee9eef472ee55b42f1', '1', '7374757069642070696e6b20616e696d616c>0', '0'),
    ),
    (
        '(uint256,bytes)',
        (0, b''),
        words('0', '40', '0', '0'),
    ),
    # empty tuples
    (
        '()', (), b'',
    ),
    (
        '((),((),((),())))',
        ((), ((), ((), ()))),
        b'',
    ),
    # nested tuples
    (
        '(int,(int,int[]))',
        (1, (2, (3, 3))),
        words('1', '40', '2', '40', '2', '3', '3')
    ),
    (
        '((bytes,bool),(bytes,bool))',
        ((b'<NAME>', False), (b'boaty mcboatface', True)),
        words(
            '40',  # offset of first (bytes,bool)
            'c0',  # offset of second (bytes,bool)
            '40',  # offset of bytes in first (bytes,bool)
            '0',  # encoding for `False`
            '12',  # length of b'david attenborough'
            '646176696420617474656e626f726f756768>0',  # encoding of b'david attenborough'
            '40',  # offset of bytes in second (bytes,bool)
            '1',  # encoding for `True`
            '10',  # length of b'boaty mcboatface'
            '626f617479206d63626f617466616365>0',  # encoding of b'boaty mcboatface'
        )
    ),
]

CORRECT_SINGLE_ENCODINGS = CORRECT_TUPLE_ENCODINGS + [
    # encode_single/decode_single accept tuple of type components
    (('uint', '256', []), 2 ** 256 - 1, words('f<f')),

    #####
    # (type string, python value, byte string)
    #####

    # uint<M>
    ('uint8', 255, words('ff')),
    ('uint8', 21, words('15')),
    ('uint8', 1, words('1')),
    ('uint256', 2 ** 256 - 1, words('f<f')),
    ('uint256', 2 ** 256 - 100, words('f<9c')),
{'fraction_non_alphanumeric': 0.1884793314430682, 'fraction_numerical': 0.08386807939113565, 'mean_word_length': 2.7130193905817173, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '6563470', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2485, 'n_words': 517}