music21.common.objects

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()

SingletonCounter

class music21.common.objects.SingletonCounter

A simple counter that can produce unique numbers 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

SlottedObject

class music21.common.objects.SlottedObject

Mixin.

Provides template for classes implementing slots allowing it to be pickled properly.

Only use SlottedObjects 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.SlottedObject):
...     __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:

TO BE DEPRECATED... soon...

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