Python-从元组返回值

问题描述 投票:1回答:3

我正在使用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]

NOTE: 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: []

日志中无异常!

python oop methods instance generator
3个回答
2
投票

这很难诊断,但是分配不使用的变量通常更常见:


0
投票

如果它们只是元组,您可以这样:


0
投票

with

© www.soinside.com 2019 - 2024. All rights reserved.