How to detect typos in Python programs

Bob Gailer bgailer at alum.rpi.edu
Fri Jul 25 14:20:57 EDT 2003


At 07:26 PM 7/25/2003 +0530, Manish Jethani wrote:

>Hi all,
>
>Is there a way to detect typos in a Python program, before
>actually having to run it.  Let's say I have a function like this:
>
>   def server_closed_connection():
>     session.abost()
>
>Here, abort() is actually misspelt.  The only time my program
>follows this path is when the server disconnects from its
>end--and that's like once in 100 sessions.  So sometimes I
>release the program, people start using it, and then someone
>reports this typo after 4-5 days of the release (though it's
>trivial to fix manually at the user's end, or I can give a patch).
>
>How can we detect these kinds of errors at development time?
>It's not practical for me to have a test script that can make
>the program go through all (most) the possible code paths.

consider:
  use a regular expression to get a list of all the identifiers in the program
  count occurrence of each by adding to/updating a dictionary
  sort and display the result

program_text = """  def server_closed_connection():
     session.abost()"""
import re
words = re.findall(r'([A-Za-z_]\w*)\W*', program_text) # list of all 
identifiers
wordDict = {}
for word in words: wordDict[word] = wordDict.setdefault(word,0)+1 # dict of 
identifiers w/ occurrence count
wordList = wordDict.items()
wordList.sort()
for wordCount in wordList: print '%-25s %3s' % wordCount

output (approximate, as I used tabs):

abost                           1
def                             1
server_closed_connection        1
session                 1

You can then examine this list for suspect names, especially those that 
occur once. We could apply some filtering to remove keywords and builtin names.

We could add a comment at the start of the program containing all the valid 
names, and extend this process to report just the ones that are not in the 
valid list.

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003


More information about the Python-list mailing list