Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Spell-checking Python source code

Reply
Thread Tools

Spell-checking Python source code

 
 
John Zenger
Guest
Posts: n/a
 
      09-08-2007
To my horror, someone pointed out to me yesterday that a web app I
wrote has been prominently displaying a misspelled word. The word was
buried in my code.

Is there a utility out there that will help spell-check literal
strings entered into Python source code? I don't mean spell-check
strings entered by the user; I mean, go through the .py file, isolate
strings, and tell me when the strings contain misspelled words. In an
ideal world, my IDE would do this with a red wavy line.

I guess a second-best thing would be an easy technique to open a .py
file and isolate all strings in it.

(I know that the better practice is to isolate user-displayed strings
from the code, but in this case that just didn't happen.)

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=
Guest
Posts: n/a
 
      09-08-2007
John Zenger wrote:
> To my horror, someone pointed out to me yesterday that a web app I
> wrote has been prominently displaying a misspelled word. The word was
> buried in my code.
>
> Is there a utility out there that will help spell-check literal
> strings entered into Python source code? I don't mean spell-check
> strings entered by the user; I mean, go through the .py file, isolate
> strings, and tell me when the strings contain misspelled words. In an
> ideal world, my IDE would do this with a red wavy line.
>
> I guess a second-best thing would be an easy technique to open a .py
> file and isolate all strings in it.
>
> (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.



 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      09-08-2007
> >
> > (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.
 
Reply With Quote
 
=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=
Guest
Posts: n/a
 
      09-08-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?


 
Reply With Quote
 
DaveM
Guest
Posts: n/a
 
      09-09-2007
On Sat, 08 Sep 2007 14:04:55 -0700, John Zenger <(E-Mail Removed)>
wrote:

> In an ideal world, my IDE would do this with a red wavy line.


I can't help with your problem, but this is the first thing I turn off in
Word. It drives me _mad_.

Sorry - just had to share that.

DaveM
 
Reply With Quote
 
David Trudgett
Guest
Posts: n/a
 
      09-09-2007
John Zenger writes:

> In an ideal world, my IDE would do this with a red wavy line.


You didn't mention which IDE you use; however, if you use Emacs, there
is flyspell-prog-mode which does that for you (checks your spelling
"on the fly", but only within comments and strings).

Regards,
David Trudgett

--
These are not the droids you are looking for. Move along.
 
Reply With Quote
 
Miki
Guest
Posts: n/a
 
      09-09-2007
>> In an ideal world, my IDE would do this with a red wavy line.
>
> You didn't mention which IDE you use; however, if you use Emacs, there
> is flyspell-prog-mode which does that for you (checks your spelling
> "on the fly", but only within comments and strings).

Same in Vim (:set spell)

HTH,
--
Miki <(E-Mail Removed)>
http://pythonwise.blogspot.com

 
Reply With Quote
 
David
Guest
Posts: n/a
 
      09-09-2007
> 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?


tokenize.tokenize() takes a callable object as it's second arg. So you
can use a class which you construct with the file, and you give it an
appropriate __call__ method.

http://docs.python.org/ref/callable-types.html

Although with a short script a global var may be simpler.
 
Reply With Quote
 
Benjamin
Guest
Posts: n/a
 
      09-10-2007
On Sep 8, 4:04 pm, John Zenger <(E-Mail Removed)> wrote:
> To my horror, someone pointed out to me yesterday that a web app I
> wrote has been prominently displaying a misspelled word. The word was
> buried in my code.
>
> Is there a utility out there that will help spell-check literal
> strings entered into Python source code? I don't mean spell-check
> strings entered by the user; I mean, go through the .py file, isolate
> strings, and tell me when the strings contain misspelled words. In an
> ideal world, my IDE would do this with a red wavy line.
>
> I guess a second-best thing would be an easy technique to open a .py
> file and isolate all strings in it.
>
> (I know that the better practice is to isolate user-displayed strings
> from the code, but in this case that just didn't happen.)


This is when it's good to use put all your UI strings in a file and
get the advantages of spelling checking ease and the ability to
translate the app.

 
Reply With Quote
 
David
Guest
Posts: n/a
 
      09-10-2007
On 9/9/07, David <(E-Mail Removed)> wrote:
> > 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?

>


You can also use closures for this.

http://ivan.truemesh.com/archives/000411.html

See the example in the "Assignment considered awkward" section. Since
you're not assigning to your file variable in processStrings you don't
have to use the list work-around mentioned later in the doc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cscope like source code navigation for python code Gelonida Python 3 08-08-2010 12:15 PM
Panel with vivible=false in source code / large source Jens-Oliver Murer ASP .Net 1 10-17-2008 01:00 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi C Programming 0 12-29-2004 05:10 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi Cisco 0 12-29-2004 05:03 PM
can't use View, Source menu to open the source code. Jenny Javascript 3 12-17-2004 07:01 AM



Advertisments