Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data filter falsey #52499

Merged
merged 8 commits into from
Apr 24, 2019
Merged

Conversation

github-abcde
Copy link
Contributor

@github-abcde github-abcde commented Apr 11, 2019

What does this PR do?

This adds a function filter_falsey to salt.utils.data. The function can be used to filter values from a list or dict that evaluate to false (but are not bool(False)). Optionally, a level of recursion can be set to process nested dicts/lists. Also optionally, a list of types to not filter can be passed in ignore_types.

What issues does this PR fix or reference?

None that I know of.

Previous Behavior

The functionality does not exist.

New Behavior

We are now able to filter falsey values from dicts or lists.
This is especially handy when having to process a lot of kwargs that are explicitly specified in the function argument list, which default to None, but need not be passed on if they are None.
For example:

def some_function(foo=None, bar=None, baz=None):
    new_kwargs = {}
    if foo is not None:
        new_kwargs.update('Foo': foo)
    if bar is not None:
        new_kwargs.update('Bar': bar)
    if baz is not None:
        new_kwargs.update('Baz': baz)
    some_other_function(**new_kwargs)

will become

def some_function(foo=None, bar=None, baz=None):
    new_kwargs = salt.utils.data.filter_falsey({
        'Foo': foo,
        'Bar': bar,
        'Baz': baz,
    })
    some_other_function(**new_kwargs)

Which is a typical use-case for most Boto calls.

Tests written?

Yes

Commits signed with GPG?

Yes

salt/utils/data.py Show resolved Hide resolved
@github-abcde
Copy link
Contributor Author

@twangboy I've implemented the changes you requested. Is there anything else I can do for this PR?

@twangboy twangboy merged commit f11b41f into saltstack:develop Apr 24, 2019
@twangboy
Copy link
Contributor

@github-abcde Looks good. Thanks!

@github-abcde github-abcde deleted the data_filter_falsey branch April 25, 2019 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants