Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python.
Nathan Rice
nathan.alexander.rice at gmail.com
Tue Dec 20 14:45:06 EST 2011
Elementwise provides a proxy object for iterables which supports
chained method calls, as well as elementwise expressions and some
built-in functions.
Example:
class ExampleList(ElementwiseProxyMixin, list):
def __new__(cls, iterable):
return list.__new__(cls, iterable)
foo = ExampleList([1, 2, 3, 4])
# You could also do: efoo = ElementwiseProxy(foo)
efoo = foo.each
assert list(efoo.bit_length()) == [1, 2, 2, 3]
print "bit length: ", list(efoo.bit_length())
assert list(efoo + 1) == [2, 3, 4, 5]
print "with addition of 1: ", list(efoo + 1)
assert list(efoo * 2) == [2, 4, 6, 8]
print "with multiplication by 2: ", list(efoo * 2)
assert list(efoo == 2) == [False, True, False, False]
print "testing equality: ", efoo == 2
assert list((efoo + 1) * 2 + 3) == [7, 9, 11, 13]
print "chaining addition and multiplication: ", (efoo + 1) * 2 + 3
Each ElementwiseProxy also has a "parent" attribute so you can
backtrack in the chain as needed rather than store each intermediate
value, if you think you might need them.
There are still some issues with proper support of things like bool()
and int(), which refuse to return things that are not of the correct
type.
Get it:
PyPi: http://pypi.python.org/pypi/elementwise/0.111220
GitHub: https://github.com/nathan-rice/Elementwise
This was developed as a proof of concept for expanding the role of
element-wise syntax in python, and to that end I welcome comments.
Nathan Rice
More information about the Python-list
mailing list