Utility constants, dictionaries, functions, and objects used throughout music21.
>>> fromRoman('ii')
2
>>> fromRoman('vii')
7
>>> toRoman(2)
'II'
>>> toRoman(7)
'VII'
returns true if a and b are equal except for whitespace differences
>>> a = " hello there "
>>> b = "hello there"
>>> c = " bye there "
>>> basicallyEqual(a,b)
True
>>> basicallyEqual(a,c)
False
returns true if a and b are equal except for whitespace differences
>>> a = " hello there "
>>> b = "hello there"
>>> c = " bye there "
>>> basicallyEqual(a,b)
True
>>> basicallyEqual(a,c)
False
dotMultiplier(dots) returns how long to multiply the note length of a note in order to get the note length with n dots
>>> dotMultiplier(1)
1.5
>>> dotMultiplier(2)
1.75
>>> dotMultiplier(3)
1.875
Given a format defined either by a format name or an extension, return the format name as well as the output exensions. Note that .mxl and .mx are only considered MusicXML input formats.
>>> findFormat('mx')
('musicxml', '.xml')
>>> findFormat('.mxl')
('musicxml', '.xml')
>>> findFormat('musicxml')
('musicxml', '.xml')
>>> findFormat('jpeg')
('jpeg', '.jpg')
>>> findFormat('lily')
('lilypond', '.ly')
>>> findFormat('jpeg')
('jpeg', '.jpg')
>>> findFormat('humdrum')
('humdrum', '.krn')
>>> findFormat('txt')
('text', '.txt')
>>> findFormat('textline')
('textline', '.txt')
Given a file path (relative or absolute) find format and extension used (not the output extension)
>>> findFormatExtFile('test.mx')
('musicxml', '.mx')
>>> findFormatExtFile('long/file/path/test-2009.03.02.xml')
('musicxml', '.xml')
>>> findFormatExtFile('long/file/path.intermediate.png/test-2009.03.xml')
('musicxml', '.xml')
>>> findFormatExtFile('test.mus')
('finale', '.mus')
>>> findFormatExtFile('test')
(None, None)
Windows drive + pickle
>>> findFormatExtFile('d:/long/file/path/test.p')
('pickle', '.p')
On a windows networked filesystem
>>> findFormatExtFile('\\long\file\path\test.krn')
('humdrum', '.krn')
Given a URL, attempt to find the extension. This may scrub arguments in a URL, or simply look at the last characters.
>>> urlA = 'http://kern.ccarh.org/cgi-bin/ksdata?l=users/craig/classical/schubert/piano/d0576&file=d0576-06.krn&f=xml'
>>> urlB = 'http://kern.ccarh.org/cgi-bin/ksdata?l=users/craig/classical/schubert/piano/d0576&file=d0576-06.krn&f=kern'
>>> urlC = 'http://kern.ccarh.org/cgi-bin/ksdata?l=users/craig/classical/bach/cello&file=bwv1007-01.krn&f=xml'
>>> urlD = 'http://static.wikifonia.org/4918/musicxml.mxl'
>>> urlE = 'http://static.wikifonia.org/4306/musicxml.mxl'
>>> urlF = 'http://junk'
>>> findFormatExtURL(urlA)
('musicxml', '.xml')
>>> findFormatExtURL(urlB)
('humdrum', '.krn')
>>> findFormatExtURL(urlC)
('musicxml', '.xml')
>>> findFormatExtURL(urlD)
('musicxml', '.mxl')
>>> findFormatExtURL(urlE)
('musicxml', '.mxl')
>>> findFormatExtURL(urlF)
(None, None)
Given a file path (relative or absolute) return the format
>>> findFormatFile('test.xml')
'musicxml'
>>> findFormatFile('long/file/path/test-2009.03.02.xml')
'musicxml'
>>> findFormatFile('long/file/path.intermediate.png/test-2009.03.xml')
'musicxml'
Windows drive + pickle
>>> findFormatFile('d:/long/file/path/test.p')
'pickle'
On a windows networked filesystem
>>> findFormatFile('\\long\file\path\test.krn')
'humdrum'
Given an input format, find and return all possible input extensions.
>>> a = findInputExtension('musicxml')
>>> a
['.xml', '.mxl', '.mx']
>>> a = findInputExtension('mx')
>>> a
['.xml', '.mxl', '.mx']
>>> a = findInputExtension('humdrum')
>>> a
['.krn']
Format one or more data elements into string suitable for printing straight to stderr or other outputs
>>> a = formatStr('test', '1', 2, 3)
>>> print a
test 1 2 3
<BLANKLINE>
Return a string from an md5 haslib
>>> getMd5('test')
'098f6bcd4621d373cade4e832627b4f6'
Given a string, extract any numbers. Return two strings, the numbers (as strings) and the remaining characters.
>>> getNumFromStr('23a')
('23', 'a')
>>> getNumFromStr('23a954sdfwer')
('23954', 'asdfwer')
>>> getNumFromStr('')
('', '')
Returns True if is the object can be iter’d over
>>> isIterable([])
True
>>> isIterable('sharp')
False
>>> isIterable((None, None))
True
>>> import music21.stream
>>> isIterable(music21.stream.Stream())
True
Returns True if is a List or a Set or a Tuple #TODO: add immutable sets and pre 2.6 set support
>>> isListLike([])
True
>>> isListLike('sharp')
False
>>> isListLike((None, None))
True
>>> import music21.stream
>>> isListLike(music21.stream.Stream())
False
check if usrData is a number (float, int, long, Decimal), return boolean IMPROVE: when 2.6 is everywhere: add numbers class.
>>> isNum(3.0)
True
>>> isNum(3)
True
>>> isNum('three')
False
returns True if argument is either a power of 2 or a reciprocal of a power of 2. Uses almostEquals so that a float whose reminder after taking a log is nearly zero is still True
>>> isPowerOfTwo(3)
False
>>> isPowerOfTwo(18)
False
>>> isPowerOfTwo(1024)
True
>>> isPowerOfTwo(1024.01)
False
>>> isPowerOfTwo(1024.00001)
True
Check of usrData is some form of string, including unicode.
>>> isStr(3)
False
>>> isStr('sharp')
True
>>> isStr(u'flat')
True
Test if an object is a weakref
>>> class Mock(object): pass
>>> a1 = Mock()
>>> a2 = Mock()
>>> isWeakref(a1)
False
>>> isWeakref(3)
False
>>> isWeakref(wrapWeakref(a1))
True
>>> lcm([3,4,5])
60
>>> lcm([3,4])
12
>>> lcm([1,2])
2
>>> lcm([3,6])
6
Given a number, return an integer if it is very close to an integer, otherwise, return a float.
>>> numToIntOrFloat(1.0)
1
>>> numToIntOrFloat(1.00003)
1.00003
>>> numToIntOrFloat(1.5)
1.5
>>> numToIntOrFloat(1.0000000005)
1
Given two files, sort by most recent. Return only the file paths.
>>> a = os.listdir(os.curdir)
>>> b = sortFilesRecent(a)
Given a camel-cased string, or a mixture of numbers and characters, create a space separated string.
>>> spaceCamelCase('thisIsATest')
'this Is A Test'
>>> spaceCamelCase('ThisIsATest')
'This Is A Test'
>>> spaceCamelCase('movement3')
'movement 3'
>>> spaceCamelCase('opus41no1')
'opus 41 no 1'
>>> spaceCamelCase('opus23402no219235')
'opus 23402 no 219235'
>>> spaceCamelCase('opus23402no219235').title()
'Opus 23402 No 219235'
Function that changes all memory addresses in the given textString with (replacement). This is useful for testing that a function gives an expected result even if the result contains references to memory locations. So for instance:
>>> stripAddresses("{0.0} <music21.clef.TrebleClef object at 0x02A87AD0>")
'{0.0} <music21.clef.TrebleClef object at ADDRESS>'
while this is left alone:
>>> stripAddresses("{0.0} <music21.humdrum.MiscTandam *>I humdrum control>")
'{0.0} <music21.humdrum.MiscTandam *>I humdrum control>'
utility function that gets an object that might be an object itself or a weak reference to an object.
>>> class Mock(object): pass
>>> a1 = Mock()
>>> a2 = Mock()
>>> a2.strong = a1
>>> a2.weak = wrapWeakref(a1)
>>> unwrapWeakref(a2.strong) is a1
True
>>> unwrapWeakref(a2.weak) is a1
True
>>> unwrapWeakref(a2.strong) is unwrapWeakref(a2.weak)
True
A replacement for dictionaries that behave a bit more like perl hashes. No more KeyErrors. The difference between defHash and defaultdict is that the Dict values come first in the definition and that default can be set to None (which it is) or to any object. If you want a factory that makes hashes with a particular different default, use: falsehash = lambda h = None: defHash(h, default = False) a = falsehash({“A”: falsehash(), “B”: falsehash()}) print(a[“A”][“hi”]) # returns False there’s probably a way to use this to create a data structure of arbitrary dimensionality, though it escapes this author. if callDefault is True then the default is called: defHash(default = list, callDefault = True) will create a new List for each element
inherits from: dict
defHash attributes
Attributes without Documentation: default, callDefault
Attributes inherited from dict: fromkeys
defHash methods
- get(key, *args)¶
- No documentation.
Methods inherited from dict: clear(), copy(), has_key(), items(), iteritems(), iterkeys(), itervalues(), keys(), pop(), popitem(), setdefault(), update(), values()
A replacement for lists that behave a bit more like perl arrays. No more ListErrors.
inherits from: list
defList attributes
Attributes without Documentation: default, callDefault
defList methods
Methods inherited from list: append(), count(), extend(), index(), insert(), pop(), remove(), reverse(), sort()