knife.lazy.mapknife — Lazier evaluated mapping knife¶
- class knife.lazy.mapknife(*things, **kw)¶
Lazier evaluated mapping knife. Provides mapping operations for incoming things.
>>> from knife.lazy import mapknife
- __init__(*things, snapshots=5)¶
Parameters: - things – incoming things
- snapshots (integer) – snapshots to keep (default: 5)
- __len__()¶
Number of incoming things.
- __iter__()¶
Iterate over outgoing things.
- append(*things)¶
Insert things after other incoming things.
Parameters: things – incoming things Return type: knife object >>> from knife import __ >>> __(3, 4, 5).append(1, 2, 3, 4, 5, 6).peek() [3, 4, 5, 1, 2, 3, 4, 5, 6]
- apply(worker, *args, **kw)¶
Assign callable used to work on incoming things plus any positional arguments and keyword arguments it will use.
Note
Global positional arguments and keyword arguments assigned with params are reset whenever apply is called.
Parameters: worker – a callable Return type: knife object
- argmap(merge=False)¶
Feed each incoming thing to worker as positional arguments.
Parameters: merge (bool) – merge global positional params with positional arguments derived from incoming things when passed to worker Return type: knife object >>> from knife import __ >>> # default behavior >>> test = __((1, 2), (2, 3), (3, 4)) >>> test.worker(lambda x, y: x * y).argmap().get() [2, 6, 12] >>> # merge global positional arguments with iterable arguments >>> test.original().worker( ... lambda x, y, z, a, b: x * y * z * a * b ... ).params(7, 8, 9).argmap(merge=True).get() [1008, 3024, 6048]
See also
- itertools.starmap
- function in Python standard library
- ascii(errors='strict')¶
encode outgoing things as bytes with the 'latin-1' codec.
Parameters: errors (str) – error handling for encoding Return type: knife object >>> from stuf.six import u, b >>> test = __([1], True, r't', b('i'), u('g'), None, (1,)) >>> test.ascii().oneach().peek() ['[1]', 'True', 't', 'i', 'g', 'None', '(1,)']
- back()¶
Switch back to knife object that piped its incoming things through this knife object.
Return type: knife object
- baseline()¶
Restore incoming things back to the baseline snapshot.
Return type: knife object >>> from knife import __ >>> undone = __(1, 2, 3).prepend(1, 2, 3, 4, 5, 6) >>> undone.peek() [1, 2, 3, 4, 5, 6, 1, 2, 3] >>> undone.snapshot().append(1).append(2).peek() [1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2] >>> undone.baseline().peek() [1, 2, 3, 4, 5, 6, 1, 2, 3]
- bytes(encoding='utf_8', errors='strict')¶
encode outgoing things as bytes.
Parameters: - encoding (str) – character encoding
- errors (str) –
error handling for encoding
Return type: knife object
>>> test = __([1], True, r't', b('i'), u('g'), None, (1,)) >>> test.bytes().oneach().peek() ['[1]', 'True', 't', 'i', 'g', 'None', '(1,)']
- invoke(name)¶
Feed global positional arguments and keyword arguments to each incoming thing’s name method.
Parameters: name (str) – method name Return type: knife object >>> # invoke list.index() >>> __([5, 1, 7], [3, 2, 1]).params(1).invoke('index').get() [1, 2] >>> # invoke list.sort() but return sorted list instead of None >>> __([5, 1, 7], [3, 2, 1]).invoke('sort').get() [[1, 5, 7], [1, 2, 3]]
- kwargmap(merge=False)¶
Feed each incoming thing as a tuple of positional arguments and keyword arguments to worker.
Parameters: merge (bool) – merge global positional or keyword params with positional and keyword arguments derived from incoming things into a single tuple of wildcard positional and keyword arguments like (*iterable_args + global_args, **global_kwargs + iterable_kwargs) when passed to worker Return type: knife object >>> # default behavior >>> test = __( ... ((1, 2), {'a': 2}), ((2, 3), {'a': 2}), ((3, 4), {'a': 2}) ... ) >>> def tester(*args, **kw): ... return sum(args) * sum(kw.values()) >>> test.worker(tester).kwargmap().get() [6, 10, 14] >>> # merging global and iterable derived positional and keyword args >>> test.original().worker(tester).params( ... 1, 2, 3, b=5, w=10, y=13 ... ).kwargmap(merge=True).get() [270, 330, 390]
- map()¶
Feed each incoming thing to worker.
Return type: knife object >>> __(1, 2, 3).worker(lambda x: x * 3).map().get() [3, 6, 9]
- mapping(keys=False, values=False)¶
Run worker on incoming mapping things.
Parameters: Return type: knife object
>>> # filter items >>> __(dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)]) ... ).worker(lambda x, y: x * y).mapping().get() [2, 6, 12, 2, 6, 12] >>> # mapping keys only >>> __(dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)]) ... ).mapping(keys=True).get() [1, 2, 3, 1, 2, 3] >>> # mapping values only >>> __(dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)]) ... ).mapping(values=True).get() [2, 3, 4, 2, 3, 4]
- oneach()¶
Toggle whether each outgoing thing should be individually wrapped with the wrapper assigned with wrap (default wrapper is list ) or whether all outgoing things should be wrapped all at once.
Note
knife object default behavior is to wrap all outgoing things all at once. knife objects reset back to this behavior after get or peek is called.
Return type: knife object
- original()¶
Restore incoming things back to the original snapshot.
Note
THe original snapshot of incoming things is taken following the first knife method call but before the second knife method call (if there is a second method call)
Return type: knife object >>> undone = __(1, 2, 3).prepend(1, 2, 3, 4, 5, 6) >>> undone.peek() [1, 2, 3, 4, 5, 6, 1, 2, 3] >>> undone.original().peek() [1, 2, 3]
- params(*args, **kw)¶
Assign positional arguments and keyword arguments to be used globally.
Return type: knife object
- pattern(pattern, type='parse', flags=0)¶
Compile search pattern for use as worker.
Note
Global positional arguments and keyword arguments assigned with params are reset whenever a new pattern is compiled.
Parameters: Return type: knife object
>>> # using parse expression >>> test = __('first test', 'second test', 'third test') >>> test.pattern('first {}').filter().get() 'first test' >>> # using glob pattern >>> test.original().pattern('second*', type='glob').filter().get() 'second test' >>> # using regular expression >>> test.original().pattern('third .', type='regex').filter().get() 'third test'
- pipe(knife)¶
Pipe incoming things from some other knife object through this knife object.
Parameters: knife – another knife object Return type: knife object
- prepend(*things)¶
Insert things before other incoming things.
Parameters: things – incoming things Return type: knife object >>> __(3, 4, 5).prepend(1, 2, 3, 4, 5, 6).peek() [1, 2, 3, 4, 5, 6, 3, 4, 5]
- undo(snapshot=0)¶
Revert incoming things to a previous snapshot.
Note
A snapshot of current incoming things is taken when a knife method is called but before the main body of the method executes.
Parameters: snapshot (int) – number of steps ago 1, 2, 3, etc. Return type: knife object >>> undone = __(1, 2, 3).prepend(1, 2, 3, 4, 5, 6) >>> undone.peek() [1, 2, 3, 4, 5, 6, 1, 2, 3] >>> # undo back one step >>> undone.append(1).undo().peek() [1, 2, 3, 4, 5, 6, 1, 2, 3] >>> # undo back one step >>> undone.append(1).append(2).undo().peek() [1, 2, 3, 4, 5, 6, 1, 2, 3, 1] >>> # undo back 2 steps >>> undone.append(1).append(2).undo(2).peek() [1, 2, 3, 4, 5, 6, 1, 2, 3, 1]
- unicode(encoding='utf_8', errors='strict')¶
unicode (str under Python 3) decode outgoing things.
Parameters: - encoding (str) –
Unicode encoding
- errors (str) –
error handling for decoding
Return type: knife object
>>> test = __([1], True, r't', b('i'), u('g'), None, (1,)) >>> test.unicode().oneach().peek() [u'[1]', u'True', u't', u'i', u'g', u'None', u'(1,)']
- encoding (str) –
- worker(worker)¶
Assign callable used to work on incoming things.
Note
Global positional arguments and keyword arguments assigned with params are reset whenever a new worker is assigned.
Parameters: worker – a callable Return type: knife object