[Tutor] Simple program with menu to view videos

Kent Johnson kent37 at tds.net
Fri Jan 16 19:29:09 CET 2009


On Fri, Jan 16, 2009 at 11:35 AM, David <david at abbottdavid.com> wrote:
>  I am asking for some tips on how I can make
> this program with less code and using some classes, if that is the best way
> to proceed. I have a lot of code that is just copied from one function to
> the next.

When you are copy/pasting code, think about how you can make a
function that abstracts the copied bits. For example you could have
this function:

def play(filename):
    player = "/usr/bin/mplayer"
    fname = "/home/david/Desktop/core_video/data/" + filename
    subprocess.call([player, fname])

Then for example you would have
def lesson10f():
    play("python_1006.flv")
    menu()

So that is a start. Now look at what you are doing with the individual
sub-lessons. You really just want a way to play a series of lessons.
This can be done with a loop:

def lesson8():
  files = ['python_0800a.flv', 'python_0801.flv', <snip>, 'python_0806.flv']
  playall(files)

def playall(files):
  for name in files:
    play(name)

Since the lesson names follow a regular pattern you might even be able
to generate the names automatically from the lesson number.

Finally, put a loop in your menu function rather than having the
lessons call back to the menu.

Kent


More information about the Tutor mailing list