本文整理汇总了Python中music21.common.isListLike函数的典型用法代码示例。如果您正苦于以下问题:Python isListLike函数的具体用法?Python isListLike怎么用?Python isListLike使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isListLike函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getWorkList
def getWorkList(workName, movementNumber=None, extList=None):
'''Search the corpus and return a list of works, always in a list. If no matches are found, an empty list is returned.
>>> len(getWorkList('beethoven/opus18no1'))
8
>>> len(getWorkList('beethoven/opus18no1', 1))
2
>>> len(getWorkList('beethoven/opus18no1', 1, '.krn'))
1
>>> len(getWorkList('beethoven/opus18no1', 1, '.xml'))
1
>>> len(getWorkList('beethoven/opus18no1', 0, '.xml'))
0
'''
if not common.isListLike(extList):
extList = [extList]
paths = getPaths(extList)
post = []
# permit workName to be a list of paths/branches
if common.isListLike(workName):
workName = os.path.sep.join(workName)
# replace with os-dependent separators
workSlashes = workName.replace('/', os.path.sep)
for path in paths:
if workName.lower() in path.lower():
post.append(path)
elif workSlashes.lower() in path.lower():
post.append(path)
post.sort()
postMvt = []
if movementNumber is not None and len(post) > 0:
movementStrList = ['movement%s' % movementNumber]
for fp in post:
for movementStr in movementStrList:
if movementStr.lower() in fp.lower():
postMvt.append(fp)
if len(postMvt) == 0:
pass # return an empty list
else:
postMvt = post
if len(postMvt) == 0:
return []
else:
return postMvt
示例2: getWork
def getWork(workName, movementNumber=None, fileExtensions=None):
'''
Search the corpus, then the virtual corpus, for a work, and return a file
path or URL. N.B. does not parse the work: but it's suitable for passing
to converter.parse.
This method will return either a list of file paths or, if there is a
single match, a single file path. If no matches are found an Exception is
raised.
::
>>> import os
>>> from music21 import corpus
>>> a = corpus.getWork('opus74no2', 4)
>>> a.endswith(os.path.sep.join([
... 'haydn', 'opus74no2', 'movement4.mxl']))
True
::
>>> a = corpus.getWork(['haydn', 'opus74no2', 'movement4.xml'])
>>> a.endswith(os.path.sep.join([
... 'haydn', 'opus74no2', 'movement4.mxl']))
True
::
>>> trecentoFiles = corpus.getWork('trecento')
>>> len(trecentoFiles) > 100 and len(trecentoFiles) < 200
True
'''
if not common.isListLike(fileExtensions):
fileExtensions = [fileExtensions]
results = getWorkList(workName, movementNumber, fileExtensions)
if len(results) == 0:
if common.isListLike(workName):
workName = os.path.sep.join(workName)
if workName.endswith(".xml"): # might be compressed MXL file
newWorkName = workName[0:len(workName) - 4] + ".mxl"
return getWork(newWorkName, movementNumber, fileExtensions)
results = getVirtualWorkList(workName, movementNumber, fileExtensions)
if len(results) == 1:
return results[0]
elif len(results) == 0:
raise CorpusException(
'Could not find a file/url that met these criteria')
return results
示例3: getDoc
def getDoc(self, partName):
element = self.getElement(partName)
if hasattr(self.srcNameEval, '_DOC_ATTR'):
docAttr = self.srcNameEval._DOC_ATTR
else:
docAttr = {}
match = None
if partName in docAttr.keys():
match = docAttr[partName]
# if its an undocumented public attribute and basic python
# data structure, we do not want to show that documentation
elif (element.kind in ['data'] and (
common.isStr(element.object) or
common.isListLike(element.object) or
common.isNum(element.object)
)):
pass
else:
try:
match = element.object.__doc__
except AttributeError:
match = None
if match == None:
return NO_DOC
else:
return match
示例4: __setitem__
def __setitem__(self, key, value):
"""
Dictionary-like setting. Changes are made and written to the user
configuration file.
>>> from music21 import environment
>>> us = environment.UserSettings()
>>> us['musicxmlPath'] = 'asdfwerasdffasdfwer'
Traceback (most recent call last):
UserSettingsException: attempting to set a path that does not exist: asdfwerasdffasdfwer
>>> us['localCorpusPath'] = '/path/to/local'
Traceback (most recent call last):
UserSettingsException: attempting to set a path that does not exist: /path/to/local
"""
# NOTE: testing setting of any UserSettings key will result
# in a change in your local preferences files
# before setting value, see if this is a path and test existence
# this will accept localCorpusPath
if key in self._environment.getKeysToPaths():
# try to expand user if found; otherwise return unaltered
if value is not None:
value = os.path.expanduser(value)
if not os.path.exists(value):
raise UserSettingsException("attempting to set a path that does not exist: {}".format(value))
# when setting a local corpus setting, if not a list, append
elif key == "localCorpusSettings":
if not common.isListLike(value):
raise UserSettingsException("localCorpusSettings must be provided as a list.")
# location specific, cannot test further
self._environment.__setitem__(key, value)
self._environment.write()
示例5: __init__
def __init__(self, target=()):
super().__init__()
if not common.isListLike(target):
target = (target,)
self.target = target
self.numToFind = len(target)
示例6: getWork
def getWork(workName, movementNumber=None, extList=None):
'''Search the corpus, then the virtual corpus, for a work. This method will return either a list of file paths or, if there is a single match, a single file path. If no matches are found an Exception is raised.
>>> import os
>>> a = getWork('opus74no2', 4)
>>> a.endswith(os.path.sep.join(['haydn', 'opus74no2', 'movement4.xml']))
True
>>> a = getWork(['haydn', 'opus74no2', 'movement4.xml'])
>>> a.endswith(os.path.sep.join(['haydn', 'opus74no2', 'movement4.xml']))
True
'''
if not common.isListLike(extList):
extList = [extList]
post = getWorkList(workName, movementNumber, extList)
if len(post) == 0:
post = getVirtualWorkList(workName, movementNumber, extList)
if len(post) == 1:
return post[0]
elif len(post) == 0:
raise CorpusException("Could not find a file/url that met this criteria")
else: # return a list
return post
示例7: parse
def parse(value, *args, **keywords):
'''Given a file path, encoded data in a Python string, or a URL, attempt to parse the item into a Stream. Note: URL downloading will not happen automatically unless the user has set their Environment "autoDownload" preference to "allow".
>>> s = parse(["E4 r f# g=lastG trip{b-8 a g} c", "3/4"])
>>> s = parse("E8 f# g#' G f g# g G#", "2/4")
'''
#environLocal.printDebug(['attempting to parse()', value])
if 'forceSource' in keywords.keys():
forceSource = keywords['forceSource']
else:
forceSource = False
if common.isListLike(value) or len(args) > 0: # tiny notation list
if len(args) > 0: # add additional args to a lost
value = [value] + list(args)
return parseData(value)
elif os.path.exists(value):
return parseFile(value, forceSource=forceSource)
elif value.startswith('http://'):
# its a url; may need to broaden these criteria
return parseURL(value, forceSource=forceSource)
else:
return parseData(value)
示例8: parseFile
def parseFile(self, fp, number=None):
'''
parse fp and number
'''
from music21 import converter
from music21 import musedata as musedataModule
from music21.musedata import translate as musedataTranslate
mdw = musedataModule.MuseDataWork()
af = converter.ArchiveManager(fp)
#environLocal.printDebug(['ConverterMuseData: parseFile', fp, af.isArchive()])
# for dealing with one or more files
if fp.endswith('.zip') or af.isArchive():
#environLocal.printDebug(['ConverterMuseData: found archive', fp])
# get data will return all data from the zip as a single string
for partStr in af.getData(dataFormat='musedata'):
#environLocal.printDebug(['partStr', len(partStr)])
mdw.addString(partStr)
else:
if os.path.isdir(fp):
mdd = musedataModule.MuseDataDirectory(fp)
fpList = mdd.getPaths()
elif not common.isListLike(fp):
fpList = [fp]
else:
fpList = fp
for fp in fpList:
mdw.addFile(fp)
#environLocal.printDebug(['ConverterMuseData: mdw file count', len(mdw.files)])
musedataTranslate.museDataWorkToStreamScore(mdw, self.stream)
示例9: parseWork
def parseWork(workName, movementNumber=None, number=None,
extList=None, forceSource=False):
'''Search the corpus, then the virtual corpus, for a work, and return a parsed :class:`music21.stream.Stream`.
If `movementNumber` is defined, and a movement is included in the corpus, that movement will be returned.
If `number` is defined, and the work is a collection with multiple components, that work number will be returned.
If `forceSource` is True, the original file will always be loaded and pickled files, if available, will be ignored.
>>> aStream = parseWork('opus74no1/movement3')
'''
if not common.isListLike(extList):
extList = [extList]
post = getWorkList(workName, movementNumber, extList)
#environLocal.printDebug(['result of getWorkList()', post])
if len(post) == 0:
post = getVirtualWorkList(workName, movementNumber, extList)
if len(post) == 1:
fp = post[0]
elif len(post) == 0:
raise CorpusException("Could not find a work that met this criteria")
else: # greater than zero:
fp = post[0] # get first
return converter.parse(fp, forceSource=forceSource, number=number)
示例10: getWorkList
def getWorkList(
self,
workName,
movementNumber=None,
fileExtensions=None,
):
'''
Given a work name, search all virtual works and return a list of URLs
for any matches.
>>> from music21 import corpus
>>> virtualCorpus = corpus.VirtualCorpus()
>>> virtualCorpus.getWorkList('bach/bwv1007/prelude')
['http://kern.ccarh.org/cgi-bin/ksdata?l=cc/bach/cello&file=bwv1007-01.krn&f=xml']
>>> virtualCorpus.getWorkList('junk')
[]
'''
if not common.isListLike(fileExtensions):
fileExtensions = [fileExtensions]
for obj in VirtualCorpus._virtual_works:
if obj.corpusPath is not None and \
workName.lower() in obj.corpusPath.lower():
return obj.getUrlByExt(fileExtensions)
return []
示例11: _setPitches
def _setPitches(self, value):
if common.isListLike(value):
if 'Pitch' in value[0].classes:
self.pitch = value[0]
else:
raise NoteException('must provide a list containing a Pitch, not: %s' % value)
else:
raise NoteException('cannot set pitches with provided object: %s' % value)
示例12: cacheMetadata
def cacheMetadata(
corpusNames=('local', 'core', 'virtual'),
useMultiprocessing=True,
):
'''
Cache metadata from corpuses in `corpusNames` as local cache files:
Call as ``metadata.cacheMetadata()``
'''
from music21 import corpus
from music21 import metadata
if not common.isListLike(corpusNames):
corpusNames = (corpusNames,)
timer = common.Timer()
timer.start()
# store list of file paths that caused an error
failingFilePaths = []
# the core cache is based on local files stored in music21
# virtual is on-line
for corpusName in corpusNames:
if corpusName == 'core':
metadataBundle = metadata.MetadataBundle.fromCoreCorpus()
paths = corpus.getCorePaths()
useCorpus = True
elif corpusName == 'local':
metadataBundle = metadata.MetadataBundle.fromLocalCorpus()
paths = corpus.getLocalPaths()
useCorpus = False
elif corpusName == 'virtual':
metadataBundle = metadata.MetadataBundle.fromVirtualCorpus()
paths = corpus.getVirtualPaths()
useCorpus = False
else:
message = 'invalid corpus name provided: {0!r}'.format(corpusName)
raise MetadataCacheException(message)
message = 'metadata cache: starting processing of paths: {0}'.format(
len(paths))
environLocal.printDebug(message)
failingFilePaths += metadataBundle.addFromPaths(
paths,
useCorpus=useCorpus,
useMultiprocessing=useMultiprocessing,
)
message = 'cache: writing time: {0} md items: {1}'.format(
timer, len(metadataBundle))
environLocal.printDebug(message)
del metadataBundle
message = 'cache: final writing time: {0} seconds'.format(timer)
environLocal.printDebug(message)
for failingFilePath in failingFilePaths:
message = 'path failed to parse: {0}'.format(failingFilePath)
environLocal.printDebug(message)
示例13: cacheMetadata
def cacheMetadata(corpusNames=('local',)):
'''
Rebuild the metadata cache.
'''
if not common.isListLike(corpusNames):
corpusNames = [corpusNames]
for name in corpusNames:
corpora.Corpus._metadataBundles[name] = None
metadata.cacheMetadata(corpusNames)
示例14: getPaths
def getPaths(extList=None, expandExtensions=True):
'''Get all paths in the corpus that match a known extension, or an extenion
provided by an argument.
If `expandExtensions` is True, a format for an extension, and related extensions, will replaced by all known input extensions. This is convenient when an input format might match for multiple extensions.
>>> a = getPaths()
>>> len(a) > 30
True
>>> a = getPaths('krn')
>>> len(a) >= 4
True
>>> a = getPaths('abc')
>>> len(a) >= 10
True
'''
if not common.isListLike(extList):
extList = [extList]
if extList == [None]:
extList = _ALL_EXTENSIONS
elif expandExtensions:
extMod = []
for e in extList:
extMod += common.findInputExtension(e)
extList = extMod
#environLocal.printDebug(['getting paths with extensions:', extList])
paths = []
for moduleName in MODULES:
if not hasattr(moduleName, '__path__'):
# when importing a package name (a directory) the moduleName
# may be a list of all paths contained within the package
# this seems to be dependent on the context of the call:
# from the command line is different than from the interpreter
dirListing = moduleName
else:
# returns a list with one or more paths
# the first is the path to the directory that contains xml files
dir = moduleName.__path__[0]
dirListing = [os.path.join(dir, x) for x in os.listdir(dir)]
for fp in dirListing:
if fp in paths:
continue
match = False
for ext in extList:
if fp.endswith(ext):
match = True
break
if match:
if fp not in paths:
paths.append(fp)
return paths
示例15: _dynamicToWeight
def _dynamicToWeight(targets):
# permit a stream
if hasattr(targets, 'isStream') and targets.isStream:
pass
elif not common.isListLike(targets):
targets = [targets]
summation = 0
for e in targets: # a Stream
summation += e.volumeScalar # for dynamics
return summation / float(len(target))