Skip to content

feat: optimize utility_methods.py #3667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions newsfragments/3667.performance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Optimize performance for:
web3._utils.utility_methods.all_in_dict
web3._utils.utility_methods.any_in_dict
web3._utils.utility_methods.none_in_dict
14 changes: 8 additions & 6 deletions web3/_utils/utility_methods.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import (
Any,
Dict,
Iterable,
Mapping,
Set,
Union,
)
Expand All @@ -13,7 +13,7 @@


def all_in_dict(
values: Iterable[Any], d: Union[Dict[Any, Any], TxData, TxParams]
values: Iterable[Any], d: Union[Mapping[Any, Any], TxData, TxParams]
) -> bool:
"""
Returns a bool based on whether ALL of the provided values exist
Expand All @@ -24,11 +24,12 @@ def all_in_dict(
:return: True if ALL values exist in keys;
False if NOT ALL values exist in keys
"""
return all(_ in dict(d) for _ in values)
d = dict(d)
return all(_ in d for _ in values)


def any_in_dict(
values: Iterable[Any], d: Union[Dict[Any, Any], TxData, TxParams]
values: Iterable[Any], d: Union[Mapping[Any, Any], TxData, TxParams]
) -> bool:
"""
Returns a bool based on whether ANY of the provided values exist
Expand All @@ -39,11 +40,12 @@ def any_in_dict(
:return: True if ANY value exists in keys;
False if NONE of the values exist in keys
"""
return any(_ in dict(d) for _ in values)
d = dict(d)
return any(_ in d for _ in values)


def none_in_dict(
values: Iterable[Any], d: Union[Dict[Any, Any], TxData, TxParams]
values: Iterable[Any], d: Union[Mapping[Any, Any], TxData, TxParams]
) -> bool:
"""
Returns a bool based on whether NONE of the provided values exist
Expand Down