Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python.

Ian Kelly ian.g.kelly at gmail.com
Tue Dec 20 19:47:47 EST 2011


On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice
<nathan.alexander.rice at gmail.com> wrote:
> 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.

A couple of notes on the code:

    @property
    def iterable(self):
        iterable = object.__getattribute__(self, "_iterable")
        if isinstance(iterable, types.GeneratorType):
            # Should probably look into a better way to do this
            return itertools.tee(iterable, 1)[0]
        else:
            return iterable

I don't think tee is going to do what you think it does here.  The
semantics of tee are that once you tee off an iterable, you're no
longer supposed to use the original iterable.
"itertools.tee(iterable, 1)[0]" is more or less equivalent to
"iter(iterable)".  A more correct approach might be:

    @property
    def iterable(self):
        iterable = object.__getattribute__(self, "_iterable")
        if isinstance(iterable, collections.Iterator):
            iterator, self.iterable = itertools.tee(iterable, 2)
            return iterator
        else:
            return iterable

That also checks for iterators specifically rather than just
generators.  Alternatively, you could just check for a "__next__" or
"next" method, depending on your Python version, if you don't want to
introduce a dependency on Python 2.6.

----------

    def __str__(self):
        return ", ".join(e.__str__() for e in
object.__getattribute__(self, "iterable"))

    ...

    def __unicode__(self):
        return object.__unicode__(str(self))

So "unicode(efoo)" will first construct a str from each object in the
iterable, and then attempt to decode that str using the default ASCII
codec, which may or may not be correct.  The attempt at code reuse is
admirable, but really __unicode__ should not rely on __str__ if the
encoding is unknown.  This ought to be:

    def __unicode__(self):
        return u", ".join(unicode(e) for e in
object.__getattribute__(self, "iterable"))

If the objects in question don't have __unicode__ methods, then Python
will fall back on __str__ and the default encoding for you.

That's all that jumps out at me.  Also, are you aware that your README
is empty?  Although maybe that's just some weird github thing.

Cheers,
Ian



More information about the Python-list mailing list