
    3j[(                   (   d Z ddlmZ ddlmZmZmZ ddlZddlZ	ddlm
Z
 ddlZddlZddlmZ ddlmZmZ ddlmZ dd	lmZmZmZmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ e	j@                  rddlm!Z"  e	jF                  d      Z$ e	jF                  d      Z% e	jF                  dd      Z&e	jN                  ee	jP                  e	jR                  e	jP                     ge	jP                  f   ejT                  f   Z+ G d de,      Z- G d de	j\                  d      Z/ G d dej`                  ee         Z1 G d de1ee2e            Z3 G d de1e   ee         Z4 G d  d!ejj                        Z6e1e4e3gZ7e8d"k(  rddlZ ejr                  e6       yy)#z
this class contains iterators and filters for walking through streams

StreamIterators are explicitly allowed to access private methods on streams.
    )annotations)CallableIterableSequenceN)overload)common)tempAttributesaveAttributes)OffsetSpecial)
M21ObjType
OffsetQLIn
StreamTypeChangedM21ObjType)note)filters)prebase)base)SitesExceptionstreamTSStreamIteratorTypeStreamIterator)boundc                      e Zd Zy) StreamIteratorInefficientWarningN)__name__
__module____qualname__     D/DATA/.local/lib/python3.12/site-packages/music21/stream/iterator.pyr   r   1   s    r"   r   c                  @    e Zd ZU ded<   ded<   ded<   ded<   ded	<   y
)ActiveInformationzstreamModule.Stream | Noner   intelementIndex&t.Literal['_elements', '_endElements']iterSectionsectionIndexzbase.Music21Object | NonelastYieldedN)r   r   r    __annotations__r!   r"   r#   r%   r%   5   s     $$77((r"   r%   F)totalc                     e Zd ZdZddddd	 	 	 	 	 	 	 	 	 d5dZd Zd6dZd7d	Zd
 Ze	d8d       Z
e	d9d       Z
e	d:d       Z
d;dZ
d<dZd=dZd Zd Zd6dZd>dZd>dZd?dZd?dZd?dZd?dZdd	 	 	 d@dZdAdZdBdZe	dCd       Ze	dDdEd       Z	 dD	 	 	 dFd ZedGd!       Zdd"	 d6d#Zdd"	 d6d$ZdHd%Ze	dd"	 	 	 	 	 dId&       Ze	dd"	 	 	 	 	 dJd'       Ze	dd"	 	 	 	 	 dKd(       Ze	dd"	 	 	 	 	 dLd)       Zdd"	 	 	 	 	 dMd*Zdd"dNd+Z dd"d,Z!dd"d-Z"	 dOddddddd.	 d6d/Z#ed0        Z$ed1        Z%ed2        Z&ed3        Z'ed4        Z(y)Pr   a1
  
    An Iterator object used to handle getting items from Streams.
    The :meth:`~music21.stream.Stream.__iter__` method
    returns this object, passing a reference to self.

    Note that this iterator automatically sets the active site of
    returned elements to the source Stream.

    There is one property to know about: .overrideDerivation which overrides the set
    derivation of the class when .stream() is called

    Sets:

    * StreamIterator.srcStream -- the Stream iterated over
    * StreamIterator.elementIndex -- current index item
    * StreamIterator.streamLength -- length of elements.

    * StreamIterator.srcStreamElements -- srcStream._elements
    * StreamIterator.activeInformation -- a dict that contains information
      about where we are in the parse.  Especially useful for recursive
      streams:

          * `stream` = the stream that is currently active,
          * `elementIndex` = where in `.elements` we are,
          * `iterSection` is `_elements` or `_endElements`,
          * `sectionIndex` is where we are in the iterSection, or -1 if
            we have not started.
          * `lastYielded` the element that was last returned by the iterator.
            (for OffsetIterators, contains the first element last returned)
          * (This dict is shared among all sub iterators.)

    Constructor keyword-only arguments:

    * `filterList` is a list of stream.filters.Filter objects to apply

    * if `restoreActiveSites` is True (default), then on iterating, the activeSite is set
      to the Stream being iterated over.

    * if `ignoreSorting` is True (default is False) then the Stream is not sorted before
      iterating.  If the Stream is already sorted, then this value does not matter, and
      no time will be saved by setting to False.

    * For `activeInformation` see above.

    * Changed in v5.2: all arguments except srcStream are keyword only.
    * Changed in v8:
      - filterList must be a list or None, not a single filter.
      - StreamIterator inherits from typing.Sequence, hence index
      was moved to elementIndex.
    * Changed in v9: cleanupOnStop is deprecated.  Was not working properly before: noone noticed.
    * Changed in v10: remove cleanupOnStop.

    OMIT_FROM_DOCS

    Informative exception for user error:

    >>> s = stream.Stream()
    >>> sIter = stream.iterator.StreamIterator(s, filterList=[note.Note])
    Traceback (most recent call last):
    TypeError: filterList expects Filters or callables,
    not types themselves; got <class 'music21.note.Note'>

    THIS IS IN OMIT -- Add info above.
    NTF
filterListrestoreActiveSitesactiveInformationignoreSortingc               N   |s*|j                   du r|j                  r|j                          || _        d| _        t        j                  t        t        df   |j                        | _
        t        | j                        | _        t        | j                  j                        | _        d| _        d| _        || _        d | _        |g }|D ]   }t'        |t(              st+        d|        || _        d | _        i | _        ||| _        y i | _        | j5                          y )NFr   .	_elementszCfilterList expects Filters or callables, not types themselves; got )isSortedautoSortsort	srcStreamr'   tcasttupler   elementssrcStreamElementslenstreamLengthr6   elementsLengthr*   r)   r1   overrideDerivation
isinstancetype	TypeErrorr   _len_matchingElementsr2   updateActiveInformation)selfr:   r0   r1   r2   r3   xs          r#   __init__zStreamIterator.__init__   s    !3!3u!<ASASNN%.!" "#j#o(>	@R@R!S!$T%;%;!< $'t~~'?'?#@ "$CN(:,0JA!T"YZ[Y\]_ _  *4"	DF (8ID"%'D"((*r"   c                N   | j                   j                  j                  }| j                   j                  }t	        |t
              rt        |      }|dk(  r;| j                   j                  dk7  r"dt        | j                   j                        z   }d| d| d| j                   S )NMeasurer   zm.zfor :z @:)
r:   	__class__r   idrD   r&   hexnumberstrr'   )rJ   streamClasssrcStreamIds      r#   _reprInternalzStreamIterator._reprInternal   s    nn..77nn''k3'k*K)#(=(=(BT^^%:%:!;;Kk]!K=D4E4E3FGGr"   c                &    | j                          | S N)resetrJ   s    r#   __iter__zStreamIterator.__iter__   s    

r"   c                l   | j                   | j                  k  r| j                   | j                  k\  r&d| _        | j                   | j                  z
  | _        n| j                   | _        	 | j
                  | j                      }| xj                   dz  c_         | j                  |      du r| j                  du r| j                  j                  |       | j                          || j                  d<   |S | j                          t               # t        $ r | xj                   dz  c_         Y 1w xY w)N_endElements   FTr+   )r'   rA   rB   r)   r*   r?   
IndexErrormatchesFiltersr1   r:   coreSelfActiveSiterI   r2   cleanupStopIteration)rJ   es     r#   __next__zStreamIterator.__next__   s   $"3"33  D$7$77#1 $($5$58K8K$K!$($5$5!**4+<+<= """1%.&&$.11!4((*45D""=1Ho#  !!Q&!s   +D D32D3c                    |dk(  ryt        | j                  |      s$t        | j                  j                  d|      t        j                  |dz   t        d       | j                         }t        ||      S )a  
        DEPRECATED in v8 -- will be removed in v9.

        In case an attribute is defined on Stream but not on a StreamIterator,
        create a Stream and then return that attribute.  This is NOT performance
        optimized -- calling this repeatedly will mean creating a lot of different
        streams.  However, it will prevent most code that worked on v2. from breaking
        on v3 and onwards.

        Deprecated in v8. The upgrade path is to just call `.stream()` on the iterator
        before accessing the attribute.

        >>> s = stream.Measure()
        >>> s.insert(0, note.Rest())
        >>> s.repeatAppend(note.Note('C'), 2)

        >>> s.definesExplicitSystemBreaks
        False

        >>> s.notes
        <music21.stream.iterator.StreamIterator for Measure:0x101c1a208 @:0>

        >>> import warnings  #_DOCS_HIDE
        >>> with warnings.catch_warnings(): #_DOCS_HIDE
        ...      warnings.simplefilter('ignore') #_DOCS_HIDE
        ...      explicit = s.notes.definesExplicitSystemBreaks #_DOCS_HIDE
        >>> #_DOCS_SHOW explicit = s.notes.definesExplicitSystemBreaks
        >>> explicit
        False

        Works with methods as well:

        >>> with warnings.catch_warnings(): #_DOCS_HIDE
        ...      warnings.simplefilter('ignore') #_DOCS_HIDE
        ...      popC = s.notes.pop(0) #_DOCS_HIDE
        >>> #_DOCS_SHOW popC = s.notes.pop(0)
        >>> popC
        <music21.note.Note C>

        But remember that a new Stream is being created each time that an attribute
        only defined on a Stream is called, so for instance, so you can pop() forever,
        always getting the same element.

        >>> with warnings.catch_warnings(): #_DOCS_HIDE
        ...      warnings.simplefilter('ignore') #_DOCS_HIDE
        ...      popC = s.notes.pop(0) #_DOCS_HIDE
        >>> #_DOCS_SHOW popC = s.notes.pop(0)
        >>> popC
        <music21.note.Note C>
        >>> with warnings.catch_warnings(): #_DOCS_HIDE
        ...      warnings.simplefilter('ignore') #_DOCS_HIDE
        ...      popC = s.notes.pop(0) #_DOCS_HIDE
        >>> #_DOCS_SHOW popC = s.notes.pop(0)
        >>> popC
        <music21.note.Note C>
        >>> with warnings.catch_warnings(): #_DOCS_HIDE
        ...      warnings.simplefilter('ignore') #_DOCS_HIDE
        ...      popC = s.notes.pop(0) #_DOCS_HIDE
        >>> #_DOCS_SHOW popC = s.notes.pop(0)
        >>> popC
        <music21.note.Note C>

        If run with -w, this call will send a StreamIteratorInefficientWarning to stderr
        reminding developers that this is not an efficient call, and .stream() should be
        called (and probably cached) explicitly.

        Failures are explicitly given as coming from the StreamIterator object.

        >>> s.asdf
        Traceback (most recent call last):
        AttributeError: 'Measure' object has no attribute 'asdf'
        >>> s.notes.asdf
        Traceback (most recent call last):
        AttributeError: 'StreamIterator' object has no attribute 'asdf'

        OMIT_FROM_DOCS

        srcStream is accessible, but not with "__getattr__", which joblib uses

        >>> s.notes.srcStream is s
        True
        >>> s.notes.__getattr__('srcStream') is None
        True
        r:   Nz object has no attribute zG is not defined on StreamIterators. Call .stream() first for efficiency   )
stacklevel)
hasattrr:   AttributeErrorrP   r   warningswarnr   r   getattr)rJ   attrsOuts      r#   __getattr__zStreamIterator.__getattr__   sw    n ;t~~t, DNN$;$;#>>WX\W_!`aa\\,	
 {{}tT""r"   c                     y rY   r!   rJ   ks     r#   __getitem__zStreamIterator.__getitem__B      r"   c                     y rY   r!   rs   s     r#   ru   zStreamIterator.__getitem__F  rv   r"   c                     y rY   r!   rs   s     r#   ru   zStreamIterator.__getitem__J  rv   r"   c                4   | j                         }t        |t              rr|j                  d      r|dd }|D ]L  }t        |j                  t              s|j                  j                         |j                         k(  sJ|c S  t        |      ||   }|S )a  
        Iterators can request other items by index or slice.

        >>> s = stream.Stream()
        >>> s.insert(0, note.Note('F#'))
        >>> s.repeatAppend(note.Note('C'), 2)
        >>> sI = s.iter()
        >>> sI
        <music21.stream.iterator.StreamIterator for Stream:0x104743be0 @:0>

        >>> sI.srcStream is s
        True

        >>> for n in sI:
        ...    printer = (repr(n), repr(sI[0]))
        ...    print(printer)
        ('<music21.note.Note F#>', '<music21.note.Note F#>')
        ('<music21.note.Note C>', '<music21.note.Note F#>')
        ('<music21.note.Note C>', '<music21.note.Note F#>')
        >>> sI.srcStream is s
        True

        To request an element by id, put a '#' sign in front of the id,
        like in HTML DOM queries:

        >>> bach = corpus.parse('bwv66.6')
        >>> soprano = bach.recurse()['#Soprano']
        >>> soprano
        <music21.stream.Part Soprano>

        This behavior is often used to get an element from the Parts iterator:

        >>> bach.parts['#soprano']  # notice: case-insensitive retrieval
        <music21.stream.Part Soprano>

        Slices work:

        >>> nSlice = sI[1:]
        >>> for n in nSlice:
        ...     print(n)
        <music21.note.Note C>
        <music21.note.Note C>

        Filters, such as "notes" apply.

        >>> s.insert(0, clef.TrebleClef())
        >>> s[0]
        <music21.clef.TrebleClef>
        >>> s.iter().notes[0]
        <music21.note.Note F#>

        * Changed in v8: for strings: prepend a '#' sign to get elements by id.
          The old behavior still works until v9.
          This is an attempt to unify __getitem__ behavior in
          StreamIterators and Streams.
        #r_   N)matchingElementsrD   rT   
startswithrQ   lowerKeyError)rJ   rt   feelre   s        r#   ru   zStreamIterator.__getitem__N  s    r ""$a||C abEbeeS)beekkmqwwy.HI  1+qEr"   c                    | j                   | j                   S t        | j                  d            }|| _         | j                          |S )am  
        returns the length of the elements that
        match the filter set.

        >>> s = converter.parse('tinynotation: 3/4 c4 d e f g a', makeNotation=False)
        >>> len(s)
        7
        >>> len(s.iter())
        7
        >>> len(s.iter().notes)
        6
        >>> [n.name for n in s.iter().notes]
        ['C', 'D', 'E', 'F', 'G', 'A']
        Fr1   )rG   r@   r{   rZ   )rJ   lenMatchings     r#   __len__zStreamIterator.__len__  sF     99 99$//5/IJ	

r"   c                    | j                   t        | j                         S t        | dd      5  | D ]  } ddd       y 	 ddd       y# 1 sw Y   yxY w)a  
        return True if anything matches the filter
        otherwise, return False

        >>> s = converter.parse('tinyNotation: 2/4 c4 r4')
        >>> bool(s)
        True
        >>> iterator = s.recurse()
        >>> bool(iterator)
        True
        >>> bool(iterator.notesAndRests)
        True
        >>> bool(iterator.notes)
        True

        test cache

        >>> len(iterator.notes)
        1
        >>> bool(iterator.notes)
        True
        >>> bool(iterator.notes)
        True

        >>> iterator = s.recurse()
        >>> bool(iterator)
        True
        >>> bool(iterator)
        True
        >>> bool(iterator)
        True

        >>> bool(iterator.getElementsByClass(chord.Chord))
        False

        test false cache:

        >>> len(iterator.getElementsByClass(chord.Chord))
        0
        >>> bool(iterator.getElementsByClass(chord.Chord))
        False

        Nr1   FT)rG   boolr	   )rJ   _s     r#   __bool__zStreamIterator.__bool__  sT    X 99 		?" 4!5u= >= > 	 > s   A
A

Ac                *    || j                  d      v S )zQ
        Does the iterator contain `item`?  Needed for AbstractBaseClass
        Fr   r{   )rJ   items     r#   __contains__zStreamIterator.__contains__  s     t,,,FFFr"   c              #  d   K   | j                         }|j                          |E d {    y 7 wrY   )r{   reverse)rJ   mes     r#   __reversed__zStreamIterator.__reversed__  s$     ""$


s   &0.0c                     t        |       | j                  t        j                  | j                        | j                  t        j                  | j
                              }|S )z{
        Returns a new copy of the same iterator.
        (a shallow copy of some things except activeInformation)
        )r0   r1   r2   )rE   r:   copyr   r1   r2   )rJ   outs     r#   clonezStreamIterator.clone  sM    
 !d4jNNyy.#66"ii(>(>?	
 
r"   c                N    t        |        	 t        |       S # t        $ r Y yw xY w)a  
        Efficiently return the first matching element, or None if no
        elements match.

        Does not require creating the whole list of matching elements.

        >>> s = converter.parse('tinyNotation: 3/4 D4 E2 F4 r2 G2 r4')
        >>> s.recurse().notes.first()
        <music21.note.Note D>
        >>> s[note.Rest].first()
        <music21.note.Rest half>

        If no elements match, returns None:

        >>> print(s[chord.Chord].first())
        None

        * New in v7.

        OMIT_FROM_DOCS

        Ensure that next continues after the first note running:

        >>> notes = s.recurse().notes
        >>> notes.first()
        <music21.note.Note D>
        >>> next(notes)
        <music21.note.Note E>

        Now reset on new iteration:

        >>> for n in notes:
        ...     print(n)
        <music21.note.Note D>
        <music21.note.Note E>
        ...

        An Empty stream:

        >>> s = stream.Stream()
        >>> s.iter().notes.first() is None
        True
        N)iternextrd   r[   s    r#   firstzStreamIterator.first  s-    X 	T
	: 		s   
 	$$c                2    | j                         }|sy|d   S )a  
        Returns the last matching element, or None if no elements match.

        Currently is not efficient (does not iterate backwards, for instance),
        but easier than checking for an IndexError.  Might be refactored later
        to iterate the stream backwards instead if it gets a lot of use.

        >>> s = converter.parse('tinyNotation: 3/4 D4 E2 F4 r2 G2 r4')
        >>> s.recurse().notes.last()
        <music21.note.Note G>
        >>> s[note.Rest].last()
        <music21.note.Rest quarter>

        * New in v7.

        OMIT_FROM_DOCS

        Check on empty Stream:

        >>> s2 = stream.Stream()
        >>> s2.iter().notes.last() is None
        True

        Next has a different feature from first(), will start again from beginning.
        This behavior may change.

        >>> notes = s.recurse().notes
        >>> notes.last()
        <music21.note.Note G>
        >>> next(notes)
        <music21.note.Note D>
        Nr5   r   )rJ   r   s     r#   lastzStreamIterator.last+  s"    B ""$"vr"   c                    | j                   }| j                  |d<   | j                  dz
  |d<   | j                  |d<   | j                  |d<   d|d<   y)z
        Updates the (shared) activeInformation dictionary
        with information about where we are.

        Call before any element return.
        r   r_   r'   r)   r*   Nr+   )r2   r:   r'   r)   r*   )rJ   ais     r#   rI   z&StreamIterator.updateActiveInformationS  s[     ##~~8!..2> ,,=!..> =r"   c                    d| _         d| _        | j                          d| j                  d<   | j                  D ]-  }t        |t        j                        s|j                          / y)*
        reset prior to iteration
        r   r6   Nr+   )r'   r)   rI   r2   r   rD   StreamFilterrZ   )rJ   fs     r#   rZ   zStreamIterator.reseta  sW     &$$&04}-A!W112	 r"   c                     d| _         i | _        y)z
        reset any cached data. -- do not use this at
        the start of iteration since we might as well
        save this information. But do call it if
        the filter changes.
        N)rG   rH   r[   s    r#   resetCacheszStreamIterator.resetCachesm  s     	!#r"   c                     y)z[
        stop iteration; and cleanup if need be.  Can be subclassed. does nothing.
        Nr!   r[   s    r#   rc   zStreamIterator.cleanupw  s     	r"   r   c                  || j                   v r| j                   |   S || j                  }t        | dd      5  || _        | D cg c]  }| }}| j                          ddd       | j                   |<   |S c c}w # 1 sw Y   xY w)a
  
        Returns a list of elements that match the filter.

        This sort of defeats the point of using a generator, so only used if
        it's requested by __len__ or __getitem__ etc.

        Subclasses should override to cache anything they need saved (index,
        recursion objects, etc.)

        activeSite will not be set.

        Cached for speed.

        >>> s = converter.parse('tinynotation: 3/4 c4 d e f g a', makeNotation=False)
        >>> s.id = 'tn3/4'
        >>> sI = s.iter()
        >>> sI
        <music21.stream.iterator.StreamIterator for Part:tn3/4 @:0>

        >>> sI.matchingElements()
        [<music21.meter.TimeSignature 3/4>, <music21.note.Note C>, <music21.note.Note D>,
         <music21.note.Note E>, <music21.note.Note F>, <music21.note.Note G>,
         <music21.note.Note A>]

        >>> sI_notes = sI.notes
        >>> sI_notes
        <music21.stream.iterator.StreamIterator for Part:tn3/4 @:0>

        Adding a filter to the Stream iterator returns a new Stream iterator; it
        does not change the original.

        >>> sI_notes is sI
        False

        >>> sI.filters
        []

        >>> sI_notes.filters
        [<music21.stream.filters.ClassFilter <class 'music21.note.NotRest'>>]

        >>> sI_notes.matchingElements()
        [<music21.note.Note C>, <music21.note.Note D>,
         <music21.note.Note E>, <music21.note.Note F>, <music21.note.Note G>,
         <music21.note.Note A>]

        If restoreActiveSites is False then the elements will not have
        their activeSites changed (callers should use it when they do not plan to actually
        expose the elements to users, such as in `__len__`).  By default, it is `None`
        which means to take from the iterator's `restoreActiveSites` attribute.

        A demonstration of restoreActiveSites = False.  First we create a second stream
        from the first, so that all elements are in two streams, then we'll check the
        id of iterating through the second stream normally, and then the first stream
        with restoreActiveSites=False, and then the first stream without the restoreActiveSites:

        >>> s2 = stream.Part()
        >>> s2.id = 'second'
        >>> s2.elements = s
        >>> {e.activeSite.id for e in s2.iter().notes.matchingElements()}
        {'second'}
        >>> {e.activeSite.id for e in sI_notes.matchingElements(restoreActiveSites=False)}
        {'second'}
        >>> {e.activeSite.id for e in sI_notes.matchingElements()}
        {'tn3/4'}

        * New in v7: restoreActiveSites.
        * Changed in v9.3: restoreActiveSites allows `None` which takes from the iterator's
          `restoreActiveSites` attribute.
        Nr1   r'   )rH   r1   r
   rZ   )rJ   r1   rK   r   s       r#   r{   zStreamIterator.matchingElements  s    T !7!77))*<==%!%!8!8D"6G&8D#!"T!TB"JJL	 H 6812	 # HGs   A>	A9A>9A>>Bc                    | j                   D ]  }	 	  |||       du r y	  y# t        $ r< t        j                  rt	        |t         j
                        sJ  ||      du rY  yY Gw xY w# t        $ r  w xY w)zL
        returns False if any filter returns False, True otherwise.
        FT)r   rF   r;   TYPE_CHECKINGrD   r   rd   )rJ   re   r   s      r#   ra   zStreamIterator.matchesFilters  s    
 A
%DzU*$ +   ! %)!W-A-ABBBtu}$ %%
 ! s,   #A+?A("A+%A+'A((A++A6c                .    ddl m} |j                         S )a  
        Returns a new stream.Stream.  The same thing as calling:

        >>> s = stream.Stream()

        This is used in places where returnStreamSubclass is False, so we
        cannot just call `type(StreamIterator.srcStream)()`

        >>> p = stream.Part()
        >>> pi = p.iter()
        >>> s = pi._newBaseStream()
        >>> s
        <music21.stream.Stream 0x1047eb2e8>
        r   r   )music21r   StreamrJ   r   s     r#   _newBaseStreamzStreamIterator._newBaseStream  s     	#}}r"   c                     y rY   r!   )rJ   returnStreamSubClasss     r#   r   zStreamIterator.stream   rv   r"   c                    | j                   }|S rY   )	streamObj)rJ   r   rK   s      r#   r   zStreamIterator.stream  s     r"   c                   | j                   }d}|r	 |j                         }n| j                         }|j	                  |       ||j
                  _        | j                  | j                  |j
                  _        nwg }| j                  D ]F  }t        |t        j                        r|j                  }n|j                  }|j                  |       H dj                  |      |j
                  _        | j!                         }|D ]q  }		 |j#                  |	d      }
t        |
t(              s|j+                  |
|	d       <|
t,        j.                  k(  r|j1                  |	       a|j1                  |	       s |r|j3                  |       |S # t        $ r | j                         }Y yw xY w# t$        $ r |	j'                  |      }
d}Y w xY w)a  
        return a new stream from this iterator.

        Does nothing except copy if there are no filters, but a drop in
        replacement for the old .getElementsByClass() etc. if it does.

        In other words:

        `s.getElementsByClass()` == `s.iter().getElementsByClass().stream()`

        >>> s = stream.Part()
        >>> s.insert(0, note.Note('C'))
        >>> s.append(note.Rest())
        >>> s.append(note.Note('D'))
        >>> b = bar.Barline()
        >>> s.storeAtEnd(b)

        >>> s2 = s.iter().getElementsByClass(note.Note).stream()
        >>> s2.show('t')
        {0.0} <music21.note.Note C>
        {2.0} <music21.note.Note D>
        >>> s2.derivation.method
        'getElementsByClass'
        >>> s2
        <music21.stream.Part ...>

        >>> s3 = s.iter().stream()
        >>> s3.show('t')
        {0.0} <music21.note.Note C>
        {1.0} <music21.note.Rest quarter>
        {2.0} <music21.note.Note D>
        {3.0} <music21.bar.Barline type=regular>

        >>> s3.elementOffset(b, returnSpecial=True)
        <OffsetSpecial.AT_END>

        >>> s4 = s.iter().getElementsByClass(bar.Barline).stream()
        >>> s4.show('t')
        {0.0} <music21.bar.Barline type=regular>

        Note that this routine can create Streams that have elements that the original
        stream did not, in the case of recursion:

        >>> bach = corpus.parse('bwv66.6')
        >>> bn = bach.flatten()[34]
        >>> bn
        <music21.note.Note E>

        >>> bn in bach
        False
        >>> bfn = bach.recurse().notes.stream()
        >>> bn in bfn
        True
        >>> bn.getOffsetBySite(bfn)
        2.0
        >>> bn.getOffsetInHierarchy(bach)
        2.0

        OMIT_FROM_DOCS

        >>> s4._endElements[0] is b
        True
        F.T)returnSpecial)
ignoreSort)clearIsSorted)r:   rP   rF   r   mergeAttributes
derivationoriginrC   methodr   rD   r   derivationStrr   appendjoinr{   elementOffsetr   getOffsetInHierarchyrT   
coreInsertr   AT_ENDcoreStoreAtEndcoreElementsChanged)rJ   r   ssr   foundderivationMethodsr   dStrr   re   os              r#   r   zStreamIterator.stream  s   F ^^ .  '')Eb!"$"".&*&=&=E# "\\a!5!56??D::D!((. " '*hh/@&AE#""$A%$$Qd$; a%  A$ 7,,,((+ ((+! $ %%M%BS  .++-.. " % **2. $	%s#   F 
F0F-,F-0GGc                N    t        | j                  d   | j                  d         S )zu
        Returns the element list (`_elements` or `_endElements`)
        for the current activeInformation.
        r   r)   )rn   r2   r[   s    r#   activeElementListz StreamIterator.activeElementList  s(     t--h79O9OP]9^__r"   returnClonec                   |r| j                         }n| }|j                          |j                  D ]  }||k(  s	|c S  |j                  j                  |       |S )aW  
        Return a new StreamIterator with an additional filter.
        Also resets caches -- so do not add filters any other way.

        If returnClone is False then adds without creating a new StreamIterator

        * Changed in v6: Encourage creating new StreamIterators: change
          default to return a new StreamIterator.
        )r   r   r   r   )rJ   	newFilterr   r   r   s        r#   	addFilterzStreamIterator.addFilter  sT     **,CCAA~
  	9%
r"   c                   |r| j                         }n| }|j                          ||j                  v r4|j                  j                  |j                  j	                  |             |S )zI
        Return a new StreamIterator where oldFilter is removed.
        )r   r   r   popindex)rJ   	oldFilterr   r   s       r#   removeFilterzStreamIterator.removeFilter  sQ     **,CC#KKOOCKK--i89
r"   c                `    | j                  t        j                  |            }|D ]  }|c S  y)a  
        Returns a single element (or None) that matches elementId.

        If chaining filters, this should be the last one, as it returns an element

        >>> s = stream.Stream(id='s1')
        >>> s.append(note.Note('C'))
        >>> r = note.Rest()
        >>> r.id = 'restId'
        >>> s.append(r)
        >>> r2 = s.recurse().getElementById('restId')
        >>> r2 is r
        True
        >>> r2.id
        'restId'
        N)r   r   IdFilter)rJ   	elementIdr   re   s       r#   getElementByIdzStreamIterator.getElementById  s/    " nnW--i89AH r"   c                    y rY   r!   rJ   classFilterListr   s      r#   getElementsByClassz!StreamIterator.getElementsByClass      
 	r"   c                    y rY   r!   r   s      r#   r   z!StreamIterator.getElementsByClass  r   r"   c                    y rY   r!   r   s      r#   r   z!StreamIterator.getElementsByClass  r   r"   c                    y rY   r!   r   s      r#   r   z!StreamIterator.getElementsByClass  r   r"   c               N    | j                  t        j                  |      |      S )a  
        Add a filter to the Iterator to remove all elements
        except those that match one
        or more classes in the `classFilterList`. A single class
        can also be used for the `classFilterList` parameter instead of a List.

        >>> s = stream.Stream(id='s1')
        >>> s.append(note.Note('C'))
        >>> r = note.Rest()
        >>> s.append(r)
        >>> s.append(note.Note('D'))
        >>> for el in s.iter().getElementsByClass(note.Rest):
        ...     print(el)
        <music21.note.Rest quarter>

        ActiveSite is restored.

        >>> s2 = stream.Stream(id='s2')
        >>> s2.insert(0, r)
        >>> r.activeSite.id
        's2'

        >>> for el in s.iter().getElementsByClass(note.Rest):
        ...     print(el.activeSite.id)
        s1

        Strings work in addition to classes, but your IDE will not know that
        `el` is a :class:`~music21.note.Rest` object.

        >>> for el in s.iter().getElementsByClass('Rest'):
        ...     print(el)
        <music21.note.Rest quarter>
        r   r   r   ClassFilterr   s      r#   r   z!StreamIterator.getElementsByClass  s$    X ~~g11/BP[~\\r"   c               6   |j                  d      r)| j                  t        j                  |dd       |      S |j                  d      r)| j                  t        j                  |dd       |      S | j                  t        j
                  |      |      S )aR  
        First implementation of a query selector, similar to CSS QuerySelectors used in
        HTML DOM:

        * A leading `#` indicates the id of an element, so '#hello' will find elements
          with `el.id=='hello'` (should only be one)
        * A leading `.` indicates the group of an element, so '.high' will find elements
          with `'high'` in el.groups.
        * Any other string is considered to be the type/class of the element.  So `Note`
          will find all Note elements.  Can be fully qualified like `music21.note.Note`
          or partially qualified like `note.Note`.

        Eventually, more complex query selectors will be implemented.  This is just a start.

        Setting up an example:

        >>> s = converter.parse('tinyNotation: 4/4 GG4 AA4 BB4 r4 C4 D4 E4 F4 r1')
        >>> s[note.Note].last().id = 'last'
        >>> for n in s[note.Note]:
        ...     if n.octave == 3:
        ...         n.groups.append('tenor')

        >>> list(s.recurse().getElementsByQuerySelector('.tenor'))
        [<music21.note.Note C>,
         <music21.note.Note D>,
         <music21.note.Note E>,
         <music21.note.Note F>]

        >>> list(s.recurse().getElementsByQuerySelector('Rest'))
        [<music21.note.Rest quarter>,
         <music21.note.Rest whole>]

        Note that unlike with stream slices, the querySelector does not do anything special
        for id searches.  `.first()` will need to be called to find the element (if any)

        >>> s.recurse().getElementsByQuerySelector('#last').first()
        <music21.note.Note F>

        * New in v7.
        rz   r_   Nr   r   )r|   r   r   r   GroupFilterr   )rJ   querySelectorr   s      r#   getElementsByQuerySelectorz)StreamIterator.getElementsByQuerySelector(  s    R ##C(>>'"2"2=3D"ES^>__##C(>>'"5"5mAB6G"HVa>bb~~g11-@k~ZZr"   c               N    | j                  t        j                  |      |      S )a  
        Adds a filter, removing all Elements that do not
        match the one or more classes in the `classFilterList`.

        In lieu of a list, a single class can be used as the `classFilterList` parameter.

        >>> a = stream.Stream()
        >>> a.repeatInsert(note.Rest(), range(10))
        >>> for x in range(4):
        ...     n = note.Note('G#')
        ...     n.offset = x * 3
        ...     a.insert(n)
        >>> found = a.iter().getElementsNotOfClass(note.Note)
        >>> len(found)
        10
        >>> found = a.iter().getElementsNotOfClass('Rest')
        >>> len(found)
        4
        >>> found = a.iter().getElementsNotOfClass(['Note', 'Rest'])
        >>> len(found)
        0

        >>> b = stream.Stream()
        >>> b.repeatInsert(note.Rest(), range(15))
        >>> a.insert(b)

        >>> found = a.recurse().getElementsNotOfClass([note.Rest, 'Stream'])
        >>> len(found)
        4
        >>> found = a.recurse().getElementsNotOfClass([note.Note, 'Stream'])
        >>> len(found)
        25
        r   )r   r   ClassNotFilterr   s      r#   getElementsNotOfClassz$StreamIterator.getElementsNotOfClassX  s$    D ~~g44_ES^~__r"   c               N    | j                  t        j                  |      |      S )a  
        >>> n1 = note.Note('C')
        >>> n1.groups.append('trombone')
        >>> n2 = note.Note('D')
        >>> n2.groups.append('trombone')
        >>> n2.groups.append('tuba')
        >>> n3 = note.Note('E')
        >>> n3.groups.append('tuba')
        >>> s1 = stream.Stream()
        >>> s1.append(n1)
        >>> s1.append(n2)
        >>> s1.append(n3)

        >>> tboneSubStream = s1.iter().getElementsByGroup('trombone')
        >>> for thisNote in tboneSubStream:
        ...     print(thisNote.name)
        C
        D
        >>> tubaSubStream = s1.iter().getElementsByGroup('tuba')
        >>> for thisNote in tubaSubStream:
        ...     print(thisNote.name)
        D
        E
        r   )r   r   r   )rJ   groupFilterListr   s      r#   getElementsByGroupz!StreamIterator.getElementsByGroup|  s#    2 ~~g11/BP[~\\r"   )includeEndBoundarymustFinishInSpanmustBeginInSpanincludeElementsThatEndAtStartstopAfterEndr   c               \    | j                  t        j                  |||||||      |      S )a$  
        Adds a filter keeping only Music21Objects that
        are found at a certain offset or within a certain
        offset time range (given the start and optional stop values).

        There are several attributes that govern how this range is
        determined:

        If `mustFinishInSpan` is True then an event that begins
        between offsetStart and offsetEnd but which ends after offsetEnd
        will not be included.  The default is False.

        For instance, a half note at offset 2.0 will be found in
        getElementsByOffset(1.5, 2.5) or getElementsByOffset(1.5, 2.5,
        mustFinishInSpan = False) but not by getElementsByOffset(1.5, 2.5,
        mustFinishInSpan = True).

        The `includeEndBoundary` option determines if an element
        begun just at the offsetEnd should be included.  For instance,
        the half note at offset 2.0 above would be found by
        getElementsByOffset(0, 2.0) or by getElementsByOffset(0, 2.0,
        includeEndBoundary = True) but not by getElementsByOffset(0, 2.0,
        includeEndBoundary = False).

        Setting includeEndBoundary to False at the same time as
        mustFinishInSpan is set to True is probably NOT what you want to do
        unless you want to find things like clefs at the end of the region
        to display as courtesy clefs.

        The `mustBeginInSpan` option determines whether notes or other
        objects that do not begin in the region but are still sounding
        at the beginning of the region are excluded.  The default is
        True -- that is, these notes will not be included.
        For instance the half note at offset 2.0 from above would not be found by
        getElementsByOffset(3.0, 3.5) or getElementsByOffset(3.0, 3.5,
        mustBeginInSpan = True) but it would be found by
        getElementsByOffset(3.0, 3.5, mustBeginInSpan = False)

        Setting includeElementsThatEndAtStart to False is useful for zeroLength
        searches that set mustBeginInSpan == False to not catch notes that were
        playing before the search but that end just before the end of the search type.
        See the code for allPlayingWhileSounding for a demonstration.

        This chart, like the examples below, demonstrates the various
        features of getElementsByOffset.  It is one of the most complex
        methods of music21 but also one of the most powerful, so it
        is worth learning at least the basics.

            .. image:: images/getElementsByOffset.*
                :width: 600

        >>> st1 = stream.Stream()
        >>> n0 = note.Note('C')
        >>> n0.duration.type = 'half'
        >>> n0.offset = 0
        >>> st1.insert(n0)
        >>> n2 = note.Note('D')
        >>> n2.duration.type = 'half'
        >>> n2.offset = 2
        >>> st1.insert(n2)
        >>> out1 = list(st1.iter().getElementsByOffset(2))
        >>> len(out1)
        1
        >>> out1[0].step
        'D'
        >>> out2 = list(st1.iter().getElementsByOffset(1, 3))
        >>> len(out2)
        1
        >>> out2[0].step
        'D'
        >>> out3 = list(st1.iter().getElementsByOffset(1, 3, mustFinishInSpan=True))
        >>> len(out3)
        0
        >>> out4 = list(st1.iter().getElementsByOffset(1, 2))
        >>> len(out4)
        1
        >>> out4[0].step
        'D'
        >>> out5 = list(st1.iter().getElementsByOffset(1, 2, includeEndBoundary=False))
        >>> len(out5)
        0
        >>> out6 = list(st1.iter().getElementsByOffset(1, 2, includeEndBoundary=False,
        ...                                          mustBeginInSpan=False))
        >>> len(out6)
        1
        >>> out6[0].step
        'C'
        >>> out7 = list(st1.iter().getElementsByOffset(1, 3, mustBeginInSpan=False))
        >>> len(out7)
        2
        >>> [el.step for el in out7]
        ['C', 'D']

        Note, that elements that end at the start offset are included if mustBeginInSpan is False

        >>> out8 = list(st1.iter().getElementsByOffset(2, 4, mustBeginInSpan=False))
        >>> len(out8)
        2
        >>> [el.step for el in out8]
        ['C', 'D']

        To change this behavior set includeElementsThatEndAtStart=False

        >>> out9 = list(st1.iter().getElementsByOffset(2, 4, mustBeginInSpan=False,
        ...                                          includeElementsThatEndAtStart=False))
        >>> len(out9)
        1
        >>> [el.step for el in out9]
        ['D']

        >>> a = stream.Stream(id='a')
        >>> n = note.Note('G')
        >>> n.quarterLength = 0.5
        >>> a.repeatInsert(n, list(range(8)))
        >>> b = stream.Stream(id='b')
        >>> b.repeatInsert(a, [0, 3, 6])
        >>> c = list(b.iter().getElementsByOffset(2, 6.9))
        >>> len(c)
        2
        >>> c = list(b.flatten().iter().getElementsByOffset(2, 6.9))
        >>> len(c)
        10

        Testing multiple zero-length elements with mustBeginInSpan:

        >>> c = clef.TrebleClef()
        >>> ts = meter.TimeSignature('4/4')
        >>> ks = key.KeySignature(2)
        >>> s = stream.Stream()
        >>> s.insert(0.0, c)
        >>> s.insert(0.0, ts)
        >>> s.insert(0.0, ks)
        >>> len(list(s.iter().getElementsByOffset(0.0, mustBeginInSpan=True)))
        3
        >>> len(list(s.iter().getElementsByOffset(0.0, mustBeginInSpan=False)))
        3

        On a :class:`~music21.stream.iterator.RecursiveIterator`,
        `.getElementsByOffset(0.0)`, will get everything
        at the start of the piece, which is useful:

        >>> bwv66 = corpus.parse('bwv66.6')
        >>> list(bwv66.recurse().getElementsByOffset(0.0))
        [<music21.metadata.Metadata object at 0x10a32f490>,
         <music21.stream.Part Soprano>,
         <music21.instrument.Instrument 'P1: Soprano: Instrument 1'>,
         <music21.stream.Measure 0 offset=0.0>,
         <music21.clef.TrebleClef>,
         <music21.tempo.MetronomeMark Quarter=96 (playback only)>,
         <music21.key.Key of f# minor>,
         <music21.meter.TimeSignature 4/4>,
         <music21.note.Note C#>,
         <music21.stream.Part Alto>,
         ...
         <music21.note.Note E>,
         <music21.stream.Part Tenor>,
         ...]

        However, any other offset passed to `getElementsByOffset` on a
        `RecursiveIterator` without additional arguments, is unlikely to be useful,
        because the iterator ends as soon as it encounters an element
        with an offset beyond the `offsetEnd` point.  For instance,
        calling `.getElementsByOffset(1.0).notes` on a :class:`~music21.stream.Part`,
        in bwv66.6 only gets the note that appears at offset 1.0 of a measure that begins
        or includes offset 1.0.
        (Fortunately, this piece begins with a one-beat pickup, so there is such a note):

        >>> soprano = bwv66.parts['#Soprano']  # = getElementById('Soprano')
        >>> for el in soprano.recurse().getElementsByOffset(1.0):
        ...     print(el, el.offset, el.getOffsetInHierarchy(bwv66), el.activeSite)
        <music21.stream.Measure 1 offset=1.0> 1.0 1.0 <music21.stream.Part Soprano>
        <music21.note.Note B> 1.0 2.0 <music21.stream.Measure 1 offset=1.0>

        RecursiveIterators will probably want to use
        :meth:`~music21.stream.iterator.RecursiveIterator.getElementsByOffsetInHierarchy`
        instead.  Or to get all elements with a particular local offset, such as everything
        on the third quarter note of a measure, use the `stopAfterEnd=False` keyword,
        which lets the iteration continue to search for elements even after encountering
        some within Streams whose offsets are greater than the end element.

        >>> len(soprano.recurse().getElementsByOffset(2.0, stopAfterEnd=False))
        9

        * Changed in v5.5: all arguments changing behavior are keyword only.
        * New in v6.5: `stopAfterEnd` keyword.

        OMIT_FROM_DOCS

        Same test as above, but with floats

        >>> out1 = list(st1.iter().getElementsByOffset(2.0))
        >>> len(out1)
        1
        >>> out1[0].step
        'D'
        >>> out2 = list(st1.iter().getElementsByOffset(1.0, 3.0))
        >>> len(out2)
        1
        >>> out2[0].step
        'D'
        >>> out3 = list(st1.iter().getElementsByOffset(1.0, 3.0, mustFinishInSpan=True))
        >>> len(out3)
        0
        >>> out3b = list(st1.iter().getElementsByOffset(0.0, 3.001, mustFinishInSpan=True))
        >>> len(out3b)
        1
        >>> out3b[0].step
        'C'
        >>> out3b = list(st1.iter().getElementsByOffset(1.0, 3.001, mustFinishInSpan=True,
        ...                                           mustBeginInSpan=False))
        >>> len(out3b)
        1
        >>> out3b[0].step
        'C'

        >>> out4 = list(st1.iter().getElementsByOffset(1.0, 2.0))
        >>> len(out4)
        1
        >>> out4[0].step
        'D'
        >>> out5 = list(st1.iter().getElementsByOffset(1.0, 2.0, includeEndBoundary=False))
        >>> len(out5)
        0
        >>> out6 = list(st1.iter().getElementsByOffset(1.0, 2.0, includeEndBoundary=False,
        ...                                          mustBeginInSpan=False))
        >>> len(out6)
        1
        >>> out6[0].step
        'C'
        >>> out7 = list(st1.iter().getElementsByOffset(1.0, 3.0, mustBeginInSpan=False))
        >>> len(out7)
        2
        >>> [el.step for el in out7]
        ['C', 'D']
        )r   r   r   r   r   r   )r   r   OffsetFilter)	rJ   offsetStart	offsetEndr   r   r   r   r   r   s	            r#   getElementsByOffsetz"StreamIterator.getElementsByOffset  sC    n ~~  #5!1 /.K) $  
 	
r"   c                @    | j                  t        j                        S )a  
        Returns all :class:`~music21.note.NotRest` objects

        (will sometime become simply Note and Chord objects.)

        >>> s = stream.Stream()
        >>> s.append(note.Note('C'))
        >>> s.append(note.Rest())
        >>> s.append(note.Note('D'))
        >>> for el in s.iter().notes:
        ...     print(el)
        <music21.note.Note C>
        <music21.note.Note D>
        )r   r   NotRestr[   s    r#   noteszStreamIterator.notes  s      &&t||44r"   c                @    | j                  t        j                        S )a  
        Returns all :class:`~music21.note.GeneralNote` objects, including
        Rests and Unpitched elements.

        >>> s = stream.Stream()
        >>> s.append(meter.TimeSignature('4/4'))
        >>> s.append(note.Note('C'))
        >>> s.append(note.Rest())
        >>> s.append(note.Note('D'))
        >>> for el in s.iter().notesAndRests:
        ...     print(el)
        <music21.note.Note C>
        <music21.note.Rest quarter>
        <music21.note.Note D>

        Chained filters (this makes no sense since notes is a subset of notesAndRests):

        >>> for el in s.iter().notesAndRests.notes:
        ...     print(el)
        <music21.note.Note C>
        <music21.note.Note D>
        )r   r   GeneralNoter[   s    r#   notesAndRestszStreamIterator.notesAndRests  s    0 &&t'7'788r"   c                D    ddl m} | j                  |j                        S )z5
        Adds a ClassFilter for Part objects
        r   r   )r   r   r   Partr   s     r#   partszStreamIterator.parts  s    
 	#&&v{{33r"   c                D    ddl m} | j                  |j                        S )z8
        Adds a ClassFilter for Spanner objects
        r   )spanner)r   r   r   Spanner)rJ   r   s     r#   spannerszStreamIterator.spanners  s    
 	$&&w77r"   c                D    ddl m} | j                  |j                        S )z6
        Adds a ClassFilter for Voice objects
        r   r   )r   r   r   Voicer   s     r#   voiceszStreamIterator.voices  s    
 	#&&v||44r"   )
r:   r   r0   zlist[FilterType] | Noner1   r   r2   zActiveInformation | Noner3   r   returnzt.Selfr  r   )rt   r&   r  r   )rt   slicer  list[M21ObjType])rt   rT   r  M21ObjType | None)rt   zint | slice | strr  z$M21ObjType | list[M21ObjType] | None)r  r&   )r  r   )r  r  r  None)r1   zbool | Noner  r
  )re   zbase.Music21Objectr  r   )r  streamModule.Stream)r   zt.Literal[False]r  r  )T)r   zt.Literal[True]r  r   )r   r   r  z streamModule.Stream | StreamType)r  r(   )r   rT   r  r  )r   rT   r   r   r  StreamIterator[M21ObjType])r   Iterable[str]r   r   r  r  )r   type[ChangedM21ObjType]r   r   r  z!StreamIterator[ChangedM21ObjType])r   Iterable[type]r   r   r  r  )r   Dt.Union[str, type[ChangedM21ObjType], Iterable[str], Iterable[type]]r   r   r  zFt.Union[StreamIterator[M21ObjType], StreamIterator[ChangedM21ObjType]])r   rT   rY   ))r   r   r    __doc__rL   rW   r\   rf   rq   r   ru   r   r   r   r   r   r   r   rI   rZ   r   rc   r{   ra   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r  r  r!   r"   r#   r   r   >   s]   ?H 6:,0=A',/+&/+ 3	/+
 &*/+ %;/+ !%/+b	H:d#L      EN,4lG
0d$P!
$ )-Y &Y 
	Yv&$     &*v"v 
(vp ` ` 	
 
@ 	
 
(,  04,/ )- 9S   04,9 )- 9S   04,C )- 9Z   04,: )- 9S  !,]
,] ,] 
P,]\ MQ -[` EI "`H BF ]< B

  &*B
 
B
N 5 5" 9 92 4 4 8 8 5 5r"   c                       e Zd ZdZddddd	 d fdZd fdZ fdZedd		 	 	 	 	 dd
       Zedd		 	 	 	 	 dd       Zedd		 	 	 	 	 dd       Zedd		 	 	 	 	 dd       Zdd		 	 	 	 	 ddZ xZ	S )OffsetIteratora}  
    An iterator that with each iteration returns a list of elements
    that are at the same offset (or all at end)

    >>> s = stream.Stream()
    >>> s.insert(0, note.Note('C'))
    >>> s.insert(0, note.Note('D'))
    >>> s.insert(1, note.Note('E'))
    >>> s.insert(2, note.Note('F'))
    >>> s.insert(2, note.Note('G'))
    >>> s.storeAtEnd(bar.Repeat('end'))
    >>> s.storeAtEnd(clef.TrebleClef())

    >>> oIter = stream.iterator.OffsetIterator(s)
    >>> for groupedElements in oIter:
    ...     print(groupedElements)
    [<music21.note.Note C>, <music21.note.Note D>]
    [<music21.note.Note E>]
    [<music21.note.Note F>, <music21.note.Note G>]
    [<music21.bar.Repeat direction=end>, <music21.clef.TrebleClef>]

    Does it work again?

    >>> for groupedElements2 in oIter:
    ...     print(groupedElements2)
    [<music21.note.Note C>, <music21.note.Note D>]
    [<music21.note.Note E>]
    [<music21.note.Note F>, <music21.note.Note G>]
    [<music21.bar.Repeat direction=end>, <music21.clef.TrebleClef>]

    >>> for groupedElements in oIter.notes:
    ...     print(groupedElements)
    [<music21.note.Note C>, <music21.note.Note D>]
    [<music21.note.Note E>]
    [<music21.note.Note F>, <music21.note.Note G>]

    >>> for groupedElements in stream.iterator.OffsetIterator(s).getElementsByClass(clef.Clef):
    ...     print(groupedElements)
    [<music21.clef.TrebleClef>]
    NTFr/   c               X    t         |   |||||       d| _        g | _        d | _        y )Nr/   F)superrL   raiseStopIterationNextnextToYieldnextOffsetToYield)rJ   r:   r0   r1   r2   r3   rP   s         r#   rL   zOffsetIterator.__init__  s?     	$.,>+<'4 	 	 ',#-/26r"   c                   | j                   r
t               g }	 | j                  r| j                  }| j                  }n,t        |          }| j                  j                  |      }|g}| j                  | j                  k  r~t        |          }| j                  j                  |      }||k(  r|j                  |       n#|g| _        || _        |d   | j                  d<   |S | j                  | j                  k  r}y y # t        $ r2}|r d| _         |d   | j                  d<   |cY d }~S t               |d }~ww xY w)Nr   r+   T)r  rd   r  r  r  rf   r:   r   r'   rA   r   r2   )rJ   retElementListretElOffsetretElnextElnextElOffsetparent_stoprP   s          r#   rf   zOffsetIterator.__next__!  s7   &&/!+-	7!%!1!1"44(*"nn::5A"'
 ##t'8'88)+#~~;;FC;."))&1(.xD$-9D*<J1<MD**=9)) ##t'8'88  	7.2+8Fq8I&&}5%%#o;6	7s*   CD (D 	D?D:)D?/D::D?c                L    t         |           g | _        d| _        d| _        y)z'
        runs before iteration
        NF)r  rZ   r  r  r  rJ   rP   s    r#   rZ   zOffsetIterator.resetF  s&     	!%&+#r"   r   c                    y rY   r!   r   s      r#   r   z!OffsetIterator.getElementsByClassR  r   r"   c                    y rY   r!   r   s      r#   r   z!OffsetIterator.getElementsByClassY  r   r"   c                    y rY   r!   r   s      r#   r   z!OffsetIterator.getElementsByClass`  r   r"   c                    y rY   r!   r   s      r#   r   z!OffsetIterator.getElementsByClassn  r   r"   c               N    | j                  t        j                  |      |      S )zm
        Identical to the same method in StreamIterator, but needs to be duplicated
        for now.
        r   r   r   s      r#   r   z!OffsetIterator.getElementsByClassu  s#     ~~g11/BP[~\\r"   r  )r  r
  )r   rT   r   r   r  OffsetIterator[M21ObjType])r   r  r   r   r  r+  )r   r  r   r   r  z!OffsetIterator[ChangedM21ObjType])r   r  r   r   r  r+  )r   r  r   r   r  zFt.Union[OffsetIterator[M21ObjType], OffsetIterator[ChangedM21ObjType]])
r   r   r    r  rL   rf   rZ   r   r   __classcell__rP   s   @r#   r  r    s6   'X !$(#'$7 7(#7J,  04,/ )- 9S   04,9 )- 9S   04,C )- 9Z   04,: )- 9S  04]-] )-]!K]r"   r  c                  *    e Zd ZdZddddddd	 d fdZddZ fdZdd	 fd

ZddZd Z	d Z
	 dddddd	 ddZedd	 	 	 	 	 dd       Zedd	 	 	 	 	 dd       Zedd	 	 	 	 	 dd       Zedd	 	 	 	 	 dd       Zdd	 	 	 	 	 d fdZ xZS ) RecursiveIteratora  
    One of the most powerful iterators in music21.  Generally not called
    directly, but created by being invoked on a stream with `Stream.recurse()`

    >>> b = corpus.parse('bwv66.6')
    >>> ri = stream.iterator.RecursiveIterator(b, streamsOnly=True)
    >>> for x in ri:
    ...     print(x)
    <music21.stream.Part Soprano>
    <music21.stream.Measure 0 offset=0.0>
    <music21.stream.Measure 1 offset=1.0>
    <music21.stream.Measure 2 offset=5.0>
    ...
    <music21.stream.Part Alto>
    <music21.stream.Measure 0 offset=0.0>
    ...
    <music21.stream.Part Tenor>
    ...
    <music21.stream.Part Bass>
    ...

    But this is how you'll actually use it:

    >>> for x in b.recurse(streamsOnly=True, includeSelf=True):
    ...     print(x)
    <music21.stream.Score bach/bwv66.6.mxl>
    <music21.stream.Part Soprano>
    <music21.stream.Measure 0 offset=0.0>
    <music21.stream.Measure 1 offset=1.0>
    <music21.stream.Measure 2 offset=5.0>
    ...
    <music21.stream.Part Alto>
    <music21.stream.Measure 0 offset=0.0>
    ...
    <music21.stream.Part Tenor>
    ...
    <music21.stream.Part Bass>
    ...

    >>> hasExpressions = lambda el, i: True if (hasattr(el, 'expressions')
    ...       and el.expressions) else False
    >>> expressive = b.recurse().addFilter(hasExpressions)
    >>> expressive
    <music21.stream.iterator.RecursiveIterator for Score:bach/bwv66.6.mxl @:0>

    >>> for el in expressive:
    ...     print(el, el.expressions)
    <music21.note.Note C#> [<music21.expressions.Fermata>]
    <music21.note.Note A> [<music21.expressions.Fermata>]
    <music21.note.Note F#> [<music21.expressions.Fermata>]
    <music21.note.Note C#> [<music21.expressions.Fermata>]
    <music21.note.Note G#> [<music21.expressions.Fermata>]
    <music21.note.Note F#> [<music21.expressions.Fermata>]

    >>> len(expressive)
    6
    >>> expressive[-1].measureNumber
    9
    >>> bool(expressive)
    True
    NTF)r0   r1   r2   streamsOnlyincludeSelfr3   c                   t         |   |||||       || _        || _        || _        d| _        |du r.| j                  j                  t        j                  d             d | _	        y )Nr/           Tr   )
r  rL   
returnSelfr1  r3   iteratorStartOffsetInHierarchyr   r   r   childRecursiveIterator)	rJ   r:   r0   r1   r2   r0  r1  r3   rP   s	           r#   rL   zRecursiveIterator.__init__  su     	$.,>+<'4 	 	 &&* /2+$LL 3 3H =>EI#r"   c                   | j                   | j                  k  rG| j                  	 t        | j                        S | j
                  du r}| j                  | j                        rbd| j                  d<   d| j                  d<   | j                  | j                  d<   d| _        t        j                  t        | j                        S | j
                  du rd| _        | j                   | j                  k\  r&d| _        | j                   | j                  z
  | _        n| j                   | _        	 | j                  | j                      }| xj                   d	z  c_         |j"                  rt        j$                  rt'        |t(        j*                        sJ t-        || j.                  | j0                  | j                  d| j2                  
      }| j4                  | j                  j7                  |      z   }||_        || _        | j                  |      du r| j.                  du r| j                  j9                  |       | j;                          || j                  d<   |S | j                  	 t        | j                        S d| j                  d<   | j=                          t	               # t        $ r d| _        Y w xY w# t         $ r | xj                   d	z  c_         Y w xY w# t        $ r
 d| _        Y ww xY w)z{
        Get the next element of the stream under iteration.

        The same __iter__ as the superclass is used.
        NTr   r5   r'   r+   Fr^   r_   )r:   r1   r0   r2   r1  r3   )r'   rA   r6  r   rd   r4  ra   r:   r2   r;   r<   r   rB   r)   r*   r?   r`   isStreamr   rD   streamModuler   r/  r1   r   r3   r5  r   rb   rI   rc   )rJ   re   r6  newStartOffsets       r#   rf   zRecursiveIterator.__next__  s    $"3"33 **67 ; ;<<
 $&4+>+>t~~+N37&&x09;&&~68<&&}5"'vvj$..99D("'  D$7$77#1 $($5$58K8K$K!$($5$5!**4+<+<= " zz??%a)<)<===HY'+'>'>#||&*&<&< %"&"4"4I& #'"E"E$(NN$@$@$C#D IW&E.D+""1%.&&$.11!4((*45D""=1H &&23D7788
 15}-oK % 726D/7,  !!Q&!R ! 3.2+3s5   J- .K /K) -K KK&%K&)K<;K<c                R    | j                   | _        d| _        t        |           y)r   N)r1  r4  r6  r  rZ   r%  s    r#   rZ   zRecursiveIterator.reset=  s#     **&*#r"   r   c               l    t        | d      5  t        | 	  |      }d d d        |S # 1 sw Y   S xY w)Nr6  r   )r	   r  r{   )rJ   r1   r   rP   s      r#   r{   z"RecursiveIterator.matchingElementsE  s8     4!9:)=O)PB ;	 ;	s   )3c                |    | g}| }|j                   *|j                   }|j                  |       |j                   *|S )aE  
        Returns a stack of RecursiveIterators at this point in the iteration.  Last is most recent.

        >>> b = corpus.parse('bwv66.6')
        >>> bRecurse = b.recurse()
        >>> i = 0
        >>> for _ in bRecurse:
        ...     i += 1
        ...     if i > 13:
        ...         break
        >>> bRecurse.iteratorStack()
        [<music21.stream.iterator.RecursiveIterator for Score:bach/bwv66.6.mxl @:2>,
         <music21.stream.iterator.RecursiveIterator for Part:Soprano @:3>,
         <music21.stream.iterator.RecursiveIterator for Measure:m.1 @:3>]
        )r6  r   )rJ   	iterStackrK   s      r#   iteratorStackzRecursiveIterator.iteratorStackL  sK      F	&&2((AQ &&2 r"   c                \    | j                         D cg c]  }|j                   c}S c c}w )a  
        Returns a stack of Streams at this point.  Last is most recent.

        However, the current element may be the same as the last element in the stack

        >>> b = corpus.parse('bwv66.6')
        >>> bRecurse = b.recurse()
        >>> i = 0
        >>> for x in bRecurse:
        ...     i += 1
        ...     if i > 12:
        ...         break
        >>> bRecurse.streamStack()
        [<music21.stream.Score bach/bwv66.6.mxl>,
         <music21.stream.Part Soprano>,
         <music21.stream.Measure 1 offset=1.0>]
        )r?  r:   )rJ   is     r#   streamStackzRecursiveIterator.streamStackc  s+    $ &*%7%7%9:%9%9:::s   )c                    | j                   d   }|y| j                         }|d   }|j                  }|j                  }||u rt	        j
                  |      S t	        j
                  ||j                  |      z         S )a{  
        Called on the current iterator, returns the current offset in the hierarchy.
        Or None if we are not currently iterating.

        >>> b = corpus.parse('bwv66.6')
        >>> bRecurse = b.recurse().notes
        >>> print(bRecurse.currentHierarchyOffset())
        None
        >>> for n in bRecurse:
        ...     print(n.measureNumber, bRecurse.currentHierarchyOffset(), n)
        0 0.0 <music21.note.Note C#>
        0 0.5 <music21.note.Note B>
        1 1.0 <music21.note.Note A>
        1 2.0 <music21.note.Note B>
        1 3.0 <music21.note.Note C#>
        1 4.0 <music21.note.Note E>
        2 5.0 <music21.note.Note C#>
        ...
        9 34.5 <music21.note.Note E#>
        9 35.0 <music21.note.Note F#>
        0 0.0 <music21.note.Note E>
        1 1.0 <music21.note.Note F#>
        ...

        After iteration completes, the figure is reset to None:

        >>> print(bRecurse.currentHierarchyOffset())
        None

        The offsets are with respect to the position inside the stream
        being iterated, so, for instance, this will not change the output from above:

        >>> o = stream.Opus()
        >>> o.insert(20.0, b)
        >>> bRecurse = b.recurse().notes
        >>> for n in bRecurse:
        ...     print(n.measureNumber, bRecurse.currentHierarchyOffset(), n)
        0 0.0 <music21.note.Note C#>
        ...

        But of course, this will add 20.0 to all numbers:

        >>> oRecurse = o.recurse().notes
        >>> for n in oRecurse:
        ...     print(n.measureNumber, oRecurse.currentHierarchyOffset(), n)
        0 20.0 <music21.note.Note C#>
        ...

        * New in v4.
        r+   Nr5   )r2   r?  r:   r5  r   opFracr   )rJ   	lastYieldr?  newestIterator
lastStreamlastStartOffsets         r#   currentHierarchyOffsetz(RecursiveIterator.currentHierarchyOffsetw  s    f **=9	**,&r*#--
(GG
"==11==:3K3KI3V!VWWr"   r   r   r   r   c               Z    t        j                  ||||||      }| j                  |      S )ak  
        Adds a filter keeping only Music21Objects that
        are found at a certain offset or within a certain
        offset time range (given the `offsetStart` and optional `offsetEnd` values) from
        the beginning of the hierarchy.

        >>> b = corpus.parse('bwv66.6')
        >>> for n in b.recurse().getElementsByOffsetInHierarchy(8, 9.5).notes:
        ...     print(n,
        ...           n.getOffsetInHierarchy(b),
        ...           n.measureNumber,
        ...           n.getContextByClass(stream.Part).id)
        <music21.note.Note C#> 8.0 2 Soprano
        <music21.note.Note A> 9.0 3 Soprano
        <music21.note.Note B> 9.5 3 Soprano
        <music21.note.Note G#> 8.0 2 Alto
        <music21.note.Note F#> 9.0 3 Alto
        <music21.note.Note G#> 9.5 3 Alto
        <music21.note.Note C#> 8.0 2 Tenor
        <music21.note.Note C#> 9.0 3 Tenor
        <music21.note.Note D> 9.5 3 Tenor
        <music21.note.Note E#> 8.0 2 Bass
        <music21.note.Note F#> 9.0 3 Bass
        <music21.note.Note B> 9.5 3 Bass

        * Changed in v5.5: all behavior-changing options are keyword only.
        rJ  )r   OffsetHierarchyFilterr   )rJ   r   r   r   r   r   r   r   s           r#   getElementsByOffsetInHierarchyz0RecursiveIterator.getElementsByOffsetInHierarchy  s9    H ))1-+*GI ~~a  r"   r   c                    y rY   r!   r   s      r#   r   z$RecursiveIterator.getElementsByClass  r   r"   c                    y rY   r!   r   s      r#   r   z$RecursiveIterator.getElementsByClass  r   r"   c                    y rY   r!   r   s      r#   r   z$RecursiveIterator.getElementsByClass  r   r"   c                    y rY   r!   r   s      r#   r   z$RecursiveIterator.getElementsByClass  r   r"   c                   t         |   ||      }t        |t              r;t	        |t
        j                        r!t        j                  t        t           |      S t        j                  t        t           |      S )Nr   )r  r   rD   rE   
issubclassr   Music21Objectr;   r<   r/  r   r   )rJ   r   r   r   rP   s       r#   r   z$RecursiveIterator.getElementsByClass
  sa     g(k(Rot,OTM_M_1`66+,=>DD66+J7==r"   r  r  )r  zlist[RecursiveIterator]rY   r  )r   rT   r   r   r  RecursiveIterator[M21ObjType])r   r  r   r   r  rU  )r   r  r   r   r  z$RecursiveIterator[ChangedM21ObjType])r   r  r   r   r  rU  )r   zWt.Union[str, type[ChangedM21ObjType], Iterable[str], Iterable[type[ChangedM21ObjType]]]r   r   r  zLt.Union[RecursiveIterator[M21ObjType], RecursiveIterator[ChangedM21ObjType]])r   r   r    r  rL   rf   rZ   r{   r?  rB  rI  rM  r   r   r,  r-  s   @r#   r/  r/    s   <D J 
J@Tl 6: .;(?XJ +!
  $" *.+! 4:+!Z  04,/ )- 9V   04,9 )- 9V   04,C )- 9]   04,: )- 9V  04>-> )->!N> >r"   r/  c                  *    e Zd Zd Zd Zd Zd Zd Zy)Testc                   ddl m} |j                         }t        j                         }t        j
                         }|j                  ||g       t        |j                               }| j                  t        |      d       | j                  |d   |       | j                  |d   |       t        |j                         j                        }| j                  t        |      d       | j                  |d   |       y )Nr   r   rh   r_   )r   r   r   r   RestNoter   listr   assertEqualr@   assertIsr   )rJ   r   srnall_ss_notess          r#   testSimpleClonezTest.testSimpleClone  s    "MMOIIKIIK	!QQVVXUQ'eAh"eAh"qvvx~~&Wq)gaj!$r"   c                   ddl m} |j                         }t        j                         }t        j
                         }|j                  ||g       |j                         }t        |      }| j                  ||       |j                  }t        |      }| j                  ||       t        |      }	| j                  |	|       y )Nr   r   )r   r   r   r   rY  rZ  r   r   r   r]  r   )
rJ   r   r^  r_  r`  sIterr0sIter2obj0n0s
             r#   testAddingFiltersMidIterationz"Test.testAddingFiltersMidIteration+  s    "MMOIIKIIK	!Q%[b! $$F|dA %[b!r"   c                    ddl m} |j                  d      }|j                         }|j	                  d      }| j                  |j                  j                  d       y )Nr   )	converterz!tinyNotation: 4/4 c1 c4 d=id2 e fid2rh   )r   rl  parserecurser   r\  
activeSiterS   )rJ   rl  r^  recr`  s        r#   testRecursiveActiveSiteszTest.testRecursiveActiveSites?  sI    %OO?@iiku%,,a0r"   c                *   ddl m} |j                         }|j                         }|j	                  t        j                  d             |j	                  t        j                  d             |j                  dt        j                  d             |j	                  |       |j                  d      }g }|D ]!  }|j	                  |j                                # | j                  |g d       |j                         }| j                  |       y )	Nr   r   DECT)r1  )r3  r3        ?rw  g       @)r   r   r   rN   r   r   rZ  insertro  rI  assertListEqualassertIsNone)rJ   r   pmpRecurse
allOffsetsr   currentOffsets           r#   testCurrentHierarchyOffsetResetz$Test.testCurrentHierarchyOffsetResetF  s    "KKMNN	3 	3 	DIIcN#	999.
Ah==?@ Z)BC 779-(r"   c                   ddl m} ddlm} |j	                         }t        j                         }t        j                         }|j                  ||g       |j                         }|j                  |       |j                         }|j                  |       |j                         }t        |      }	| j                  |	|       |j                  }
| j                  |
|       y )Nr   r   )r/  )r   r   music21.stream.iteratorr/  rN   r   rY  rZ  r   r   Scorero  r   r]  r6  assertIsInstance)rJ   r   ImportedRecursiveIteratorr|  r_  r`  r{  scre  p0childs              r#   &testAddingFiltersMidRecursiveIterationz+Test.testAddingFiltersMidRecursiveIterationV  s    "ZNNIIKIIK	!QKKM	\\^
		!

%[b!,,e%>?r"   N)r   r   r    rc  rj  rr  r  r  r!   r"   r#   rW  rW    s    %(1) @r"   rW  __main__):r  
__future__r   collections.abcr   r   r   r   typingr;   r   unittestrl   r   r   music21.common.classToolsr	   r
   music21.common.enumsr   music21.common.typesr   r   r   r   r   music21.streamr   r   r   music21.sitesr   r   r   r9  TypeVarr   r   r   UnionAnyOptionalr   
FilterTypeUserWarningr   	TypedDictr%   ProtoM21Objectr   r[  r  r/  TestCaserW  
_DOC_ORDERr   mainTestr!   r"   r#   <module>r     sv  
 # 8 8       C . V V  "   (??.AIIcNAIIcNQYY3;KL  WWXquuajj&78!%%?@'BVBVVW
	{ 	)5 )b5W++Xj-A b5L-`]^Xd:.>%? `]JP>z2HZ4H P>fM@8 M@d /@
zGT r"   