[Tutor] Newbie append puzzle
Daniel Ehrenberg
littledanehren at yahoo.com
Sun Nov 2 15:55:00 EST 2003
> 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
Your program uses the string module, which is
obsolete. Most of the string methods have been
replaced with methods on the string type, acting as an
object. Here's the part where you used the string
module:
> def makeaList(s):
> anothermp3 = string.split(s)
> return anothermp3
Using the builtin string methods, you would instead
write:
> def makeaList(s):
> anothermp3 = s.split()
> return anothermp3
but I would probably impliment it like this:
> makeaList = lambda s: s.split()
and I'd define it outside of the while loop. If you
define it inside of the loop, it is defined over and
over again, slowing the program.
I don't think the filenames you used will work as you
intended. When you were making a new playlist, you
opened the file playlist.new (playlist is the name and
new is the file extention, not a new playlist). When
you're reading the playlist, you open a file named
playlist with no file extention. These files are in
the directory where the script is run. If you open a
file that doesn't exist, the file is automatically
created. I would open something like playlist.dsp
whether or not the playlist exists.
What are the ao and mad modules that you're using?
They're not builtin so I can't use your mp3 player.
Where could I find those modules?
Daniel Ehrenberg
__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/
More information about the Tutor
mailing list