Tabnanny really useful?

  • Thread starter Franz Steinhaeusler
  • Start date
F

Franz Steinhaeusler

Hi, I looked at tabnanny to check
a python source file.

But I didn't find anything, tabnanny
is able to find, what couldn't be found
by compile command.

Or have I missed something?

best regards,
 
J

John Roth

Franz Steinhaeusler said:
Hi, I looked at tabnanny to check
a python source file.

But I didn't find anything, tabnanny
is able to find, what couldn't be found
by compile command.

Or have I missed something?

Tabnanny is intended to check whether indentation
has mixed tabs and spaces. Files with mixed tabs
and spaces _can_ compile just fine if the editor
that produced them agrees with the compiler about
the number of spaces that a tab occupies.

On the other hand, such files are quite likely to
be messed up almost beyond repair if one tries
to edit them in an editor that treats tabs differently
from the one that produced it.

Files that stick to one or the other can always
be edited and compiled properly. The python
recommendation is to use spaces, but not
everyone agrees with this.

Most modern Python-aware editors handle
the situation reasonably, although you may have
to set parameters.

John Roth
 
F

Franz Steinhaeusler

Tabnanny is intended to check whether indentation
has mixed tabs and spaces. Files with mixed tabs
and spaces _can_ compile just fine if the editor
that produced them agrees with the compiler about
the number of spaces that a tab occupies.

Thanks for your explanation.

I tried an found:
def a():
->print
->.print

where point is a space.

tabnanny here complains and python compile it just fine.
 
F

Fredrik Lundh

Franz said:
Thanks for your explanation.

I tried an found:
def a():
->print
->.print

where point is a space.

tabnanny here complains and python compile it just fine.

really? that's a syntax error (you cannot change indentation nillywilly
inside a block), and the Python I'm using surely flags this as an error:

$ python -c "print repr(open('franz.py').read())"
'def a():\n\tprint\n\t print\n'

$ python franz.py
File "franz.py", line 3
print
^
SyntaxError: invalid syntax

while tabnanny gives it one thumb up:

$ python -m tabnanny -v franz.py
'franz.py': Clean bill of health.

what Python version are you using?

</F>
 
F

Franz Steinhaeusler

really? that's a syntax error (you cannot change indentation nillywilly
inside a block), and the Python I'm using surely flags this as an error:

$ python -c "print repr(open('franz.py').read())"
'def a():\n\tprint\n\t print\n'

$ python franz.py
File "franz.py", line 3
print
^
SyntaxError: invalid syntax

while tabnanny gives it one thumb up:

$ python -m tabnanny -v franz.py
'franz.py': Clean bill of health.

what Python version are you using?

</F>

Oh sorry, I meant
def a():
->print
..->print

C:\Python23\Lib>tabnanny.py -v c:\franz.py
'c:\\franz.py': *** Line 3: trouble in tab city! ***
offending line: ' \tprint\n'
indent not equal e.g. at tab size 1

C:\Python23\Lib>python -c "print repr(open('c:/franz.py').read())"
'def a():\n\tprint\n \tprint\n'

C:\Python23\Lib>c:/franz.py

C:\Python23\Lib>
 
S

Steve Holden

Franz said:
Oh sorry, I meant
def a():
->print
..->print

C:\Python23\Lib>tabnanny.py -v c:\franz.py
'c:\\franz.py': *** Line 3: trouble in tab city! ***
offending line: ' \tprint\n'
indent not equal e.g. at tab size 1

C:\Python23\Lib>python -c "print repr(open('c:/franz.py').read())"
'def a():\n\tprint\n \tprint\n'

C:\Python23\Lib>c:/franz.py

C:\Python23\Lib>

Well, you've probably answered your own question, then. Do you think
tabnanny is a useful piece of code now? I used it a lot when I first
started using Python, and still run it over code from unknown sources
(no pun intended) from time to time.

regards
Steve
 
F

Franz Steinhaeusler

Franz Steinhaeusler wrote:

[...]
Oh sorry, I meant
def a():
->print
..->print

C:\Python23\Lib>tabnanny.py -v c:\franz.py
'c:\\franz.py': *** Line 3: trouble in tab city! ***
offending line: ' \tprint\n'
indent not equal e.g. at tab size 1

C:\Python23\Lib>python -c "print repr(open('c:/franz.py').read())"
'def a():\n\tprint\n \tprint\n'

C:\Python23\Lib>c:/franz.py

C:\Python23\Lib>

Well, you've probably answered your own question, then. Do you think
tabnanny is a useful piece of code now?

Not really soo useful, because most syntax and also indentation errors
are actually detected by invoking python, i.e. the command compile.
But as combination for this: yes why not.
I looked for Stanis spe editor, which uses a combination of these two.

The background is:

I'm a member of the wxPython project Drpython (Python text editor
and much more), and wanted also check the usefulness of a kind of syntax
check, which should run, before saving a Python file.
PythonCard Codeeditor also uses tabnanny, as far as i can remember.

regards
 
S

Steve Holden

Franz said:
Franz Steinhaeusler wrote:

[...]
Oh sorry, I meant
def a():
->print
..->print

C:\Python23\Lib>tabnanny.py -v c:\franz.py
'c:\\franz.py': *** Line 3: trouble in tab city! ***
offending line: ' \tprint\n'
indent not equal e.g. at tab size 1

C:\Python23\Lib>python -c "print repr(open('c:/franz.py').read())"
'def a():\n\tprint\n \tprint\n'

C:\Python23\Lib>c:/franz.py

C:\Python23\Lib>

Well, you've probably answered your own question, then. Do you think
tabnanny is a useful piece of code now?


Not really soo useful, because most syntax and also indentation errors
are actually detected by invoking python, i.e. the command compile.
But as combination for this: yes why not.
I looked for Stanis spe editor, which uses a combination of these two.

The background is:

I'm a member of the wxPython project Drpython (Python text editor
and much more), and wanted also check the usefulness of a kind of syntax
check, which should run, before saving a Python file.
PythonCard Codeeditor also uses tabnanny, as far as i can remember.



regards

I've used drpython, and liked it. I think it would be a good way for
people to start to use the language, as it avoids the "just the command
line" syndrome without being as complex as IDLE or PythonWin. In short,
just about right for a beginner.

regards
Steve
 
F

Franz Steinhaeusler

Hello Steve,
I've used drpython, and liked it.

thank you, I'm sure, our project Admin will be pleased to hear this :)
I think it would be a good way for
people to start to use the language,

yes, this project is intended to fulfill this purpose.
as it avoids the "just the command
line" syndrome without being as complex as IDLE or PythonWin.

I think, it isn't too far away from these two anymore ;)
Considering the expansion with scripts and plugins,
and there are a several of them available.
In short,
just about right for a beginner.

I use it exclusively for a few months for any Python source editing.
regards
Steve

regards,
 
J

John Roth

Do you think tabnanny is a useful piece of code now? I used it a lot when
I first started using Python, and still run it over code from unknown
sources (no pun intended) from time to time.

I think it's a lot less useful today than it was a few
years ago, but it's still useful if you're stuck with an
editor that doesn't give you a robust set of options,
or if you've got to check a lot of modules in a library.

I think most python-aware editors have included
the needed functionality, or at least some reasonable
approximation.

I know what I would like to see in an editor:

First, it autodetects whether the module uses
tabs consistently, spaces consistently or a
mixture. If it uses tabs consistently, it then
uses the current default.

If it uses spaces consistently, it should also
autodetect the indentation setting in use in
the module and offer to change it if it's
different from the current default indentation
setting.

If it's inconsistent, it should make an attempt
to deduce the model the creating software
used; if it can it should change it to the
default setting without complaining. Otherwise
it should complain.

John Roth
 
S

Steve Holden

John Roth wrote:
[...]
I know what I would like to see in an editor:

First, it autodetects whether the module uses
tabs consistently, spaces consistently or a
mixture. If it uses tabs consistently, it then
uses the current default.

If it uses spaces consistently, it should also
autodetect the indentation setting in use in
the module and offer to change it if it's
different from the current default indentation
setting.

If it's inconsistent, it should make an attempt
to deduce the model the creating software
used; if it can it should change it to the
default setting without complaining. Otherwise
it should complain.

John Roth
Sounds like WingIDE to me. I'm a recent convert, but one feature that
impressed me was the instant warning I got when I indented code with
spaces in a tab-oriented source file.

regards
Steve
 
F

Franz Steinhaeusler

I think it's a lot less useful today than it was a few
years ago, but it's still useful if you're stuck with an
editor that doesn't give you a robust set of options,
or if you've got to check a lot of modules in a library.

I think most python-aware editors have included
the needed functionality, or at least some reasonable
approximation.

I know what I would like to see in an editor:

First, it autodetects whether the module uses
tabs consistently, spaces consistently or a
mixture. If it uses tabs consistently, it then
uses the current default.
If it uses spaces consistently, it should also
autodetect the indentation setting in use in
the module and offer to change it if it's
different from the current default indentation
setting.

If it's inconsistent, it should make an attempt
to deduce the model the creating software
used; if it can it should change it to the
default setting without complaining. Otherwise
it should complain.


Again, DrPython does this almost as you described ;)

There is an option in the preferences:
"Use File's Indentation"

and the indent setttings are set to the one,
what is found in the opened source file.

In the status line, the mode "SPACES" or "TABS" or
"MIXED" is displayed.

There are also the functions:
Edit=>Whitespace=>Check Indentation
Edit=>Whitespace=>Set Indentation to spaces
(replaces all tabs with the preset nr of spaces for tab

Exception 1:
(it could set then the tab mode to spaces)
and respectively:
Edit=>Whitespace=>Set Indentation to spaces

Exception 2:

If open a file, there could be a check, whether
tabs and spaces indentations are mixed, try to correct
if possible or complain (let the user decide).

=> Feature Request ;)

regards,
 
Y

Yet Another Mike

I'm told Tabnanny was inspired by lint, the Unix utiltity to check C sources
(and probably others). Lint was primarily useful in days long ago when CPUs
were slow and a compile used a significant amount of resources. In a
multiuser environment (we ran an Intel 286 in multiuser mode!!), the
compiles could bring everyone else to a crawl. Lint was used because it was
a less-CPU intensive way to catch bonehead errors and fix them before using
precious compile time.

Today,lint and Tabnanny may still have their uses as noted by others to
cleanup the tab/space conundrum.
 
M

Mike Meyer

Yet Another Mike said:
I'm told Tabnanny was inspired by lint, the Unix utiltity to check C sources
(and probably others). Lint was primarily useful in days long ago when CPUs
were slow and a compile used a significant amount of resources. In a
multiuser environment (we ran an Intel 286 in multiuser mode!!), the
compiles could bring everyone else to a crawl. Lint was used because it was
a less-CPU intensive way to catch bonehead errors and fix them before using
precious compile time.

Originally, lint caught errors the C compiler didn't flag as
errors. For example:

int *main = { ..... } ;

The C compiler would build and link that just fine on v7. lint would
complain about it. Of course, if you used the right ints to fill the
array, it would produce a valid executable.

<mike
 
J

JanC

Steve Holden schreef:
Sounds like WingIDE to me. I'm a recent convert, but one feature that
impressed me was the instant warning I got when I indented code with
spaces in a tab-oriented source file.

From the SciTE/Scintilla docs:

| tab.timmy.whinge.level
|
| For Python code, checks whether indenting is consistent. The default, 0
| turns off indentation checking, 1 checks whether each line is
| potentially inconsistent with the previous line, 2 checks whether any
| space characters occur before a tab character in the indentation, 3
| checks whether any spaces are in the indentation, and 4 checks for any
| tab characters in the indentation.
| 1 is a good level to use.

It's also in de wxSTC docs:
<http://www.yellowbrain.com/stc/lexing.html#setlexer>
(Below the lexers table.)
 
S

Stuart Bishop

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Franz Steinhaeusler wrote:

| Not really soo useful, because most syntax and also indentation errors
| are actually detected by invoking python, i.e. the command compile.
| But as combination for this: yes why not.
| I looked for Stanis spe editor, which uses a combination of these two.

Just because Python compiles and runs something does not mean that there
are no indentation errors. tabnanny works great because it removes all
ambiguous cases so you never have to worry about it. We use it as part
of our test suite to guard against misconfigured editors.

(again using '.' instead of space) Why would you want to allow '.\tprint
foo' in Python source when you could clean it up to be the totally
unambigous '........print foo'. As a more rational example, would you
want to allow '....\tprint foo', which will totally confuse someone with
their editor misconfigured with 4 char tabs?


- --
Stuart Bishop <[email protected]>
http://www.stuartbishop.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFByp4nAfqZj7rGN0oRAoq9AJ4scAe0A3aFnx7jRZuqtRXXaxAcRgCfYkVf
FbH2HBFA4/44KgZfpiPuvNU=
=nyQt
-----END PGP SIGNATURE-----
 

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
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top