Wrapper module which provides iterable object with infix style method chaining.
Project description
About this module
Wrapper 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 install
Requires python2.6 or later (include python3.x). You need pip or distribute or setuptools:
$ pip install chainreaction
or use easy_install:
$ easy_install chainreaction
Recent changed
0.2.0
Supported python2.6 .
Changed to use itertools in ‘Chainable.filter/map’ methods in python2.x .
0.1.2
Supported ‘for’ statement.
How to use
The simplest and an only way to use this module is to call the react function. react is 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 into react, 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) 4
If 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)) 0
Tiny 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-._~" True
Basic API
- unwrap
Retrieves iterable from wrapped object.
- dump
Elements dump.
- isiterator
True if wrapped object is iterator.
- tostring
Returns 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 API
- tolist
Returns a new list wrapped.
- totuple
Returns a new tuple wrapped.
- toset
Returns a new set wrapped.
str, bytes, bytearray specific API
- reverse
Returns a new reversed str wrapped.
seq specific API
- accumulate(f)
Returns a seq of accumulated value.
- reverse
Returns a new reversed seq wrapped.
- sort
Returns a new sorted seq wrapped.
- toset
Returns a new set wrapped.
set specific API
- min
Returns a minimum value.
- max
Returns a maximum value.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
File details
Details for the file chainreaction-0.2.0.zip
.
File metadata
- Download URL: chainreaction-0.2.0.zip
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65eb0b486c5939c14e7248a0649b3ce46496a11c85546bed337d5be82c68196c |
|
MD5 | 4a25e4f2c8c15735962e3ac8a1aac99a |
|
BLAKE2b-256 | 7f1aea637cce5c76f1412cc36302d98aee4eb81b317807731b258cc680ba66c6 |
File details
Details for the file chainreaction-0.2.0.tar.gz
.
File metadata
- Download URL: chainreaction-0.2.0.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a064ecdd518613a657c61b4701e29946b05e4d4a15082d58d1d8121262d55da0 |
|
MD5 | d8032c17090c7aa4e67e12b4a7bd04f8 |
|
BLAKE2b-256 | dbc0343836dc4c00094d366790adfe73b00b8ecb7947ad668dd60f105e6063dd |