music21.common.objects¶
EqualSlottedObjectMixin¶
- class music21.common.objects.EqualSlottedObjectMixin[source]¶
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
FrozenObject¶
FrozenObject bases
FrozenObject methods
Methods inherited from EqualSlottedObjectMixin:
RelativeCounter¶
- class music21.common.objects.RelativeCounter(iterable=None, /, **kwds)[source]¶
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[source]¶
- RelativeCounter.asProportion() RelativeCounter[source]¶
SingletonCounter¶
- class music21.common.objects.SingletonCounter[source]¶
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[source]¶
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[source]¶
An object for timing. Call it to get the current time since starting.
>>> timer = common.Timer() >>> now = timer() >>> nowNow = timer() >>> nowNow > now True
Call
stopto stop it. Callingstartagain 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
defaultlist¶
- class music21.common.objects.defaultlist(fx: Callable[[], Any])[source]¶
A list that calls a function every time something is missing, rather than giving IndexErrors.
` a = common.defaultlist(lambda:True) a[5] is True `Deprecated in v11: use a plain list instead while checking to see if the value exists. This was a transitional helper for the early days of music21 when Myke was still thinking in Perl. (was called PerlArray early on)
defaultlist methods