dstandish commented on a change in pull request #20573:
URL: https://github.com/apache/airflow/pull/20573#discussion_r776778730



##########
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:
       yeah the reason i went this way is cus the mode of `is_empty` will never 
change within a call of prune_dict.  and so i thought it would be an 
optimization to define once and not have to evaluate the mode in every call to 
is_empty
   
   BUT, i realize now that in each recursion step we have to define again 
anyway.  we could make it so that we only define once  _and_ we never have to 
reevaluate by moving the actual work to another inner function, BUT, this 
results in essentially no performance gain anyway :) 




-- 
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]


Reply via email to