《Python极客项目编程 》——1.4 完整代码

1.4 完整代码

下面是完整的程序。在https://github.com/electronut/pp/tree/master/playlist/ ,你也可以找到本项目的代码和一些测试数据。

import re, argparse
import sys
from matplotlib import pyplot
import plistlib
import numpy as np

def findCommonTracks(fileNames):
    """
    Find common tracks in given playlist files,
    and save them to common.txt.
    """
    # a list of sets of track names
    trackNameSets = []
    for fileName in fileNames:
        # create a new set
        trackNames = set()
        # read in playlist
        plist = plistlib.readPlist(fileName)
        # get the tracks
        tracks = plist['Tracks']
        # iterate through the tracks
        for trackId, track in tracks.items():
            try:
                # add the track name to a set
                trackNames.add(track['Name'])
            except:
                # ignore
                pass
        # add to list
        trackNameSets.append(trackNames)
        # get the set of common tracks
        commonTracks = set.intersection(*trackNameSets)
        # write to file
        if len(commonTracks) > 0:
            f = open("common.txt", 'w')
            for val in commonTracks:
                s = "%s\n" % val
                f.write(s.encode("UTF-8"))
            f.close()
            print("%d common tracks found. "
                  "Track names written to common.txt." % len(commonTracks))
        else:
            print("No common tracks!")

    def plotStats(fileName):
        """
        Plot some statistics by reading track information from playlist.
        """
        # read in a playlist
        plist = plistlib.readPlist(fileName)
        # get the tracks from the playlist
        tracks = plist['Tracks']
        # create lists of song ratings and track durations
        ratings = []
        durations = []
        # iterate through the tracks
        for trackId, track in tracks.items():
            try:
                ratings.append(track['Album Rating'])
                durations.append(track['Total Time'])
            except:
                # ignore
                pass
        # ensure that valid data was collected
        if ratings == [] or durations == []:
            print("No valid Album Rating/Total Time data in %s." % fileName)
            return

        # scatter plot
        x= np.array(durations, np.int32)
        # convert to minutes
        x = x/60000.0
        y = np.array(ratings, np.int32)
        pyplot.subplot(2, 1, 1)
        pyplot.plot(x, y, 'o')
        pyplot.axis([0, 1.05*np.max(x), -1, 110])
        pyplot.xlabel('Track duration')
        pyplot.ylabel('Track rating')

        # plot histogram
        pyplot.subplot(2, 1, 2)
        pyplot.hist(x, bins=20)
        pyplot.xlabel('Track duration')
        pyplot.ylabel('Count')
        # show plot
        pyplot.show()

    def findDuplicates(fileName):
        """
        Find duplicate tracks in given playlist.
        """
        print('Finding duplicate tracks in %s...' % fileName)
        # read in playlist
        plist = plistlib.readPlist(fileName)
        # get the tracks from the Tracks dictionary
        tracks = plist['Tracks']
        # create a track name dictionary
        trackNames = {}
        # iterate through tracks
        for trackId, track in tracks.items():
            try:
                name = track['Name']
                duration = track['Total Time']
                # look for existing entries
                if name in trackNames:
                    # if a name and duration match, increment the count
                    # round the track length to the nearest second
                    if duration//1000 == trackNames[name][0]//1000:
                        count = trackNames[name][1]
                        trackNames[name] = (duration, count+1)
                else:
                    # add dictionary entry as tuple (duration, count)
                    trackNames[name] = (duration, 1)
            except:
                # ignore
                pass
        # store duplicates as (name, count) tuples
        dups = []
        for k, v in trackNames.items():
            if v[1] > 1:
                dups.append((v[1], k))
        # save duplicates to a file
        if len(dups) > 0:
            print("Found %d duplicates. Track names saved to dup.txt" % len(dups))
        else:
            print("No duplicate tracks found!")
        f = open("dups.txt", 'w')
        for val in dups:
            f.write("[%d] %s\n" % (val[0], val[1]))
        f.close()

    # gather our code in a main() function
    def main():
        # create parser
        descStr = """
        This program analyzes playlist files (.xml) exported from iTunes.
        """
        parser = argparse.ArgumentParser(description=descStr)
        # add a mutually exclusive group of arguments
        group = parser.add_mutually_exclusive_group()

        # add expected arguments
        group.add_argument('--common', nargs='*', dest='plFiles', required=False)
        group.add_argument('--stats', dest='plFile', required=False)
        group.add_argument('--dup', dest='plFileD', required=False)

        # parse args
        args = parser.parse_args()

        if args.plFiles:
            # find common tracks
            findCommonTracks(args.plFiles)
        elif args.plFile:
            # plot stats
            plotStats(args.plFile)
    elif args.plFileD:
        # find duplicate tracks
        findDuplicates(args.plFileD)
    else:
        print("These are not the tracks you are looking for.")

# main method
if __name__ == '__main__':
    main()
时间: 2024-09-14 04:27:06

《Python极客项目编程 》——1.4 完整代码的相关文章

《Python极客项目编程 》——2.3 代码

2.3 代码 首先,定义类Sipro,来绘制这些曲线.我们会用这个类一次画一条曲线(利用draw()方法),并利用一个定时器和update()方法,产生一组随机螺线的动画.为了绘制Spiro对象并产生动画,我们将使用SpiroAnimator类. 要查看完整的项目代码,请直接跳到2.4节. 2.3.1 Spiro构造函数 下面是Spiro构造函数: # a class that draws a Spirograph class Spiro: # constructor def __init__(

《Python极客项目编程 》——第2章 万花尺 2.1 参数方程

第2章 万花尺 我们可以用万花尺玩具(如图2-1所示)来绘制数学曲线.这种玩具由两个不同尺寸的塑料齿轮组成,一大一小.小的齿轮有几个孔.把钢笔或铅笔放入一个孔,然后在较大齿轮(内部有齿)内旋转里面的小齿轮,保持笔与外轮接触,可以画出无数复杂而奇妙的对称图案. 在这个项目中,我们将用Python来创建动画,像万花尺一样绘制曲线.我们的spiro.py程序将用Python和参数方程来描述程序的万花尺齿轮的运动,并绘制曲线(我称之为螺线).我们可以将完成的画图保存为PNG图像文件,并用命令行选项来指定

《Python极客项目编程 》——第一部分 热身运动 第1章 解析iTunes播放列表 1.1 iTunes播放列表文件剖析

第一部分 热身运动 "在初学者的头脑中有很多可能性, 在专家的头脑中,可能性很少." --铃木俊隆 第1章 解析iTunes播放列表 我们的Python探险始于一个简单的项目,该项目在iTunes播放列表文件中查找重复的乐曲音轨,并绘制各种统计数据,如音轨长度和评分.你可以从查看iTunes播放列表格式开始,然后学习如何用Python提取这些文件的信息.为了绘制这些数据,要用到matplotlib库. 在这个项目中,我们将学习以下主题: XML和属性列表(p-list)文件: Pyth

《Python极客项目编程 》——1.6 小结

1.6 小结 在这个项目中,我们开发了一个程序,分析了iTunes播放列表.在这个过程中,我们学习了一些有用的Python结构.在接下来的项目中,你将基于这里介绍的一些基础知识,探索各种有趣的主题,深入地研究Python.

《Python极客项目编程 》——2.2 所需模块

2.2 所需模块 我们将利用下面的模块创建螺线: turtle模块用于绘图: pillow,这是Python图像库(PIL)的一个分支,用于保存螺线图像.

《Python极客项目编程 》——2.6 小结

2.6 小结 在这个项目中,我们学习了如何创建万花尺那样的曲线.我们还学习了如何调整输入参数,来生成各种不同的曲线,并在屏幕上产生动画.我希望你喜欢创造这些螺线(在第13章你会惊喜地发现,可以学到如何将螺线投影到墙上).

《Python极客项目编程 》——1.2 所需模块

1.2 所需模块 在这个项目中,我们用内置模块plistlib来读取播放列表文件.我们还用matplotlib库来绘图,用numpy的数组来存储数据.

《Python极客项目编程 》——1.5 运行程序

1.5 运行程序 下面是该程序的运行示例: $ python playlist.py --common test-data/maya.xml test-data/rating.xml 下面是输出: 5 common tracks found. Track names written to common.txt. $ cat common.txt God Shuffled His Feet Rubric Floe Stairway To Heaven Pi's Lullaby moksha:pla

《Python极客项目编程 》——2.7 实验

2.7 实验 下面有一些方法可以进一步尝试螺线. 1.现在你已知道如何画圆,请写一个程序来绘制随机的对数螺线.找到参数形式的对数螺线方程,然后用它来绘制螺线. 2.你可能已经注意到,画曲线时,海龟光标总是朝右,但这不是海龟移动的方式!请调整海龟的方向,在绘制曲线时,让它朝向绘制的方向(提示:每步计算连续点之间的方向矢量,用turtle.setheading()方法来调整海龟的方向). 3.尝试用海龟绘制Koch snowflake(科赫雪花),它是利用递归(即调用自身的函数)的分形曲线.可以像这