prev | Version 1107 (Mon Nov 27 20:46:00 2006) | next |
Figure 10.1: Chunking in Short-Term Memory
Figure 10.2: Actual Chess Position
Figure 10.3: Retention of Actual Chess Position
Figure 10.4: Random Chess Position
Figure 10.5: Retention of Random Chess Position
PEP-008: Python Style Guide
Rule | Good | Bad |
---|---|---|
No whitespace immediately inside parentheses | max(candidates[sublist]) | max( candidates[ sublist ] ) |
…or before the parenthesis starting indexing or slicing | max (candidates [sublist] ) | |
No whitespace immediately before comma or colon | if limit > 0: print minimum, limit | if limit > 0 : print minimum , limit |
Use space around arithmetic and in-place operators | x += 3 * 5 | x+=3*5 |
No spaces when specifying default parameter values | def integrate(func, start=0.0, interval=1.0) | def integrate(func, start = 0.0, interval = 1.0) |
Never use names that are distinguished only by "l" , "1" , "0" , or "O" | tempo_long and tempo_init | tempo_l and tempo_1 |
Short lower-case names for modules (i.e., files) | geology | Geology or geology_package |
Upper case with underscores for constants | TOLERANCE or MAX_AREA | Tolerance or MaxArea |
Camel case for class names | SingleVariableIntegrator | single_variable_integrator |
Lowercase with underscores for function and method names | divide_region | divRegion |
…and member variables | max_so_far | maxSoFar |
Use is and is not when comparing to special values | if current is not None: | if current != None: |
Use isinstance when checking types | if isinstance(current, Rock): | if type(current) == Rock: |
Table 10.1: Basic Python Style Rules |
temperature
shouldn't be used to store 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 understand…ctsp
curr_ave_temp
instead of current_average_temperature
is OK…curnt_av_tmp
i
and j
for indices in tightly-nested for
loopsExperimentalRecord
, rather than ER
or ExpRec
import sys, os import reader, splitter, transpose a=[] b=[] c=[] d=sys.argv[1] a=reader.rdlines(d) b=splitter.splitsec(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!'%(c[0],i+1) break else: output=file('%s.%d.dat'%(c[0],i+1),'w') print>>output,transpose.txpose(b[i]) output.close()
import sys, os import reader, splitter, transpose input_file_name = sys.argv[1] lines = reader.read_lines_from_file(input_file_name) sections = splitter.split_into_sections(lines) file_name_stem = input_file_name.split('.')[0] for i in range(len(sections)): output_file_name = '%s.%d.dat' % (file_name_stem, i+1) if os.path.isfile(output_file_name): print '%s already exists!' % output_file_name break else: output = file(output_file_name, 'w') print >> output, transpose.transpose(sections[i]) output.close()
# What's missing, and what's extra? def diff_filelist(dir_path, manifest, ignore=[os.curdir, os.pardir, '.svn']): def show_diff(title, diff): if diff: print title for d in diff: print '\t' + d expected = Set() inf = open(manifest, 'r') for line in inf: expected.add(line.strip()) inf.close() actual = Set() contents = os.listdir(dir_path) for c in contents: if c not in ignore: actual.add(c) show_diff('missing:', expected - actual) show_diff('surplus:', actual - expected)
dir_path
suggests “directory path”diff
if it isn't emptymanifest
filedir_path
if __name__ == '__main__': if len(sys.argv) != 3: print >> sys.stderr, "usage: diff_filelist directory_path manifest_file" sys.exit(1) diff_filelist(sys.argv[1], sys.argv[2])
grep
or “Find in Files” to search for othersfor line in input:
break
to handle end-of-inputcount = 0 while 1: line = infile.readline() if not line: break count += 1
1
instead of True
because older Pythons didn't define True
PyLint
parses programs to create an abstract syntax treeFigure 10.6: Abstract Syntax Tree
PyChecker
imports the module (or modules)PyChecker
can't analyze it$Revision: 1.1 $
when you submit changes__version__
__version__ = "$Revision: 1.1 $" if __name__ == '__main__': print __version__
biomes.dat
has a header $Revision: 1.1 $
ecoanalyzer.py
# $Revision: 1.1 $ # From: biomes.dat version 421 # By: ecoanalyzer.py version 37 # Parameters: sliding_average 20 trim False # On: 2006-02-22 12:14:07 EST
$Revision: 1.1 $
header will be expanded when the file is first checked in/** * 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... }
getConcestor
public Species getConcestor(Species left, Species right, float threshold)
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.
Parameters:
left
- one of the base species for the search
right
- the other base species for the search
threshold
- the degree of certainty requiredParameters:
the common ancestor, or null if none is found
See Also:
Image
__doc__
attribute'''This module provides functions that search and compare genomes. All functions assume that their input arguments are in valid CCSN-2 format; unless specifically noted, they do not modify their arguments, print, or have other side effects. ''' __version__ = '$Revision: 1.1 $' def get_concestor(left, right, threshold): '''Find the least common ancestor of two species. This function searches for a least common ancestor based on DNA comparison with certainty no less than the specified threshold. If one can be found, it is returned; otherwise, the function returns None. get_concestor(X, X, t) returns X for any threshold. left : one of the base species for the search right : the other base species for the search threshold : the degree of certainty required ''' pass # implementation would go here
$ python
>>> import genome
>>> print genome.__doc__
This module provides functions that search and compare genomes.
All functions assume that their input arguments are in valid CCSN-2
format; unless specifically noted, they do not modify their arguments,
print, or have other side effects.
>>> print genome.get_concestor.__doc__
Find the least common ancestor of two species.
This function searches for a least common ancestor based on DNA
comparison with certainty no less than the specified threshold.
If one can be found, it is returned; otherwise, the function
returns None. get_concestor(X, X, t) returns X for any threshold.
left : one of the base species for the search
right : the other base species for the search
threshold : the degree of certainty required
Docutils
will extract, format, and cross-reference docstringsprev | Copyright © 2005-06 Python Software Foundation. | next |