[Python-ideas] Vectorization [was Re: Add list.join() please]

David Mertz mertz at gnosis.cx
Sat Feb 2 22:02:37 EST 2019


Trying to make iterators behave in a semi-nice way also. I kinda like this
(example remains silly, but it shows idea).

>>> for n, mon in enumerate(vi.upper().replace('J','_').title()):
...     print(mon)
...     if n>3: break
...
...
_An
Feb
Mar
Apr
May
>>> vi
<Vector of <list_iterator object at 0x104bd7b70>>
>>> list(vi)
['Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
>>> vi
<Vector of <list_iterator object at 0x104bd7b70>>
>>> list(vi)
[]


On Sat, Feb 2, 2019 at 9:03 PM David Mertz <mertz at gnosis.cx> wrote:

> Slightly more on my initial behavior:
>
> >>> Vector({1:2,3:4})
> TypeError: Ambiguity vectorizing a map, perhaps try it.keys(),
> it.values(), or it.items()
>
> >>> Vector(37)
> TypeError: Vector can only be initialized with an iterable
>
> >>> Vector("hello")
> <Vector of 'hello'>
>
>
> I'm wondering if maybe making a vector out of a scalar should simply be a
> length-one vector. What do you think?
>
> Also, should a single string be treated like a vector of characters or
> like a scalar? It feels kinda pointless to make a vector of characters
> since I cannot think of anything it would do better than a plain string
> already does (largely just the same thing slower).
>
> On Sat, Feb 2, 2019 at 8:54 PM David Mertz <mertz at gnosis.cx> wrote:
>
>> Here is a very toy proof-of-concept:
>>
>> >>> from vector import Vector
>> >>> l = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()
>> >>> v = Vector(l)
>> >>> v
>> <Vector of ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
>> 'Sep', 'Oct', 'Nov', 'Dec']>
>> >>> v.strip().lower().replace('a','X')
>> <Vector of ['jXn', 'feb', 'mXr', 'Xpr', 'mXy', 'jun', 'jul', 'Xug',
>> 'sep', 'oct', 'nov', 'dec']>
>> >>> vt = Vector(tuple(l))
>> >>> vt
>> <Vector of ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
>> 'Sep', 'Oct', 'Nov', 'Dec')>
>> >>> vt.lower().replace('o','X')
>> <Vector of ('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
>> 'sep', 'Xct', 'nXv', 'dec')>
>>
>>
>> My few lines are at https://github.com/DavidMertz/stringpy
>>
>> One thing I think I'd like to be different is to have some way of
>> accessing EITHER the collection being held OR each element.  So now I just
>> get:
>>
>> >>> v.__len__()
>> <Vector of [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]>
>>
>>
>> Yes, that's an ugly spelling of `len(v)`, but let's bracket that for the
>> moment.  It would be nice also to be able to ask "what's the length of the
>> vector, in a non-vectorized way" (i.e. 12 in this case).  Maybe some naming
>> convention like:
>>
>> >>> v.collection__len__()
>> 12
>>
>>
>> This last is just a possible behavior, not in the code I just uploaded.
>>
>>
>> On Sat, Feb 2, 2019 at 6:47 PM Chris Angelico <rosuav at gmail.com> wrote:
>>
>>> On Sun, Feb 3, 2019 at 10:36 AM Ben Rudiak-Gould <benrudiak at gmail.com>
>>> wrote:
>>> >
>>> > On Sat, Feb 2, 2019 at 3:23 PM Christopher Barker <pythonchb at gmail.com>
>>> wrote:
>>> >>
>>> >> a_list_of_strings.strip().lower().title()
>>> >>
>>> >> is a lot nicer than:
>>> >>
>>> >> [s.title() for s in (s.lower() for s in [s.strip(s) for s in
>>> a_list_of_strings])]
>>> >>
>>> >> or
>>> >>
>>> >> list(map(str.title, (map(str.lower, (map(str.strip,
>>> a_list_of_strings)))) # untested
>>> >
>>> > In this case you can write
>>> >
>>> >     [s.strip().lower().title() for s in a_list_of_strings]
>>>
>>> What if it's a more complicated example?
>>>
>>> len(sorted(a_list_of_strings.casefold())[:100])
>>>
>>> where the len() is supposed to give back a list of the lengths of the
>>> first hundred strings, sorted case insensitively? (Okay so it's a
>>> horrible contrived example. Bear with me.)
>>>
>>> With current syntax, this would need multiple map calls or
>>> comprehensions:
>>>
>>> [len(s) for s in sorted(s.casefold() for s in a_list_of_strings)[:100]]
>>>
>>> (Better examples welcomed.)
>>>
>>> ChrisA
>>> _______________________________________________
>>> Python-ideas mailing list
>>> Python-ideas at python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>
>>
>> --
>> Keeping medicines from the bloodstreams of the sick; food
>> from the bellies of the hungry; books from the hands of the
>> uneducated; technology from the underdeveloped; and putting
>> advocates of freedom in prisons.  Intellectual property is
>> to the 21st century what the slave trade was to the 16th.
>>
>
>
> --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190202/32f76297/attachment-0001.html>


More information about the Python-ideas mailing list