Request for feedback on my first Python program
Max M
maxm at mxm.dk
Fri May 30 05:03:55 EDT 2003
Scott Meyers wrote:
> I'm a C++ programmer who's writing his very first Python program. This
> means the program is going to be gross, and I apologize for that in
> advance. I don't really have anybody I can show it to for feedback, so I'm
> hoping I can get some comments here. If there is a better place for me to
> seek guidance, please let me know.
This is probably the best place around.
I would only do a few things differently. The "getMeaningfulLines()" was
way more complicated than it needed to be.
###################################
import sys
import os
# Error status codes
BADINVOCATION = 1
def usage(programName):
print "Usage: " + os.path.basename(programName) + " [file]"
sys.exit(BADINVOCATION)
# Take a list of strings, return an equivalent list where the following
# strings have been removed:
# - those with no non-whitespace characters
# - those whose first non-whitespace character is a "#"
def getMeaningfulLines(lines):
meaningfulLines = []
for line in lines:
line = line.strip()
if line and line[0] != '#':
meaningfulLines.append(line)
return meaningfulLines
# This is how a "main" is normally done in Python.
if __name__ == '__main__':
if len(sys.argv) != 2: usage(sys.argv[0])
paths = getMeaningfulLines(open(sys.argv[1]).readlines())
for path in paths:
fType = "non-directory and non-file"
if os.path.isdir(path):
fType = "directory"
elif os.path.isfile(path):
fType = "file"
print "%s line is a %s" % (path, fType)
--
hilsen/regards Max M Rasmussen, Denmark
http://www.futureport.dk/
Fremtiden, videnskab, skeptiscisme og transhumanisme
More information about the Python-list
mailing list