File: humanize.py

package info (click to toggle)
python-eth-utils 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,140 kB
  • sloc: python: 5,985; makefile: 238
file content (203 lines) | stat: -rw-r--r-- 4,582 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from typing import (
    Any,
    Iterable,
    Iterator,
    Tuple,
    Union,
)
from urllib import (
    parse,
)

from eth_typing import (
    URI,
    Hash32,
)

from eth_utils.currency import (
    denoms,
    from_wei,
)

from .toolz import (
    sliding_window,
    take,
)


def humanize_seconds(seconds: Union[float, int]) -> str:
    if int(seconds) == 0:
        return "0s"

    unit_values = _consume_leading_zero_units(_humanize_seconds(int(seconds)))

    return "".join((f"{amount}{unit}" for amount, unit in take(3, unit_values)))


SECOND = 1
MINUTE = 60
HOUR = 60 * 60
DAY = 24 * HOUR
YEAR = 365 * DAY
MONTH = YEAR // 12
WEEK = 7 * DAY


UNITS = (
    (YEAR, "y"),
    (MONTH, "m"),
    (WEEK, "w"),
    (DAY, "d"),
    (HOUR, "h"),
    (MINUTE, "m"),
    (SECOND, "s"),
)


def _consume_leading_zero_units(
    units_iter: Iterator[Tuple[int, str]]
) -> Iterator[Tuple[int, str]]:
    for amount, unit in units_iter:
        if amount == 0:
            continue
        else:
            yield (amount, unit)
            break

    yield from units_iter


def _humanize_seconds(seconds: int) -> Iterator[Tuple[int, str]]:
    remainder = seconds

    for duration, unit in UNITS:
        if not remainder:
            break

        num = remainder // duration
        yield num, unit

        remainder %= duration


DISPLAY_HASH_CHARS = 4


def humanize_bytes(value: bytes) -> str:
    if len(value) <= DISPLAY_HASH_CHARS + 1:
        return value.hex()
    value_as_hex = value.hex()
    head = value_as_hex[:DISPLAY_HASH_CHARS]
    tail = value_as_hex[-1 * DISPLAY_HASH_CHARS :]
    return f"{head}..{tail}"


def humanize_hexstr(value: str) -> str:
    tail = value[-1 * DISPLAY_HASH_CHARS :]

    if value[:2] == "0x":
        if len(value[2:]) <= DISPLAY_HASH_CHARS * 2:
            return value
        head = value[2 : DISPLAY_HASH_CHARS + 2]
        return f"0x{head}..{tail}"
    else:
        if len(value) <= DISPLAY_HASH_CHARS * 2:
            return value
        head = value[:DISPLAY_HASH_CHARS]
        return f"{head}..{tail}"


def humanize_hash(value: Hash32) -> str:
    return humanize_bytes(value)


def humanize_ipfs_uri(uri: URI) -> str:
    if not is_ipfs_uri(uri):
        raise TypeError(
            f"{uri} does not look like a valid IPFS uri. Currently, "
            "only CIDv0 hash schemes are supported."
        )

    parsed = parse.urlparse(uri)
    ipfs_hash = parsed.netloc
    head = ipfs_hash[:DISPLAY_HASH_CHARS]
    tail = ipfs_hash[-1 * DISPLAY_HASH_CHARS :]
    return f"ipfs://{head}..{tail}"


def is_ipfs_uri(value: Any) -> bool:
    if not isinstance(value, str):
        return False

    parsed = parse.urlparse(value)
    if parsed.scheme != "ipfs" or not parsed.netloc:
        return False

    return _is_CIDv0_ipfs_hash(parsed.netloc)


def _is_CIDv0_ipfs_hash(ipfs_hash: str) -> bool:
    if ipfs_hash.startswith("Qm") and len(ipfs_hash) == 46:
        return True
    return False


def _find_breakpoints(*values: int) -> Iterator[int]:
    yield 0
    for index, (left, right) in enumerate(sliding_window(2, values), 1):
        if left + 1 == right:
            continue
        else:
            yield index
    yield len(values)


def _extract_integer_ranges(*values: int) -> Iterator[Tuple[int, int]]:
    """
    Return a tuple of consecutive ranges of integers.

    :param values: a sequence of ordered integers

    - fn(1, 2, 3) -> ((1, 3),)
    - fn(1, 2, 3, 7, 8, 9) -> ((1, 3), (7, 9))
    - fn(1, 7, 8, 9) -> ((1, 1), (7, 9))
    """
    for left, right in sliding_window(2, _find_breakpoints(*values)):
        chunk = values[left:right]
        yield chunk[0], chunk[-1]


def _humanize_range(bounds: Tuple[int, int]) -> str:
    left, right = bounds
    if left == right:
        return str(left)
    else:
        return f"{left}-{right}"


def humanize_integer_sequence(values_iter: Iterable[int]) -> str:
    """
    Return a concise, human-readable string representing a sequence of integers.

    - fn((1, 2, 3)) -> '1-3'
    - fn((1, 2, 3, 7, 8, 9)) -> '1-3|7-9'
    - fn((1, 2, 3, 5, 7, 8, 9)) -> '1-3|5|7-9'
    - fn((1, 7, 8, 9)) -> '1|7-9'
    """
    values = tuple(values_iter)
    if not values:
        return "(empty)"
    else:
        return "|".join(map(_humanize_range, _extract_integer_ranges(*values)))


def humanize_wei(number: int) -> str:
    if number >= denoms.finney:
        unit = "ether"
    elif number >= denoms.mwei:
        unit = "gwei"
    else:
        unit = "wei"
    amount = from_wei(number, unit)
    x = f"{str(amount)} {unit}"
    return x