Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81ec713
commit 773bd53
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""Filters for working with data structures, munging, etc...""" | ||
|
||
|
||
def filter_vals(obj, vals): | ||
"""Filter a dictionary by values. | ||
Args: | ||
string (dict): The dictionary to filter. | ||
Returns: | ||
string (dict): The filtered dict. | ||
""" | ||
if obj is None or not isinstance(vals, list): | ||
return obj | ||
newdict = {} | ||
for k, v in obj.items(): | ||
if v in vals: | ||
continue | ||
newdict[k] = v | ||
return newdict | ||
|
||
|
||
def filter_keys(obj, keys): | ||
"""Filter a dictionary by keys. | ||
Args: | ||
string (dict): The dictionary to filter. | ||
Returns: | ||
string (dict): The filtered dict. | ||
""" | ||
if obj is None or not isinstance(keys, list): | ||
return obj | ||
newdict = {} | ||
for k, v in obj.items(): | ||
if k in keys: | ||
continue | ||
newdict[k] = v | ||
return newdict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Test munging filters.""" | ||
|
||
from flask_extras.filters import munging | ||
|
||
|
||
class TestFilterVals: | ||
"""All tests for filter_vals function.""" | ||
|
||
def test_title_returns_invalid_first(self): | ||
"""Test function.""" | ||
assert munging.filter_vals({}, None) == {} | ||
|
||
def test_title_returns_invalid_second(self): | ||
"""Test function.""" | ||
assert munging.filter_vals(None, []) is None | ||
|
||
def test_title_returns_invalid_both(self): | ||
"""Test function.""" | ||
assert munging.filter_vals(None, None) is None | ||
|
||
def test_title_returns_valid_empty(self): | ||
"""Test function.""" | ||
assert munging.filter_vals(dict(), []) == {} | ||
|
||
def test_title_returns_valid_filtered_empty(self): | ||
"""Test function.""" | ||
assert munging.filter_vals(dict(foo='bar'), ['bar']) == {} | ||
|
||
def test_title_returns_valid_filtered(self): | ||
"""Test function.""" | ||
assert munging.filter_vals( | ||
dict(foo='bar', bar='foo'), ['bar']) == dict(bar='foo') | ||
|
||
def test_title_returns_valid_filtered_invalid_val(self): | ||
"""Test function.""" | ||
d = dict(foo='bar', bar='foo') | ||
assert munging.filter_vals(d, ['baz']) == d | ||
|
||
|
||
class TestFilterKeys: | ||
"""All tests for filter_keys function.""" | ||
|
||
def test_title_returns_invalid_first(self): | ||
"""Test function.""" | ||
assert munging.filter_keys({}, None) == {} | ||
|
||
def test_title_returns_invalid_second(self): | ||
"""Test function.""" | ||
assert munging.filter_keys(None, []) is None | ||
|
||
def test_title_returns_invalid_both(self): | ||
"""Test function.""" | ||
assert munging.filter_keys(None, None) is None | ||
|
||
def test_title_returns_valid_empty(self): | ||
"""Test function.""" | ||
assert munging.filter_keys(dict(), []) == {} | ||
|
||
def test_title_returns_valid_filtered_empty(self): | ||
"""Test function.""" | ||
assert munging.filter_keys(dict(foo='bar'), ['foo']) == {} | ||
|
||
def test_title_returns_valid_filtered(self): | ||
"""Test function.""" | ||
assert munging.filter_keys( | ||
dict(foo='bar', bar='foo'), ['bar']) == dict(foo='bar') | ||
|
||
def test_title_returns_valid_filtered_invalid_val(self): | ||
"""Test function.""" | ||
d = dict(foo='bar', bar='foo') | ||
assert munging.filter_keys(d, ['baz']) == d |