Skip to content
Open
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
15 changes: 8 additions & 7 deletions eth_utils/applicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
def apply_formatter_at_index(
formatter: Callable[..., Any], at_index: int, value: List[Any]
) -> Generator[List[Any], None, None]:
if at_index + 1 > len(value):
try:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can do this check optimistically since we expect it to succeed most often

item = value[at_index]
except IndexError:
raise IndexError(
f"Not enough values in iterable to apply formatter. Got: {len(value)}. "
f"Need: {at_index + 1}"
)
for index, item in enumerate(value):
if index == at_index:
yield formatter(item)
else:
yield item
) from None

yield from value[:at_index]
yield formatter(item)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then we don't need to perform any additional checks

yield from value[at_index + 1 :]


def combine_argument_formatters(*formatters: List[Callable[..., Any]]) -> Formatters:
Expand Down
1 change: 1 addition & 0 deletions newsfragments/306.performance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
optimize apply_formatter_at_index