forked from iexbase/tron-api-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformatters.py
More file actions
60 lines (49 loc) · 1.74 KB
/
Copy pathformatters.py
File metadata and controls
60 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# --------------------------------------------------------------------
# Copyright (c) iEXBase. All rights reserved.
# Licensed under the MIT License.
# See License.txt in the project root for license information.
# --------------------------------------------------------------------
from collections.abc import Mapping, Iterable
from eth_utils import to_dict
from trx_utils import (
reject_recursive_repeats,
is_string
)
from tronapi.common.toolz import (
curry
)
@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
@reject_recursive_repeats
def recursive_map(func, data):
"""
Apply func to data, and any collection items inside data (using map_collection).
Define func so that it only applies to the type of value that you want it to apply to.
"""
def recurse(item):
return recursive_map(func, item)
items_mapped = map_collection(recurse, data)
return func(items_mapped)
def map_collection(func, collection):
"""
Apply func to each element of a collection, or value of a dictionary.
If the value is not a collection, return it unmodified
"""
datatype = type(collection)
if isinstance(collection, Mapping):
return datatype((key, func(val)) for key, val in collection.items())
if is_string(collection):
return collection
elif isinstance(collection, Iterable):
return datatype(map(func, collection))
else:
return collection