music21.common.misc¶
If it doesn’t fit anywhere else in the common directory, you’ll find it here...
Functions¶
-
music21.common.misc.cpus()¶ Returns the number of CPUs or if >= 3, one less (to leave something out for multiprocessing)
-
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:
- common.defaultDeepcopy(obj, 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.
-
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()¶ 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).
Return type: str
-
music21.common.misc.pitchList(pitchL)¶ utility method that replicates the previous behavior of lists of pitches
-
music21.common.misc.runNonParallel(iterable, parallelFunction, updateFunction=None, updateMultiply=3, unpackIterable=False)¶ This is intended to be a perfect drop in replacement for runParallel, except that it runs on one core only, and not in parallel.
Used, for instance, if we’re already in a parallel function.
-
music21.common.misc.runParallel(iterable, parallelFunction, updateFunction=None, updateMultiply=3, unpackIterable=False)¶ runs parallelFunction over iterable in parallel, optionally calling updateFunction after each common.cpus * updateMultiply calls.
Setting updateMultiply too small can make it so that cores wait around when they could be working if one CPU has a particularly hard task. Setting it too high can make it seem like the job has hung.
updateFunction should take three arguments: the current position, the total to run, and the most recent results. It does not need to be pickleable, and in fact, a bound method might be very useful here. Or updateFunction can be “True” which just prints a generic message.
If unpackIterable is True then each element in iterable is considered a list or tuple of different arguments to delayFunction.
As of Python 2.7, partial functions are pickleable, so if you need to pass the same arguments to parallelFunction each time, make it a partial function before passing it to runParallel.
Note that parallelFunction, iterable’s contents, and the results of calling parallelFunction must all be pickleable, and that if pickling the contents or unpickling the results takes a lot of time, you won’t get nearly the speedup from this function as you might expect. The big culprit here is definitely music21 streams.
-
music21.common.misc.runningUnderIPython()¶ return bool if we are running under iPython Notebook (not iPython)
(no tests, since will be different)
This post:
http://stackoverflow.com/questions/15411967/ how-can-i-check-if-code-is-executed-in-the-ipython-notebook
says not to do this, but really, I can’t think of another way to have different output as default.
Return type: bool
-
music21.common.misc.sortModules(moduleList)¶ Sort a lost of imported module names such that most recently modified is first. In ties, last accesstime is used then module name
Will return a different order each time depending on the last mod time
Return type: list(str)