music21.common.stringTools¶
Tools for working with strings
Functions¶
-
music21.common.stringTools.
camelCaseToHyphen
(usrStr, replacement='-')¶ Given a camel-cased string, or a mixture of numbers and characters, create a space separated string.
The replacement can be specified to be something besides a hyphen, but only a single character and not (for internal reasons) an uppercase character.
- code from http://stackoverflow.com/questions/1175208/
- elegant-python-function-to-convert-camelcase-to-camel-case
>>> common.camelCaseToHyphen('movementName') 'movement-name' >>> common.camelCaseToHyphen('movementNameName') 'movement-name-name'
>>> common.camelCaseToHyphen('fileName', replacement='_') 'file_name'
Some things you cannot do:
>>> common.camelCaseToHyphen('fileName', replacement='NotFound') Traceback (most recent call last): Exception: Replacement must be a single character.
>>> common.camelCaseToHyphen('fileName', replacement='A') Traceback (most recent call last): Exception: Replacement cannot be an uppercase character.
-
music21.common.stringTools.
formatStr
(msg, *arguments, **keywords)¶ Format one or more data elements into string suitable for printing straight to stderr or other outputs
>>> a = common.formatStr('test', '1', 2, 3) >>> print(a) test 1 2 3
-
music21.common.stringTools.
getMd5
(value=None)¶ Return an md5 hash from a string. If no value is given then the current time plus a random number is encoded.
>>> common.getMd5('test') '098f6bcd4621d373cade4e832627b4f6'
Return type: str
-
music21.common.stringTools.
getNumFromStr
(usrStr, numbers='0123456789')¶ Given a string, extract any numbers. Return two strings, the numbers (as strings) and the remaining characters.
>>> common.getNumFromStr('23a') ('23', 'a') >>> common.getNumFromStr('23a954sdfwer') ('23954', 'asdfwer') >>> common.getNumFromStr('') ('', '')
Return type: tuple(str)
-
music21.common.stringTools.
hyphenToCamelCase
(usrStr, replacement='-')¶ given a hyphen-connected-string, change it to a camelCaseConnectedString.
The replacement can be specified to be something besides a hyphen.
code from http://stackoverflow.com/questions/4303492/
how-can-i-simplify-this-conversion-from-underscore-to-camelcase-in-python>>> common.hyphenToCamelCase('movement-name') 'movementName'
>>> common.hyphenToCamelCase('movement_name', replacement='_') 'movementName'
-
music21.common.stringTools.
normalizeFilename
(name)¶ take a name that might contain unicode characters, punctuation, or spaces and normalize it so that it is POSIX compliant (except for the limit on length).
Takes in a string or unicode string and returns a string (unicode in Py3) without any accented characters.
>>> common.normalizeFilename(u'03-Niccolò all’lessandra.not really.xml') '03-Niccolo_alllessandra_not_really.xml'
Return type: str
-
music21.common.stringTools.
spaceCamelCase
(usrStr, replaceUnderscore=True, fixMeList=None)¶ Given a camel-cased string, or a mixture of numbers and characters, create a space separated string.
If replaceUnderscore is True (default) then underscores also become spaces (but without the _)
>>> common.spaceCamelCase('thisIsATest') 'this Is A Test' >>> common.spaceCamelCase('ThisIsATest') 'This Is A Test' >>> common.spaceCamelCase('movement3') 'movement 3' >>> common.spaceCamelCase('opus41no1') 'opus 41 no 1' >>> common.spaceCamelCase('opus23402no219235') 'opus 23402 no 219235' >>> common.spaceCamelCase('opus23402no219235').title() 'Opus 23402 No 219235'
There is a small list called fixMeList that can fix mistakes.
>>> common.spaceCamelCase('PMFC22') 'PMFC 22'
>>> common.spaceCamelCase('hello_myke') 'hello myke' >>> common.spaceCamelCase('hello_myke', replaceUnderscore = False) 'hello_myke'
Return type: str
-
music21.common.stringTools.
stripAccents
(inputString)¶ removes accents from unicode strings.
>>> s = u'trés vite'
>>> u'é' in s True
This works on Python2, but the doctest does not.
>>> if ext.six.PY3: ... common.stripAccents(s) ... else: 'tres vite' 'tres vite'
-
music21.common.stringTools.
toUnicode
(usrStr)¶ Convert this tring to a unicode string; if already a unicode string, do nothing.
>>> common.toUnicode('test') 'test' >>> common.toUnicode(u'test') 'test'
Note: this method is NOT USED and could disappear without notice.
Return type: str
-
music21.common.stringTools.
whitespaceEqual
(a, b)¶ returns True if a and b are equal except for whitespace differences
>>> a = " hello \nthere " >>> b = "hello there" >>> c = " bye there " >>> common.whitespaceEqual(a,b) True >>> common.whitespaceEqual(a,c) False