【发布时间】:2014-04-27 16:49:17
【问题描述】:
我正在使用pyacoustid,但我不明白为什么这段代码有效(艺术家实际上是艺术家等等..):
first = True
for score, rid, title, artist in self.fpresults:
if first:
first = False
else:
print
print '%s - %s' % (artist, title)
print 'http://musicbrainz.org/recording/%s' % rid
print 'Score: %i%%' % (int(score * 100))
虽然这个块没有(当我打印时它似乎是空的):
def getFingerprintArtist(self):
"""
Returns tuples with possible artists fetched from the MusicBrainz DB
"""
return [artist for score, rid, title, artist in self.fpresults]
这是整个班级(欢迎提出建议!):
class SongFP:
"""
Song with FINGERPRINTS
"""
fpresults = None
def __init__(self, path = None):
"""
:param path: the path of the song
"""
self.path = path
try:
self.fpresults = acoustid.match(api_key, path)
except acoustid.NoBackendError:
logger(paths['log'], "ERROR: chromaprint library/tool not found")
except acoustid.FingerprintGenerationError:
logger(paths['log'], "ERROR: fingerprint could not be calculated")
except acoustid.WebServiceError, exc:
logger(paths['log'], ("ERROR: web service request failed: %s" % exc.message))
def setPath(self, path):
self.path = path
def printResults(self):
first = True
for score, rid, title, artist in self.fpresults:
if first:
first = False
else:
print
print '%s - %s' % (artist, title)
print 'http://musicbrainz.org/recording/%s' % rid
print 'Score: %i%%' % (int(score * 100))
def setFPResults(self):
self.fpresults = acoustid.match(api_key, self.path)
def getFingerprintArtist(self):
"""
Returns tuples with possible artists fetched from the MusicBrainz DB
"""
return [artist for score, rid, title, artist in self.fpresults]
def getFingerprintTitle(self):
"""
Returns tuples with possible titles fetched from the MusicBrainz DB
"""
return [title for score, rid, title, artist in self.fpresults]
def getFingerPrintID(self):
"""
Returns tuples with IDs fetched from the MusicBrainz DB
"""
return [rid for score, rid, title, artist in self.fpresults]
def getFingerPrintScore(self):
"""
Returns tuples with scores fetched from the MusicBrainz DB
"""
return [score for score, rid, title, artist in self.fpresults]
注意: acoustid.match(api_key, path) 返回元组!
编辑:
这个小例子
songfp = SongFP(sys.argv[1])
songfp.printResults()
SongFP 在哪里
class SongFP:
"""
Song with FINGERPRINTS
"""
fpresults = None
def __init__(self, path = None):
"""
:param path: the path of the song
"""
self.path = path
try:
self.fpresults = acoustid.match(api_key, path)
except acoustid.NoBackendError:
logger(paths['log'], "ERROR: chromaprint library/tool not found")
except acoustid.FingerprintGenerationError:
logger(paths['log'], "ERROR: fingerprint could not be calculated")
except acoustid.WebServiceError, exc:
logger(paths['log'], ("ERROR: web service request failed: %s" % exc.message))
def getFingerprintArtist(self):
"""
Returns tuples with possible artists fetched from the MusicBrainz DB
"""
return [artist for _, _, _, artist in self.fpresults]
def getFingerprintTitle(self):
"""
Returns tuples with possible titles fetched from the MusicBrainz DB
"""
return [title for _, _, title, _ in self.fpresults]
def getFingerprintID(self):
"""
Returns tuples with IDs fetched from the MusicBrainz DB
"""
return [rid for _, rid, _, _ in self.fpresults]
def getFingerprintScore(self):
"""
Returns tuples with scores fetched from the MusicBrainz DB
"""
return [score for score, _, _, _ in self.fpresults]
def printResults(self):
print("Titles: %s" % self.getFingerprintTitle())
print("Artists: %s" % self.getFingerprintArtist())
print("IDs: %s" % self.getFingerprintID())
print("Scores: %s" % self.getFingerprintScore())
当被称为./app song.mp3 时,只输出某个字段(如果一个字段为空,则其他字段也应为空,反之亦然,因为它会获取在线 MP3 元数据)
Titles: [u'Our Day Will Come', u'Our Day Will Come', u'Our Day Will Come', u'Our Day Will Come', u'Our Day Will Come']
Artists: []
IDs: []
Scores: []
日志中没有例外!
【问题讨论】:
-
请发布异常回溯。
-
我已经检查了日志,没有出现异常。
-
你说它不起作用。我们需要更多信息。如果代码没有产生异常,那么发生了什么?
-
这正是我写问题的原因!
-
@Krishath 让我们换一种说法 - 你希望它做什么而它没有做什么?
标签: python oop methods instance generator