checking for mis-spelled variable names / function names

N

News123

Hi,

Let's imagine following code

def specialfunc():
print "very special function"

name= getuserinput()
if name == 'one_name_out_of_a_million':
print "Hey your name '%s' is really rare" % namee
specialfunk()

my python script could survive thousands of runs before falling into
the mis-spelled code section. ('namee' instead of 'name' and
'specialfunck()' instead of 'specialfunc()'
I know that good designers should always test their code and have
complete code coverage, before releasing their beasts into the wild, but
in my experience this is not always what happens.

I fell already over quite of my own sins, but also over typoes of other
python authors.

Is there any way in python to check for mis-spelled variable / function
names?


In perl for example 'use strict;' would detect bad variable names,
though it wouldn't detect calls to undeclared functions.

I am aware, that there is absolutely valid (and useful) python code with
undefined functions / variable names.

However for the scripts that I write I would prefer to identify as many
typoes as possibe already when trying to run the script the first (and
not the millionth) time.

Do you have ideas suggestions?
Are there any 'lint' like tools trying to analyze python code for
potential stupidities?

If yes, what would be 'the' way to add these tools / modules at )least
during the development cycle) to the scripts.


thanks in advance for any thoughts / suggestions.


bye


N
 
C

Chris Rebert

Hi,

Let's imagine following code

def specialfunc():
print "very special function"

name= getuserinput()
if name == 'one_name_out_of_a_million':
print "Hey your name '%s' is really rare" % namee
specialfunk()

my python script could survive thousands of runs before falling into
the mis-spelled code section. ('namee' instead of 'name' and
'specialfunck()' instead of 'specialfunc()'
I know that good designers should always test their code and have
complete code coverage, before releasing their beasts into the wild, but
in my experience this is not always what happens.

I fell already over quite of my own sins, but also over typoes of other
python authors.

Is there any way in python to check for mis-spelled variable / function
names?


In perl for example 'use strict;' would detect bad variable names,
though it wouldn't detect calls to undeclared functions.

I am aware, that there is absolutely valid (and useful) python code with
undefined functions / variable names.

However for the scripts that I write I would prefer to identify as many
typoes as possibe already when trying to run the script the first (and
not the millionth) time.

Do you have ideas suggestions?
Are there any 'lint' like tools trying to analyze python code for
potential stupidities?

PyLint: http://www.logilab.org/857
PyChecker: http://pychecker.sourceforge.net/

I believe they both check for variable name typos, among many other things.

Cheers,
Chris
 
J

John Machin

Hi,

Let's imagine following code

def specialfunc():
        print "very special function"

name= getuserinput()
if name == 'one_name_out_of_a_million':
        print "Hey your name '%s' is really rare" % namee
        specialfunk()

my python script could survive thousands of runs before falling into
the mis-spelled code section. ('namee' instead of 'name' and
'specialfunck()' instead of 'specialfunc()'
I know that good designers should always test their code and have
complete code coverage, before releasing their beasts into the wild, but
in my experience this is not always what happens.

I fell already over quite of my own sins, but also over typoes of other
python authors.

Is there any way in python to check for mis-spelled variable / function
names?

In perl for example 'use strict;' would detect bad variable names,
though it wouldn't detect calls to undeclared functions.

I am aware, that there is absolutely valid (and useful) python code with
 undefined functions / variable names.

However for the scripts that I write I would prefer to identify as many
typoes as possibe already when trying to run the script the first (and
not the millionth) time.

Do you have ideas suggestions?
Are there any 'lint' like tools trying to analyze python code for
potential stupidities?

If yes, what would be 'the' way to add these tools / modules at )least
during the development cycle) to the scripts.

C:\junk>type typo.py
def specialfunc():
print "very special function"

### name= getuserinput() ### won't even compile; gets NameError
def get_check_name(name):
if name == 'one_name_out_of_a_million':
print "Hey your name '%s' is really rare" % namee
specialfunk()

C:\junk>pychecker typo.py

C:\junk>c:\python25\python.exe c:\python25\Lib\site-packages\pychecker
\checker.py typo.py
Processing typo...

Warnings...

typo.py:7: No global (namee) found
typo.py:8: No global (specialfunk) found
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top