new.instancemethod __iter__
Steve Holden
steve at holdenweb.com
Sun Feb 7 07:49:13 EST 2010
Martin Drautzburg wrote:
> Steven D'Aprano wrote:
>
>> If you want iterator operations "similar to itertools", why does this
>> mean you need to replace anything? Just create your own iterators.
>>
>> Or use pre-processing and post-processing to get what you want.
>>
>> Can you show an example of what you would like to happen?
>
> Steven,
>
> my classes repesent musical objects. The fundamental paradigm I want to
> apply is that of a Sequence, i.e. the most abstract aspect of music is
> that "things" occur in a certain order.
>
> Then I have a TimedSequence class, which is a Sequences whose elements
> have a "time" attribute. I now want to be able to append such Sequences
> by writing
>
> s1 = TimedSequence (time=1,'a') # one-element Sequence
> s2 = TimedSequence (time=2,'b')
>
> y = s1*2 + s2
>
> Naively appending those sequences would give me
> Time=1,'a'
> Time=1,'a'
> Time=2,'b'
>
> but this is not what I want. Time needs to progress if I append a
> sequence to another. So what I really want is something like
>
> Time=1,'a'
> Time=2,'a'
> Time=3,'b'
>
> This implies that time is shifted to the next integer, but this is not
> always the case. I need to know about some kind of "alignment". In
> music this translates to "let a sequence start at the beginning of a
> bar", or half bar or quarter note or whatever.
>
> So I want to write
>
> y = s1*2 + s2(align=10)
>
> which should iterate as
>
> Time=1,'a'
> Time=2,'a'
> Time=10,'b'
>
> I have no difficulty passing "align" to the object (using __call__) and
> use it while I furnish my own __iter__() method. However I don't quite
> see how I can do this with bare itertools, though I may be wrong here.
>
> Bare in mind that it is not only about somehow getting the job done. The
> beauty of the resulting syntax is also important.
>
In that case why not just assume that the timing of a sequence is
relative to the current time unless the "align" argument is given?
You might also need an event of zero duration to set the start time for
a sequence.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
More information about the Python-list
mailing list