[Tutor] Please comment my -first- script
Kent Johnson
kent_johnson at skillsoft.com
Wed Aug 18 04:43:35 CEST 2004
Bernard,
This is good work for a first program! I have some small suggestions,
nothing major. Most of them are matters of style and idiom, you can choose
what you prefer.
- You might want to break up the program by using some functions. This will
make it more readable and easier to maintain. (I find it easier to write
that way, too, but you've already done that :-) For example the section
that creates aNames could be a separate function, part of the loop on
sSeqName could be broken out.
- This import
import string
s = string
can be written as
import string as s
- split and zfill are instance methods on string objects (as well as
functions in the string module), so instead of
s.split( oFirstFile, '.' )
you can write
oFirstFile.split('.')
and similarly for zfill.
- Raw strings are handy for Windows path names, instead of
'C:\\FRAME2\\SEQUENCES\\' you can write r'C:\FRAME2\SEQUENCES\'
- os.path.join() is a portable way to create paths - though in this case it
probably doesn't matter.
- Instead of
if os.path.exists( sRoot ) == False:
I would write
if not os.path.exists( sRoot ):
- Instead of
elif len( aFirstName ) == 2:
sExt = aFirstName[1]
else:
sExt = aFirstName[2]
you might be able to use
else:
sExt = aFirstName[-1]
- You might want to use a dict or a set instead of a list for aNames. Sets
are good for keeping a collection without duplicates. For example:
>>> import sets
>>> s=sets.Set()
>>> s.add(1)
>>> s.add(1)
>>> s.add(3)
>>> s
Set([1, 3])
>>> for i in s:
... print i
...
1
3
Before Python 2.3 dicts were used for this purpose. You have to assign a
dummy value to the dict:
>>> d={}
>>> d[1] = 1
>>> d[1] = 1
>>> d[3] = 3
>>> for i in d.keys():
... print i
...
1
3
- This loop
for i in range( iCount ):
iFrame = i + iStart
could be written like this:
for iFrame in range( iStart, iEnd+1):
- You could use os.path.getsize() to find the file size.
- Is running time a concern? I don't know how many files you are checking;
if it is enough that you notice how long it takes to run, you might want to
try a different way of checking the sequences.
You program goes through all the files in the directory twice - once to
compile aNames, again to check the sequence numbers. When you check the
sequence, you can make up to five calls to os.path.exists().
A different way to do this would be to keep a list of the sequence numbers
you find, as you iterate the files the first time. If every file is a
sequence file with a name in the form sName.00xx.sExt, you could build a
dict that maps from sName to a list of integers found. The loop to build
the dict would look something like this:
aNames = {}
for oFile in oFiles:
sName, sSeq, sExt = oFile.split('.')
aNames.setdefault(sName, []).append(sSeq)
The loop to test for missing sequences would be something like this:
for sName, sSeqList in aNames.items():
for iFrame in range(iStart, iEnd + 1):
# Search in sSeqList for iFrame using a similar loop to what you
have now with sZero
This only iterates the file list once and it moves the sequence check from
a filesystem check to looking at a list. But you have working code so I
wouldn't change it unless you have a lot of files! I'm not even certain my
method will be faster - the filesystem may have cached all the directory
information.
Anyway I hope this is helpful. These are all very minor points - I'm not
sure any of them are important enough to change working code!
Kent
At 12:39 AM 8/18/2004 +0100, Bernard Lebel wrote:
>Hi everyone,
>
>All right, I finally managed to write my first full-fledge Python script!!
>It's not an entirely new idea, because it is actually a rewrite of a JScript
>script I did several months. That said, the JScript version was 858 lines
>longs, while the Python version is 192 lines. Granted, the JScript version
>was not that optimized and the perfect aesthetic example, but it worked. I
>must admit that Python has a lot of funcitonalities that JScript doesn't
>have, and that are making life such eaiser.
>
>Anyway, what the attaced file does is that it will analyze a directory tree
>to find sequences of files, and analyse individual sequences to see if files
>are missing or if files are incomplete (under 1k). Since I work in 3D
>animation, this is very useful to summarize the results of a rendering for a
>given shot. The user gives the sequence information, the first file and the
>last file, and the script does the rest.
>Sorry about the French strings, but the script is intended for the employees
>at my company (wich are French).
>
>I'd like to have some advice if some things could have been done better.
>File was renamed to txt for safer internet transfer.
>
>
>Thanks in advance for your time.
>
>Bernard
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list