DOS, UNIX and tabs

B

Ben

Hi,

I have a python script on a unix system that runs fine. I have a python
script on a windows system that runs fine. Both use tabs to indent
sections of the code. I now want to run them on the same system,
actually in the same script by combining bits and pieces. But whatever
I try my windows tabs get converted to spaces when I transfer it to the
unix system and the interpreter complains that the indentation style is
not consistent throughout the file. Short of going through 350 lines of
code and manually replacing spaces with tabs what an I do? I'm thinking
there surely must be a simple solution I have missed here!

Cheers,

Ben
 
B

Ben

I've found the unexpand command, which seems to do the trick. However,
it outputs to standard output, and I haven't worked out yet how to
capture that output to a file...

Ben
 
G

Grant Edwards

I've found the unexpand command, which seems to do the trick. However,
it outputs to standard output, and I haven't worked out yet how to
capture that output to a file...

unexpand <file1 >file2
 
B

Ben

Great - that worked.Thanks!
Is that a general method in linux you can always use to redirect
standard output to a file?

Cheers,

Ben
 
G

Grant Edwards

Great - that worked.Thanks!

Is that a general method in linux you can always use to redirect
standard output to a file?

Yup. The "<" operator redirects stdin, the ">" operator
redirects stdout. "2>" redirects stderr.
 
G

Gabriel Genellina

The python style guide [1] recommends four spaces per indentation
level.

[1] http://www.python.org/dev/peps/pep-0008/

It's not quite absolute on the topic:

For new projects, spaces-only are strongly recommended over tabs.

Of course you can do it anyway you like, but you should have a
*strong* reason for not following a *strong* recommendation.
(Just a note, you can use untabify.py (inside the Tools dir) to
convert tabs to spaces, instead of unexpand)


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
E

Erik Johnson

Ben Finney said:
Sebastian 'lunar' Wiesner said:
Just a tip for you: In python you never use tabs for indentation.

For some value of "you".
The python style guide [1] recommends four spaces per indentation
level.

[1] http://www.python.org/dev/peps/pep-0008/

It's not quite absolute on the topic:

For new projects, spaces-only are strongly recommended over tabs.

Even if were, read the Introduction. This is a coding standard intended
to apply to code which is going to checked in as part of the core python
build, not all Python! It's probably a pretty good standard to be following
in general, but come on... If Guido really wanted this enforced across the
board he could simply call anything that doesn't meet this standard to the
letter a SyntaxError and just stop there. For example, the standard states:

- Imports should usually be on separate lines, e.g.:

Yes: import os
import sys

No: import sys, os
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: Sorry, only one module per import line!


I'm sure that's not Guido's intention. ;)

-ej
 
S

Steven D'Aprano

Ben said:
I have a python script on a windows system that runs fine. Both use
tabs to indent sections of the code.

Just a tip for you: In python you never use tabs for indentation. The
python style guide [1] recommends four spaces per indentation level.

[1] http://www.python.org/dev/peps/pep-0008/

[obligatory pot-shot in the never-ending spaces versus tabs war]

In Python, I frequently use tabs for indentation, and I never have any
trouble *except* when posting code to Usenet, where other people's news
readers can't cope with tabs.

But I think we all agree that mixing tabs and spaces is A Very Bad Thing.
 
S

Sebastian 'lunar' Wiesner

Erik Johnson said:
Ben Finney said:
Sebastian 'lunar' Wiesner said:
Just a tip for you: In python you never use tabs for indentation.

For some value of "you".
The python style guide [1] recommends four spaces per indentation
level.

[1] http://www.python.org/dev/peps/pep-0008/

It's not quite absolute on the topic:

For new projects, spaces-only are strongly recommended over tabs.

Even if were, read the Introduction. This is a coding standard
intended
to apply to code which is going to checked in as part of the core
python
build, not all Python! It's probably a pretty good standard to be
following in general, but come on...

It is, and especially the problems with tabs shows you, why it is good
practice to follow the standard in your own code, too...
 
S

Steven D'Aprano

It is, and especially the problems with tabs shows you, why it is good
practice to follow the standard in your own code, too...

I don't know what "problems" with tabs you are talking about. I never have
problems with tabs. *Other people* who choose to use software that doesn't
understand tabs have problems.

I've spent a lot of time reading both sides of the tabs versus spaces
argument, and I haven't found anything yet that explains why tabs are, in
and of themselves, bad.
 
C

Christophe Cavalaria

Steven said:
I don't know what "problems" with tabs you are talking about. I never have
problems with tabs. *Other people* who choose to use software that doesn't
understand tabs have problems.

I've spent a lot of time reading both sides of the tabs versus spaces
argument, and I haven't found anything yet that explains why tabs are, in
and of themselves, bad.

You gave the reason in your post : because other people who are using
software that doesn't understand tabs as YOU expect them to have problems
with your code.

Tabs aren't a problem at all as long as nobody else than you edit your code.
 
S

Sebastian 'lunar' Wiesner

Steven D'Aprano said:
I don't know what "problems" with tabs you are talking about. I never
have problems with tabs. *Other people* who choose to use software
that doesn't understand tabs have problems.

Mmmh, maybe you never worked together with a team of other programmers
or have such a high position, that you can afford to ignore complaints
of your co-workers...
 
F

Felix Benner

Sebastian said:
Ben said:
I have a python script on a windows system that runs fine. Both use
tabs to indent sections of the code.

Just a tip for you: In python you never use tabs for indentation. The
python style guide [1] recommends four spaces per indentation level.

[1] http://www.python.org/dev/peps/pep-0008/

I like using tabs. And the style guide doesn't give a reason why one
shouldn't and neither does the thread
http://www.python.org/search/hypermail/python-1994q2/0198.html in the
archive.
So what's the point in typing four spaces for indentation instead of one
tab?
 
F

Felix Benner

Christophe said:
Steven D'Aprano wrote:
You gave the reason in your post : because other people who are using
software that doesn't understand tabs as YOU expect them to have problems
with your code.

Tabs aren't a problem at all as long as nobody else than you edit your code.

Sorry, but that's a silly argument. With the same argument we should
stop using python alltogether since the usual MBA will understand
nothing but VBA.
 
C

Christophe Cavalaria

Felix said:
Sorry, but that's a silly argument. With the same argument we should
stop using python alltogether since the usual MBA will understand
nothing but VBA.

No it isn't. If you have to work with some MBA that understands nothing but
VBA, why the hell are you writing Python code in the first place?
 
D

Duncan Booth

Felix Benner said:
So what's the point in typing four spaces for indentation instead of one
tab?

So long as you always use only tabs there is no problem. So long as you
only use spaces there is no problem. If you mix tabs and spaces you can
introduce bugs. In particular, some people set their editor up to expand 1
tab to the next multiple of 4 spaces on their screen, but the usual
convention for tabs (and one that Python follows internally) is that tabs
expand to the next multiple of 8 spaces.

Usually when you mix spaces and tabs what you get either works the way you
intend, or it generates a syntax error. Once however when this recurring
question popped up I did a search through a load of Python files and
actually found once instance of some code which had been released and ran
whether tabs were expanded to 4 or 8 space boundaries. Reading that code it
was apparent that it had been written using 4 space tabs on the screen, but
that when it ran it did something different than had been intended.

So, given that mixing tabs and spaces is deadly choose one or the other and
stick to it. If you intend to work with other people then choose the same
convention as they use. If you are never going to work with others then use
whichever scheme makes you most comfortable.

Be careful as not all open source projects use the same convention: in
previous discussions on this newsgroup there were people arguing quite
strongly for using the tab convention. A straw poll indicated that there
was 1 open source project with 3 developers using tabs, and all other open
source projects use spaces only as the stated (but not always strictly
enforced) convention. Your experience may of course differ.

Of course nobody in their right minds actually types 4 spaces for
indentation: they use an editor where if the automatic indentation
isn't correct then hitting the tab key inserts the correct number of spaces
(and with luck where hitting the backspace key deletes back to the previous
tabstop).
 
M

Marc 'BlackJack' Rintsch

I like using tabs. And the style guide doesn't give a reason why one
shouldn't and neither does the thread
http://www.python.org/search/hypermail/python-1994q2/0198.html in the
archive.
So what's the point in typing four spaces for indentation instead of one
tab?

You don't need to type four spaces. Every decent editor lets you use the
Tab key and inserts the proper amount of spaces for you. Same for
Backspace removing the proper amount of spaces to get to the previous
"tab stop".

There are plenty of "reasons" from both sides. This is a religious issue,
so please search the net for answers and don't start another
flam^H^H^H^Hdebate here. Please!

Ciao,
Marc 'BlackJack' Rintsch
 
G

Grant Edwards

I've spent a lot of time reading both sides of the tabs versus spaces
argument, and I haven't found anything yet that explains why tabs are, in
and of themselves, bad.

They aren't. Using tabs isn't bad. Using both tabs and spaces
is bad, so the people managing the official Python source tree
picked one. Maybe they've got reasons for liking spaces over
tabs. Maybe they just flipped a coin. It doens't matter. What
matters is picking one and sticking with it.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top