@@ -19,10 +19,10 @@ def wrap_val(v):
19
19
def _is_dunder (name ):
20
20
"""Returns True if a __dunder__ name, False otherwise."""
21
21
return (
22
- len (name ) > 4
23
- and name [:2 ] == name [- 2 :] == "__"
24
- and name [2 ] != "_"
25
- and name [- 3 ] != "_"
22
+ len (name ) > 4
23
+ and name [:2 ] == name [- 2 :] == "__"
24
+ and name [2 ] != "_"
25
+ and name [- 3 ] != "_"
26
26
)
27
27
28
28
@@ -37,21 +37,20 @@ def raise_errs_if_needed(errors):
37
37
raise errors [0 ].__class__ (messages ) from errors [0 ]
38
38
39
39
40
-
41
40
def first_in (my_list : Iterable , ignore_none : bool = False ) -> Optional :
42
41
"""
43
- get the first in an Iterable (i.e. list, tuple, generator, dict, etc.).
44
- Optionally ignoring None values.
42
+ get the first in an Iterable (i.e. list, tuple, generator, dict, etc.).
43
+ Optionally ignoring None values.
45
44
46
- Arguments:
47
- my_list(Iterable):
48
- The input iterable, such as a list
49
- ignore_none(bool): optional
50
- whether or not to ignore None values. Default is False
45
+ Arguments:
46
+ my_list(Iterable):
47
+ The input iterable, such as a list
48
+ ignore_none(bool): optional
49
+ whether or not to ignore None values. Default is False
51
50
52
51
53
- Returns:
54
- The first value found in the input, or None if none found
52
+ Returns:
53
+ The first value found in the input, or None if none found
55
54
"""
56
55
if ignore_none :
57
56
return next (filter (None , iter (my_list )), None )
@@ -60,28 +59,28 @@ def first_in(my_list: Iterable, ignore_none: bool = False) -> Optional:
60
59
61
60
def nested (func , default = None ):
62
61
"""
63
- get a nested value if it exists, or return the given default if it doesn't
62
+ get a nested value if it exists, or return the given default if it doesn't
64
63
65
- Arguments:
66
- func(function):
67
- A function with no arguments that returns the nested value
68
- default:
69
- the default value, in case the nested value does not exist
64
+ Arguments:
65
+ func(function):
66
+ A function with no arguments that returns the nested value
67
+ default:
68
+ the default value, in case the nested value does not exist
70
69
71
70
72
- Returns:
73
- the nested attribute(s) or the default value. For example:
71
+ Returns:
72
+ the nested attribute(s) or the default value. For example:
74
73
75
- For example:
74
+ For example:
76
75
77
- .. code-block:: python
76
+ .. code-block:: python
78
77
79
- d = D(c=C(b=B(a=A(i=5))))
80
- assert nested(lambda: d.c.b.a.i) == 5
78
+ d = D(c=C(b=B(a=A(i=5))))
79
+ assert nested(lambda: d.c.b.a.i) == 5
81
80
82
- d.c.foo = [1, 2, 3]
83
- assert nested(lambda: d.c.foo[100]) is None
84
- assert nested(lambda: d.c.foo[2]) == 3
81
+ d.c.foo = [1, 2, 3]
82
+ assert nested(lambda: d.c.foo[100]) is None
83
+ assert nested(lambda: d.c.foo[2]) == 3
85
84
"""
86
85
try :
87
86
return func ()
@@ -91,27 +90,27 @@ def nested(func, default=None):
91
90
92
91
def flatten (iterable , ignore_none = False ) -> list :
93
92
"""
94
- Flatten an iterable completely, getting rid of None values
93
+ Flatten an iterable completely, getting rid of None values
95
94
96
- Arguments:
97
- iterable(Iterable):
98
- the input
99
- ignore_none(bool): optional
100
- whether to skip None values or not. Default is False.
95
+ Arguments:
96
+ iterable(Iterable):
97
+ the input
98
+ ignore_none(bool): optional
99
+ whether to skip None values or not. Default is False.
101
100
102
- Returns:
103
- A flattened list
101
+ Returns:
102
+ A flattened list
104
103
105
- .. code-block:: python
104
+ .. code-block:: python
106
105
107
- flatten(
108
- [
109
- [[1]],
110
- [[2], 3, None, (5,)],
111
- []
112
- ], ignore_none=True) == [1, 2, 3, 5]
106
+ flatten(
107
+ [
108
+ [[1]],
109
+ [[2], 3, None, (5,)],
110
+ []
111
+ ], ignore_none=True) == [1, 2, 3, 5]
113
112
114
- """
113
+ """
115
114
res = (
116
115
sum ([flatten (x , ignore_none ) for x in iterable ], [])
117
116
if isinstance (iterable , (list , tuple , Generator ))
@@ -124,29 +123,29 @@ def flatten(iterable, ignore_none=False) -> list:
124
123
125
124
def deep_get (dictionary , deep_key , default = None , do_flatten = False ):
126
125
"""
127
- Get a nested value from within a dictionary. Supports also nested lists, in
128
- which case the result is a a list of values
126
+ Get a nested value from within a dictionary. Supports also nested lists, in
127
+ which case the result is a a list of values
129
128
130
- Arguments:
131
- dictionary(dict):
132
- the input
133
- deep_key(str):
134
- nested key of the form aaa.bbb.ccc.ddd
135
- default:
136
- the default value, in case the path does not exist
137
- do_flatten(bool): optional
138
- flatten the outputs, in case the result is multiple outputs
129
+ Arguments:
130
+ dictionary(dict):
131
+ the input
132
+ deep_key(str):
133
+ nested key of the form aaa.bbb.ccc.ddd
134
+ default:
135
+ the default value, in case the path does not exist
136
+ do_flatten(bool): optional
137
+ flatten the outputs, in case the result is multiple outputs
139
138
140
- Returns:
141
- the nested attribute(s) or the default value. For example:
139
+ Returns:
140
+ the nested attribute(s) or the default value. For example:
142
141
143
- For example:
142
+ For example:
144
143
145
- .. code-block:: python
144
+ .. code-block:: python
146
145
147
- example = {"a": {"b": [{"c": [None, {"d": [1]}]}, {"c": [None, {"d": [2]}, {"d": 3}]}, {"c": []}]}}
148
- assert deep_get(example, "a.b.c.d") == [[[1]], [[2], 3], []]
149
- assert deep_get(example, "a.b.c.d", do_flatten=True) == [1, 2, 3]
146
+ example = {"a": {"b": [{"c": [None, {"d": [1]}]}, {"c": [None, {"d": [2]}, {"d": 3}]}, {"c": []}]}}
147
+ assert deep_get(example, "a.b.c.d") == [[[1]], [[2], 3], []]
148
+ assert deep_get(example, "a.b.c.d", do_flatten=True) == [1, 2, 3]
150
149
151
150
"""
152
151
@@ -159,36 +158,40 @@ def _get_next_level(d: Optional[Union[Mapping, Iterable]], key, default):
159
158
return default
160
159
161
160
keys = deep_key .split ("." )
162
- result = reduce (lambda d , key : _get_next_level (d , key , default ) if d else default , keys , dictionary )
161
+ result = reduce (
162
+ lambda d , key : _get_next_level (d , key , default ) if d else default ,
163
+ keys ,
164
+ dictionary ,
165
+ )
163
166
if isinstance (result , (list , tuple , Generator )) and do_flatten :
164
167
result = flatten (result )
165
168
return result
166
169
167
170
168
171
def default_factories (func ):
169
172
"""
170
- A function decorator that allows to have default values that are generators of the actual
171
- default values to be used. This is useful when the default values are mutable, like
172
- dicts or lists
173
+ A function decorator that allows to have default values that are generators of the actual
174
+ default values to be used. This is useful when the default values are mutable, like
175
+ dicts or lists
173
176
174
- Arguments:
175
- iterable(Iterable):
176
- the input
177
- ignore_none(bool): optional
178
- whether to skip None values or not. Default is False.
177
+ Arguments:
178
+ iterable(Iterable):
179
+ the input
180
+ ignore_none(bool): optional
181
+ whether to skip None values or not. Default is False.
179
182
180
- Returns:
181
- A flattened list
183
+ Returns:
184
+ A flattened list
182
185
183
- For example:
186
+ For example:
184
187
185
- .. code-block:: python
188
+ .. code-block:: python
186
189
187
- @default_factories
188
- def func(a, b = 0, c = list, d = dict):
189
- return a, b, c, d
190
+ @default_factories
191
+ def func(a, b = 0, c = list, d = dict):
192
+ return a, b, c, d
190
193
191
- assert func(1) == 1, 0, [] , {}
194
+ assert func(1) == 1, 0, [] , {}
192
195
193
196
"""
194
197
func_signature = signature (func , follow_wrapped = True )
@@ -199,7 +202,11 @@ def decorated(*args, **kwargs):
199
202
200
203
for k , v in func_signature .parameters .items ():
201
204
if k not in bound .arguments :
202
- default = v .default () if callable (v .default ) and v .default != v .empty else v .default
205
+ default = (
206
+ v .default ()
207
+ if callable (v .default ) and v .default != v .empty
208
+ else v .default
209
+ )
203
210
if v .default != v .empty :
204
211
kwargs [k ] = default
205
212
return func (* args , ** kwargs )
0 commit comments