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

Data filter falsey #52499

merged 8 commits into from Apr 24, 2019

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

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

@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
10 of 11 checks passed
@twangboy
Copy link
Contributor

@twangboy twangboy commented Apr 24, 2019

@github-abcde Looks good. Thanks!

@github-abcde github-abcde deleted the data_filter_falsey branch Apr 25, 2019
@waynew waynew added this to PR needs port to master in PRs to port to master Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
PRs to port to master
  
PR needs port to master
Development

Successfully merging this pull request may close these issues.

None yet

3 participants