-
Notifications
You must be signed in to change notification settings - Fork 1
Recipes
Thor Whalen edited this page Apr 13, 2023
·
1 revision
Here's a demo of how to use FuncFactory with an exclude argument.
import i2
def simple_chunker(wf, chk_size):
return zip(*([iter(wf)] * chk_size))
mk_chunker = i2.FuncFactory(simple_chunker)
assert str(i2.Sig(mk_chunker)) == '(wf, chk_size)'
# But we don't want that wf to show up in the factory, so:
mk_chunker = i2.FuncFactory(simple_chunker, exclude='wf')
assert str(i2.Sig(mk_chunker)) == '(chk_size)' # that's better!
# and yes, it works:
chunker = mk_chunker(2)
assert list(chunker([1,2,3,4])) == [(1, 2), (3, 4)]Note: exclude (as include) can be a string (single arg name or several, space separated), or an iterable of strings, but also an integer or iterable of integers; Indeed, you can specify your exclusion list positionally.
One way to use this is that you can make function factory... factories, that will omit the first argument(s) in their signature, which is a frequent need. Like so:
import i2
from functools import partial
func_factory_without_first_arg = partial(i2.FuncFactory, exclude=0)And then...
mk_chunker = func_factory_without_first_arg(simple_chunker)
assert str(i2.Sig(mk_chunker)) == '(chk_size)' # that's better!
chunker = mk_chunker(2)
assert list(chunker([1,2,3,4])) == [(1, 2), (3, 4)]