kaxil commented on a change in pull request #20573:
URL: https://github.com/apache/airflow/pull/20573#discussion_r776730075
##########
File path: airflow/utils/helpers.py
##########
@@ -310,3 +310,53 @@ def exactly_one(*args) -> bool:
"Not supported for iterable args. Use `*` to unpack your iterable
in the function call."
)
return sum(map(bool, args)) == 1
+
+
+def prune_dict(val: Any, mode='strict'):
+ """
+ Given dict ``val``, returns new dict based on ``val`` with all
+ empty elements removed.
+
+ What constitutes "empty" is controlled by the ``mode`` parameter. If mode
is 'strict'
+ then only ``None`` elements will be removed. If mode is ``truthy``, then
element ``x``
+ will be removed if ``bool(x) is False``.
+ """
+ if mode == 'truthy':
+
+ def is_empty(x):
+ return bool(x) is False
+
+ elif mode == 'strict':
+
+ def is_empty(x):
+ return x is None
+
+ else:
+ raise ValueError("allowable values for `mode` include 'truthy' and
'strict'")
+
Review comment:
or the following and pass `mode` explicitly. Either way should be fine
as the function definition is local to `prune_dict`:
```suggestion
def is_empty(x, mode='truthy'):
if mode == 'truthy':
return bool(x) is False
if mode == 'strict':
return x is None
raise ValueError("allowable values for `mode` include 'truthy' and
'strict'")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]