Package: chainreaction
About this moduleWrapper module which provides iterable object with infix style method chaining.Unreadble:>>> filter(lambda y: y % 2, map(lambda x: x + 1, iterable))Readable:>>> iterable = react(iterable)
>>> iterable.map(lambda x: x + 1).filter(lambda y: y % 2)How to installRequires python2.6 or later (include python3.x).
You needpipordistributeorsetuptools:$ pip install chainreactionor use easy_install:$ easy_install chainreactionRecent changed0.2.0Supported python2.6 .Changed to use itertools in ‘Chainable.filter/map’ methods in python2.x .0.1.2Supported ‘for’ statement.How to useThe simplest and an only way to use this module is to call thereactfunction.reactis a factory function which looks up‘__iter__’/‘__getitem__’from an given iterable, and returns wrapped object.Example to use:>>> from chainreaction.reactor import react
>>> react("hello")
>>> react([1, 2, 3])
>>> react(iter([1, 2, 3])
>>> class DictLike(dict):pass
>>> react(DictLike())Chainable object support any methods of its wrapped object,
and try to wrap their return value.
By convention, side effective methods return nothing, but chainable object
returns its side affected object wrapped with appropriate wrappper.
If iterator was passed intoreact, it would be consumed when it called
any iterative methods.Example to use:>>> tobewrapped = "hello"
>>> wrapped = react(tobewrapped)
>>> wrapped = wrapped.upper().map(lambda c: ord(c))
>>> [chr(c) for c in wrapped.unwrap] = [c for c in tobewrapped.upper()]
True
>>> tobewrapped = [1, 2, 3]
>>> wrapped = react(tobewrapped)
>>> wrapped.append(4).count()
4
>>> len(tobewrapped)
4If an iterator was given, this iterator would be consumed
when it called any method:>>> tobewrapped = iter([1,2,3])
>>> wrapped = react(tobewrapped)
>>> wrapped.count()
4
>>> len(list(tobewrapped))
0Tiny percent encoding implementation example:>>> test = bytes("hello world-._~", encoding="UTF-8")
>>> wrapped = react(test)
>>> safe = set()
>>> react("0123456789").foreach(lambda c: safe.add(ord(c)))
>>> react(range(ord('a'), ord('z') + 1)).foreach(lambda b: safe.add(b))
>>> react(range(ord('A'), ord('Z') + 1)).foreach(lambda b: safe.add(b))
>>> react("-._~").foreach(lambda c: safe.add(ord(c)))
>>> test = wrapped.map(lambda b: b if b > 0 else 256 + b)
>>> test = test.map(lambda i: '+' if chr(i).isspace() else chr(i) if i in safe else "%{0:x}".format(i))
>>> test.tostring == "hello+world-._~"
TrueBasic APIunwrapRetrieves iterable from wrapped object.dumpElements dump.isiteratorTrue if wrapped object is iterator.tostringReturns almost same with “”.join((str(x) for x in iterable)). This method always returns str object. str wrapped returns unwrapped, bytes or bytearray wrapped returns str converted, dict wrapped returns “key=value” pairs joined with “&”.foreach(f)Applies a given f to all elements.filter(pred)Selects all elements which satisfy a given predicate.map(f)Builds a new collection by applying a given f.forall(pred)True if all elements satisfy a given predicate.forany(pred)True if any elements satisfy a given predicate.dropwhile(pred)Drops longest prefix of elements that satisfy a given predicate.dropright(pred)Drops longest suffix of elements that satisfy a given predicate.takwhile(pred)Takes longest prefix of elements that satisfy a given predicate.takeright(pred)Takes longest suffix of elements that satisfy a given predicate.mkstring(joiner=””)Returns wrapped str object using a joiner string.counts(pred=lambda x: True)Counts the number of elements that satisfy a given predicate.contains(key)Tests whether this wrapped object contains a given key as an element.reduce(f)Returns a value(not wrapped) using a given f.iterator specific APItolistReturns a new list wrapped.totupleReturns a new tuple wrapped.tosetReturns a new set wrapped.str, bytes, bytearray specific APIreverseReturns a new reversed str wrapped.seq specific APIaccumulate(f)Returns a seq of accumulated value.reverseReturns a new reversed seq wrapped.sortReturns a new sorted seq wrapped.tosetReturns a new set wrapped.set specific APIminReturns a minimum value.maxReturns a maximum value.