
    3j*                       d dl mZ g dZd dlZd dlZd dlZd dlZ G d dej                        Z G d de	      Z
dd iZ G d	 d
      Z G d d      Z G d de      Z G d de      Z G d d      Zedk(  rd dlZ ej&                          yy)    )annotations)defaultlistSingletonCounterRelativeCounterSlottedObjectMixinEqualSlottedObjectMixinFrozenObjectTimerNc                  4     e Zd ZdZ fdZd Zd Zd Z xZS )r   a  
    A counter that iterates from most common to least common
    and can return new RelativeCounters that adjust for proportion or percentage.

    >>> l = ['b', 'b', 'a', 'a', 'a', 'a', 'c', 'd', 'd', 'd'] + ['e'] * 10
    >>> rc = common.RelativeCounter(l)
    >>> for k in rc:
    ...     print(k, rc[k])
    e 10
    a 4
    d 3
    b 2
    c 1

    Ties are iterated according to which appeared first in the generated list.

    >>> rcProportion = rc.asProportion()
    >>> rcProportion['b']
    0.1
    >>> rcProportion['e']
    0.5
    >>> rcPercentage = rc.asPercentage()
    >>> rcPercentage['b']
    10.0
    >>> rcPercentage['e']
    50.0

    >>> for k, perc in rcPercentage.items():
    ...     print(k, perc)
    e 50.0
    a 20.0
    d 15.0
    b 10.0
    c 5.0
    c              #  b    K   t        t         	          fdd      }|E d {    y 7 w)Nc                    |    S N )xselfs    C/DATA/.local/lib/python3.12/site-packages/music21/common/objects.py<lambda>z*RelativeCounter.__iter__.<locals>.<lambda>D   s	    d1g    T)keyreverse)sortedsuper__iter__)r   
sortedKeys	__class__s   ` r   r   zRelativeCounter.__iter__C   s'     EG,.4EtT
s   #/-/c              #  ,   K   | D ]  }|| |   f  y wr   r   )r   ks     r   itemszRelativeCounter.itemsG   s     AT!W* s   c                z     t         fd D              }i } D ]  } |   |z  ||<     j                  |      }|S )Nc              3  (   K   | ]	  }|     y wr   r   .0r   r   s     r   	<genexpr>z/RelativeCounter.asProportion.<locals>.<genexpr>L        ,t!d1gt   sumr   r   selfLenoutDictynews   `    r   asProportionzRelativeCounter.asProportionK   sI    ,t,,Aa7*GAJ  nnW%
r   c                     t         fd D              }i } D ]  } |   dz  |z  ||<     j                  |      }|S )Nc              3  (   K   | ]	  }|     y wr   r   r!   s     r   r#   z/RelativeCounter.asPercentage.<locals>.<genexpr>U   r$   r%   d   r&   r(   s   `    r   asPercentagezRelativeCounter.asPercentageT   sM    ,t,,Aa30GAJ  nnW%
r   )	__name__
__module____qualname____doc__r   r   r-   r1   __classcell__r   s   @r   r   r      s    "Jr   r   c                  4     e Zd ZdZ fdZd Zd Zd Z xZS )r   z
    Call a function for every time something is missing:

    >>> a = common.defaultlist(lambda:True)
    >>> a[5]
    True
    c                0    t         |           || _        y r   )r   __init___fx)r   fxr   s     r   r:   zdefaultlist.__init__f   s    r   c                ~    t        |       |k  r/| j                  | j                                t        |       |k  r.y y r   )lenappendr;   r   indexs     r   _fillzdefaultlist._fillj   s.    $i5 KK
# $i5 r   c                T    | j                  |       t        j                  | ||       y r   )rB   list__setitem__)r   rA   values      r   rE   zdefaultlist.__setitem__n   s     

5ue,r   c                P    | j                  |       t        j                  | |      S r   )rB   rD   __getitem__r@   s     r   rH   zdefaultlist.__getitem__r   s!    

5e,,r   )	r2   r3   r4   r5   r:   rB   rE   rH   r6   r7   s   @r   r   r   ^   s    $--r   r   rF   c                      e Zd ZdZd Zd Zy)r   aW  
    A simple counter that can produce unique numbers (in ascending order)
    regardless of how many instances exist.

    Instantiate and then call it.

    >>> sc = common.SingletonCounter()
    >>> v0 = sc()
    >>> v1 = sc()
    >>> v1 > v0
    True
    >>> sc2 = common.SingletonCounter()
    >>> v2 = sc2()
    >>> v2 > v1
    True
    c                     y r   r   r   s    r   r:   zSingletonCounter.__init__   s    r   c                :    t         d   }t         dxx   dz  cc<   |S )NrF      )_singletonCounter)r   posts     r   __call__zSingletonCounter.__call__   s!     )'"a'"r   N)r2   r3   r4   r5   r:   rP   r   r   r   r   r   z   s     r   r   c                  2    e Zd ZU dZdZded<   d Zd Zd Zy)	r   a  
    Provides template for classes implementing slots allowing it to be pickled
    properly, even if there are weakrefs in the slots, or it is subclassed
    by something that does not define slots.

    Only use SlottedObjectMixins for objects that we expect to make so many of
    that memory storage and speed become an issue. Thus, unless you are Xenakis,
    Glissdata is probably not the best example:

    >>> import pickle
    >>> class Glissdata(common.SlottedObjectMixin):
    ...     __slots__ = ('time', 'frequency')
    >>> s = Glissdata()
    >>> s.time = 0.125
    >>> s.frequency = 440.0
    >>> #_DOCS_SHOW out = pickle.dumps(s)
    >>> #_DOCS_SHOW pickleLoad = pickle.loads(out)
    >>> pickleLoad = s #_DOCS_HIDE -- cannot define classes for pickling in doctests
    >>> pickleLoad.time, pickleLoad.frequency
    (0.125, 440.0)

    OMIT_FROM_DOCS

    >>> class BadSubclass(Glissdata):
    ...     pass

    >>> bsc = BadSubclass()
    >>> bsc.amplitude = 2
    >>> #_DOCS_SHOW out = pickle.dumps(bsc)
    >>> #_DOCS_SHOW outLoad = pickle.loads(out)
    >>> outLoad = bsc #_DOCS_HIDE -- cannot define classes for pickling in doctests
    >>> outLoad.amplitude
    2

    This is in OMIT
    r   tuple[str, ...]	__slots__c                   t        | dd       | j                  j                         }ni }| j                         }|D ]J  }t        | |d       }t	        |t
        j                        r |       }t        d| d| ddz          |||<   L |S )N__dict__z#Warning: uncaught weakref found in z - z, zwill not be wrapped again)getattrrU   copy_getSlotsRecursive
isinstanceweakrefReferenceTypeprint)r   stateslotsslotsValues        r   __getstate__zSlottedObjectMixin.__getstate__   s    4T*6MM&&(EE'')DT4.F&'"7"78;D83tfBO34 5 E$K  r   c                N    |j                         D ]  \  }}t        | ||        y r   )r   setattr)r   r]   r_   rF   s       r   __setstate__zSlottedObjectMixin.__setstate__   s"     ;;=KD%D$& )r   c                    t               }| j                  j                         D ]  }|j                  t	        |dd               |S )aA  
        Find all slots recursively.

        A private attribute so as not to change the contents of inheriting
        objects private interfaces:

        >>> b = beam.Beam()
        >>> sSet = b._getSlotsRecursive()

        sSet is a set -- independent order.  Thus, for the doctest
        we need to preserve the order:

        >>> sorted(list(sSet))
        ['_editorial', '_style', 'direction', 'id', 'independentAngle', 'number', 'type']

        When a normal Beam won't cut it:

        >>> class FunkyBeam(beam.Beam):
        ...     __slots__ = ('funkiness', 'groovability')

        >>> fb = FunkyBeam()
        >>> sSet = fb._getSlotsRecursive()
        >>> sorted(list(sSet))
        ['_editorial', '_style', 'direction', 'funkiness', 'groovability',
            'id', 'independentAngle', 'number', 'type']
        rS   r   )setr   mroupdaterV   )r   r^   clss      r   rX   z%SlottedObjectMixin._getSlotsRecursive   s<    6 >>%%'CLLk267 (r   N)	r2   r3   r4   r5   rS   __annotations__ra   rd   rX   r   r   r   r   r      s#    #N "$I#'r   r   c                  ,    e Zd ZU dZdZded<   d Zd Zy)r   z
    Same as above, but __eq__ and __ne__ functions are defined based on the slots.

    Slots are the only things compared, so do not mix with a __dict__ based object.

    The equal comparison ignores differences in .id
    r   rR   rS   c                    t        |       t        |      ury| j                         D ]#  }|dk(  r	t        | |      t        ||      k7  s# y y)NFidT)typerX   rV   )r   otherthisSlots      r   __eq__zEqualSlottedObjectMixin.__eq__   sQ    :T%[(//1H4tX&'%*BB	 2
 r   c                    | |k(   S )zS
        Defining __ne__ explicitly so that it inherits the same as __eq__
        r   )r   ro   s     r   __ne__zEqualSlottedObjectMixin.__ne__
  s     EM""r   N)r2   r3   r4   r5   rS   rj   rq   rs   r   r   r   r   r      s     "$I##r   r   c                  b     e Zd ZU dZded<   d
ddZd fdZd fdZ fdZ fdZ	dd	Z
 xZS )r	   r   rR   rS   c                ~   |dk(  ryt        | dd      syt        j                         D ]p  }|j                  j                  j
                  dv s&d|j                  j                  v s?|j                  j                  d   j                  | j                  k(  sp y t        d| j                  j                   d      )Nr   Tfrozen)r:   __new__rd   r   zThis z instance is immutable.)
rV   inspectstackframef_codeco_namef_localsr   	TypeErrorr2   )r   r   sts      r   _check_initzFrozenObject._check_init  s    +tXt,--/B''+RR"(("3"33))&1;;t~~M	 "
 % 7 788OPQQr   c                H    | j                  |       t        | 	  ||       y r   )r   r   __setattr__r   r   rF   r   s      r   r   zFrozenObject.__setattr__#  s     C'r   c                F    | j                  |       t        | 	  |       y r   )r   r   __delattr__r   r   r   s     r   r   zFrozenObject.__delattr__'  s    C r   c                    t        t               d      r | j                          t        |   ||       t	        | j
                   d      )NrE    object is not subscriptable)hasattrr   r   rE   r~   r   r   s      r   rE   zFrozenObject.__setitem__+  sB    57M*GU+4>>**FGHHr   c                    t        t               d      r| j                          t        |   |       t	        | j
                   d      )N__delitem__r   )r   r   r   r   r~   r   r   s     r   r   zFrozenObject.__delitem__1  s@    57M*G$4>>**FGHHr   c                    g }| j                         D ]  }|j                  |t        | |      f       ! t        t	        |            S r   )rX   r?   rV   hashtuple)r   outss      r   __hash__zFrozenObject.__hash__7  sA    ((*AJJ74+,- +E#Jr   r   )returnbool)r   str)r   int)r2   r3   r4   rS   rj   r   r   r   rE   r   r   r6   r7   s   @r   r	   r	     s.    !#I#R(!II r   r	   c                  4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	r
   aG  
    An object for timing. Call it to get the current time since starting.

    >>> timer = common.Timer()
    >>> now = timer()
    >>> import time  #_DOCS_HIDE
    >>> time.sleep(0.01)  #_DOCS_HIDE  -- some systems are extremely fast or have wide deltas
    >>> nowNow = timer()
    >>> nowNow > now
    True

    Call `stop` to stop it. Calling `start` again will reset the number

    >>> timer.stop()
    >>> stopTime = timer()
    >>> stopNow = timer()
    >>> stopTime == stopNow
    True

    All this had better take less than one second!

    >>> stopTime < 1
    True
    c                R    t        j                          | _        d| _        d | _        y Nr   )time_tStart_tDif_tStoprK   s    r   r:   zTimer.__init__X  s    yy{
r   c                R    t        j                          | _        d| _        d| _        y)zu
        Explicit start method; will clear previous values.

        Start always happens on initialization.
        Nr   )r   r   r   r   rK   s    r   startzTimer.start^  s     yy{
r   c                r    t        j                          | _        | j                  | j                  z
  | _        y r   )r   r   r   r   rK   s    r   stopz
Timer.stoph  s#    iik[[4<</
r   c                .    d | _         d| _        d | _        y r   )r   r   r   rK   s    r   clearzTimer.clearl  s    
r   c                |    | j                   #t        j                         | j                  z
  }|S | j                  }|S )zD
        Reports current time or, if stopped, stopped time.
        )r   r   r   r   r   timeDifferences     r   rP   zTimer.__call__q  s;    
 ;;!YY[4<<7N  "ZZNr   c                    | j                   "t        j                         | j                  z
  }n| j                  }t	        t        |d            S )N   )r   r   r   r   r   roundr   s     r   __str__zTimer.__str__|  s;    ;;!YY[4<<7N!ZZN5+,,r   N)
r2   r3   r4   r5   r:   r   r   r   rP   r   r   r   r   r
   r
   ?  s%    00
	-r   r
   __main__)
__future__r   __all__collectionsrx   r   rZ   Counterr   rD   r   rN   r   r   r   r	   r
   r2   music21mainTestr   r   r   <module>r      s    #    >k)) >B-$ -2 aL  8] ]@#0 #:( * ( XB- B-J zG r   