knife documentation

knife.lazy.filterknife — Lazier evaluated filtering knife

«  knife.lazy.cmpknife — Lazier evaluated comparing knife   ::   Contents   ::   knife.lazy.mapknife — Lazier evaluated mapping knife  »

knife.lazy.filterknife — Lazier evaluated filtering knife

class knife.lazy.filterknife(*things, **kw)

Lazier evaluated filtering knife. Provides filtering operations for incoming things.

>>> from knife.lazy import filterknife
__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
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,)']
attrs(*names)

Collect attribute values from incoming things that match an attribute name found in names.

Parameters:names (str) – attribute names
Return type:knife object
>>> from knife import __
>>> from stuf import stuf
>>> stooge = [
...    stuf(name='moe', age=40),
...    stuf(name='larry', age=50),
...    stuf(name='curly', age=60),
... ]
>>> __(*stooge).attrs('name').get()
['moe', 'larry', 'curly']
>>> # multiple attribute names
>>> __(*stooge).attrs('name', 'age').get()
[('moe', 40), ('larry', 50), ('curly', 60)]
>>> # no attrs named 'place'
>>> __(*stooge).attrs('place').get()
[]

See also

operator.attrgetter
function in Python standard library
pick
function in Underscore.js
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:
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,)']
clear()

Clear everything.

Return type:knife object
duality()

Divide incoming things into two iterables, the first everything worker is True for and the second everything worker is False for.

Return type:knife object
>>> test = __(1, 2, 3, 4, 5, 6).worker(lambda x: x % 2 == 0)
>>> divide = test.duality().get()
>>> divide.true
(2, 4, 6)
>>> divide.false
(1, 3, 5)
filter(invert=False)

Collect incoming things matched by worker.

Parameters:invert (bool) – collect incoming things worker is False rather than True for
Return type:knife object
>>> # filter for true values
>>> test = __(1, 2, 3, 4, 5, 6).worker(lambda x: x % 2 == 0)
>>> test.filter().get()
[2, 4, 6]
>>> # filter for false values
>>> test.original().worker(
...   lambda x: x % 2 == 0
... ).filter(invert=True).get()
[1, 3, 5]

See also

itertools.ifilter
function in Python standard library (replaces filter in Python 3)
itertools.ifilterfalse
function in Python standard library (filterfalse in Python 3)
filter
function in Underscore.js
reject
function in Underscore.js
filter
function in Underscore.lua
reject
function in Underscore.lua
filter
function in Underscore.perl
reject
function in Underscore.perl
filter
function in Underscore.php
reject
function in Underscore.php
get()

Return outgoing things wrapped with wrap.

Note

With only one outgoing thing, only that one outgoing thing is returned. With multiple outgoing things, they are returned wrapped with the wrapper assigned with wrap (default wrapper is list).

items(*keys)
Collect values from incoming things (usually a sequence or

mapping) that match a key found in keys.

argument str keys:
 keys or indices
rtype:knife object
>>> stooge = [
...    dict(name='moe', age=40),
...    dict(name='larry', age=50),
...    dict(name='curly', age=60)
... ]
>>> # get items from mappings like dictionaries, etc...
>>> __(*stooge).items('name').get()
['moe', 'larry', 'curly']
>>> __(*stooge).items('name', 'age').get()
[('moe', 40), ('larry', 50), ('curly', 60)]
>>> # get items from sequences like lists, tuples, etc...
>>> stooge = [['moe', 40], ['larry', 50], ['curly', 60]]
>>> __(*stooge).items(0).get()
['moe', 'larry', 'curly']
>>> __(*stooge).items(1).get()
[40, 50, 60]
>>> __(*stooge).items('place').get()
[]

See also

operator.itemgetter
function in Python standard library
pick
function in Underscore.js
members(inverse=False)

Collect values from shallowly from classes or objects matched by worker.

Parameters:invert (bool) – collect incoming things that worker is False rather than True for
Returns:sequence of tuple of keys and value
Return type:knife object
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'
peek()

Preview current incoming things wrapped with wrap.

Note

With only one outgoing thing, only that one thing is returned. With multiple outgoing things, everything is returned wrapped with the wrapper assigned with wrap (default wrapper is list).

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]
snapshot()

Take a baseline snapshot of current incoming things.

Return type:knife object
traverse(invert=False)

Collect values from deeply nested scopes from incoming things matched by worker.

Parameters:invert (bool) – collect incoming things that worker is False rather than True for
Returns:sequence of ChainMaps containing collections.OrderedDict
Return type:knife object
>>> class stooge:
...    name = 'moe'
...    age = 40
>>> class stooge2:
...    name = 'larry'
...    age = 50
>>> class stooge3:
...    name = 'curly'
...    age = 60
...    class stooge4(object):
...        name = 'beastly'
...        age = 969
>>> def test(x):
...    if x[0] == 'name':
...        return True
...    elif x[0].startswith('__'):
...        return True
...    return False
>>> # using worker while filtering for False values
>>> __(stooge, stooge2, stooge3).worker(test).traverse(
...   invert=True
... ).get() 
[ChainMap(OrderedDict([('classname', 'stooge'), ('age', 40)])),
ChainMap(OrderedDict([('classname', 'stooge2'), ('age', 50)])),
ChainMap(OrderedDict([('classname', 'stooge3'), ('age', 60)]),
OrderedDict([('age', 969), ('classname', 'stooge4')]))]
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:
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,)']
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
wrap(wrapper)

Assign object, type, or class used to wrap outgoing things.

Note

A knife object resets back to its default wrapper (list) after get or peek is called.

Parameters:wrapper – an object, type, or class
Return type:knife object
>>> __(1, 2, 3, 4, 5, 6).wrap(tuple).peek()
(1, 2, 3, 4, 5, 6)

«  knife.lazy.cmpknife — Lazier evaluated comparing knife   ::   Contents   ::   knife.lazy.mapknife — Lazier evaluated mapping knife  »