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: Any, memo=None, *, ignoreAttributes: Iterable[str] = ())

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)

Does a deepcopy of the state returned by __reduce_ex__ for protocol 4.

  • Changed in v9: callInit is removed, replaced with ignoreAttributes. uses __reduce_ex__ internally.

music21.common.misc.flattenList(originalList: Iterable[Iterable[_T]]) list[_T]

Flatten a list of lists into a flat list:

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

But not a list of lists-of-lists!

>>> l2 = [[1, 2, 3], [4, 5], [6, [7, 8]]]
>>> common.flattenList(l2)
[1, 2, 3, 4, 5, 6, [7, 8]]
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
https://www.music21.org/music21docs/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
https://www.music21.org/music21docs/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.runningInNotebook() bool

return bool if we are running under Jupyter 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)

music21.common.misc.runningUnderIPython() bool

DEPRECATED in v9: use runningInNotebook() instead

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>]