1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.
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.
Metadata
Release files for chainreaction 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
File added late
1 file was uploaded more than 14 days after the first file in this release.
While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.
We recommend inspecting the release files before installing.
Source distributions (sdists)
| File | Size | Uploaded | |
|---|---|---|---|
| chainreaction-0.2.0.zip | 21.1 kB | Details | |
| chainreaction-0.2.0.tar.gz | 15.3 kB | Details |
Release files / chainreaction-0.2.0.zip
File added late
This file was uploaded more than 14 days after the first file in this release.
While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.
We recommend inspecting the release file before installing.
| Download URL | chainreaction-0.2.0.zip |
|---|---|
| Size | 21.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
65eb0b486c5939c14e7248a0649b3ce46496a11c85546bed337d5be82c68196c
|
|
BLAKE2b-256 checksum How to use checksums |
7f1aea637cce5c76f1412cc36302d98aee4eb81b317807731b258cc680ba66c6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / chainreaction-0.2.0.tar.gz
| Download URL | chainreaction-0.2.0.tar.gz |
|---|---|
| Size | 15.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a064ecdd518613a657c61b4701e29946b05e4d4a15082d58d1d8121262d55da0
|
|
BLAKE2b-256 checksum How to use checksums |
dbc0343836dc4c00094d366790adfe73b00b8ecb7947ad668dd60f105e6063dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |