music21.common.objects

EqualSlottedObjectMixin

class music21.common.objects.EqualSlottedObjectMixin

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

EqualSlottedObjectMixin bases

EqualSlottedObjectMixin methods

EqualSlottedObjectMixin.__eq__(other)

Return self==value.

FrozenObject

class music21.common.objects.FrozenObject

FrozenObject bases

FrozenObject methods

Methods inherited from EqualSlottedObjectMixin:

RelativeCounter

class music21.common.objects.RelativeCounter(iterable=None, /, **kwds)

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

RelativeCounter methods

RelativeCounter.asPercentage()
RelativeCounter.asProportion()
RelativeCounter.items() a set-like object providing a view on D's items

SingletonCounter

class music21.common.objects.SingletonCounter

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

SlottedObjectMixin

class music21.common.objects.SlottedObjectMixin

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
>>> out = pickle.dumps(s)
>>> pickleLoad = pickle.loads(out)
>>> pickleLoad.time, pickleLoad.frequency
(0.125, 440.0)

Timer

class music21.common.objects.Timer

An object for timing. Call it to get the current time since starting.

>>> timer = common.Timer()
>>> now = timer()
>>> 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

Timer methods

Timer.clear()
Timer.start()

Explicit start method; will clear previous values.

Start always happens on initialization.

Timer.stop()

defaultlist

class music21.common.objects.defaultlist(fx)

Call a function for every time something is missing:

>>> a = common.defaultlist(lambda:True)
>>> a[5]
True

defaultlist methods

defaultlist.__getitem__(index)

x.__getitem__(y) <==> x[y]