[Tutor] Newbie append puzzle

Tim Ronning tim.ronning at start.no
Sun Nov 2 14:18:54 EST 2003


På Sun, 2 Nov 2003 16:27:50 +0100 (CET), skrev Michael Janssen 
<Janssen at rz.uni-frankfurt.de>:

>
> certainly choice "4" "mp3" strings manage somehow to get rid of their
> newlines. The newline was originally delivered by "raw_input". Hint:
> string.split splits per default by any kind of whitespace....
>
> Best (instead of making shure that your code never tries to "append" a
> string, that doesn't end in newline) is to rewrite your "append"
> function, so that it test itself if the "mp3" string ends with a
> newline. I assume, you want to write that bit yourself (Feel free to
> ask on tutor if you can't get it right - or post your solution)?
>
> Note: now where you need to enhance your "append" function it becomes
> even more sensible to define it only once: Just define it before any
> if-clause and you won't have to do every bugfix twice.
>
> Hey, welcome at tutor and python :-)
>
>
> Michael

Thanks for your answer Michael.
I have re-done a couple of things incl. the append function. Not shure if 
this is what you had in mind though. How do you test for "\n" in a string ? 
Anyway the following code works, but I greatly appriciate input on smarter 
ways to do things. Thats what learning is all about, -learning to do smart 
things!

MP3 Player in 100 lines!

#!/usr/bin/env python
########################################
##               DSPmP                ##
##    Dead.Simple.Python.mp3.Player   ##
##                                    ##
##    Author: Tim Ronning             ##
##    Version: 0.0.2                  ##
##    Date: 02.11.03                  ##
##                                    ##
########################################

import sys, ao, mad, string, os

choice = '1'

while choice != '5':
    print """
    ************************
    *         DSPmP        *
    ************************
    * 1)....Play List      *
    * 2)....Show List      *
    * 3)....Add MP3's      *
    * 4)....Remove MP3's   *
    * 5)....Quit           *
    ************************
    ************************
    """
    choice = raw_input("Choice: ")
    def makeaList(s):
            anothermp3 = string.split(s)
            return anothermp3
    def append(mp3):
        if choice == "3":
            app = open("playlist","a")
            app.write(mp3)
            app.close()
        else:
            app = open("playlist.new","a")
            app.write(mp3 + "\n")
            app.close
    if choice == '1':
        playlist = open("playlist","r")
        mp3list = []
        for line in playlist.readlines():
            mp3list = mp3list + makeaList(line)

        playlist.close()
        print mp3list
        items = len(mp3list)
        j=0
        while j < items:
            tune = mp3list[j]
            mf = mad.MadFile(tune)
            dev = ao.AudioDevice('alsa09', rate=mf.samplerate())
            while 1:
                buf = mf.read()
                if buf is None:
                    break
                dev.play(buf, len(buf))
            j = j + 1

    elif choice == '2':
        playlist = open("playlist","r")
        for line in playlist.readlines():
            print line
        playlist.close()

    elif choice == '3':
        newmp3 = raw_input("New mp3: ")
        append(newmp3)

    elif choice == '4':
        playlist = open("playlist","r")
        mp3list = []
        for line in playlist.readlines():
            mp3list = mp3list + makeaList(line)

        playlist.close()
        delnr = int(raw_input("Which number? "))
        del mp3list[delnr]
        listlen = len(mp3list)
        n = 0
        while n < listlen:
            append(mp3list[n])
            n = n + 1
        os.system("mv -f playlist.new playlist")

    else:
        sys.exit()

- - - - -END CODE - - - - -

Best regards
-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the Tutor mailing list