Spell-checking Python source code

Ricardo Aráoz ricaraoz at gmail.com
Sat Sep 8 18:30:11 EDT 2007


David wrote:
>>> (I know that the better practice is to isolate user-displayed strings
>>> from the code, but in this case that just didn't happen.)
>>>
>> Use the re module, identify the strings and write them to another file,
>> then open the file with your spell checker. Program shouldn't be more
>> than 10 lines.
>>
>>
> 
> Have a look at the tokenize python module for the regular expressions
> for extracting strings (for all possible Python string formats). On a
> Debian box you can find it here: /usr/lib/python2.4/tokenize.py
> 
> It would probably be simpler to hack a copy of that script so it
> writes all the strings in your source to a text file, which you then
> spellcheck.
> 
> Another method would be to log all the strings your web app writes, to
> a text file, then run through your entire site, and then spellcheck
> your logfile.
> 

Nice module :

import tokenize

def processStrings(type, token, (srow, scol), (erow, ecol), line):
    if tokenize.tok_name[type] == 'STRING' :
        print tokenize.tok_name[type], token, \
              (srow, scol), (erow, ecol), line

file = open("myprogram.py")

tokenize.tokenize(
    file.readline,
    processStrings
    )

How would you go about writing the output to a file? I mean, I would
like to open the file at main level and pass a handle to the file to
processStrings to write to it, finally close output file at main level.
Probably a class with a processString method?





More information about the Python-list mailing list