Skip to content

Commit

Permalink
Replace formatters with eth_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjw committed Jan 16, 2019
1 parent 6132c30 commit 5f60251
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 114 deletions.
10 changes: 0 additions & 10 deletions tests/core/utilities/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from web3._utils.formatters import (
apply_formatters_to_dict,
map_collection,
recursive_map,
)
Expand Down Expand Up @@ -40,12 +39,3 @@ def test_recursive_collection_cycle():
data.append(data)
with pytest.raises(ValueError):
recursive_map(square_int, data)


def test_format_dict_error():
with pytest.raises(ValueError) as exc_info:
apply_formatters_to_dict(
{'myfield': int},
{'myfield': 'a'},
)
assert 'myfield' in str(exc_info.value)
2 changes: 1 addition & 1 deletion tests/ens/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def ens_setup():
# ** Set up ENS contracts **

# remove account that creates ENS, so test transactions don't have write access
accounts = w3.eth.accounts
accounts = list(w3.eth.accounts)
ens_key = accounts.pop()

# create ENS contract
Expand Down
7 changes: 5 additions & 2 deletions web3/_utils/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import web3._utils.formatters
from eth_utils import (
apply_formatters_to_dict,
)

from web3._utils.toolz import (
concat,
)
Expand Down Expand Up @@ -27,7 +30,7 @@ def __new__(mcs, name, bases, namespace, normalizers=None):
verify_attr(name, key, all_keys)

if normalizers:
processed_namespace = web3._utils.formatters.apply_formatters_to_dict(
processed_namespace = apply_formatters_to_dict(
normalizers,
namespace,
)
Expand Down
6 changes: 3 additions & 3 deletions web3/_utils/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
to_hex,
to_tuple,
)
from eth_utils.curried import (
apply_formatter_if,
)

import web3
from web3._utils.encoding import (
encode_single_packed,
hexstr_if_str,
to_bytes,
)
from web3._utils.formatters import (
apply_formatter_if,
)
from web3._utils.normalizers import (
BASE_RETURN_NORMALIZERS,
)
Expand Down
6 changes: 3 additions & 3 deletions web3/_utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
is_string,
is_text,
)
from eth_utils.curried import (
apply_formatter_if,
)
from hexbytes import (
HexBytes,
)

from web3._utils.formatters import (
apply_formatter_if,
)
from web3._utils.threads import (
TimerClass,
)
Expand Down
67 changes: 3 additions & 64 deletions web3/_utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
)

from eth_utils import (
is_dict,
is_list_like,
is_string,
to_dict,
to_list,
)
from eth_utils.curried import (
apply_formatter_at_index,
)

from web3._utils.decorators import (
Expand All @@ -28,21 +29,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,43 +37,6 @@ def apply_formatters_to_args(*formatters):
))


@curry
def apply_formatter_if(condition, formatter, value):
if condition(value):
return formatter(value)
else:
return value


@curry
@to_dict
def apply_formatters_to_dict(formatters, value):
for key, item in value.items():
if key in formatters:
try:
yield key, formatters[key](item)
except (TypeError, ValueError) as exc:
raise type(exc)("Could not format value %r as field %r" % (item, key)) from exc
else:
yield key, item


@curry
@to_list
def apply_formatter_to_array(formatter, value):
for item in value:
yield formatter(item)


@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 Expand Up @@ -128,16 +77,6 @@ def inner(*args, **kwargs):
return inner


@curry
@to_dict
def apply_key_map(key_mappings, value):
for key, item in value.items():
if key in key_mappings:
yield key_mappings[key], item
else:
yield key, item


def is_array_of_strings(value):
if not is_list_like(value):
return False
Expand Down
27 changes: 14 additions & 13 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import operator

from eth_utils.curried import (
apply_formatter_at_index,
apply_formatter_if,
apply_formatter_to_array,
apply_formatters_to_dict,
apply_one_of_formatters,
combine_argument_formatters,
is_address,
is_bytes,
Expand Down Expand Up @@ -32,11 +37,6 @@
to_hex,
)
from web3._utils.formatters import (
apply_formatter_at_index,
apply_formatter_if,
apply_formatter_to_array,
apply_formatters_to_dict,
apply_one_of_formatters,
hex_to_integer,
integer_to_hex,
is_array_of_dicts,
Expand Down Expand Up @@ -190,10 +190,11 @@ def is_attrdict(val):
'receiptsRoot': to_hexbytes(32),
'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),
)),
'transactions': apply_one_of_formatters(
(
(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 @@ -251,8 +252,8 @@ def is_attrdict(val):


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))),
))

TRANSACTION_PARAM_FORMATTERS = {
Expand Down Expand Up @@ -300,8 +301,8 @@ def is_attrdict(val):
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
6 changes: 3 additions & 3 deletions web3/_utils/rpc_abi.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from eth_utils import (
to_dict,
)
from eth_utils.curried import (
apply_formatter_at_index,
)

from web3._utils.abi import (
map_abi_data,
)
from web3._utils.formatters import (
apply_formatter_at_index,
)
from web3._utils.toolz import (
curry,
)
Expand Down
6 changes: 3 additions & 3 deletions web3/_utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
is_list_like,
is_string,
)
from eth_utils.curried import (
apply_formatter_to_array,
)
from eth_utils.hexadecimal import (
encode_hex,
)
Expand All @@ -31,9 +34,6 @@
length_of_array_type,
sub_type_of_array_type,
)
from web3._utils.formatters import (
apply_formatter_to_array,
)
from web3._utils.toolz import (
compose,
groupby,
Expand Down
4 changes: 2 additions & 2 deletions web3/middleware/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from eth_utils import (
to_dict,
)

from web3._utils.formatters import (
from eth_utils.curried import (
apply_formatter_if,
)

from web3._utils.method_formatters import (
STANDARD_NORMALIZERS,
)
Expand Down
11 changes: 4 additions & 7 deletions web3/providers/eth_tester/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
is_null,
keccak,
)
from eth_utils.curried import (
apply_formatter_if,
)

from web3._utils.formatters import (
apply_formatter_if,
static_return,
)
from web3._utils.toolz import (
compose,
Expand Down Expand Up @@ -57,12 +60,6 @@ def preprocess_params(eth_tester, params, preprocessor_fn):
return eth_tester, preprocessor_fn(params)


def static_return(value):
def inner(*args, **kwargs):
return value
return inner


def client_version(eth_tester, params):
# TODO: account for the backend that is in use.
from eth_tester import __version__
Expand Down
8 changes: 5 additions & 3 deletions web3/providers/eth_tester/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
is_hex,
is_string,
)

from web3._utils.formatters import (
from eth_utils.curried import (
apply_formatter_if,
apply_formatter_to_array,
apply_formatters_to_args,
apply_formatters_to_dict,
apply_key_map,
)

from web3._utils.formatters import (
apply_formatters_to_args,
hex_to_integer,
integer_to_hex,
is_array_of_dicts,
Expand Down

0 comments on commit 5f60251

Please sign in to comment.