faster-eth-utils 5.3.18__cp312-cp312-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of faster-eth-utils might be problematic. Click here for more details.

Files changed (53) hide show
  1. faster_eth_utils/__init__.py +144 -0
  2. faster_eth_utils/__json/eth_networks.json +1 -0
  3. faster_eth_utils/__main__.py +5 -0
  4. faster_eth_utils/abi.cp312-win32.pyd +0 -0
  5. faster_eth_utils/abi.py +847 -0
  6. faster_eth_utils/address.cp312-win32.pyd +0 -0
  7. faster_eth_utils/address.py +145 -0
  8. faster_eth_utils/applicators.cp312-win32.pyd +0 -0
  9. faster_eth_utils/applicators.py +209 -0
  10. faster_eth_utils/conversions.cp312-win32.pyd +0 -0
  11. faster_eth_utils/conversions.py +191 -0
  12. faster_eth_utils/crypto.cp312-win32.pyd +0 -0
  13. faster_eth_utils/crypto.py +21 -0
  14. faster_eth_utils/currency.cp312-win32.pyd +0 -0
  15. faster_eth_utils/currency.py +141 -0
  16. faster_eth_utils/curried/__init__.py +306 -0
  17. faster_eth_utils/debug.cp312-win32.pyd +0 -0
  18. faster_eth_utils/debug.py +20 -0
  19. faster_eth_utils/decorators.cp312-win32.pyd +0 -0
  20. faster_eth_utils/decorators.py +119 -0
  21. faster_eth_utils/encoding.cp312-win32.pyd +0 -0
  22. faster_eth_utils/encoding.py +6 -0
  23. faster_eth_utils/exceptions.cp312-win32.pyd +0 -0
  24. faster_eth_utils/exceptions.py +11 -0
  25. faster_eth_utils/functional.cp312-win32.pyd +0 -0
  26. faster_eth_utils/functional.py +89 -0
  27. faster_eth_utils/hexadecimal.cp312-win32.pyd +0 -0
  28. faster_eth_utils/hexadecimal.py +80 -0
  29. faster_eth_utils/humanize.cp312-win32.pyd +0 -0
  30. faster_eth_utils/humanize.py +201 -0
  31. faster_eth_utils/logging.py +146 -0
  32. faster_eth_utils/module_loading.cp312-win32.pyd +0 -0
  33. faster_eth_utils/module_loading.py +31 -0
  34. faster_eth_utils/network.cp312-win32.pyd +0 -0
  35. faster_eth_utils/network.py +92 -0
  36. faster_eth_utils/numeric.cp312-win32.pyd +0 -0
  37. faster_eth_utils/numeric.py +43 -0
  38. faster_eth_utils/py.typed +0 -0
  39. faster_eth_utils/pydantic.py +101 -0
  40. faster_eth_utils/toolz.cp312-win32.pyd +0 -0
  41. faster_eth_utils/toolz.py +84 -0
  42. faster_eth_utils/types.cp312-win32.pyd +0 -0
  43. faster_eth_utils/types.py +68 -0
  44. faster_eth_utils/typing/__init__.py +18 -0
  45. faster_eth_utils/typing/misc.py +14 -0
  46. faster_eth_utils/units.cp312-win32.pyd +0 -0
  47. faster_eth_utils/units.py +31 -0
  48. faster_eth_utils-5.3.18.dist-info/METADATA +193 -0
  49. faster_eth_utils-5.3.18.dist-info/RECORD +53 -0
  50. faster_eth_utils-5.3.18.dist-info/WHEEL +5 -0
  51. faster_eth_utils-5.3.18.dist-info/licenses/LICENSE +21 -0
  52. faster_eth_utils-5.3.18.dist-info/top_level.txt +3 -0
  53. faster_eth_utils__mypyc.cp312-win32.pyd +0 -0
@@ -0,0 +1,141 @@
1
+ import decimal
2
+ from decimal import (
3
+ localcontext,
4
+ )
5
+ from typing import (
6
+ Final,
7
+ Union,
8
+ final,
9
+ )
10
+
11
+ from .types import (
12
+ is_integer,
13
+ is_string,
14
+ )
15
+ from .units import (
16
+ units,
17
+ )
18
+
19
+
20
+ @final
21
+ class denoms:
22
+ wei: Final = int(units["wei"])
23
+ kwei: Final = int(units["kwei"])
24
+ babbage: Final = int(units["babbage"])
25
+ femtoether: Final = int(units["femtoether"])
26
+ mwei: Final = int(units["mwei"])
27
+ lovelace: Final = int(units["lovelace"])
28
+ picoether: Final = int(units["picoether"])
29
+ gwei: Final = int(units["gwei"])
30
+ shannon: Final = int(units["shannon"])
31
+ nanoether: Final = int(units["nanoether"])
32
+ nano: Final = int(units["nano"])
33
+ szabo: Final = int(units["szabo"])
34
+ microether: Final = int(units["microether"])
35
+ micro: Final = int(units["micro"])
36
+ finney: Final = int(units["finney"])
37
+ milliether: Final = int(units["milliether"])
38
+ milli: Final = int(units["milli"])
39
+ ether: Final = int(units["ether"])
40
+ kether: Final = int(units["kether"])
41
+ grand: Final = int(units["grand"])
42
+ mether: Final = int(units["mether"])
43
+ gether: Final = int(units["gether"])
44
+ tether: Final = int(units["tether"])
45
+
46
+
47
+ MIN_WEI: Final = 0
48
+ MAX_WEI: Final = 2**256 - 1
49
+
50
+ _NumberType = Union[int, float, str, decimal.Decimal]
51
+
52
+
53
+ def _from_wei(number: int, unit_value: decimal.Decimal) -> Union[int, decimal.Decimal]:
54
+ if number == 0:
55
+ return 0
56
+
57
+ if number < MIN_WEI or number > MAX_WEI:
58
+ raise ValueError("value must be between 0 and 2**256 - 1")
59
+
60
+ with localcontext() as ctx:
61
+ ctx.prec = 999
62
+ d_number = decimal.Decimal(value=number, context=ctx)
63
+ result_value = d_number / unit_value
64
+
65
+ return result_value
66
+
67
+
68
+ def _to_wei(number: _NumberType, unit_value: decimal.Decimal) -> int:
69
+ if is_integer(number) or is_string(number):
70
+ d_number = decimal.Decimal(value=number) # type: ignore [arg-type]
71
+ elif isinstance(number, float):
72
+ d_number = decimal.Decimal(value=str(number))
73
+ elif isinstance(number, decimal.Decimal):
74
+ d_number = number
75
+ else:
76
+ raise TypeError("Unsupported type. Must be one of integer, float, or string")
77
+
78
+ if d_number == decimal.Decimal(0):
79
+ return 0
80
+
81
+ s_number = str(number)
82
+
83
+ if d_number < 1 and "." in s_number:
84
+ with localcontext() as ctx:
85
+ multiplier = len(s_number) - s_number.index(".") - 1
86
+ ctx.prec = multiplier
87
+ d_number = decimal.Decimal(value=number, context=ctx) * 10**multiplier # type: ignore [arg-type]
88
+ unit_value /= 10**multiplier
89
+
90
+ with localcontext() as ctx:
91
+ ctx.prec = 999
92
+ result_value = decimal.Decimal(value=d_number, context=ctx) * unit_value
93
+
94
+ if result_value < MIN_WEI or result_value > MAX_WEI:
95
+ raise ValueError("Resulting wei value must be between 0 and 2**256 - 1")
96
+
97
+ return int(result_value)
98
+
99
+
100
+ def from_wei(number: int, unit: str) -> Union[int, decimal.Decimal]:
101
+ """
102
+ Takes a number of wei and converts it to any other ether unit.
103
+ """
104
+ if unit.lower() not in units:
105
+ raise ValueError(f"Unknown unit. Must be one of {'/'.join(units.keys())}")
106
+
107
+ unit_value = units[unit.lower()]
108
+
109
+ return _from_wei(number, unit_value)
110
+
111
+
112
+ def to_wei(number: _NumberType, unit: str) -> int:
113
+ """
114
+ Takes a number of a unit and converts it to wei.
115
+ """
116
+ if unit.lower() not in units:
117
+ raise ValueError(f"Unknown unit. Must be one of {'/'.join(units.keys())}")
118
+
119
+ unit_value = units[unit.lower()]
120
+
121
+ return _to_wei(number, unit_value)
122
+
123
+
124
+ def from_wei_decimals(number: int, decimals: int) -> Union[int, decimal.Decimal]:
125
+ """
126
+ Takes a number of wei and converts it to a decimal with the specified
127
+ number of decimals.
128
+ """
129
+ unit_value = decimal.Decimal(10) ** decimal.Decimal(value=decimals)
130
+
131
+ return _from_wei(number, unit_value)
132
+
133
+
134
+ def to_wei_decimals(number: _NumberType, decimals: int) -> int:
135
+ """
136
+ Takes a number of a unit and converts it to wei with the specified
137
+ number of decimals.
138
+ """
139
+ unit_value = decimal.Decimal(10) ** decimal.Decimal(value=decimals)
140
+
141
+ return _to_wei(number, unit_value)
@@ -0,0 +1,306 @@
1
+ import sys
2
+ from typing import (
3
+ TYPE_CHECKING,
4
+ Any,
5
+ Callable,
6
+ Dict,
7
+ Generator,
8
+ Optional,
9
+ Sequence,
10
+ Tuple,
11
+ TypeVar,
12
+ Union,
13
+ overload,
14
+ )
15
+ from typing_extensions import TypeGuard
16
+
17
+ from faster_eth_utils import (
18
+ CamelModel,
19
+ ExtendedDebugLogger,
20
+ HasExtendedDebugLogger,
21
+ HasExtendedDebugLoggerMeta,
22
+ HasLogger,
23
+ HasLoggerMeta,
24
+ Network,
25
+ ValidationError,
26
+ abi_to_signature,
27
+ add_0x_prefix,
28
+ apply_formatter_at_index,
29
+ apply_formatter_if as non_curried_apply_formatter_if,
30
+ apply_formatter_to_array,
31
+ apply_formatters_to_dict as non_curried_apply_formatters_to_dict,
32
+ apply_formatters_to_sequence,
33
+ apply_key_map,
34
+ apply_one_of_formatters as non_curried_apply_one_of_formatters,
35
+ apply_to_return_value,
36
+ big_endian_to_int,
37
+ clamp,
38
+ collapse_if_tuple,
39
+ combine_argument_formatters,
40
+ combomethod,
41
+ decode_hex,
42
+ denoms,
43
+ encode_hex,
44
+ event_abi_to_log_topic,
45
+ event_signature_to_log_topic,
46
+ filter_abi_by_name,
47
+ filter_abi_by_type,
48
+ flatten_return,
49
+ from_wei,
50
+ from_wei_decimals,
51
+ function_abi_to_4byte_selector,
52
+ function_signature_to_4byte_selector,
53
+ get_abi_input_names,
54
+ get_abi_input_types,
55
+ get_abi_output_names,
56
+ get_abi_output_types,
57
+ get_aligned_abi_inputs,
58
+ get_all_event_abis,
59
+ get_all_function_abis,
60
+ get_extended_debug_logger,
61
+ get_logger,
62
+ get_normalized_abi_inputs,
63
+ hexstr_if_str as non_curried_hexstr_if_str,
64
+ humanize_bytes,
65
+ humanize_hash,
66
+ humanize_hexstr,
67
+ humanize_integer_sequence,
68
+ humanize_ipfs_uri,
69
+ humanize_seconds,
70
+ humanize_wei,
71
+ import_string,
72
+ int_to_big_endian,
73
+ is_0x_prefixed,
74
+ is_address,
75
+ is_binary_address,
76
+ is_boolean,
77
+ is_bytes,
78
+ is_canonical_address,
79
+ is_checksum_address,
80
+ is_checksum_formatted_address,
81
+ is_dict,
82
+ is_hex,
83
+ is_hex_address,
84
+ is_hexstr,
85
+ is_integer,
86
+ is_list,
87
+ is_list_like,
88
+ is_normalized_address,
89
+ is_null,
90
+ is_number,
91
+ is_same_address,
92
+ is_string,
93
+ is_text,
94
+ is_tuple,
95
+ keccak,
96
+ name_from_chain_id,
97
+ network_from_chain_id,
98
+ remove_0x_prefix,
99
+ replace_exceptions,
100
+ reversed_return,
101
+ setup_DEBUG2_logging,
102
+ short_name_from_chain_id,
103
+ sort_return,
104
+ text_if_str as non_curried_text_if_str,
105
+ to_bytes,
106
+ to_canonical_address,
107
+ to_checksum_address,
108
+ to_dict,
109
+ to_hex,
110
+ to_int,
111
+ to_list,
112
+ to_normalized_address,
113
+ to_ordered_dict,
114
+ to_set,
115
+ to_text,
116
+ to_tuple,
117
+ to_wei,
118
+ to_wei_decimals,
119
+ )
120
+ from faster_eth_utils.toolz import (
121
+ curry,
122
+ )
123
+
124
+ if TYPE_CHECKING:
125
+ if sys.version_info >= (3, 9):
126
+ from _typeshed import SupportsBool
127
+ # We have to sacrifice a little bit of specificity on dinosaur Python3.8
128
+ else:
129
+ SupportsBool = Any
130
+
131
+
132
+ TArg = TypeVar("TArg")
133
+ TOther = TypeVar("TOther")
134
+ TReturn = TypeVar("TReturn")
135
+ TValue = TypeVar("TValue")
136
+
137
+
138
+ @overload
139
+ def apply_formatter_if(
140
+ condition: Callable[[TArg], TypeGuard[TOther]],
141
+ ) -> Callable[[Callable[[TOther], TReturn]], Callable[[TArg], Union[TReturn, TArg]]]:
142
+ ...
143
+
144
+ @overload
145
+ def apply_formatter_if(
146
+ condition: Callable[[TArg], TypeGuard[TOther]], formatter: Callable[[TOther], TReturn]
147
+ ) -> Callable[[TArg], Union[TReturn, TArg]]:
148
+ ...
149
+
150
+ @overload
151
+ def apply_formatter_if(
152
+ condition: Callable[[TArg], TypeGuard[TOther]], formatter: Callable[[TOther], TReturn], value: TArg
153
+ ) -> Union[TReturn, TArg]:
154
+ ...
155
+
156
+ @overload
157
+ def apply_formatter_if(
158
+ condition: Callable[[TArg], bool], formatter: Callable[[TArg], TReturn], value: TArg
159
+ ) -> Union[TReturn, TArg]:
160
+ ...
161
+
162
+ def apply_formatter_if( # type: ignore
163
+ condition: Union[Callable[[TArg], TypeGuard[TOther]], Callable[[TArg], bool]],
164
+ formatter: Optional[Union[Callable[[TOther], TReturn], Callable[[TArg], TReturn]]] = None,
165
+ value: Optional[TArg] = None,
166
+ ) -> Union[
167
+ Callable[[Callable[[TOther], TReturn]], Callable[[TArg], Union[TReturn, TArg]]],
168
+ Callable[[TArg], Union[TReturn, TArg]],
169
+ TReturn,
170
+ TArg,
171
+ ]:
172
+ pass
173
+
174
+
175
+ @overload
176
+ def apply_one_of_formatters(
177
+ formatter_condition_pairs: Sequence[
178
+ Tuple[Callable[[TArg], "SupportsBool"], Callable[[TArg], TReturn]]
179
+ ],
180
+ ) -> Callable[[TArg], TReturn]: ...
181
+
182
+
183
+ @overload
184
+ def apply_one_of_formatters(
185
+ formatter_condition_pairs: Sequence[
186
+ Tuple[Callable[[TArg], "SupportsBool"], Callable[[TArg], TReturn]]
187
+ ],
188
+ value: TArg,
189
+ ) -> TReturn: ...
190
+
191
+
192
+ # This is just a stub to appease mypy, it gets overwritten later
193
+ def apply_one_of_formatters(
194
+ formatter_condition_pairs: Sequence[
195
+ Tuple[Callable[[TArg], "SupportsBool"], Callable[[TArg], TReturn]]
196
+ ],
197
+ value: Optional[TArg] = None,
198
+ ) -> TReturn: ...
199
+
200
+
201
+ @overload
202
+ def hexstr_if_str(
203
+ to_type: Callable[..., TReturn],
204
+ ) -> Callable[[Union[bytes, int, str]], TReturn]: ...
205
+
206
+
207
+ @overload
208
+ def hexstr_if_str(
209
+ to_type: Callable[..., TReturn], to_format: Union[bytes, int, str]
210
+ ) -> TReturn: ...
211
+
212
+
213
+ # This is just a stub to appease mypy, it gets overwritten later
214
+ def hexstr_if_str( # type: ignore
215
+ to_type: Callable[..., TReturn], to_format: Optional[Union[bytes, int, str]] = None
216
+ ) -> TReturn: ...
217
+
218
+
219
+ @overload
220
+ def text_if_str(
221
+ to_type: Callable[..., TReturn],
222
+ ) -> Callable[[Union[bytes, int, str]], TReturn]: ...
223
+
224
+
225
+ @overload
226
+ def text_if_str(
227
+ to_type: Callable[..., TReturn], text_or_primitive: Union[bytes, int, str]
228
+ ) -> TReturn: ...
229
+
230
+
231
+ # This is just a stub to appease mypy, it gets overwritten later
232
+ def text_if_str( # type: ignore
233
+ to_type: Callable[..., TReturn],
234
+ text_or_primitive: Optional[Union[bytes, int, str]] = None,
235
+ ) -> TReturn: ...
236
+
237
+
238
+ @overload
239
+ def apply_formatters_to_dict(
240
+ formatters: Dict[Any, Any], unaliased: bool = False
241
+ ) -> Callable[[Union[Dict[Any, Any], CamelModel]], Dict[Any, Any]]:
242
+ ...
243
+
244
+
245
+ @overload
246
+ def apply_formatters_to_dict(
247
+ formatters: Dict[Any, Any], value: Union[Dict[Any, Any], CamelModel], unaliased: bool = False
248
+ ) -> Dict[Any, Any]:
249
+ ...
250
+
251
+
252
+ # This is just a stub to appease mypy, it gets overwritten later
253
+ def apply_formatters_to_dict(
254
+ formatters: Dict[Any, Any],
255
+ value: Optional[Union[Dict[Any, Any], CamelModel]] = None,
256
+ unaliased: bool = False,
257
+ ) -> Dict[Any, Any]: ...
258
+
259
+
260
+ apply_formatter_at_index = curry(apply_formatter_at_index)
261
+ apply_formatter_if = curry(non_curried_apply_formatter_if) # noqa: F811
262
+ apply_formatter_to_array = curry(apply_formatter_to_array)
263
+ apply_formatters_to_dict = curry(non_curried_apply_formatters_to_dict) # noqa: F811
264
+ apply_formatters_to_sequence = curry(apply_formatters_to_sequence)
265
+ apply_key_map = curry(apply_key_map)
266
+ apply_one_of_formatters = curry(non_curried_apply_one_of_formatters) # noqa: F811
267
+ filter_abi_by_name = curry(filter_abi_by_name)
268
+ filter_abi_by_type = curry(filter_abi_by_type)
269
+ flatten_return = curry(flatten_return)
270
+ from_wei = curry(from_wei)
271
+ from_wei_decimals = curry(from_wei_decimals)
272
+ get_aligned_abi_inputs = curry(get_aligned_abi_inputs)
273
+ get_logger = curry(get_logger)
274
+ get_normalized_abi_inputs = curry(get_normalized_abi_inputs)
275
+ hexstr_if_str = curry(non_curried_hexstr_if_str) # noqa: F811
276
+ is_same_address = curry(is_same_address)
277
+ sort_return = curry(sort_return)
278
+ text_if_str = curry(non_curried_text_if_str) # noqa: F811
279
+ to_ordered_dict = curry(to_ordered_dict)
280
+ to_wei = curry(to_wei)
281
+ to_wei_decimals = curry(to_wei_decimals)
282
+ clamp = curry(clamp)
283
+
284
+ # Delete any methods and classes that are not intended to be importable from
285
+ # `eth_utils.curried`. We do this approach instead of __all__ because this approach
286
+ # actually prevents importing the wrong thing, while __all__ only affects
287
+ # `from eth_utils.curried import *`
288
+ del Any
289
+ del Callable
290
+ del Dict
291
+ del Generator
292
+ del Optional
293
+ del Sequence
294
+ del TReturn
295
+ del TValue
296
+ del Tuple
297
+ del TypeGuard
298
+ del TypeVar
299
+ del Union
300
+ del curry
301
+ del non_curried_apply_formatter_if
302
+ del non_curried_apply_one_of_formatters
303
+ del non_curried_apply_formatters_to_dict
304
+ del non_curried_hexstr_if_str
305
+ del non_curried_text_if_str
306
+ del overload
Binary file
@@ -0,0 +1,20 @@
1
+ import platform
2
+ import subprocess
3
+ import sys
4
+
5
+
6
+ def pip_freeze() -> str:
7
+ result = subprocess.run("python -m pip freeze".split(), stdout=subprocess.PIPE)
8
+ return f"python -m pip freeze result:\n{result.stdout.decode()}"
9
+
10
+
11
+ def python_version() -> str:
12
+ return f"Python version:\n{sys.version}"
13
+
14
+
15
+ def platform_info() -> str:
16
+ return f"Operating System: {platform.platform()}"
17
+
18
+
19
+ def get_environment_summary() -> str:
20
+ return "\n\n".join([python_version(), platform_info(), pip_freeze()])
@@ -0,0 +1,119 @@
1
+ import functools
2
+ from typing import (
3
+ Any,
4
+ Callable,
5
+ Dict,
6
+ Final,
7
+ Generic,
8
+ Optional,
9
+ Tuple,
10
+ Type,
11
+ TypeVar,
12
+ Union,
13
+ final,
14
+ )
15
+
16
+ from typing_extensions import Concatenate, ParamSpec
17
+
18
+ P = ParamSpec("P")
19
+
20
+ T = TypeVar("T")
21
+
22
+ TInstance = TypeVar("TInstance", bound=object)
23
+ """A TypeVar representing an instance that a method can bind to."""
24
+
25
+
26
+ @final
27
+ class combomethod(Generic[TInstance, P, T]):
28
+ def __init__(
29
+ self, method: Callable[Concatenate[Union[TInstance, Type[TInstance]], P], T]
30
+ ) -> None:
31
+ self.method: Final = method
32
+
33
+ def __repr__(self) -> str:
34
+ return f"combomethod({self.method})"
35
+
36
+ def __get__(
37
+ self,
38
+ obj: Optional[TInstance],
39
+ objtype: Type[TInstance],
40
+ ) -> Callable[P, T]:
41
+
42
+ @functools.wraps(self.method)
43
+ def _wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
44
+ if obj is not None:
45
+ return self.method(obj, *args, **kwargs)
46
+ else:
47
+ return self.method(objtype, *args, **kwargs) # type: ignore [arg-type]
48
+
49
+ return _wrapper
50
+
51
+
52
+ _return_arg_type_deco_cache: Final[
53
+ Dict[int, Callable[[Callable[P, T]], Callable[P, Any]]]
54
+ ] = {}
55
+ # No need to hold so many unique instances in memory
56
+
57
+
58
+ def return_arg_type(at_position: int) -> Callable[[Callable[P, T]], Callable[P, Any]]:
59
+ """
60
+ Wrap the return value with the result of `type(args[at_position])`.
61
+ """
62
+ if deco := _return_arg_type_deco_cache.get(at_position):
63
+ return deco
64
+
65
+ def decorator(to_wrap: Callable[P, Any]) -> Callable[P, Any]:
66
+ @functools.wraps(to_wrap)
67
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> Any:
68
+ result = to_wrap(*args, **kwargs)
69
+ ReturnType = type(args[at_position])
70
+ return ReturnType(result) # type: ignore [call-arg]
71
+
72
+ return wrapper
73
+
74
+ _return_arg_type_deco_cache[at_position] = decorator
75
+
76
+ return decorator
77
+
78
+
79
+ ExcType = Type[BaseException]
80
+
81
+ ReplaceExceptionsCache = Dict[
82
+ Tuple[Tuple[ExcType, ExcType], ...],
83
+ Callable[[Callable[P, T]], Callable[P, T]],
84
+ ]
85
+
86
+ _replace_exceptions_deco_cache: Final[ReplaceExceptionsCache[..., Any]] = {}
87
+ # No need to hold so many unique instances in memory
88
+
89
+
90
+ def replace_exceptions(
91
+ old_to_new_exceptions: Dict[ExcType, ExcType],
92
+ ) -> Callable[[Callable[P, T]], Callable[P, T]]:
93
+ """
94
+ Replaces old exceptions with new exceptions to be raised in their place.
95
+ """
96
+ cache_key = tuple(old_to_new_exceptions.items())
97
+ if deco := _replace_exceptions_deco_cache.get(cache_key):
98
+ return deco
99
+
100
+ old_exceptions = tuple(old_to_new_exceptions)
101
+
102
+ def decorator(to_wrap: Callable[P, T]) -> Callable[P, T]:
103
+ @functools.wraps(to_wrap)
104
+ def wrapped(*args: P.args, **kwargs: P.kwargs) -> T:
105
+ try:
106
+ return to_wrap(*args, **kwargs)
107
+ except old_exceptions as err:
108
+ try:
109
+ raise old_to_new_exceptions[type(err)](err) from err
110
+ except KeyError:
111
+ raise TypeError(
112
+ f"could not look up new exception to use for {repr(err)}"
113
+ ) from err
114
+
115
+ return wrapped
116
+
117
+ _replace_exceptions_deco_cache[cache_key] = decorator
118
+
119
+ return decorator
@@ -0,0 +1,6 @@
1
+ def int_to_big_endian(value: int) -> bytes:
2
+ return value.to_bytes((value.bit_length() + 7) // 8 or 1, "big")
3
+
4
+
5
+ def big_endian_to_int(value: bytes) -> int:
6
+ return int.from_bytes(value, "big")
@@ -0,0 +1,11 @@
1
+ """
2
+ faster-eth-utils exceptions always inherit from eth-utils exceptions, so porting to faster-eth-utils
3
+ does not require any change to your existing exception handlers. They will continue to work.
4
+ """
5
+
6
+ import eth_utils.exceptions
7
+
8
+ class ValidationError(eth_utils.exceptions.ValidationError):
9
+ """
10
+ Raised when something does not pass a validation check.
11
+ """
@@ -0,0 +1,89 @@
1
+ import collections
2
+ import functools
3
+ import itertools
4
+ from typing import ( # noqa: F401
5
+ Callable,
6
+ Dict,
7
+ Iterable,
8
+ List,
9
+ Mapping,
10
+ Set,
11
+ Tuple,
12
+ TypeVar,
13
+ Union,
14
+ )
15
+
16
+ from typing_extensions import ParamSpec
17
+
18
+ from .toolz import (
19
+ compose as _compose,
20
+ )
21
+
22
+ P = ParamSpec("P")
23
+ T = TypeVar("T")
24
+
25
+
26
+ def identity(value: T) -> T:
27
+ return value
28
+
29
+
30
+ TGIn = TypeVar("TGIn")
31
+ TGOut = TypeVar("TGOut")
32
+ TFOut = TypeVar("TFOut")
33
+
34
+
35
+ def combine(
36
+ f: Callable[[TGOut], TFOut], g: Callable[[TGIn], TGOut]
37
+ ) -> Callable[[TGIn], TFOut]:
38
+ def combined(x: TGIn) -> TFOut:
39
+ return f(g(x))
40
+ return combined
41
+
42
+
43
+ TCb = TypeVar("TCb")
44
+
45
+
46
+ def apply_to_return_value(
47
+ callback: Callable[[T], TCb]
48
+ ) -> Callable[[Callable[P, T]], Callable[P, TCb]]:
49
+ def outer(fn: Callable[P, T]) -> Callable[P, TCb]:
50
+ @functools.wraps(fn)
51
+ def inner(*args: P.args, **kwargs: P.kwargs) -> TCb:
52
+ return callback(fn(*args, **kwargs))
53
+ return inner
54
+ return outer
55
+
56
+
57
+ TVal = TypeVar("TVal")
58
+ TKey = TypeVar("TKey")
59
+
60
+ def to_tuple(fn: Callable[P, Iterable[TVal]]) -> Callable[P, Tuple[TVal, ...]]:
61
+ def to_tuple_wrap(*args: P.args, **kwargs: P.kwargs) -> Tuple[TVal, ...]:
62
+ return tuple(fn(*args, **kwargs))
63
+ return to_tuple_wrap
64
+
65
+ def to_list(fn: Callable[P, Iterable[TVal]]) -> Callable[P, List[TVal]]:
66
+ def to_list_wrap(*args: P.args, **kwargs: P.kwargs) -> List[TVal]:
67
+ return list(fn(*args, **kwargs))
68
+ return to_list_wrap
69
+
70
+ def to_set(fn: Callable[P, Iterable[TVal]]) -> Callable[P, Set[TVal]]:
71
+ def to_set_wrap(*args: P.args, **kwargs: P.kwargs) -> Set[TVal]:
72
+ return set(fn(*args, **kwargs))
73
+ return to_set_wrap
74
+
75
+ def to_dict(
76
+ fn: Callable[P, Union[Mapping[TKey, TVal], Iterable[Tuple[TKey, TVal]]]]
77
+ ) -> Callable[P, Dict[TKey, TVal]]:
78
+ def to_dict_wrap(*args: P.args, **kwargs: P.kwargs) -> Dict[TKey, TVal]:
79
+ return dict(fn(*args, **kwargs))
80
+ return to_dict_wrap
81
+
82
+ to_ordered_dict = apply_to_return_value( # type: ignore [assignment]
83
+ collections.OrderedDict
84
+ ) # type: Callable[[Callable[P, Union[Mapping[TKey, TVal], Iterable[Tuple[TKey, TVal]]]]], Callable[P, collections.OrderedDict[TKey, TVal]]] # noqa: E501
85
+ sort_return = _compose(to_tuple, apply_to_return_value(sorted))
86
+ flatten_return = _compose(
87
+ to_tuple, apply_to_return_value(itertools.chain.from_iterable)
88
+ )
89
+ reversed_return = _compose(to_tuple, apply_to_return_value(reversed), to_tuple)