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'
First letter can be uppercase as well:
>>> 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): ValueError: Replacement must be a single character.
>>> common.camelCaseToHyphen('fileName', replacement='A') Traceback (most recent call last): ValueError: Replacement cannot be an uppercase character.
- Return type
str
-
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
- Return type
str
-
music21.common.stringTools.getMd5(value=None) → str¶ 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'
-
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('23a954Hello') ('23954', 'aHello') >>> 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.
This code is 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'
- Return type
str
-
music21.common.stringTools.normalizeFilename(name: str) → str¶ 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('03-Niccolò all’lessandra.not really.xml') '03-Niccolo_alllessandra_not_really.xml'
-
music21.common.stringTools.removePunctuation(s)¶ Remove all punctuation from a string.
>>> common.removePunctuation('This, is! my (face).') 'This is my face'
- 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 = 'trés vite' >>> 'é' in s True >>> common.stripAccents(s) 'tres vite'
- Return type
str
-
music21.common.stringTools.whitespaceEqual(a, b)¶ returns True if a and b are equal except for whitespace differences
>>> a = " hello \n there " >>> b = "hello there" >>> c = " bye there " >>> common.whitespaceEqual(a, b) True >>> common.whitespaceEqual(a, c) False
- Return type
bool