File: utils_py3.py

package info (click to toggle)
pyrlp 0.5.1-1.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 304 kB
  • sloc: python: 1,717; makefile: 234; sh: 16
file content (67 lines) | stat: -rw-r--r-- 1,434 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import abc
import binascii
from math import ceil


class Atomic(type.__new__(abc.ABCMeta, 'metaclass', (), {})):
    """ABC for objects that can be RLP encoded as is."""
    pass


Atomic.register(str)
Atomic.register(bytes)


def str_to_bytes(value):
    if isinstance(value, bytearray):
        value = bytes(value)
    if isinstance(value, bytes):
        return value
    return bytes(value, 'utf-8')


def bytes_to_str(value):
    if isinstance(value, str):
        return value
    return value.decode('utf-8')


def ascii_chr(value):
    return bytes([value])


def int_to_big_endian(value):
    byte_length = max(ceil(value.bit_length() / 8), 1)
    return (value).to_bytes(byte_length, byteorder='big')


def big_endian_to_int(value):
    return int.from_bytes(value, byteorder='big')


def is_integer(value):
    return isinstance(value, int)


def decode_hex(s):
    if isinstance(s, str):
        return bytes.fromhex(s)
    if isinstance(s, (bytes, bytearray)):
        return binascii.unhexlify(s)
    raise TypeError('Value must be an instance of str or bytes')


def encode_hex(b):
    if isinstance(b, str):
        b = bytes(b, 'utf-8')
    if isinstance(b, (bytes, bytearray)):
        return str(binascii.hexlify(b), 'utf-8')
    raise TypeError('Value must be an instance of str or bytes')


def safe_ord(c):
    try:
        return ord(c)
    except TypeError:
        assert isinstance(c, int)
        return c