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

credo credo2004 at fmail.co.uk
Mon Jan 5 16:47:45 EST 2004


Hi Pythoneers,

Okay, ive been trying to teach myself python for a few weeks and i'm
making some slow progress, (not a natural coder here). This started
mainly
due to me switching from Windows -> GNU/Linux about a year ago. 

I find i am getting comfortable with GNU/Linux now, and it has recently
encouraged 
me to try my hand at programming; for those times when you wished you had
a 
little tool to do something :). 
The possibilty of helping the open source commnunity by contributing to
software 
development etc is also a plus, although a distant dream at this point.
;) 

My primary goal is to have some fun with this. I believe selecting Python
as my first
language was the right choice :)

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

Its a function to convert mp3 -> wav using the 'lame' encoder. I also
went beyond a 
simple 'for' loop to add some interaction with the user, purely as an
exercise.

--------------------------------------- code
-----------------------------------------

#! /usr/bin/env python

# mp32wav
# A *basic* function to convert mp3 to wav format.
# 28/12/03 - credo - credo2004 at fmail.co.uk 

#####################################
# To-Do                             #
#####################################

# 1. Add some PROPER error handling.
# 2. Get filenames to print in order. (ie: 01 -> 10)
# 3. Allow option for target other than cwd.
# 4. Make function modular to allow use from other progs. 

import os
import sys
import os.path

wavext = '.wav'
mp3ext = '.mp3'

def mp32wav():
    
    # Get file list from current directory.    
    cwd = os.getcwd()
    list = os.listdir(cwd)
    os.system('clear')
   
    print
    print '============================='
    print 'MP3 to convert to WAV format:' 
    print '============================='
    print 


    # Sort the mp3 to convert and add them to a list for later use, also
    # keep tally of valid and non-valid files.
    good = 0
    bad = 0
    mp3 = []
    
    for eachfile in list:
        if eachfile.endswith(mp3ext):
            mp3.append(eachfile)
            good += 1
        else:
            pass
            bad += 1
    print
    print 'Mp3 files to convert = ',good
    print 'Non-mp3 files to be left alone = ',bad
    print
    
    choice = raw_input('\nConvert? (Y/n): ')
    choice = choice.lower()

    if (choice == 'y'):
        print
        print 'Converting...'
        print

        # Pass the mp3 files to the 'lame' encoder, extract filenames and
        # concatenate with constant to create output wav based on
        original 
        # filename. I think that makes sense ;)
        # Some very basic error handling here also.
        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
    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):
        print
        print "Error! - Can't convert: Please check folder/file
        permissions :)"
        print
        raw_input('Press any key to quit: ')
        print
    else:
        print 'Finished!!!!!!!!!!!!!!!!'   
        print
        raw_input('Press any key to quit: ')
        print
    
if __name__ == '__main__':
    mp32wav()
	
--------------------------------------- code
-----------------------------------------

I know, its not pretty and theres probably a 1001 ways to make it more
elegant and efficient
but i really just went into it with some basic knowledge of some standard
libraries and an idea
of how i wanted the program to flow. The little testing i have done
indicates it does work but 
i'm working on beautifying it. See the To-do comments for some of the
obvious that i noted down :)

Some general feedback on the code and use of python language would be
appreciated. I'm looking for
tips/tricks and anything that can help me become better at this. Some of
the above code already 
makes me cringe, but i thought it was worth posting my first draft incase
i'm making some big 
mistakes early on, then i can eliminate them now.

Thanks in advance for any comments...i can accept constructive criticism
:)

Best Regards

credo

-- 
http://www.fastmail.fm - Access all of your messages and folders
                          wherever you are



More information about the Tutor mailing list