473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Code reformater?

Hello

When I copy/paste Python code from the web, every so often,
the TABs are wrong, which means that the code won't work and I have to
manually reformat the code.

Is there a code reformater that can parse the code to make it right?

Thanks.
Jan 20 '07 #1
8 1476
Vincent Delporte <ju*****@acme.comwrites:
When I copy/paste Python code from the web, every so often, the TABs
are wrong, which means that the code won't work and I have to
manually reformat the code.

Is there a code reformater that can parse the code to make it right?
Indentation is necessary for the syntax in Python. If that information
is lost, it can't be reliably recreated.

--
\ "People come up to me and say, 'Emo, do people really come up |
`\ to you?'" -- Emo Philips |
_o__) |
Ben Finney

Jan 20 '07 #2
Vincent Delporte schrieb in comp.lang.python:

Hi
When I copy/paste Python code from the web, every so often,
the TABs are wrong, which means that the code won't work and I have to
manually reformat the code.

Is there a code reformater that can parse the code to make it right?
It may help to look at the settings of your editor. I think Vim (and
surely emacs) can help you with that.

(look at :help retab, paste, expandtab in (g)Vim)

Mario
--
Mario Wehbrink
Jan 20 '07 #3

"Vincent Delporte" wrote:
Hello

When I copy/paste Python code from the web, every so often,
the TABs are wrong, which means that the code won't work and I have to
manually reformat the code.

Is there a code reformater that can parse the code to make it right?

Thanks.
Maybe my thread "help: code formatter, 08/01/2007 helps a little? Here are
some of the answers:

*
Why don't you just write one? :)
Seriously: Try.
*
Tools\scripts\reindent.py in your Python distribution.
*
Why, yes, there is:
http://lacusveris.com/PythonTidy/PythonTidy.python
*
tabnanny ?
siggi


Jan 20 '07 #4
At Saturday 20/1/2007 14:37, Siggi wrote:
When I copy/paste Python code from the web, every so often,
the TABs are wrong, which means that the code won't work and I have to
manually reformat the code.

Is there a code reformater that can parse the code to make it right?

Thanks.

Maybe my thread "help: code formatter, 08/01/2007 helps a little? Here are
some of the answers:

*
Why don't you just write one? :)
Seriously: Try.
*
Tools\scripts\reindent.py in your Python distribution.
*
Why, yes, there is:
http://lacusveris.com/PythonTidy/PythonTidy.python
*
tabnanny ?
As the indentation *is* significant in python, none of the above can
help if you lose the indentation. Try to reconstruct this:

def foo():
if a>0:
if b>0:
print 1
print 2
else:
return 3
return 4

The tools may help to make the indentation consistent (tabs/8
spaces/4 spaces/2 spaces mixed) or look better, but not to make it right.
--
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

Jan 21 '07 #5
On Sat, 20 Jan 2007 23:51:24 -0300, Gabriel Genellina wrote:
As the indentation *is* significant in python, none of the above can
help if you lose the indentation. Try to reconstruct this:

def foo():
if a>0:
if b>0:
print 1
print 2
else:
return 3
return 4

The tools may help to make the indentation consistent (tabs/8
spaces/4 spaces/2 spaces mixed) or look better, but not to make it right.

Sure -- but a heuristic that gets it right *sometimes* may still be
useful, provided the user knows that the code may not be indented
correctly.

There are lots of legal Python blocks where the indentation CAN be
reconstructed correctly, and only a relatively small proportion where it
can't -- the tool could do its best, and warn the user when there are
indents that can't be dealt with. Or even refuse the temptation to guess,
but re-indent whatever parts of the code it is sure about.

Still, it is better not to lose the indentation in the first place.

--
Steven.

Jan 21 '07 #6
At Sunday 21/1/2007 00:15, Steven D'Aprano wrote:
>On Sat, 20 Jan 2007 23:51:24 -0300, Gabriel Genellina wrote:
As the indentation *is* significant in python, none of the above can
help if you lose the indentation. Try to reconstruct this:

def foo():
if a>0:
if b>0:
print 1
print 2
else:
return 3
return 4

The tools may help to make the indentation consistent (tabs/8
spaces/4 spaces/2 spaces mixed) or look better, but not to make it right.


Sure -- but a heuristic that gets it right *sometimes* may still be
useful, provided the user knows that the code may not be indented
correctly.

There are lots of legal Python blocks where the indentation CAN be
reconstructed correctly, and only a relatively small proportion where it
can't --
I'd say absolutely the opposite. Try the example above. Or this one, simpler:

def foo():
if a>0:
if b>0:
print 1
print 2

Which level should be 'print 2' assigned to? There are 3 choices.
What if there is a third print? There are 3, 2, or 1 possibilities,
depending on where you put the previous one. The same for any other
statement below.
The problem is, after a colon, you know that a new block begins and
you must indent, but you don't know when that block ends except in a
few cases (an else clause, by example, and only if there was a single
preceding if).
>the tool could do its best, and warn the user when there are
indents that can't be dealt with. Or even refuse the temptation to guess,
but re-indent whatever parts of the code it is sure about.
....almost nothing, I'm afraid... :(
>Still, it is better not to lose the indentation in the first place.
Sure!
--
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

Jan 21 '07 #7
On Sun, 21 Jan 2007 14:15:46 +1100, Steven D'Aprano
<st***@REMOVE.THIS.cybersource.com.auwrote:
>Still, it is better not to lose the indentation in the first place.
Thanks for the tips. But it does happen when copy/pasting code from
either a web page or an e-mail that TABs are messed up, which is not a
problem with other languages, but is a problem with Python. Too bad.
Jan 22 '07 #8
In <ur********************************@4ax.com>, Vincent Delporte wrote:
On Sun, 21 Jan 2007 14:15:46 +1100, Steven D'Aprano
<st***@REMOVE.THIS.cybersource.com.auwrote:
>>Still, it is better not to lose the indentation in the first place.

Thanks for the tips. But it does happen when copy/pasting code from
either a web page or an e-mail that TABs are messed up, which is not a
problem with other languages, but is a problem with Python. Too bad.
Well then don't use code from people using TABs. If it's indented by four
spaces per level, like suggested by the style guide, there's no problem
with TABs.

Ciao,
Marc 'BlackJack' Rintsch

Jan 22 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
4
by: Stuk | last post by:
Hi, im false beginner in C so that`s why im writting here :). I have to write a Text Reformater, which should read data from text file every Verse. In text file may appear special directives (for...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.