
    rh=                       S SK Jr  S SKJr  S SKJr  S SKrS SKJrJr  S SK	r	S SK
r
S SKJrJr  S SKrS SKJr  S SKJr  S S	KJrJr  \(       a  S S
KJr  S SKJrJrJr  / SQr/ SQr\SS rS\S'   S\S'   S\S'   S\S'   S6S jr\R@                  r!\S7S j5       r"\#" / SQ5      r$\S8S j5       r%\S9S j5       r%S:S jr%\R@                  4S;S jjr&S<S jr'S=S>S jjr(S?S@S jjr)SAS  jr*S!r+SBS" jr,SCS# jr-SDS$ jr.    SES% jr/ SF   SGS& jjr0SHSIS' jjr1\" S(S)S*5      SJS+ j5       r2SKS, jr3SLS- jr4S.S/.SMS0 jjr5SNS1 jr6SOSPS2 jjr70 r8\9" \:" \5      5       H]  r;\\;   r<\<R{                  5       r>\;\8\<'   \;\8\>'   \;\8\?" \;5      \7" \;5      -   '   \\;   r@\@\<:w  d  MC  \@R{                  5       rA\;\8\@'   \;\8\A'   M_     C; " S3 S4\R                  5      rC\5\6/rD\ES5:X  a  S SKr\R                  " \C5        gg)Q    )annotations)Fraction)cacheN)isclosegcd)overloadTYPE_CHECKING)defaults)
deprecated)
OffsetQLInOffsetQL)Decimal)IterableSequence
Collection)ordinalsmusicOrdinalsordinalsToNumbersnumToIntOrFloatopFracmixedNumeralroundToHalfIntegeraddFloatPrecisionstrTrimFloatnearestMultipledotMultiplierdecimalToTupletunitNormalizeProportionunitBoundaryProportionweightedSelectionapproximateGCDcontiguousListgroupContiguousIntegers	fromRomantoRomanordinalAbbreviation)ZerothFirstSecondThirdFourthFifthSixthSeventhEighthNinthTenthEleventhTwelfth
Thirteenth
Fourteenth	Fifteenth	SixteenthSeventeenth
Eighteenth
Nineteenth	TwentiethzTwenty-firstzTwenty-secondUnison   Octave   zDouble-octave   zTriple-octave   c                     [        U 5      n[	        XSS9(       a  U$ U S-   $ ! [        [        4 a    [        U 5      n [        U 5      n N>f = f)a  
Given a number, return an integer if it is very close to an integer,
otherwise, return a float.

This routine is very important for conversion of
:class:`~music21.pitch.Accidental` objects' `.alter`  attribute
in musicXML must be 1 (not 1.0) for sharp and -1 (not -1.0) for flat,
but allows for 0.5 for half-sharp.

>>> common.numToIntOrFloat(1.0)
1
>>> common.numToIntOrFloat(1.00003)
1.00003
>>> common.numToIntOrFloat(1.5)
1.5
>>> common.numToIntOrFloat(2)
2
>>> common.numToIntOrFloat(-5)
-5
>>> common.numToIntOrFloat(1.0000000005)
1
>>> common.numToIntOrFloat(0.999999999)
1

>>> sharp = pitch.Accidental('sharp')
>>> common.numToIntOrFloat(sharp.alter)
1
>>> halfFlat = pitch.Accidental('half-flat')
>>> common.numToIntOrFloat(halfFlat.alter)
-0.5

Can also take in a string representing an int or float

>>> common.numToIntOrFloat('1.0')
1
>>> common.numToIntOrFloat('1')
1
>>> common.numToIntOrFloat('1.25')
1.25

Others raise a ValueError

>>> common.numToIntOrFloat('one')
Traceback (most recent call last):
ValueError: could not convert string to float: 'one'


Fractions also become ints or floats

>>> from fractions import Fraction
>>> common.numToIntOrFloat(Fraction(1, 2))
0.5
>>> common.numToIntOrFloat(Fraction(4, 3))
1.333333333...

Note: Decimal objects are not supported.
gư>abs_tol        )round
ValueError	TypeErrorfloatr   )valueintVals     T/home/james-whalen/.local/lib/python3.13/site-packages/music21/common/numberTools.pyr   r   I   sT    tu
 vd+3; 	" eus   # &AAc                J   U[         ::  a  X4$ U nUnSu  pEpg X-  nXXU-  -   n	U	[         :  a  OXgXHU-  -   U	4u  pEpgXX-  -
  pM,  [         U-
  U-  n
XJU-  -   nXZU-  -   nUnUn[        X-  X,-  -
  5      nX<-  n[        X-  X.-  -
  5      nX>-  nUU-  UU-  -
  nUS:  a  X4$ X4$ )a  
Used in opFrac

Copied from fractions.limit_denominator.  Their method
requires creating three new Fraction instances to get one back.
This doesn't create any call before Fraction.

DENOM_LIMIT is hardcoded to defaults.limitOffsetDenominator for speed.

returns a new n, d.

>>> common.numberTools._preFracLimitDenominator(100001, 300001)
(1, 3)

>>> from fractions import Fraction
>>> Fraction(100_000_000_001, 30_0000_000_001).limit_denominator(65535)
Fraction(1, 3)
>>> Fraction(100001, 300001).limit_denominator(65535)
Fraction(1, 3)

Timing differences are huge!

t is timeit.timeit

t('Fraction(*common.numberTools._preFracLimitDenominator(*x.as_integer_ratio()))',
   setup='x = 1000001/3000001; from music21 import common;from fractions import Fraction',
   number=100000)
1.0814228057861328

t('Fraction(x).limit_denominator(65535)',
   setup='x = 1000001/3000001; from fractions import Fraction',
   number=100000)
7.941488981246948

Nothing changed in 2023, in fact, it's faster now with the cache, and even
without the cache, it's still 4x faster.

Proof of working:

>>> import random
>>> myWay = lambda x: Fraction(*common.numberTools._preFracLimitDenominator(
...     *x.as_integer_ratio()))
>>> theirWay = lambda x: Fraction(x).limit_denominator(65535)

>>> for _ in range(50):
...     x = random.random()
...     if myWay(x) != theirWay(x):
...         print(f'boo: {x}, {myWay(x)}, {theirWay(x)}')

(n.b. -- nothing printed)
)r   r=   r=   r   r   )DENOM_LIMITabs)ndnOrgdOrgp0q0p1q1aq2kbound1nbound1dbound2nbound2dbound1minusS_nbound1minusS_dbound2minusS_nbound2minusS_d
differences                       rL   _preFracLimitDenominatorrd      s    l 	KvDDNBB
Fb&["fb0ae)1  
r	b Ar6kGr6kGGG'.T^<=N^N'.T^<=N^N >1n~6UVJQ!!!!    )g      ?g      ?g      ?g      ?      ?g      ?      ?      ?      ?      ?       @      @      @      @c                    g N nums    rL   r   r          re   c                    g rp   rq   rr   s    rL   r   r      rt   re   c                   U [         ;   a  U S-   $ [        U 5      nU[        L aX  U R                  5       nUS   [        :  a9  [        [        U6 6 nUR                  nXDS-
  -  S:X  a  UR                  US-   -  $ U$ U $ U[        L a  U S-   $ U[
        L a+  U R                  nXDS-
  -  S:X  a  U R                  US-   -  $ U $ U c  g[        U [        5      (       a  U S-   $ [        U [        5      (       a  [        [        U 5      5      $ [        U [
        5      (       a+  U R                  nXDS-
  -  S:X  a  U R                  US-   -  $ U $ [        SU  35      e)a  
opFrac -> optionally convert a number to a fraction or back.

Important music21 function for working with offsets and quarterLengths

Takes in a number and converts it to a Fraction with denominator
less than limitDenominator if it is not binary expressible; otherwise return a float.
Or if the Fraction can be converted back to a binary expressible
float then do so.

This function should be called often to ensure that values being passed around are floats
and ints wherever possible and fractions where needed.

The naming of this method violates music21's general rule of no abbreviations, but it
is important to make it short enough so that no one will be afraid of calling it often.
It also doesn't have a setting for maxDenominator so that it will expand in
Code Completion easily. That is to say, this function has been set up to be used, so please
use it.

This is a performance-critical operation. Do not alter it in any way without running
many timing tests.

>>> from fractions import Fraction
>>> defaults.limitOffsetDenominator
65535
>>> common.opFrac(3)
3.0
>>> common.opFrac(1/3)
Fraction(1, 3)
>>> common.opFrac(1/4)
0.25
>>> f = Fraction(1, 3)
>>> common.opFrac(f + f + f)
1.0
>>> common.opFrac(0.99999999842)
1.0
>>> common.opFrac(0.123456789)
Fraction(10, 81)
>>> common.opFrac(0.000001)
0.0

Please check against None before calling, but None is changed to 0.0

>>> common.opFrac(None)
0.0

* Changed in v9.3: opFrac(None) should not be called.  If it is called,
  it now returns 0.0
rE   r=   r   zCannot convert num: )_KNOWN_PASSEStyperI   as_integer_ratiorN   r   rd   _denominator
_numeratorint
isinstancer   denominator	numeratorrH   )rs   numTypeirf_outrQ   s        rL   r   r      sf   t mSy 3iG% !!#a5;6;<E""AU!''1s733 J	CSy	H	QKA>>QW--J	 
C		Sy	C		eCj!!	C	"	"OOQKA==AG,,J.se455re   c                   [        U [        5      (       dP  [        [        U 5      S5      u  p#[        U5      R	                  U5      nUS:  a  US-  nSU-
  nO1US:X  a  SnUS-
  nO#[        [        U 5      5      nX-
  nUS:  a  US-  nU(       a+  U(       a  [        U5       SU 3$ [        [        U5      5      $ US:w  a  [        U5      $ [        S5      $ )a  
Returns a string representing a mixedNumeral form of a number

>>> common.mixedNumeral(1.333333)
'1 1/3'
>>> common.mixedNumeral(0.333333)
'1/3'
>>> common.mixedNumeral(-1.333333)
'-1 1/3'
>>> common.mixedNumeral(-0.333333)
'-1/3'

>>> common.mixedNumeral(0)
'0'
>>> common.mixedNumeral(-0)
'0'

Works with Fraction objects too

>>> from fractions import Fraction
>>> common.mixedNumeral(Fraction(31, 7))
'4 3/7'
>>> common.mixedNumeral(Fraction(1, 5))
'1/5'
>>> common.mixedNumeral(Fraction(-1, 5))
'-1/5'
>>> common.mixedNumeral(Fraction(-4, 5))
'-4/5'
>>> common.mixedNumeral(Fraction(-31, 7))
'-4 3/7'

Denominator is limited by default but can be changed.

>>> common.mixedNumeral(2.0000001)
'2'
>>> common.mixedNumeral(2.0000001, limitDenominator=10000000)
'2 1/10000000'
ri   r=   rE   r    )r}   r   divmodrI   limit_denominatorr|   str)exprlimitDenominatorquotient	remainderremainderFracs        rL   r   r   k  s    P dH%%$U4[#6 +==>NOb=MH-M^H)A-MuT{#a<RM(m_Am_55s8}%%A}%%q6Mre   c                    [        U S5      u  p[        U5      nUS:  a  SnX-   $ SUs=::  a  S:  a	  O  OSnX-   $ SnX-   $ )az  
Given a floating-point number, round to the nearest half-integer. Returns int or float

>>> common.roundToHalfInteger(1.2)
1
>>> common.roundToHalfInteger(1.35)
1.5
>>> common.roundToHalfInteger(1.8)
2
>>> common.roundToHalfInteger(1.6234)
1.5

0.25 rounds up:

>>> common.roundToHalfInteger(0.25)
0.5

as does 0.75

>>> common.roundToHalfInteger(0.75)
1

unlike python round function, does the same for 1.25 and 1.75

>>> common.roundToHalfInteger(1.25)
1.5
>>> common.roundToHalfInteger(1.75)
2

negative numbers however, round up on the boundaries

>>> common.roundToHalfInteger(-0.26)
-0.5
>>> common.roundToHalfInteger(-0.25)
0
ri   rf   r   rh   rg   r=   )r   r|   )rs   rK   floatVals      rL   r   r     sc    J c3'F[F$
 	 
	 D	   re   c                    [        U [        5      (       a  [        U 5      n SnU H  n[        XUS9(       d  M  [	        U5      s  $    U $ )a  
Given a value that suggests a floating point fraction, like 0.33,
return a Fraction or float that provides greater specification, such as Fraction(1, 3)

>>> import fractions
>>> common.addFloatPrecision(0.333)
Fraction(1, 3)
>>> common.addFloatPrecision(0.33)
Fraction(1, 3)
>>> common.addFloatPrecision(0.35) == fractions.Fraction(1, 3)
False
>>> common.addFloatPrecision(0.2) == 0.2
True
>>> common.addFloatPrecision(0.125)
0.125
>>> common.addFloatPrecision(1/7) == 1/7
True
)gUUUUUU?gUUUUUU?gUUUUUU?g?rC   )r}   r   rI   r   r   )xgrainvaluesvs       rL   r   r     sI    & !S!HF1''!9  Hre   c                    S[        U5      -   S-   nX -  nUR                  S5      n[        U5      n[        US-
  US-   S5       H  nX6   S:w  a    O	US-
  nM     USU nU$ )aC  
returns a string from a float that is at most maxNum of
decimal digits long, but never less than 1.

>>> common.strTrimFloat(42.3333333333)
'42.3333'
>>> common.strTrimFloat(42.3333333333, 2)
'42.33'
>>> common.strTrimFloat(6.66666666666666, 2)
'6.67'
>>> common.strTrimFloat(2.0)
'2.0'
>>> common.strTrimFloat(-5)
'-5.0'
z%.f.r=   r   0r   )r   indexlenrange)floatNummaxNumoffBuildStringoff
offDecimaloffLenr   s          rL   r   r     sx    " S[(3.N

#C3JXFvz:>26:aZF	 7
 a-CJre   c                d   U S:  a  [        SU  S3S-   SU 3-   5      e[        R                  " X-  5      nUS-  nX-  nXS-   -  nX@s=:  a  U:  a  O  O[        SU S	U 35      eX@s=::  a  XC-   ::  a   O  OU[        X-
  S
5      [        X-
  S
5      4$ U[        XP-
  S
5      [        X-
  S
5      4$ )a  
Given a positive value `n`, return the nearest multiple of the supplied `unit` as well as
the absolute difference (error) to seven significant digits and the signed difference.

>>> print(common.nearestMultiple(0.25, 0.25))
(0.25, 0.0, 0.0)
>>> print(common.nearestMultiple(0.35, 0.25))
(0.25, 0.1..., 0.1...)
>>> print(common.nearestMultiple(0.20, 0.25))
(0.25, 0.05..., -0.05...)

Note that this one also has an error of 0.1, but it's a positive error off of 0.5

>>> print(common.nearestMultiple(0.4, 0.25))
(0.5, 0.1..., -0.1...)

>>> common.nearestMultiple(0.4, 0.25)[0]
0.5
>>> common.nearestMultiple(23404.001, 0.125)[0]
23404.0
>>> common.nearestMultiple(23404.134, 0.125)[0]
23404.125

Error is always positive, but signed difference can be negative.

>>> common.nearestMultiple(23404 - 0.0625, 0.125)
(23403.875, 0.0625, 0.0625)

>>> common.nearestMultiple(0.001, 0.125)[0]
0.0

>>> from math import isclose
>>> isclose(common.nearestMultiple(0.25, 1 / 3)[0], 0.33333333, abs_tol=1e-7)
True
>>> isclose(common.nearestMultiple(0.55, 1 / 3)[0], 0.66666666, abs_tol=1e-7)
True
>>> isclose(common.nearestMultiple(234.69, 1 / 3)[0], 234.6666666, abs_tol=1e-7)
True
>>> isclose(common.nearestMultiple(18.123, 1 / 6)[0], 18.16666666, abs_tol=1e-7)
True

>>> common.nearestMultiple(-0.5, 0.125)
Traceback (most recent call last):
ValueError: n (-0.5) is less than zero. Thus, cannot find the nearest
    multiple for a value less than the unit, 0.125
r   zn (z) is less than zero. z3Thus, cannot find the nearest multiple for a value zless than the unit, rk   r=   z"cannot place n between multiples: z,    )rG   mathfloorrF   )rP   unitmulthalfUnitmatchLow	matchHighs         rL   r   r     s    ^ 	1u3qc!67PQ1$89 : 	: ::ahDczH{Hq!I !	!=hZr)UVV-,-q|Q/q|Q1GGG %	q153JJJre   )	ri   rj   g      ?g      ?g      ?g     ?g     ?g     ?g     ?c                B    U S:  a	  [         U    $ SU S-   -  S-
  SU -  -  $ )a  
dotMultiplier(dots) returns how long to multiply the note
length of a note in order to get the note length with n dots

Since dotMultiplier always returns a power of two in the denominator,
the float will be exact.

>>> from fractions import Fraction
>>> Fraction(common.dotMultiplier(1))
Fraction(3, 2)
>>> Fraction(common.dotMultiplier(2))
Fraction(7, 4)
>>> Fraction(common.dotMultiplier(3))
Fraction(15, 8)

>>> common.dotMultiplier(0)
1.0
	      ri   )_DOT_LOOKUP)dotss    rL   r   r   b  s3    & ax4  4#:#%!t)44re   c                l   S nSnU S::  a  [        S5      eU S:  a  SnSU -  n [        R                  " U 5      u  p4X-  nU" U5      u  pgUS:X  a  [        S5      eXd-  n[	        [        U5      [        U5      5      nXh-  nXx-  nUSL a  [        U5      [        U5      4$ [        U5      [        U5      4$ )a  
For simple decimals (usually > 1), a quick way to figure out the
fraction in lowest terms that gives a valid tuplet.

No it does not work really fast.  No it does not return tuplets with
denominators over 100.  Too bad, math geeks.  This is real life.  :-)

returns (numerator, denominator)

>>> common.decimalToTuplet(1.5)
(3, 2)
>>> common.decimalToTuplet(1.25)
(5, 4)

If decNum is < 1, the denominator will be greater than the numerator:

>>> common.decimalToTuplet(0.8)
(4, 5)

If decNum is <= 0, returns a ZeroDivisionError:

>>> common.decimalToTuplet(-.02)
Traceback (most recent call last):
ZeroDivisionError: number must be greater than zero
c                    [        SS5       HD  n[        XS-  5       H/  n[        XU-  SS9(       d  M  [        U5      [        U5      4s  s  $    MF     g)z
Utility function.
r=     r   gHz>rC   )r   r   )r   r   r|   )inner_workingr   js      rL   findSimpleFraction+decimalToTuplet.<locals>.findSimpleFraction  sP     1d^E5!),=e)TBBFCJ// - $ re   Fr   z number must be greater than zeror=   TzNo such luck)ZeroDivisionErrorr   modfrG   r   r|   )	decNumr   flipNumeratorunused_remainder
multiplierworkingjyiymy_gcds	            rL   r   r   {  s    4 M{ BCCzV#'99V#4 !G!'*HR	Qw((BR#b'"F	B	BBR!!BR!!re   c                    SnU  H  nUS:  a  [        S5      eX-  nM     / nU  H  nUR                  X!-  5        M     U$ )a\  
Normalize values within the unit interval, where max is determined by the sum of the series.

>>> common.unitNormalizeProportion([0, 3, 4])
[0.0, 0.42857142857142855, 0.5714285714285714]
>>> common.unitNormalizeProportion([1, 1, 1])
[0.3333333..., 0.333333..., 0.333333...]

Works fine with a mix of ints and floats:

>>> common.unitNormalizeProportion([1.0, 1, 1.0])
[0.3333333..., 0.333333..., 0.333333...]


On 32-bit computers this number may be inexact even for small floats.
On 64-bit it works fine.  This is the 32-bit output for this result.

    common.unitNormalizeProportion([0.2, 0.6, 0.2])
    [0.20000000000000001, 0.59999999999999998, 0.20000000000000001]

Negative values should be shifted to positive region first:

>>> common.unitNormalizeProportion([0, -2, -8])
Traceback (most recent call last):
ValueError: value members must be positive
rE   zvalue members must be positive)rG   append)r   	summationr   r   s       rL   r   r     sQ    6 Is7=>>	  DQ]$ Kre   c                    [        U 5      n/ nSn[        [        U5      5       HH  nU[        U5      S-
  :w  a   UR                  X3X   -   45        X1U   -  nM5  UR                  US45        MJ     U$ )a+  
Take a series of parts with an implied sum, and create
unit-interval boundaries proportional to the series components.

>>> common.unitBoundaryProportion([1, 1, 2])
[(0.0, 0.25), (0.25, 0.5), (0.5, 1.0)]
>>> common.unitBoundaryProportion([9, 1, 1])
[(0.0, 0.8...), (0.8..., 0.9...), (0.9..., 1.0)]
rE   r=   ri   )r   r   r   r   )seriesr   boundsr   r   s        rL   r   r     su     #6*DFIs4y!CIM!MM9$+&=>?e$IMM9c*+ " Mre   c                    Ub  U" 5       nO[         R                   " 5       n[        U5      nSn[        U5       H  u  nu  pgXcs=::  a  U:  d  M  O  M  X   s  $    X   $ )a:  
Given a list of values and an equal-sized list of weights,
return a randomly selected value using the weight.

Example: sum -1 and 1 for 100 values; should be
around 0 or at least between -50 and 50 (99.99999% of the time)

>>> -50 < sum([common.weightedSelection([-1, 1], [1, 1]) for x in range(100)]) < 50
True
r   )randomr   	enumerate)r   weightsrandomGeneratorq
boundariesr   lowhighs           rL   r    r      s_     "MMO'0JE'
3{?d??=  4 =re   c           	     P   [        [        U 5      5      nSnU  H,  nXB-  nU[        U5      -
  n[        USUS9(       d  M'  US-  nM.     U[	        U 5      :X  a  U$ Sn/ n[        5       n	U  HE  n
/ nU H)  nX-  nUR                  U5        U	R                  U5        M+     UR                  U5        MG     / nU	 HP  nSnU H#  nU H  n[        XMUS9(       d  M  US-  n  M!     M%     U[	        U5      :X  d  M?  UR                  U5        MR     U(       d  [        S5      e[        U5      $ )a|  
Given a list of values, find the lowest common divisor of floating point values.

>>> common.approximateGCD([2.5, 10, 0.25])
0.25
>>> common.approximateGCD([2.5, 10])
2.5
>>> common.approximateGCD([2, 10])
2.0
>>> common.approximateGCD([1.5, 5, 2, 7])
0.5
>>> common.approximateGCD([2, 5, 10])
1.0
>>> common.approximateGCD([2, 5, 10, 0.25])
0.25
>>> common.strTrimFloat(common.approximateGCD([1/3, 2/3]))
'0.3333'
>>> common.strTrimFloat(common.approximateGCD([5/3, 2/3, 4]))
'0.3333'
>>> common.strTrimFloat(common.approximateGCD([5/3, 2/3, 5]))
'0.3333'
>>> common.strTrimFloat(common.approximateGCD([5/3, 2/3, 5/6, 3/6]))
'0.1667'
r   rE   rC   r=   )ri   rk   rl   rm   g      @rn   g      @g       @g      "@g      $@g      &@g      (@g      *@g      ,@g      .@g      0@zcannot find a common divisor)
rI   minr|   r   r   setr   addrG   max)r   r   lowestcountr   x_adjustfloatingValuedivisors	divisionsuniqueDivisionsr   collrQ   r   commonUniqueDivisionss                  rL   r!   r!     s>   2 3v;F E: 3x=0=#u55QJE  FH IeOA	AKKN"  	  D1//QJE	   C	N"!((+  !788$%%re   v9v10zUse math.lcm insteadc                2    S nSnU  H  nU" X#5      nM     U$ )a1  
Find the least common multiple of a list of values

common.lcm([3, 4, 5])
60
common.lcm([3, 4])
12
common.lcm([1, 2])
2
common.lcm([3, 6])
6

Works with any iterable, like this set

common.lcm({3, 5, 6})
30

Deprecated in v9 since Python 3.10 is the minimum version
and math.lcm works in C and is faster
c                4    [        X-  5      [        X5      -  $ )z(
find the least common multiple of a, b
)rO   r   )rX   bs     rL   _lcmlcm.<locals>._lcmp  s    
 15zSY&&re   r=   rq   )
filterListr   lcmValflValues       rL   lcmr   Z  s'    ,' Ff& Mre   c                l    U S   n[        S[        U 5      5       H  nX   nX1S-   :w  a    gUS-  nM     g)ad  
returns bool True or False if a list containing ints
contains only contiguous (increasing) values

requires the list to be sorted first


>>> l = [3, 4, 5, 6]
>>> common.contiguousList(l)
True
>>> l.append(8)
>>> common.contiguousList(l)
False

Sorting matters

>>> l.append(7)
>>> common.contiguousList(l)
False
>>> common.contiguousList(sorted(l))
True
r   r=   FT)r   r   )inputListOrTuplecurrentMaxValr   newVals       rL   r"   r"     sJ    . %Q'Mq#./0!(Q&&	 1
 re   c                   [        U 5      S::  a  U /$ / n/ nU R                  5         SnU[        U 5      S-
  :  a  X   nUR                  U5        XS-      nXTS-   :w  a  UR                  U5        / nU[        U 5      S-
  :X  a"  UR                  U5        UR                  U5        US-  nU[        U 5      S-
  :  a  M  U$ )a  
Given a list of integers, group contiguous values into sub lists

>>> common.groupContiguousIntegers([3, 5, 6])
[[3], [5, 6]]
>>> common.groupContiguousIntegers([3, 4, 6])
[[3, 4], [6]]
>>> common.groupContiguousIntegers([3, 4, 6, 7])
[[3, 4], [6, 7]]
>>> common.groupContiguousIntegers([3, 4, 6, 7, 20])
[[3, 4], [6, 7], [20]]
>>> common.groupContiguousIntegers([3, 4, 5, 6, 7])
[[3, 4, 5, 6, 7]]
>>> common.groupContiguousIntegers([3])
[[3]]
>>> common.groupContiguousIntegers([3, 200])
[[3], [200]]
r=   r   r   )r   sortr   )srcpostgroupieeNexts         rL   r#   r#     s    & 3x1}uDEHHJ	A
s3x!|
FQE
E>KKEC1LLKK	Q s3x!|
" Kre   F)strictModernc                  U R                  5       nSnSnSn/ nU H  nXt;  d  M
  [        SU 35      e   [        [        U5      5       H  nX(   nXTR	                  U5         n	 XTR	                  X(S-      5         n
X:  a+  X;   a&  U(       a  XS-  :  a  [        SSU  3-   5      eU	S	-  n	OX:  a  [        S
U  35      e UR                  U	5        M     SnU H  nX-  nM	     U$ ! [
         a     N2f = f)a"  

Convert a Roman numeral (upper or lower) to an int

https://code.activestate.com/recipes/81611-roman-numerals/


>>> common.fromRoman('ii')
2
>>> common.fromRoman('vii')
7

Works with both IIII and IV forms:

>>> common.fromRoman('MCCCCLXXXIX')
1489
>>> common.fromRoman('MCDLXXXIX')
1489


Some people consider this an error, but you see it in medieval and ancient roman documents:

>>> common.fromRoman('ic')
99

unless strictModern is True

>>> common.fromRoman('ic', strictModern=True)
Traceback (most recent call last):
ValueError: input contains an invalid subtraction element (modern interpretation): ic


But things like this are never seen, and thus cause an error:

>>> common.fromRoman('vx')
Traceback (most recent call last):
ValueError: input contains an invalid subtraction element: vx
)r=   
   d   )MDCLXVI)r     r   2   r      r=   z$value is not a valid roman numeral: r=   r   z.input contains an invalid subtraction element z(modern interpretation): r   z/input contains an invalid subtraction element: r   )upperrG   r   r   r   
IndexErrorr   )rs   r   
inputRomansubtractionValuesnumsintsplacescr   rJ   	nextValuer   rP   s                rL   r$   r$     s5   N J$.D)DF=CJ<PQQ  3z?#MZZ]#	ZZ
q5(9:;I U%?I$;$H5cU;<= = " EcUKM M # 	e' $( I	   		s   $AC&,C&&
C32C3c                   [        U [        5      (       d  [        S[        U 5       35      eSU s=:  a  S:  d  O  [	        S5      eSnSnSn[        [        U5      5       H'  n[        XU   -  5      nX2U   U-  -  nXU   U-  -  n M)     U$ )ac  
Convert a number from 1 to 3999 to a roman numeral

>>> common.toRoman(2)
'II'
>>> common.toRoman(7)
'VII'
>>> common.toRoman(1999)
'MCMXCIX'

>>> common.toRoman('hi')
Traceback (most recent call last):
TypeError: expected integer, got <... 'str'>

>>> common.toRoman(0)
Traceback (most recent call last):
ValueError: Argument must be between 1 and 3999
zexpected integer, got r   i  z#Argument must be between 1 and 3999)r     r   i  r   Z   r  (   r   r   r     r=   )r   CMr   CDr   XCr   XLr   IXr   IVr    )r}   r|   rH   rx   rG   r   r   )rs   r  r  resultr   r   s         rL   r%   r%     s    & c30c<==s>T>>??ADRDF3t9Cq'M"q'E/!Aw  Mre   c                    U S-  nUS;   a  SnO4U S-  nUS:X  a  SnO&US;   a  SnOUS:X  a  S	nOUS
:X  a  SnO[        S5      eUS:w  a  U(       a  US-  nU$ )z
Return the ordinal abbreviations for integers

>>> common.ordinalAbbreviation(3)
'rd'
>>> common.ordinalAbbreviation(255)
'th'
>>> common.ordinalAbbreviation(255, plural=True)
'ths'
r   )         thr   r=   st)r   r  r     r   r?   r   r   nd   rdzSomething really weirds)rG   )rJ   pluralvalueHundredthsr   valueMods        rL   r&   r&   9  sx     ckO,&2:q=D..D]D]D566t|Kre   c                  0    \ rS rSrSrS rS rS rS rSr	g)	Testij  z"
Tests not requiring file output.
c                    g rp   rq   selfs    rL   setUp
Test.setUpo  s    re   c                P    S H   u  pU R                  U[        U5      5        M"     g )N))r=   r   )r!  III)r  r   )assertEqualr%   )r+  r   dsts      rL   testToRomanTest.testToRomanr  s"    8HCS'#,/ 9re   c                   U R                  [        S   S5        U R                  [        S   S5        U R                  [        S   S5        U R                  [        S   S5        U R                  [        S   S5        U R                  [        S   S5        U R                  [        S	   S5        U R                  [        S
   S5        U R                  [        S   S5        U R                  [        S   S5        g )Nunisonr=   r<   firstr(   1stoctaver?   r>   eighthr/   8th)r0  r   r*  s    rL   testOrdinalsToNumbersTest.testOrdinalsToNumbersv  s    *84a8*84a8*73Q7*73Q7*5115*84a8*84a8*84a8*84a8*5115re   c                   [        S5       HN  nSn[        S5       H  nU[        SS/SS/5      -  nM     U R                  SUs=:  =(       a    S:  Os  5        MP     [        S5       HN  nSn[        S5       H  nU[        SS/SS/5      -  nM     U R                  SUs=:*  =(       a    S	:  Os  5        MP     [        S5       HN  nSn[        S5       H  nU[        SS/SS/5      -  nM     U R                  S
Us=:*  =(       a    S:*  Os  5        MP     [        S5       H<  nSn[        S5       H  nU[        SS/SS/5      -  nM     U R                  US5        M>     g )Nr   r   r   r   r=   i   i'     r  )r   r    
assertTruer0  )r+  r   r   r   unused_js        rL   testWeightedSelectionTest.testWeightedSelection  sQ   rAA4[&AwA77 ! OOD1NNsN+  rAA4[&1vqz:: ! OOAKKRK(  rAA4[&1v5z:: ! OOC1,,,-  b	HA4[&1v1v66 ! Q" "re   rq   N)
__name__
__module____qualname____firstlineno____doc__r,  r2  r;  rB  __static_attributes__rq   re   rL   r(  r(  j  s    0
6!#re   r(  __main__)rJ   r   returnzint | float)rP   r|   rQ   r|   rK  tuple[int, int])rs   r|   rK  rI   )rs   float | FractionrK  rM  )rs   r   rK  r   )r   znumbers.Real)rs   float | intrK  rN  )g{Gz?)rK  rM  )r  )r   rI   r   r|   rK  r   )rP   rI   r   rI   rK  ztuple[float, float, float])r   r|   rK  rI   )r   rI   rK  rL  )r   Sequence[int | float]rK  zlist[float])r   rO  rK  zlist[tuple[int | float, float]]rp   )r   	list[int]r   zlist[int | float]rK  r|   )g-C6?)r   z"Collection[int | float | Fraction]r   rI   rK  rI   )r   zIterable[int]rK  r|   )rK  bool)r   rP  rK  zlist[list[int]])rs   r   rK  r|   )rs   r|   rK  r   )F)rJ   r|   rK  r   )G
__future__r   	fractionsr   	functoolsr   r   r   r   numbersr   typingr   r	   unittestmusic21r
   music21.commonr   music21.common.typesr   r   decimalr   collections.abcr   r   r   __all__r   r   r   limitOffsetDenominatorrN   rd   	frozensetrw   r   r   r   r   r   r   r   r   r   r   r   r    r!   r   r"   r#   r$   r%   r&   r   r   r   ordinal_indexordinalNamelowerordinalNameLowerr   musicOrdinalNamemusicOrdinalNameLowerTestCaser(  
_DOC_ORDERrD  mainTestrq   re   rL   <module>ri     s   #       *   % 5>>, a a #b #b CL --Q" Q"p   
 
 
 
 
p6h #+"A"A?D-`<<CKL952;"|#L"4 '+./2:C&L D%/0! 1!H@*\ ). GVB@  3x=)M=)K"((*%2k"*7&'Q^c-(+>}+MMN$]3;& 0 6 6 8.;*+3@/0 * 9#8 9#| !
 zT re   