Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   checking for mis-spelled variable names / function names (http://www.velocityreviews.com/forums/t646081-checking-for-mis-spelled-variable-names-function-names.html)

News123 11-25-2008 11:16 PM

checking for mis-spelled variable names / function names
 
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

Chris Rebert 11-25-2008 11:27 PM

Re: checking for mis-spelled variable names / function names
 
On Tue, Nov 25, 2008 at 3:16 PM, News123 <news123@free.fr> wrote:
> 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
--
Follow the path of the Iguana...
http://rebertia.com

>
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>


John Machin 11-26-2008 12:37 AM

Re: checking for mis-spelled variable names / function names
 
On Nov 26, 10:16*am, News123 <news...@free.fr> wrote:
> 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



All times are GMT. The time now is 03:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.