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



##########
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:
       Instead of multiple function definitions can we do the following 
instead, WDYT? 
   
   ```suggestion
   def is_empty(x):
       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]


Reply via email to