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

credo credo2004 at fmail.co.uk
Wed Jan 7 14:32:44 EST 2004


On Mon, 5 Jan 2004 22:09:25 -0000, "Alan Gauld"
<alan.gauld at blueyonder.co.uk> said:
> > 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
> 

Hi Alan,

Thankyou for taking the time to look over my first effort. I'm pleased
that i seemed to have made a reasonable job of it :)

> 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.

Interesting that you make this point about functions as i have started to
exactly that :) A little more refinement and testing and i shall unleash
it upon this mailing list for some more feedback. 
I hope you might find the time to give me some comments on my progress as
it is very much appreciated.

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

Duh!, i see what you mean :)

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

I was aware of this and included them only for readabilty sake,
(suggested by a book or tutorial i read), however i personally dont find
that it hinders my comprehension of the code so i will try not to use
them in the future. 

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

Yep, i read about triple quotes and will use them when writing a
significant amount of text to the screen, however i find this situation:

def message():
    print ''' 
Hello to all members of the python tutor mailing list
new and old and thanx for helping a newbie pythoneer like me 
find some help, as coding is not natural to me :)
'''
.. where the text needs to be left most aligned, (despite the code
indentation), to be somewhat ugly. Especially when the text is embedded
within a nested 'if' statement etc.. 
I prefer to avoid triple quotes when dealing with just 1 or 2 lines to
preserve the indentation, otherwise i will use the triple quotes as
suggested.

I am trying to drop the habit of empty 'print' statements to achieve a
newline when i could easily embed a '\n' inside a print statement to
achieve the same. Again, its a tool i use to aid my readability of the
code. :)

Thanx again for your comments Alan and ive bookmarked your website for
further reading ;)

Best Regards

Credo

-- 
http://www.fastmail.fm - And now for something completely different




More information about the Tutor mailing list