Skip to content

Commit 129c183

Browse files
committed
feat: optimize utility_methods.py
This was originally part of my other PR but that is becoming quite cluttered and hard to review so I separated this one out. Previously, the code would create a new dict from `d` one time for each item in the iterable. Now, the code creates just one new dict from `d` at the beginning and uses that same dict to check membership for each item.
1 parent e22506e commit 129c183

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

web3/_utils/utility_methods.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import (
22
Any,
3-
Dict,
43
Iterable,
4+
Mapping,
55
Set,
66
Union,
77
)
@@ -13,7 +13,7 @@
1313

1414

1515
def all_in_dict(
16-
values: Iterable[Any], d: Union[Dict[Any, Any], TxData, TxParams]
16+
values: Iterable[Any], d: Union[Mapping[Any, Any], TxData, TxParams]
1717
) -> bool:
1818
"""
1919
Returns a bool based on whether ALL of the provided values exist
@@ -24,11 +24,12 @@ def all_in_dict(
2424
:return: True if ALL values exist in keys;
2525
False if NOT ALL values exist in keys
2626
"""
27-
return all(_ in dict(d) for _ in values)
27+
d = dict(d)
28+
return all(_ in d for _ in values)
2829

2930

3031
def any_in_dict(
31-
values: Iterable[Any], d: Union[Dict[Any, Any], TxData, TxParams]
32+
values: Iterable[Any], d: Union[Mapping[Any, Any], TxData, TxParams]
3233
) -> bool:
3334
"""
3435
Returns a bool based on whether ANY of the provided values exist
@@ -39,11 +40,12 @@ def any_in_dict(
3940
:return: True if ANY value exists in keys;
4041
False if NONE of the values exist in keys
4142
"""
42-
return any(_ in dict(d) for _ in values)
43+
d = dict(d)
44+
return any(_ in d for _ in values)
4345

4446

4547
def none_in_dict(
46-
values: Iterable[Any], d: Union[Dict[Any, Any], TxData, TxParams]
48+
values: Iterable[Any], d: Union[Mapping[Any, Any], TxData, TxParams]
4749
) -> bool:
4850
"""
4951
Returns a bool based on whether NONE of the provided values exist

0 commit comments

Comments
 (0)