Le Fri, 16 Jan 2009 11:35:13 -0500,
David da...@abbottdavid.com a écrit :
Hi,
I just received the dvd Python Fundamentals by Wesley J. Chun and it was
packaged by the distributor for use on Window and Mac but I use Linux so
I pulled the videos of of the dvd. There are 10 lessons with a total of
58 individual videos. Most lessons are 6-8 videos long. So I did as Alan
has suggested and came up with a plan of attack. What my basic program
does is plays the first video and then the next in that lesson and when
finished them all goes back to the menu. 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. I am new to python and enjoy these
little projects because I can see them in action. Here is my starting point;
http://dwabbott.com/code/index6.html
thanks
-david
Just had a look at your code. As you say: a lot of code that is just copied
from one
function to the next. The only reason for that is your functions have no
argument (parameter):
def lesson3b():
player = /usr/bin/mplayer
fname = /home/david/Desktop/core_video/data/python_0302.flv
subprocess.call([player, fname])
lesson3c()
From this instance of a specific lesson player function, you can simply define
a generic one:
def play_lesson(lesson_id):
# does not belong here:
player = /usr/bin/mplayer
fname = lesson_file_name(lesson_id) # how to get the file name?
subprocess.call([player, fname])
# does not belong here:
lesson3c()
Note that player is a constant, so that it does not belong to the function.
At best, it may be
an optional argument which could later be overriden (by e.g. a command line
argument -- if ever
you chage the player).
Also, the lesson3c() call is part of an overall scheduling logic that should be
controled from an
external loop or menu -- this depends on how you intend to use the application.
Finally, note that the lesson file names are pretty regular: There are built
out a single
prefix, a series number, then a lesson letter. Provided you do not change
this logic, you can
build the filenames easily. The prefix can be written as an optional arg, like
the player. Also, I
would distinguish series from lesson (this may be helpful at the schedular
level). An
alternative may be a lesson file lookup in a dictionary of
(lesson_id:lesson_file_name)
pairs, but there is no need for that here.
So that the core lesson playing function may look like that:
def play_lesson(series_id, lesson_id,
player = /usr/bin/mplayer,
prefix=/home/david/Desktop/core_video/data/python_):
# convert lesson lowercass letter id to a ordinal:
# (a,b,...z -- 97,98,...122)
lesson_id = ord(lesson_id) - 96
# I use below a string formatting expression
# but the file name may be constructed manually
fname = %s%02d%02d %(prefix, series_id, lesson_id)
subprocess.call([player, fname])
Now, you need a master control function, from which the user can possibly
choose which lesson to
watch, that will accordingly call the lesson player func. As said, this mainly
depends on how you
expect it as a user.
Note that the logic you first coded is a kind of masked recursivity:
play1a
play1b
play1c
playnx
playny
This would be implemented in the above version by an additional explicit
recursive call:
def play_lesson(series_id, lesson_id,
player = /usr/bin/mplayer,
prefix=/home/david/Desktop/core_video/data/python_):
...
subprocess.call([player, fname])
play_lesson(next_lesson(series_id, lesson_id))
(provided the next_lesson func exists)
This is very probably *not* the user model of an adaptative lesson player tool
;-)
For a question of taste or programming style, I would personly rather create a
lesson type
(class) that holds and handles all that stuff: identification, file_name,
play(), next()... But it
would probably be neither clearer, nore simpler. You could do it afterwards as
an introduction to
objects (the list will help you).
Denis
--
la vida e estranya
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor