forked from FISCO-BCOS/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (40 loc) · 1.33 KB
/
main.py
File metadata and controls
49 lines (40 loc) · 1.33 KB
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
from typing import (
Type,
Union,
cast,
overload,
)
from eth_utils import (
hexstr_if_str,
to_bytes,
)
class HexBytes(bytes):
"""
HexBytes is a *very* thin wrapper around the python
built-in :class:`bytes` class. It has these three changes:
1. Accepts hex strings as an initializing value
2. Returns hex with prefix '0x' from :meth:`HexBytes.hex`
3. The string representation at console is in hex
"""
def __new__(cls: Type[bytes], val: Union[bytes, int, str]) -> "HexBytes":
bytesval = hexstr_if_str(to_bytes, val)
return cast(HexBytes, super().__new__(cls, bytesval)) # type: ignore # https://github.com/python/typeshed/issues/2630 # noqa: E501
def hex(self) -> str:
"""
Just like :meth:`bytes.hex`, but prepends "0x"
"""
return '0x' + super().hex()
@overload
def __getitem__(self, key: int) -> int:
...
@overload # noqa: F811
def __getitem__(self, key: slice) -> 'HexBytes':
...
def __getitem__(self, key: Union[int, slice]) -> Union[int, bytes, 'HexBytes']: # noqa: F811
result = super().__getitem__(key)
if hasattr(result, 'hex'):
return type(self)(result)
else:
return result
def __repr__(self) -> str:
return 'HexBytes(%r)' % self.hex()