
    rh                    <   S SK Jr  S SKrS SKJr  S SKJr  S SKJr  S SKJr  S SKJ	r	  \R                  (       a  S SKJr  \R                  " S	5      r\R                  \R                   \R"                  4S
4     SS jjr\S:X  a  S SKr\R(                  " 5         gg)    )annotationsN)clef)environment)key)meter)stream)Music21Objectzstream.toolsTc                
  ^ [         R                  [        R                  [        R
                  4n0 nU(       d  U R                  S5      n [        U [        R                  5      (       a6  [        U R                  5      S:  a  U R                   H  n[        XQSS9  M     U H  m[        U4S jU 5       5      (       d  [        SU S35      eU R                  5       R!                  T5      n[        U5      S:  a  M\  US   nUS	S
  HJ  nX:X  a@  UR"                  U;   a  XHR"                     R%                  U5        M7  U/XHR"                  '   MH  UnML     M     UR'                  5        H  u  pU	R)                  U
SS9  M     U $ )a  
The repetition of some classes like notes is common.
By contrast, the explicit repetition of certain other objects like clefs
usually indicates an error e.g., resulting from a copy'n'paste.
This function serves to remove those that are likely in error and make no change.

Use the `classesToRemove` argument to specify which music21 classes to check and remove.
The classes currently supported are: time signatures, key signatures, and clefs.
The `classesToRemove` argument defaults all three.
Sometimes there are legitimate reasons to duplicate even these classes.
In that case, override the default by specifying the list of which of the three of classes.
More classes may be added, but for now they will simply raise a ValueError.

So let's create an example part with an initial set of
time signature, key signature, and clef.

>>> p = stream.Part()
>>> p.append(meter.TimeSignature('3/4'))  # first TS
>>> p.append(key.KeySignature(6))  # first KS
>>> p.append(clef.TrebleClef())  # first Clef

Then a few notes, followed by a duplicates of the
key signature, and clef.

>>> p.append(note.Note('C'))
>>> p.append(note.Note('C'))
>>> p.append(note.Note('D'))

>>> p.append(meter.TimeSignature('3/4'))  # duplicate
>>> p.append(key.KeySignature(6))  # duplicate
>>> p.append(clef.TrebleClef())  # duplicate

Finally, a few more notes, followed by a
change of time signature, key signature, and clef.

>>> p.append(note.Note('E'))
>>> p.append(note.Note('F'))
>>> p.append(note.Note('G'))

>>> p.append(meter.TimeSignature('2/4'))
>>> p.append(key.KeySignature(-5))
>>> p.append(clef.BassClef())

>>> p.append(note.Note('A'))
>>> p.append(note.Note('B'))
>>> p.append(note.Note('C5'))

Now we'll make it into a proper part with measures and see
how it looks in its original, unaltered form:

>>> p.makeMeasures(inPlace=True)
>>> p.show('t')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.key.KeySignature of 6 sharps>
    {0.0} <music21.meter.TimeSignature 3/4>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note C>
    {2.0} <music21.note.Note D>
{3.0} <music21.stream.Measure 2 offset=3.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.key.KeySignature of 6 sharps>
    {0.0} <music21.meter.TimeSignature 3/4>
    {0.0} <music21.note.Note E>
    {1.0} <music21.note.Note F>
    {2.0} <music21.note.Note G>
{6.0} <music21.stream.Measure 3 offset=6.0>
    {0.0} <music21.clef.BassClef>
    {0.0} <music21.key.KeySignature of 5 flats>
    {0.0} <music21.meter.TimeSignature 2/4>
    {0.0} <music21.note.Note A>
    {1.0} <music21.note.Note B>
{8.0} <music21.stream.Measure 4 offset=8.0>
    {0.0} <music21.note.Note C>
    {1.0} <music21.bar.Barline type=final>

Calling removeDuplicates should remove the duplicates
even with those changes now stored within measures,
not directly on the part.
Specifically, in our example,
the duplicates entries are removed from measure 2
and the actual changes in measure 3 remain.

>>> testInPlace = stream.tools.removeDuplicates(p)
>>> testInPlace.show('t')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.key.KeySignature of 6 sharps>
    {0.0} <music21.meter.TimeSignature 3/4>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note C>
    {2.0} <music21.note.Note D>
{3.0} <music21.stream.Measure 2 offset=3.0>
    {0.0} <music21.note.Note E>
    {1.0} <music21.note.Note F>
    {2.0} <music21.note.Note G>
{6.0} <music21.stream.Measure 3 offset=6.0>
    {0.0} <music21.clef.BassClef>
    {0.0} <music21.key.KeySignature of 5 flats>
    {0.0} <music21.meter.TimeSignature 2/4>
    {0.0} <music21.note.Note A>
    {1.0} <music21.note.Note B>
{8.0} <music21.stream.Measure 4 offset=8.0>
    {0.0} <music21.note.Note C>
    {1.0} <music21.bar.Barline type=final>

As the example shows, this function defaults to working on a stream inPlace.

>>> testInPlace == p
True

To avoid this, set inPlace to False.

>>> testNotInPlace = stream.tools.removeDuplicates(p, inPlace=False)
>>> testNotInPlace == p
False

This function is primarily designed and intended for use on a stream.Part object.
If called on a steam.Score, it will simply be applied to each of that score's parts in turn.

>>> s = stream.Score()
>>> s.append(p)
>>> t = stream.tools.removeDuplicates(s, inPlace=False)
>>> s.parts[0] == testInPlace
True

removeDuplicatesr   T)classesToRemoveinPlacec              3  <   >#    U  H  n[        TU5      v   M     g 7f)N)
issubclass).0supportedClass	thisClasss     N/home/james-whalen/.local/lib/python3.13/site-packages/music21/stream/tools.py	<genexpr>#removeDuplicates.<locals>.<genexpr>   s     `O_^:i88O_s   zInvalid class. Only z are supported.      N)recurse)r   TimeSignaturer   KeySignaturer   ClefcoreCopyAsDerivation
isinstancer   Scorelenpartsr   any
ValueErrorr   getElementsByClass
activeSiteappenditemsremove)
thisStreamr   r   supportedClassesremovalDictp	allStatescurrentState	thisStateactiveSiteKeyvaluesToRemover   s              @r   r   r      sc   H ++S-=-=tyyI<>K445GH
*fll++z 1$%% TR & %	`O_```34D3E_UVV&&(;;IF	y>A |"12I('';6 4 45<<YG9BK 4 45( ' %( *5):):)<%^T: *=     __main__)r(   stream.Streamr   tupler   boolreturnr3   )
__future__r   typingtmusic21r   r   r   r   r   TYPE_CHECKINGmusic21.baser	   EnvironmentenvironLocalr   r   r   r   __name__mainTest r1   r   <module>rB      s    #      ??*&&~6 05/B/BCDTDTVZV_V_.`%)g&+g"g (gX z r1   