Skip to content

Commit d9f9dd8

Browse files
committed
fixes #665
1 parent 373a91d commit d9f9dd8

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
'fastcore.basics.otherwise': ('basics.html#otherwise', 'fastcore/basics.py'),
180180
'fastcore.basics.partialler': ('basics.html#partialler', 'fastcore/basics.py'),
181181
'fastcore.basics.partition': ('basics.html#partition', 'fastcore/basics.py'),
182+
'fastcore.basics.partition_dict': ('basics.html#partition_dict', 'fastcore/basics.py'),
182183
'fastcore.basics.patch': ('basics.html#patch', 'fastcore/basics.py'),
183184
'fastcore.basics.patch_property': ('basics.html#patch_property', 'fastcore/basics.py'),
184185
'fastcore.basics.patch_to': ('basics.html#patch_to', 'fastcore/basics.py'),

fastcore/basics.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
'annotations', 'anno_ret', 'signature_ex', 'union2tuple', 'argnames', 'with_cast', 'store_attr', 'attrdict',
1212
'properties', 'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getcallable', 'getattrs',
1313
'hasattrs', 'setattrs', 'try_attrs', 'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', 'Int', 'Str',
14-
'Float', 'partition', 'flatten', 'concat', 'strcat', 'detuplify', 'replicate', 'setify', 'merge', 'range_of',
15-
'groupby', 'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex',
16-
'not_', 'argwhere', 'filter_ex', 'renumerate', 'first', 'last', 'only', 'nested_attr', 'nested_setdefault',
17-
'nested_callable', 'nested_idx', 'set_nested_idx', 'val2idx', 'uniqueify', 'loop_first_last', 'loop_first',
18-
'loop_last', 'first_match', 'last_match', 'fastuple', 'bind', 'mapt', 'map_ex', 'compose', 'maps',
19-
'partialler', 'instantiate', 'using_attr', 'copy_func', 'patch_to', 'patch', 'patch_property', 'compile_re',
20-
'ImportEnum', 'StrEnum', 'str_enum', 'ValEnum', 'Stateful', 'NotStr', 'PrettyString', 'even_mults',
21-
'num_cpus', 'add_props', 'str2bool', 'str2int', 'str2float', 'str2list', 'str2date', 'to_bool', 'to_int',
22-
'to_float', 'to_list', 'to_date', 'typed', 'exec_new', 'exec_import', 'lt', 'gt', 'le', 'ge', 'eq', 'ne',
23-
'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'mod']
14+
'Float', 'partition', 'partition_dict', 'flatten', 'concat', 'strcat', 'detuplify', 'replicate', 'setify',
15+
'merge', 'range_of', 'groupby', 'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle',
16+
'zip_cycle', 'sorted_ex', 'not_', 'argwhere', 'filter_ex', 'renumerate', 'first', 'last', 'only',
17+
'nested_attr', 'nested_setdefault', 'nested_callable', 'nested_idx', 'set_nested_idx', 'val2idx',
18+
'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'first_match', 'last_match', 'fastuple', 'bind',
19+
'mapt', 'map_ex', 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'copy_func', 'patch_to',
20+
'patch', 'patch_property', 'compile_re', 'ImportEnum', 'StrEnum', 'str_enum', 'ValEnum', 'Stateful',
21+
'NotStr', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'str2bool', 'str2int', 'str2float',
22+
'str2list', 'str2date', 'to_bool', 'to_int', 'to_float', 'to_list', 'to_date', 'typed', 'exec_new',
23+
'exec_import', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'mod']
2424

2525
# %% ../nbs/01_basics.ipynb
2626
from .imports import *
@@ -574,6 +574,13 @@ def partition(coll, f):
574574
ts,fs = typ(ts),typ(fs)
575575
return ts,fs
576576

577+
# %% ../nbs/01_basics.ipynb
578+
def partition_dict(d, f):
579+
"Partition a dict by a predicate that takes key/value params"
580+
ts,fs = {},{}
581+
for k,v in d.items(): (fs,ts)[f(k,v)][k] = v
582+
return ts,fs
583+
577584
# %% ../nbs/01_basics.ipynb
578585
def flatten(o):
579586
"Concatenate all collections and items as a generator"

nbs/01_basics.ipynb

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -672,14 +672,6 @@
672672
"execution_count": null,
673673
"metadata": {},
674674
"outputs": [
675-
{
676-
"name": "stderr",
677-
"output_type": "stream",
678-
"text": [
679-
"/Users/jhoward/miniforge3/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
680-
" from .autonotebook import tqdm as notebook_tqdm\n"
681-
]
682-
},
683675
{
684676
"data": {
685677
"text/markdown": [
@@ -3388,6 +3380,43 @@
33883380
"test_eq(ts, [1,3,5,7,9])"
33893381
]
33903382
},
3383+
{
3384+
"cell_type": "code",
3385+
"execution_count": null,
3386+
"metadata": {},
3387+
"outputs": [],
3388+
"source": [
3389+
"#| export\n",
3390+
"def partition_dict(d, f):\n",
3391+
" \"Partition a dict by a predicate that takes key/value params\"\n",
3392+
" ts,fs = {},{}\n",
3393+
" for k,v in d.items(): (fs,ts)[f(k,v)][k] = v\n",
3394+
" return ts,fs"
3395+
]
3396+
},
3397+
{
3398+
"cell_type": "code",
3399+
"execution_count": null,
3400+
"metadata": {},
3401+
"outputs": [],
3402+
"source": [
3403+
"d = {'a':1, 'b':2, 'c':3, 'd':4}\n",
3404+
"ts,fs = partition_dict(d, lambda k,v: v%2)\n",
3405+
"test_eq(fs, {'b':2, 'd':4})\n",
3406+
"test_eq(ts, {'a':1, 'c':3})"
3407+
]
3408+
},
3409+
{
3410+
"cell_type": "code",
3411+
"execution_count": null,
3412+
"metadata": {},
3413+
"outputs": [],
3414+
"source": [
3415+
"ts,fs = partition_dict(d, lambda k,v: k in 'bc')\n",
3416+
"test_eq(ts, {'b':2, 'c':3})\n",
3417+
"test_eq(fs, {'a':1, 'd':4})"
3418+
]
3419+
},
33913420
{
33923421
"cell_type": "code",
33933422
"execution_count": null,

0 commit comments

Comments
 (0)