chain (1 Sept 2012)
index
/Users/dh/chain/chain.py

This module provides a jQuery like class for python: Chain.

 
Modules
       
operator

 
Classes
       
__builtin__.list(__builtin__.object)
Chain

 
class Chain(__builtin__.list)
    A wrapper of list that will call callable on each element, and return
results in a Chain if possible.
 
 
Method resolution order:
Chain
__builtin__.list
__builtin__.object

Methods defined here:
__getattr__(self, ident)
if ident is an attribute of the first element, return that 
attribute of each element in a Chain.
if ident is a method or function name, return a method or function 
that will call each element.
 
>>> class Foo:
...     def __init__(self, a): self.a = a
...     def one(self): return 1 
...     def plus(self, x): return self.a + x 
... 
>>> foos = Chain([Foo("a"), Foo("b")])
 
Attribute of elemments will be proxied:
>>> foos.a
['a', 'b']
 
Method of element will be called with each element as self.
>>> foos.one()
[1, 1]
 
This applies to method with more than one arguments.
>>> foos.plus('_bar')
['a_bar', 'b_bar']
 
If an identifier is neither an attribute nor a method name, it will 
be treated as a function, and called on each element with that element
as the first argument.
>>> def inc(x): return x + 1
>>> Chain([1,2,3]).inc() 
[2, 3, 4]
>>> def add(x, y): return x + y 
>>> Chain([1,2,3]).add(2)
[3, 4, 5]
 
The return type is Chain,
>>> type(foos.a) == Chain 
True
 
so calls can be chained. 
>>> foos.one().inc().add(2)
[4, 4]
 
Getting any attribute on empty Chain will return an empty Chain.
>>> Chain([]).whatever
[]
applied(self, f, return_chain=True, *args, **kwargs)
return f(self, *args, **kwargs), and make the result as a Chain
if possible.
 
return_chain -- if False, will not convert result to Chain
 
>>> Chain([(1,2)]).applied(dict, return_chain = False)
{1: 2}
 
>>> Chain([1,1,2]).applied(set)
[1, 2]
concat(self)
concat a list of list of something, and return a list of something.
 
>>> Chain( [[1,2], [3,4,5]]).concat()
[1, 2, 3, 4, 5]
each(self, f)
apply f on each elemment, and return the results in a Chain.
 
f can be a callable,
 
>>> Chain([1,2,3]).each( lambda x : x * 2 )
[2, 4, 6]
 
or string contains underscore(_). In that case, it's converted to a
lambda function like this:
        lambda _ : {f as a string} 
For example, if f is '_ + 1', then the function is lambda _ : _ + 1 
 
>>> Chain([1,2,3]).each(' _ + 1')
[2, 3, 4]
 
f can also contain pipeline( | ) character, and it acts like unix 
pipeline, so ' _ + 1 | _ * 2 ' will add 1 to _ first, then double it.
 
>>> Chain([1,2,3]).each(' _ + 1 | _ * 2')
[4, 6, 8]
 
If you need | in your lambda, use lambda directly
>>> Chain([1,2,3]).each(lambda _ :  _ | 2 ) 
[3, 2, 3]
 
Empty string is treated as identity function that does nothing to the 
element. 
 
>>> Chain([1,2,3]).each('')
[1, 2, 3]
filter(self, predicate)
filter chain by predicate.
predicate -- has the same mean as `each` method's argument f. 
 
>> Chain([1,2,3,4,5]).filter(' _%2 == 0 ')
[2, 4]
find(self, predicate)
find the first element in self that satisfy predicate. 
predicate -- has the same mean as `each` method's argument f. 
 
>> Chain([1,2,3,4,5]).find(' _%2 == 0 ')
[2]
joined(self, sep)
return sep.join(self).
 
>>> Chain('abc').joined('|')
'a|b|c'
reduce(self, binary_func, *args)
return reduce(binary_func, self, *args).
 
>>> from operator import mul
>>> Chain([2,3,4]).reduce(mul)
24

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

Methods inherited from __builtin__.list:
__add__(...)
x.__add__(y) <==> x+y
__contains__(...)
x.__contains__(y) <==> y in x
__delitem__(...)
x.__delitem__(y) <==> del x[y]
__delslice__(...)
x.__delslice__(i, j) <==> del x[i:j]
 
Use of negative indices is not supported.
__eq__(...)
x.__eq__(y) <==> x==y
__ge__(...)
x.__ge__(y) <==> x>=y
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__gt__(...)
x.__gt__(y) <==> x>y
__iadd__(...)
x.__iadd__(y) <==> x+=y
__imul__(...)
x.__imul__(y) <==> x*=y
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
__iter__(...)
x.__iter__() <==> iter(x)
__le__(...)
x.__le__(y) <==> x<=y
__len__(...)
x.__len__() <==> len(x)
__lt__(...)
x.__lt__(y) <==> x<y
__mul__(...)
x.__mul__(n) <==> x*n
__ne__(...)
x.__ne__(y) <==> x!=y
__repr__(...)
x.__repr__() <==> repr(x)
__reversed__(...)
L.__reversed__() -- return a reverse iterator over the list
__rmul__(...)
x.__rmul__(n) <==> n*x
__setitem__(...)
x.__setitem__(i, y) <==> x[i]=y
__setslice__(...)
x.__setslice__(i, j, y) <==> x[i:j]=y
 
Use  of negative indices is not supported.
__sizeof__(...)
L.__sizeof__() -- size of L in memory, in bytes
append(...)
L.append(object) -- append object to end
count(...)
L.count(value) -> integer -- return number of occurrences of value
extend(...)
L.extend(iterable) -- extend list by appending elements from the iterable
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
insert(...)
L.insert(index, object) -- insert object before index
pop(...)
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
remove(...)
L.remove(value) -- remove first occurrence of value.
Raises ValueError if the value is not present.
reverse(...)
L.reverse() -- reverse *IN PLACE*
sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1

Data and other attributes inherited from __builtin__.list:
__hash__ = None
__new__ = <built-in method __new__ of type object at 0x100121300>
T.__new__(S, ...) -> a new object with type S, a subtype of T

 
Data
        __author__ = 'Hao Deng <denghao8888@gmail.com>'
__date__ = '1 Sept 2012'

 
Author
        Hao Deng <denghao8888@gmail.com>