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.

Ignores differences in .id

EqualSlottedObjectMixin bases

Iterator

class music21.common.objects.Iterator(data)

A simple Iterator object used to handle iteration of Streams and other list-like objects.

>>> i = common.Iterator([2, 3, 4])
>>> for x in i:
...     print(x)
2
3
4
>>> for y in i:
...     print(y)
2
3
4

Iterator methods

Iterator.next()

RelativeCounter

class music21.common.objects.RelativeCounter(**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 in Py3.6+ and in random order in Py3.4-3.5.

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

Timer

class music21.common.objects.Timer

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

>>> t = common.Timer()
>>> now = t()
>>> nownow = t()
>>> nownow > now
True

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

>>> t.stop()
>>> stopTime = t()
>>> stopNow = t()
>>> 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