knife documentation

knife.active.activeknife — Actively evaluated combo knife

«  knife.lazy.sliceknife — Lazier evaluated slicing knife   ::   Contents   ::   knife.active.cmpknife — Actively evaluated comparing knife  »

knife.active.activeknife — Actively evaluated combo knife

class knife.active.activeknife(*things, **kw)

Actively evaluated combo knife. Provides every knife method.

Note

Also aliased as knife when imported from knife.

>>> from knife import knife
__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.

all()

Discover if worker is True for all incoming things.

Returns:bool
Return type:knife object
>>> from knife import __
>>> __(2, 4, 6, 8).worker(lambda x: x % 2 == 0).all().get()
True

See also

all
function in Python standard library
all
function in Underscore.js
all
function in Underscore.lua
all
function in Underscore.perl
all
function in Underscore.php
any()

Discover if worker is True for any incoming thing.

Returns:bool
Return type:knife object
>>> __(1, 4, 5, 9).worker(lambda x: x % 2 == 0).any().get()
True

See also

any
function in Python standard library
any
function in Underscore.js
any
function in Underscore.lua
any
function in Underscore.perl
any
function in Underscore.php
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,)']
at(n, default=None)

Slice off incoming thing found at index n.

Parameters:
  • n (int) – index of some incoming thing
  • default – default returned if nothing is found at n
Return type:

knife object

>>> from knife import __
>>> # default behavior
>>> __(5, 4, 3, 2, 1).at(2).get()
3
>>> # return default value if nothing found at index
>>> __(5, 4, 3, 2, 1).at(10, 11).get()
11

See also

“nth”
Itertools Recipes
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
average()

Discover average value among incoming things.

Returns:a number
Return type:knife object
>>> from knife import __
>>> __(10, 40, 45).average().get()
31.666666666666668
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,)']
choice()

Randomly slice off one incoming thing.

Return type:knife object
>>> __(1, 2, 3, 4, 5, 6).choice().get() 
3

See also

random.choice
function in Python standard library
clear()

Clear everything.

Return type:knife object
combinate(n)

Discover combinations for every n incoming things.

Parameters:n (int) – number of incoming things to derive combinations from
Return type:knife object
>>> from knife import __
>>> __(40, 50, 60).combinate(2).get()
[(40, 50), (40, 60), (50, 60)]

See also

itertools.combinations
function in Python standard library
copy()

Duplicate each incoming thing.

Return type:knife object
>>> __([[1, [2, 3]], [4, [5, 6]]]).copy().get()
[[1, [2, 3]], [4, [5, 6]]]

See also

copy.deepcopy
function in Python standard library
count()

Discover how common each incoming thing is and the overall count of each incoming thing.

Returns:Collects namedtuple Count(least=int, most=int, overall=[(thing1, int), (thing2, int), ...])
Return type:knife object
>>> common = __(11, 3, 5, 11, 7, 3, 5, 11).count().get()
>>> # least common thing
>>> common.least
7
>>> # most common thing
>>> common.most
11
>>> # total count for every thing
>>> common.overall
[(11, 3), (3, 2), (5, 2), (7, 1)]
dice(n, fill=None)

Slice one iterable incoming thing into n iterable incoming things.

Parameters:
  • n (int) – number of incoming things per slice
  • fill – value to pad out incomplete iterables
Return type:

knife object

>>> __('moe', 'larry', 'curly', 30, 40, 50, True).dice(2, 'x').get()
[('moe', 'larry'), ('curly', 30), (40, 50), (True, 'x')]

See also

“grouper”
Itertools Recipes
difference(symmetric=False)

Discover difference within a series of iterable incoming things.

Parameters:symmetric (bool) – return symmetric difference
Returns:list
Return type:knife object
>>> # default behavior
>>> test = __([1, 2, 3, 4, 5], [5, 2, 10], [10, 11, 2])
>>> test.difference().get()
[1, 3, 4]
>>> # symmetric difference
>>> test.original().difference(symmetric=True).get()
[1, 2, 3, 4, 11]

See also

set.difference
function in Python standard library
set.symmetric_difference
function in Python standard library
difference
function in Underscore.js
difference
function in Underscore.perl
difference
function in Underscore.php
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
first(n=0)

Slice off n things from the starting end of incoming things or just the first incoming thing.

Parameters:n (int) – number of incoming things
Return type:knife object
>>> # default behavior
>>> __(5, 4, 3, 2, 1).first().get()
5
>>> # first things from index 0 to 2
>>> __(5, 4, 3, 2, 1).first(2).get()
[5, 4]

See also

first
function in Underscore.js
first
function in Underscore.lua
first
function in Underscore.perl
first
function in Underscore.php
flatten()

Reduce nested incoming things to flattened incoming things.

Return type:knife object
>>> from knife import __
>>> __([[1, [2], [3, [[4]]]], 'here']).flatten().get()
[1, 2, 3, 4, 'here']

See also

flatten
function in Underscore.js
flatten
function in Underscore.lua
flatten
function in Underscore.perl
flatten
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).

group()

Group incoming things using worker as the key function.

Returns:namedtuple Group(keys=keys, groups=tuple)
Return type:knife object
>>> from knife import __
>>> # default grouping
>>> __(1.3, 2.1).group().get()
[Group(keys=1.3, groups=(1.3,)), Group(keys=2.1, groups=(2.1,))]
>>> from math import floor
>>> # use worker for key function
>>> __(1.3, 2.1, 2.4).worker(floor).group().get()
[Group(keys=1.0, groups=(1.3,)), Group(keys=2.0, groups=(2.1, 2.4))]

See also

itertools.groupby
function in Python standard library
groupBy
function in Underscore.js
groupBy
function in Underscore.perl
groupBy
function in Underscore.php
initial()

Slice off every incoming thing except the last incoming thing.

Return type:knife object
>>> __(5, 4, 3, 2, 1).initial().get()
[5, 4, 3, 2]

See also

initial
function in Underscore.js
initial
function in Underscore.lua
initial
function in Underscore.perl
initial
function in Underscore.php
intersection()

Discover intersection within a series of iterable incoming things.

Returns:list
Return type:knife object
>>> __([1, 2, 3], [101, 2, 1, 10], [2, 1]).intersection().get()
[1, 2]

See also

set.intersection
function in Python standard library
intersection
function in Underscore.js
intersection
function in Underscore.perl
intersection
function in Underscore.php
invoke(name)

Feed global positional arguments and keyword arguments to each incoming thing’s name method.

Note

The original thing is returned if the return value of method name is None.

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]]

See also

invoke
function in Underscore.js
invoke
function in Underscore.lua
invoke
function in Underscore.perl
invoke
function in Underscore.php
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
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]
last(n=0)

Slice off n things from the tail end of incoming things or just the last incoming thing.

Parameters:n (int) – number of incoming things
Return type:knife object
>>> # default behavior
>>> __(5, 4, 3, 2, 1).last().get()
1
>>> # fetch last two things
>>> __(5, 4, 3, 2, 1).last(2).get()
[2, 1]

See also

last
function in Underscore.js
last
function in Underscore.lua
last
function in Underscore.perl
last
function in Underscore.php
map()

Feed each incoming thing to worker.

Return type:knife object
>>> __(1, 2, 3).worker(lambda x: x * 3).map().get()
[3, 6, 9]

See also

itertools.imap
function in Python standard library (replaces map in Python 3)
map
function in Underscore.js
map
function in Underscore.lua
map
function in Underscore.perl
map
function in Underscore.php
mapping(keys=False, values=False)

Run worker on incoming mapping things.

Parameters:
  • keys (bool) – collect mapping keys only
  • values (bool) – collect mapping values only
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]

See also

keys
function in Underscore.js
values
function in Underscore.js
keys
function in Underscore.lua
values
function in Underscore.lua
keys
function in Underscore.perl
values
function in Underscore.perl
keys
function in Underscore.php
values
function in Underscore.php
max()

Discover the maximum value among incoming things using worker as the key function.

Return type:knife object
>>> # default behavior
>>> __(1, 2, 4).max().get()
4
>>> stooges = (
...    {'name': 'moe', 'age': 40},
...    {'name': 'larry', 'age': 50},
...    {'name': 'curly', 'age': 60},
... )
>>> # using worker as key function
>>> __(*stooges).worker(lambda x: x['age']).max().get()
{'age': 60, 'name': 'curly'}

See also

max
function in Python standard library
max
function in Underscore.js
max
function in Underscore.lua
max
function in Underscore.perl
max
function in Underscore.php
median()

Discover the median value among incoming things.

Returns:a number
Return type:knife object
>>> __(4, 5, 7, 2, 1).median().get()
4
>>> __(4, 5, 7, 2, 1, 8).median().get()
4.5
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
merge()

Reduce multiple iterable incoming things into one iterable incoming thing.

Return type:knife object
>>> __(['moe', 'larry'], [30, 40], [True, False]).merge().get()
['moe', 'larry', 30, 40, True, False]

See also

itertools.chain.from_iterable
function in Python standard library
min()

Discover the minimum value among incoming things using worker as the key function.

Return type:knife object
>>> test = __(10, 5, 100, 2, 1000)
>>> test.min().get()
2
>>> test.original().worker(lambda x: x % 100 == 0).min().get()
10

See also

min
function in Python standard library
min
function in Underscore.js
min
function in Underscore.lua
min
function in Underscore.perl
min
function in Underscore.php
minmax()

Discover the minimum and maximum values among incoming things.

Returns:namedtuple MinMAx(min=value, max=value).
Return type:knife object
>>> minmax = __(1, 2, 4).minmax().get()
>>> minmax.min
1
>>> minmax.max
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'
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).

permutate(n)

Discover permutations for every n incoming things.

Parameters:n (int) – number of incoming things to derive permutations from
Return type:knife object
>>> __(40, 50, 60).permutate(2).get()
[(40, 50), (40, 60), (50, 40), (50, 60), (60, 40), (60, 50)]

See also

itertools.permutations
function in Python standard library
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]
range()

Discover the length of the smallest interval that can contain the value of every incoming thing.

Returns:a number
Return type:knife object
>>> __(3, 5, 7, 3, 11).range().get()
8
reduce(initial=None, reverse=False)

Reduce iterable incoming things down to one incoming thing using worker.

Parameters:
  • initial – starting value
  • reverse (bool) – reduce from the right side of incoming things
Return type:

knife object

>>> # reduce from left side
>>> __(1, 2, 3).worker(lambda x, y: x + y).reduce().get()
6
>>> # reduce from left side with initial value
>>> __(1, 2, 3).worker(lambda x, y: x + y).reduce(initial=1).get()
7
>>> # reduce from right side
>>> test = __([0, 1], [2, 3], [4, 5]).worker(lambda x, y: x + y)
>>> test.reduce(reverse=True).get()
[4, 5, 2, 3, 0, 1]
>>> # reduce from right side with initial value
>>> test.original().worker(
... lambda x, y: x + y
... ).reduce([0, 0], True).get()
[4, 5, 2, 3, 0, 1, 0, 0]

See also

functools.reduce
function in Python standard library
reduce
function in Underscore.js
reduceRight
function in Underscore.js
reduce
function in Underscore.lua
reduce
function in Underscore.perl
reduceRight
function in Underscore.perl
reduce
function in Underscore.php
reduceRight
function in Underscore.php
repeat(n=None, call=False)

Repeat incoming things n times or invoke worker n times.

Parameters:
  • n (int) – number of times to repeat
  • call (bool) – repeat results of invoking worker
Return type:

knife object

>>> # repeat iterable
>>> __(40, 50, 60).repeat(3).get()
[(40, 50, 60), (40, 50, 60), (40, 50, 60)]
>>> def test(*args):
...    return list(args)
>>> # with worker
>>> __(40, 50, 60).worker(test).repeat(n=3, call=True).get()
[[40, 50, 60], [40, 50, 60], [40, 50, 60]]

See also

itertools.repeat
function in Python standard library
times
function in Underscore.js
times
function in Underscore.perl
times
function in Underscore.php
rest()

Slice off every incoming thing except the first incoming thing.

Return type:knife object
>>> __(5, 4, 3, 2, 1).rest().get()
[4, 3, 2, 1]

See also

rest
function in Underscore.js
rest
function in Underscore.lua
rest
function in Underscore.perl
rest
function in Underscore.php
reverse()

Reverse the order of incoming things.

Return type:knife object
>>> __(5, 4, 3, 2, 1).reverse().get()
[1, 2, 3, 4, 5]

See also

reversed
function in Python standard library
reverse
function in Underscore.lua
sample(n)

Randomly slice off n incoming things.

Parameters:n (int) – sample size
Return type:knife object
>>> __(1, 2, 3, 4, 5, 6).sample(3).get() 
[2, 4, 5]

See also

random.sample
function in Python standard library
shuffle()

Randomly sort incoming things.

Return type:knife object
>>> __(5, 4, 3, 2, 1).shuffle().get() 
[3, 1, 5, 4, 2]

See also

random.shuffle
function in Python standard library
shuffle
function in Underscore.js
shuffle
function in Underscore.php
slice(start, stop=False, step=False)

Take slice out of incoming things.

Parameters:
  • start (int) – starting index of slice
  • stop (int) – stopping index of slice
  • step (int) – size of step in slice
Return type:

knife object

>>> # slice from index 0 to 3
>>> __(5, 4, 3, 2, 1).slice(2).get()
[5, 4]
>>> # slice from index 2 to 4
>>> __(5, 4, 3, 2, 1).slice(2, 4).get()
[3, 2]
>>> # slice from index 2 to 4 with 2 steps
>>> __(5, 4, 3, 2, 1).slice(2, 4, 2).get()
3

See also

itertools.islice
function in Python standard library
slice
function in Underscore.lua
snapshot()

Take a baseline snapshot of current incoming things.

Return type:knife object
sort()

Reorder incoming things using worker as the key function.

Return type:knife object
>>> # default sort
>>> __(4, 6, 65, 3, 63, 2, 4).sort().get()
[2, 3, 4, 4, 6, 63, 65]
>>> from math import sin
>>> # using worker as key function
>>> __(1, 2, 3, 4, 5, 6).worker(sin).sort().get()
[5, 4, 6, 3, 1, 2]

See also

sorted
function in Python standard library
sortBy
function in Underscore.js
sort
function in Underscore.lua
sortBy
function in Underscore.perl
sortBy
function in Underscore.php
sum(start=0, precision=False)

Discover the total value of adding start and incoming things together.

Parameters:
  • start (int or float) – starting number
  • precision (bool) – add floats with extended precision
Return type:

knife object

>>> # default behavior
>>> __(1, 2, 3).sum().get()
6
>>> # with a starting mumber
>>> __(1, 2, 3).sum(start=1).get()
7
>>> # add floating points with extended precision
>>> __(.1, .1, .1, .1, .1, .1, .1, .1).sum(precision=True).get()
0.8

See also

sum
function in Python standard library
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,)']
union()

Discover union within a series of iterable incoming things.

Returns:list
Return type:knife object
>>> __([1, 2, 3], [101, 2, 1, 10], [2, 1]).union().get()
[1, 10, 3, 2, 101]

See also

set.union
function in Python standard library
union
function in Underscore.js
union
function in Underscore.perl
union
function in Underscore.php
unique()

Discover unique incoming things.

Return type:knife object
>>> # default behavior
>>> __(1, 2, 1, 3, 1, 4).unique().get()
[1, 2, 3, 4]
>>> # using worker as key function
>>> __(1, 2, 1, 3, 1, 4).worker(round).unique().get()
[1.0, 2.0, 3.0, 4.0]

See also

uniq
function in Underscore.js
uniq
function in Underscore.perl
uniq
function in Underscore.php
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)
zip()

Convert multiple iterable incoming things to a series of tuples composed of things found at the same index position within the original iterables.

Return type:knife object
>>> test = __(['moe', 'larry'], [30, 40], [True, False])
>>> test.zip().get()
[('moe', 30, True), ('larry', 40, False)]

See also

itertools.izip
function in Python standard library (replaces zip in Python 3)
zip
function in Underscore.js
zip
function in Underscore.perl
zip
function in Underscore.php

«  knife.lazy.sliceknife — Lazier evaluated slicing knife   ::   Contents   ::   knife.active.cmpknife — Actively evaluated comparing knife  »