Skip to content

Commit 70fb6e8

Browse files
committed
Test and document split_keys()
1 parent e4c210a commit 70fb6e8

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

docs/cheatsheet.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Collections
3131
===================== ==============================================================
3232
Join :func:`merge` :func:`merge_with` :func:`join` :func:`join_with`
3333
Transform :func:`walk` :func:`walk_keys` :func:`walk_values`
34-
Filter :func:`select` :func:`select_keys` :func:`select_values` :func:`compact`
34+
Filter :func:`select` :func:`select_keys` :func:`select_values` :func:`split_keys` :func:`compact`
3535
Dicts :ref:`*<colls>` :func:`flip` :func:`zipdict` :func:`pluck` :func:`where` :func:`itervalues` :func:`iteritems` :func:`zip_values` :func:`zip_dicts` :func:`project` :func:`omit`
3636
Misc :func:`empty` :func:`get_in` :func:`get_lax` :func:`set_in` :func:`update_in` :func:`del_in` :func:`has_path`
3737
===================== ==============================================================

docs/colls.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ All functions in this section support :ref:`extended_fns`.
111111
# Construct a dict of methods
112112
select_values(inspect.isfunction, cls.__dict__)
113113

114+
115+
.. function:: split_keys(pred, coll)
116+
117+
Splits a dictionary into two based on a predicate applied to its keys.
118+
119+
Say, you can separate custom HTTP headers from standard ones::
120+
121+
custom, standard = split_keys(r'^X-', headers)
122+
# custom -> {'X-Custom-Header': 'value'}
123+
# standard -> {'Content-Type': 'application/json', ...}
124+
125+
114126
.. function:: compact(coll)
115127

116128
Removes falsy values from given collection. When compacting a dict all keys with falsy values are removed.

docs/descriptions.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@
428428
Select part of <em>coll</em> with values passing <em>pred</em>.<br>
429429
Works with dicts and collections of pairs.
430430
</div>
431+
<div name="split_keys">
432+
<b>split_keys<em>(pred, coll)</em></b><br><br>
433+
Splits <em>coll</em> part with keys passing <em>pred</em><br>
434+
from the one with remaining pairs.
435+
</div>
431436
<div name="compact">
432437
<b>compact<em>(coll)</em></b><br><br>
433438
Removes falsy values from given collection.

tests/test_colls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ def test_select_values():
151151
assert select_values(r'a', {1: 'a', 2: 'b'}) == {1: 'a'}
152152

153153

154+
def test_split_keys():
155+
d = {'a': 1, 'b': 2, 1: 3, 2: 4}
156+
yes, no = split_keys(lambda k: isinstance(k, str), d)
157+
assert yes == {'a': 1, 'b': 2}
158+
assert no == {1: 3, 2: 4}
159+
160+
154161
def test_compact():
155162
assert eq(compact([0, 1, None, 3]), [1, 3])
156163
assert eq(compact((0, 1, None, 3)), (1, 3))

0 commit comments

Comments
 (0)