[Tutor] Newbie: First program - looking for feedback :)

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Jan 5 17:09:25 EST 2004


> With this in mind, and with some embarrassment, i post my first
*real*
> python program.

For a first program its fine. You obviously understand how
to write functions so you might want to consider breaking
it up into several smaller functions - ONe to deal with
the user input and get the list of files, another to process
the files, and another to do the reporting of the results.

This separation of concerns helps to build reusable functions
- for example the function that takes a filename, converts
it and reports success could be used from a GUI version
of the program. And a function which can ask the user to
provide a list of filenames could be used in several other,
similar tools.

Some specific comments:

>         for eachfile in mp3:
>             name = os.path.splitext(eachfile)
>             result = os.system('lame --decode %s %s%s'  %
>             (eachfile,name[0],wavext))
>             if (result == 0):
>                 print
>                 print 'Converted %s -> %s%s' %
(eachfile,name[0],wavext)
>                 print
>             else:
>                 result = 1

This last else isn't really needed because result will be set
by the system() call each time through the loop.

>     else:
>         print
>         sys.exit()
>
>     # Had this problem myself, so added this as an afterthought.
Need to
>     clean
>     # and add some proper error/exception handling.
>     if (result == 1):


And this then gets changed to

        if result != 0

Note you don't really need the parems round the test
- unlike in C.

> I know, its not pretty and theres probably a 1001 ways
> to make it more elegant and efficient

Probably, but it seems to work and for a first attempt it's OK.

Add the new features etc you mention and try "refactoring"
it into smaller functions and it should make a nice project.

One last thought - you might like to use triple quotes instead
of multiple print statements, but thats mostly a matter of
personal taste.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list