Skip to content

Commit

Permalink
Use eth-utils apply_one_of_formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed Oct 24, 2019
1 parent 84db8dc commit 1546a78
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
28 changes: 3 additions & 25 deletions web3/_utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
is_list_like,
is_string,
to_dict,
to_list,
)
from eth_utils.curried import (
apply_formatter_at_index,
)
from eth_utils.toolz import (
compose,
Expand All @@ -28,21 +30,6 @@ def hex_to_integer(value):
integer_to_hex = hex


@curry
@to_list
def apply_formatter_at_index(formatter, at_index, value):
if at_index + 1 > len(value):
raise IndexError(
"Not enough values in iterable to apply formatter. Got: {0}. "
"Need: {1}".format(len(value), at_index + 1)
)
for index, item in enumerate(value):
if index == at_index:
yield formatter(item)
else:
yield item


def apply_formatters_to_args(*formatters):
return compose(*(
apply_formatter_at_index(formatter, index)
Expand All @@ -51,15 +38,6 @@ def apply_formatters_to_args(*formatters):
))


@curry
def apply_one_of_formatters(formatter_condition_pairs, value):
for formatter, condition in formatter_condition_pairs:
if condition(value):
return formatter(value)
else:
raise ValueError("The provided value did not satisfy any of the formatter conditions")


def map_collection(func, collection):
"""
Apply func to each element of a collection, or value of a dictionary.
Expand Down
17 changes: 7 additions & 10 deletions web3/middleware/pythonic.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import codecs
import operator

from eth_utils import (
apply_one_of_formatters,
)
from eth_utils.curried import (
apply_formatter_at_index,
apply_formatter_if,
apply_formatter_to_array,
apply_formatters_to_dict,
apply_formatters_to_sequence,
apply_one_of_formatters,
is_address,
is_bytes,
is_integer,
Expand Down Expand Up @@ -38,7 +36,6 @@
to_hex,
)
from web3._utils.formatters import (
apply_one_of_formatters,
hex_to_integer,
integer_to_hex,
is_array_of_dicts,
Expand Down Expand Up @@ -182,8 +179,8 @@ def to_hexbytes(num_bytes, val, variable_length=False):
'stateRoot': to_hexbytes(32),
'totalDifficulty': to_integer_if_hex,
'transactions': apply_one_of_formatters((
(apply_formatter_to_array(transaction_formatter), is_array_of_dicts),
(apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings),
(is_array_of_dicts, apply_formatter_to_array(transaction_formatter)),
(is_array_of_strings, apply_formatter_to_array(to_hexbytes(32))),
)),
'transactionsRoot': to_hexbytes(32),
}
Expand Down Expand Up @@ -261,8 +258,8 @@ def to_hexbytes(num_bytes, val, variable_length=False):


filter_result_formatter = apply_one_of_formatters((
(apply_formatter_to_array(log_entry_formatter), is_array_of_dicts),
(apply_formatter_to_array(to_hexbytes(32)), is_array_of_strings),
(is_array_of_dicts, apply_formatter_to_array(log_entry_formatter)),
(is_array_of_strings, apply_formatter_to_array(to_hexbytes(32))),
))


Expand Down Expand Up @@ -308,8 +305,8 @@ def to_hexbytes(num_bytes, val, variable_length=False):
block_number_formatter,
]),
'eth_estimateGas': apply_one_of_formatters((
(estimate_gas_without_block_id, is_length(1)),
(estimate_gas_with_block_id, is_length(2)),
(is_length(1), estimate_gas_without_block_id),
(is_length(2), estimate_gas_with_block_id),
)),
'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0),
# personal
Expand Down

0 comments on commit 1546a78

Please sign in to comment.