prev | Draft Version 574 (Thu Dec 1 09:18:36 2005) | next |
# Fill in citations. def bibRef(root, bibOutput, bibInfo): cites = root.getElementsByTagName('cite') for cite in cites: ref = str(cite.getAttribute('ref')) if (not ref) or (ref not in bibInfo): print "Cannot satisfy '%s'" % ref assert False cite.setAttribute('display', bibInfo[ref]) cite.setAttribute('href', '%s#%s' % (bibOutput, ref))
<cite ref="castro-xml"/>
becomes <cite display="Castro 2002" href="bib.html#castro-xml" ref="castro-xml"/>
bibRef
stands for “bibliographic reference”bibRef
root
, bibOutput
, and bibInfo
arebody
…ref
attribute…bibInfo
(presumably a dictionary)…display
and href
attributesbibOutput
must be the name of the output filegrep bibRef expandrefcite.py
finds exactly one call# Substitute information. for filename in args: filename, _, _, root = getFile(filename) genTime(root) crossRef(root, xrefInfo) bibRef(root, bibOutput, bibInfo) outfile = open(filename, 'w') outfile.write(root.toxml('utf-8')) outfile.close()
bibOutput
is initialized to bib.html
, so our guess was pretty goodtemperature
probably doesn't store a user name or the number of pottery shards found at a dig sitecurrent_surface_temperature_of_probe
is meaningful, but not readablecstp
is easier to read, but hard to understandi
and j
for indices in tightly-nested for
loopsExperimentalRecord
, rather than ER
or ExpRec
Before | After |
---|---|
import reader,splitter,transpose import sys,os a=[] b=[] c=[] d=sys.argv[1] a=reader.readLinesFromFile(d) b=splitter.splitIntoSections(a) c=d.split('.') for i in range(len(b)): if os.path.isfile('%s.%d.dat'%(c[0],i+1)): print '%s.%d.dat already exists and cannot be overwritten!'%(c[0],i+1) break else: output=file('%s.%d.dat'%(c[0],i+1),'w') print>>output,transpose.transpose(b[i]) output.close() | import sys, os import reader, splitter, transpose inputFileName = sys.argv[1] lines = reader.readLinesFromFile(inputFileName) sections = splitter.splitIntoSections(lines) fileNameStem = inputFileName.split('.')[0] for i in range(len(sections)): outputFileName = '%s.%d.dat' % (fileNameStem, i+1) if os.path.isfile(outputFileName): print '%s already exists and cannot be overwritten!' % outputFileName break else: output = file(outputFileName, 'w') print >> output, transpose.transpose(sections[i]) output.close() |
ALL_UPPER_CASE_WITH_UNDERSCORES
CamelCase
with first letter capitalizedcamelCase
with first letter in lower casecurrent_maximum
this_
, _this
, or m_this
self.this
anywaycurrAveTemp
instead of currentAverageTemperature
currAvTemp
and currAveTemp
produces hard-to-spot bugsPEP-008: Python Style Guide
unless you have a really good reason not toz = (y = x) + 2
assigns the value of x
to y
, then adds 2 to that and assigns it to z
Without Idiom | Using Idiom |
---|---|
line = infile.readLine(); while (line != null) { ...do something with line... line = infile.readLine(); } | while ((line = infile.readLine()) != null) { ...do something with line... } |
text
, then check for end of input, all in the top of the loopwhile (line = infile.readLine()) { ...do something with line... }
for line in input:
break
statement in it to handle end-of-inputwhile 1: line = infile.readline() if not line: break ...do something with line...
True
, since older Pythons didn't define True
eitherwhile 1: break
idiom, but I used itPyLint
class Metal: name = "aluminum" def __init__(self): self.name = arg def meth(self, val): self.v = val
pylint badcode.py
************* Module badcode W: 8: Bad indentation. Found 6 spaces, expected 8 W: 0: Missing docstring W: 0: Missing required attribute "__revision__" W: 1:Metal: Missing docstring E: 5:Metal.__init__: Undefined variable 'arg' W: 7:Metal.meth: Missing docstring R: 1:Metal: Not enough public methods (1/2) W: 8:Metal.meth: Attribute 'v' defined outside __init__
PyChecker
PyChecker
can't analyze itpychecker badcode.py
Warnings... badcode.py:5: No global (arg) found
Java | HTML |
---|---|
/** * Returns the least common ancestor of two species based on DNA * comparison, with certainty no less than the specified threshold. * Note that getConcestor(X, X, t) returns X for any threshold. * * @param left one of the base species for the search * @param right the other base species for the search * @param threshold the degree of certainty required * @return the common ancestor, or null if none is found * @see Species */ public Species getConcestor(Species left, Species right, float threshold) { ...implementation... } | <p><strong>getConcestor</strong></p> <p><code>public Species getConcestor(Species left, Species right, float threshold)</code></p> <blockquote> <p>Returns the least common ancestor of two species based on DNA comparison, with certainty no less than the specified threshold. Note that getConcestor(X, X, t) returns X for any threshold.</p> <p><strong>Parameters:</strong></p> <blockquote> <p><code>left</code> - one of the base species for the search <p><code>right</code> - the other base species for the search <p><code>threshold</code> - the degree of certainty required </blockquote> <p><strong>Parameters:</strong></p> <blockquote> <p>the common ancestor, or null if none is found</p> </blockquote> <p><strong>See Also:</strong></p> <blockquote> <p><code>Image</code></p> </blockquote> </blockquote> |
PyDoc
module does similar things$Revision: 421$
when you submit changes, and replace the text after the colon with the appropriate information__version__ = "$Revision: 423 $" if __name__ == '__main__': print __version__
biomes.dat
has a header $Revision: 421$
ecoanalyzer.py
# $Revision: $ # From: biomes.dat version 421 # By: ecoanalyzer.py version 37 # On: 2005-09-22 12:14:07 EST
return
statements embedded in the middle of the method…searched
list, plus the number in the toSearch
list, stays constant)assert x < y
, and you don't know why x
has to be less than y
at that point, you've lost tracklog("GRID", LOG_DEBUG, "Refining a %d X %d grid", dimX, dimY)
Level | Meaning |
---|---|
DEBUG | Debugging output (of interest to programmers only) |
INFO | Normal operations (e.g., a user logging in) |
WARN | Something that requires an administrator's attention (e.g., a user is out of disk space) |
ERROR | Something wrong with the program that it can recover from (e.g., unable to open its own configuration file) |
CRITICAL | System about to crash, missiles about to launch, yet another bad 1970s TV show being turned into a movie |
main
(or, in Python, __name__ == '__main__'
)main
)prev | Copyright © 2005, Python Software Foundation. See License for details. | next |