mod_speling for python

P

PT

Hi,
I'm not looking to get into a debate about case-sensitive vs.
insensitive programming languages,
but I was looking for suggestions about how it might be possible to
add a hook to the python parser similar to the Apache mod_speling
module (links below). So that for example if there is a NameError
exception, python walks the globals() and locals() to see if it might
be just a minor spelling/capitalization error.

I can see how this might be done for ImportErrors, using ihooks or iu,
but I don't see how to do it for NameErrors. The python compiler
module doesn't seem to be the answer.

I know there are drawbacks, disadvantages, this is "evil", etc. I
only wanted to know what ways there are to add such a hook if it is
possible without altering the C source for python itself.

Possible End-User Solution A:
Simply add an "import spelling" to a python file to enable
spell-checking, sort of like how you use the psyco module (speaking of
which, how many times have you misspelled that).

Possible End-User Solution B:
Create an alternative executable that wraps the python interpreter.
This wrapper may or may not have to use its own source code parser,
like the unreleased mobius-python project.

http://httpd.apache.org/docs/mod/mod_speling.html
http://lxr.webperf.org/source.cgi/modules/mappers/mod_speling.c
http://www.mcmillan-inc.com/importhooks.html
http://www.python.org/doc/current/lib/module-compiler.html
http://sourceforge.net/projects/mobiuspython
 
S

Skip Montanaro

prtk3> ... if there is a NameError exception, python walks the globals()
prtk3> and locals() to see if it might be just a minor
prtk3> spelling/capitalization error.

prtk3> I can see how this might be done for ImportErrors, using ihooks
prtk3> or iu, but I don't see how to do it for NameErrors. The python
prtk3> compiler module doesn't seem to be the answer.

prtk3> I know there are drawbacks, disadvantages, this is "evil", etc.
prtk3> I only wanted to know what ways there are to add such a hook if
prtk3> it is possible without altering the C source for python itself.

Take a look at sys.excepthook. Actual implementation is left as an exercise
for the reader. <wink>

Skip
 
H

Harald Massa

prtk3> I know there are drawbacks, disadvantages, this is "evil",
etc. prtk3> I only wanted to know what ways there are to add such
a hook if prtk3> it is possible without altering the C source for
python itself.

Take a look at sys.excepthook. Actual implementation is left as an
exercise for the reader. <wink>

And be carefull: it breaks the Zen of Python and thereby removes it's mojo.
 
J

Josiah Carlson

I know there are drawbacks, disadvantages, this is "evil", etc. I
only wanted to know what ways there are to add such a hook if it is
possible without altering the C source for python itself.

Ambiguities during execution are bad. I've got two functions Foo and
foO, if I type in foo, what did I mean?

- Josiah
 
S

Skip Montanaro

Josiah> Ambiguities during execution are bad. I've got two functions
Josiah> Foo and foO, if I type in foo, what did I mean?

I didn't interpret the OP's question as wanting to automatically map Foo to
foO, but to give the user a more helpful error message, suggesting other
candidate names which he might have mistyped.

Skip
 
J

Josiah Carlson

Skip said:
Josiah> Ambiguities during execution are bad. I've got two functions
Josiah> Foo and foO, if I type in foo, what did I mean?

I didn't interpret the OP's question as wanting to automatically map Foo to
foO, but to give the user a more helpful error message, suggesting other
candidate names which he might have mistyped.

That could be useful, if what you say is actually what he meant.

- Josiah
 
D

Doug Holton

This can be helpful for beginners especially, to catch not only
capitalization and minor misspellings, but perhaps other minor errors
too such as being one space off when indenting, or forgetting a colon.

The python compiler module can check syntax, it just isn't all
documented. See this example code:
http://mail.python.org/pipermail/python-dev/2000-September/009501.html
See also the tokenize module:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298
Or you might go so extreme as metaprogramming and language parsing. See
PLY: http://systems.cs.uchicago.edu/ply/ and http://gnosis.cx/TPiP/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top