Skip to content

Commit

Permalink
Use formatting methods from eth-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed Nov 13, 2019
1 parent 04ee397 commit cea24ed
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 70 deletions.
1 change: 1 addition & 0 deletions newsfragments/1479.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use formatting functions from eth-utils instead of web3
5 changes: 4 additions & 1 deletion tests/core/utilities/test_formatters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

import pytest

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

from web3._utils.formatters import (
map_collection,
recursive_map,
)
Expand Down
7 changes: 4 additions & 3 deletions web3/_utils/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from eth_utils import (
apply_formatters_to_dict,
)
from eth_utils.toolz import (
concat,
)

import web3._utils.formatters


def verify_attr(class_name, key, namespace):
if key not in namespace:
Expand All @@ -28,7 +29,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 @@ -20,6 +20,9 @@
to_hex,
to_tuple,
)
from eth_utils.curried import (
apply_formatter_if,
)
from eth_utils.toolz import (
complement,
compose,
Expand All @@ -34,9 +37,6 @@
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 @@ -6,6 +6,9 @@
is_string,
is_text,
)
from eth_utils.curried import (
apply_formatter_if,
)
from eth_utils.toolz import (
complement,
curry,
Expand All @@ -14,9 +17,6 @@
HexBytes,
)

from web3._utils.formatters import (
apply_formatter_if,
)
from web3._utils.threads import (
TimerClass,
)
Expand Down
48 changes: 3 additions & 45 deletions web3/_utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
to_dict,
to_list,
)
from eth_utils.curried import (
apply_formatter_at_index,
)
from eth_utils.toolz import (
compose,
curry,
Expand All @@ -28,21 +31,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 +39,13 @@ 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
6 changes: 3 additions & 3 deletions web3/_utils/rpc_abi.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from eth_utils import (
to_dict,
)
from eth_utils.curried import (
apply_formatter_at_index,
)
from eth_utils.toolz import (
curry,
)

from web3._utils.abi import (
map_abi_data,
)
from web3._utils.formatters import (
apply_formatter_at_index,
)

TRANSACTION_PARAMS_ABIS = {
'data': 'bytes',
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 @@ -37,9 +40,6 @@
length_of_array_type,
sub_type_of_array_type,
)
from web3._utils.formatters import (
apply_formatter_to_array,
)
from web3.exceptions import (
InvalidAddress,
)
Expand Down
6 changes: 3 additions & 3 deletions web3/middleware/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from eth_utils import (
to_dict,
)
from eth_utils.curried import (
apply_formatter_if,
)
from eth_utils.toolz import (
compose,
)

from web3._utils.formatters import (
apply_formatter_if,
)
from web3._utils.method_formatters import (
STANDARD_NORMALIZERS,
)
Expand Down
7 changes: 3 additions & 4 deletions web3/providers/eth_tester/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
is_null,
keccak,
)
from eth_utils.curried import (
apply_formatter_if,
)
from eth_utils.toolz import (
compose,
curry,
excepts,
)

from web3._utils.formatters import (
apply_formatter_if,
)


def not_implemented(*args, **kwargs):
raise NotImplementedError("RPC method not implemented")
Expand Down
6 changes: 4 additions & 2 deletions web3/providers/eth_tester/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
is_hex,
is_string,
)
from eth_utils.curried import (
apply_formatter_if,
apply_formatters_to_dict,
)
from eth_utils.toolz import (
assoc,
complement,
Expand All @@ -16,10 +20,8 @@
)

from web3._utils.formatters import (
apply_formatter_if,
apply_formatter_to_array,
apply_formatters_to_args,
apply_formatters_to_dict,
apply_key_map,
hex_to_integer,
integer_to_hex,
Expand Down

0 comments on commit cea24ed

Please sign in to comment.