music21.common.misc

If it doesn’t fit anywhere else in the common directory, you’ll find it here…

Functions

music21.common.misc.cleanedFlatNotation(music_str: str) str

Returns a copy of the given string where each occurrence of a flat note specified with a ‘b’ is replaced by a ‘-‘.

music_str is a string containing a note specified (for example in a chord)

Returns a new string with flats only specified with ‘-‘.

>>> common.cleanedFlatNotation('Cb')
'C-'
music21.common.misc.defaultDeepcopy(obj, memo, callInit=True)

Unfortunately, it is not possible to do something like:

def __deepcopy__(self, memo):
    if self._noDeepcopy:
        return self.__class__()
    else:
        copy.deepcopy(self, memo, ignore__deepcopy__=True)

Or, else: return NotImplemented

so that’s what this is for:

def __deepcopy__(self, memo):
    if self._noDeepcopy:
        return self.__class__()
    else:
        return common.defaultDeepcopy(self, memo)

looks through both __slots__ and __dict__ and does a deepcopy of anything in each of them and returns the new object.

If callInit is False, then only __new__() is called. This is much faster if you’re just going to overload every instance variable or is required if __init__ has required variables in initialization.

music21.common.misc.flattenList(originalList: List) List

Flatten a list of lists into a flat list

but not a list of lists of lists…

>>> l = [[1, 2, 3], [4, 5], [6]]
>>> common.flattenList(l)
[1, 2, 3, 4, 5, 6]
music21.common.misc.getMissingImportStr(modNameList)

Given a list of missing module names, returns a nicely-formatted message to the user that gives instructions on how to expand music21 with optional packages.

>>> print(common.getMissingImportStr(['matplotlib']))
Certain music21 functions might need the optional package matplotlib;
if you run into errors, install it by following the instructions at
http://mit.edu/music21/doc/installing/installAdditional.html
>>> print(common.getMissingImportStr(['matplotlib', 'numpy']))
Certain music21 functions might need these optional packages: matplotlib, numpy;
if you run into errors, install them by following the instructions at
http://mit.edu/music21/doc/installing/installAdditional.html
music21.common.misc.getPlatform() str

Return the name of the platform, where platforms are divided between ‘win’ (for Windows), ‘darwin’ (for MacOS X), and ‘nix’ for (GNU/Linux and other variants).

Does not discern between Linux/FreeBSD, etc.

Lowercase names are for backwards compatibility – this existed before the platform module.

music21.common.misc.macOSVersion() Tuple[int, int, int]

On a Mac returns the current version as a tuple of (currently 3) ints, such as: (10, 5, 6) for 10.5.6.

On other systems, returns (0, 0, 0)

music21.common.misc.pitchList(pitchL)

utility method that replicates the previous behavior of lists of pitches.

May be moved in v8 or later to a common.testing or test.X module.

music21.common.misc.runningUnderIPython() bool

return bool if we are running under IPython Notebook (not IPython terminal) or Google Colabatory (colab).

Methods based on:

https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook

(No tests provided here, since results will differ depending on environment)

May be moved in v8 or later to the ipython21 module. Implementation may change.

music21.common.misc.sortModules(moduleList: Iterable[Any]) List[object]

Sort a list of imported module names such that most recently modified is first. In ties, last access time is used then module name

Will return a different order each time depending on the last mod time

music21.common.misc.unique(originalList: Iterable, *, key: Callable | None = None) List

Return a List of unique items from an iterable, preserving order. (unlike casting to a set and back)

(And why is this not already in Python?)

>>> common.misc.unique([3, 2, 4, 3, 2, 5])
[3, 2, 4, 5]

Works on any iterable, but order might not be preserved for sets, etc.

>>> common.misc.unique(range(5))
[0, 1, 2, 3, 4]

If key is a function then use that to get the value:

>>> s = converter.parse('tinyNotation: c4 E d C f# e a')
>>> common.misc.unique(s.recurse().notes, key=lambda n: n.name)
 [<music21.note.Note C>,
  <music21.note.Note E>,
  <music21.note.Note D>,
  <music21.note.Note F#>,
  <music21.note.Note A>]