Skip to content

Commit f081be1

Browse files
committed
improve typing for mytoncore/utils.py
1 parent 06e8fb9 commit f081be1

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

mytoncore/utils.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,80 @@
22
import json
33
import re
44
import subprocess
5+
56
try:
67
# Python 3.9+
78
from importlib.resources import files, as_file
89
except ImportError:
910
# Python < 3.9
10-
from importlib_resources import files, as_file
11+
from importlib_resources import files, as_file # pyright: ignore[reportMissingImports]
1112

1213

13-
def str2b64(s):
14+
def str2b64(s: str):
1415
b = s.encode("utf-8")
1516
b64 = base64.b64encode(b)
1617
b64 = b64.decode("utf-8")
1718
return b64
18-
# end define
1919

2020

21-
def b642str(b64):
22-
b64 = b64.encode("utf-8")
23-
b = base64.b64decode(b64)
24-
s = b.decode("utf-8")
21+
def b642str(b64: str):
22+
b = base64.b64decode(b64.encode())
23+
s = b.decode()
2524
return s
26-
# end define
2725

2826

29-
def dict2b64(d):
27+
def dict2b64(d: dict):
3028
s = json.dumps(d)
3129
b64 = str2b64(s)
3230
return b64
33-
# end define
3431

3532

36-
def b642dict(b64):
33+
def b642dict(b64: str):
3734
s = b642str(b64)
3835
d = json.loads(s)
3936
return d
40-
# end define
4137

4238

43-
def hex2b64(input): # TODO: remove duplicates
44-
hexBytes = bytes.fromhex(input)
45-
b64Bytes = base64.b64encode(hexBytes)
46-
b64String = b64Bytes.decode()
47-
return b64String
48-
# end define
39+
def hex2b64(inp: str): # TODO: remove duplicates
40+
hex_bytes = bytes.fromhex(inp)
41+
b64_bytes = base64.b64encode(hex_bytes)
42+
b64_string = b64_bytes.decode()
43+
return b64_string
4944

5045

51-
def b642hex(input):
52-
b64Bytes = input.encode()
53-
hexBytes = base64.b64decode(b64Bytes)
54-
hexString = hexBytes.hex()
55-
return hexString
56-
# end define
46+
def b642hex(inp: str):
47+
b64_bytes = inp.encode()
48+
hex_bytes = base64.b64decode(b64_bytes)
49+
hex_string = hex_bytes.hex()
50+
return hex_string
5751

5852

59-
def xhex2hex(x):
53+
def xhex2hex(x: str) -> str | None:
6054
try:
6155
b = x[1:]
6256
h = b.lower()
6357
return h
6458
except Exception:
6559
return None
66-
#end define
6760

68-
def hex2base64(h): # TODO: remove duplicates
61+
62+
def hex2base64(h: str): # TODO: remove duplicates
6963
b = bytes.fromhex(h)
7064
b64 = base64.b64encode(b)
7165
s = b64.decode("utf-8")
7266
return s
73-
#end define
7467

7568

76-
def str2bool(str):
77-
if str == "true":
69+
def str2bool(s: str):
70+
if s == "true":
7871
return True
7972
return False
80-
# end define
8173

8274

83-
def ng2g(ng):
75+
def ng2g(ng: int | None) -> float | None:
8476
if ng is None:
85-
return
86-
return int(ng)/10**9
87-
#end define
77+
return None
78+
return int(ng) / 10**9
8879

8980

9081
def parse_db_stats(path: str):
@@ -103,23 +94,25 @@ def parse_db_stats(path: str):
10394
result[s[0]] = {}
10495
result[s[0]] = {k: float(v) for k, v in items}
10596
return result
106-
# end define
10797

108-
def get_hostname():
98+
99+
def get_hostname() -> str:
109100
return subprocess.run(["hostname"], stdout=subprocess.PIPE).stdout.decode().strip()
110101

102+
111103
def hex_shard_to_int(shard_id_str: str) -> dict:
112104
try:
113-
wc, shard_hex = shard_id_str.split(':')
105+
wc, shard_hex = shard_id_str.split(":")
114106
wc = int(wc)
115107
shard = int(shard_hex, 16)
116-
if shard >= 2 ** 63:
117-
shard -= 2 ** 64
108+
if shard >= 2**63:
109+
shard -= 2**64
118110
return {"workchain": wc, "shard": shard}
119111
except (ValueError, IndexError):
120112
raise Exception(f'Invalid shard ID "{shard_id_str}"')
121113

122-
def signed_int_to_hex64(value):
114+
115+
def signed_int_to_hex64(value: int):
123116
if value < 0:
124117
value = (1 << 64) + value
125118
return f"{value:016X}"
@@ -148,11 +141,11 @@ def _count_trailing_zeroes64(value: int) -> int:
148141
if u == 0:
149142
return 64
150143
return ((u & -u).bit_length()) - 1
144+
151145
return 63 - _count_trailing_zeroes64(_to_unsigned64(shard_id))
152146

153147

154148
def shard_prefix(shard_id: int, length_: int):
155-
156149
def _to_signed64(v: int) -> int:
157150
return v - (1 << 64) if v >= (1 << 63) else v
158151

@@ -162,7 +155,9 @@ def _to_signed64(v: int) -> int:
162155
x = _lower_bit64(u)
163156
y = 1 << (63 - length_)
164157
if y < x:
165-
raise ValueError("requested prefix length is longer (more specific) than current shard id")
158+
raise ValueError(
159+
"requested prefix length is longer (more specific) than current shard id"
160+
)
166161
mask_non_lower = (~(y - 1)) & _MASK64 # equals -y mod 2^64; clears bits below y
167162
res_u = (u & mask_non_lower) | y
168163
return _to_signed64(res_u)

0 commit comments

Comments
 (0)