473,569 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Colons, indentation and reformatting. (2)

I was just perusing a Wikipedia entry on the "off side rule" at
http://en.wikipedia.org/wiki/Off-side_rule .
It says that the colon in Python is purely for readability, and cites
our FAQ entry
http://www.python.org/doc/faq/genera...ns-required-fo...
Jan 9 '07 #1
9 1572

Paddy wrote:
I was just perusing a Wikipedia entry on the "off side rule" at
http://en.wikipedia.org/wiki/Off-side_rule .
It says that the colon in Python is purely for readability, and cites
our FAQ entry
http://www.python.org/doc/faq/genera...ns-required-fo...
.
However, near the top of the Alternatives section, it states that for C
type, curly braces using languages:
"An advantage of this is that program code can be automatically
reformatted and neatly indented without fear of the block structure
changing".

Thinking about it a little, it seems that a colon followed by
non-indented code that has just been pasted in could also be used by a
Python-aware editor as a flag to re-indent the pasted code.
How would you re-indent this?

if x>0: print x

>
Tell me it is not so, or I will be editing the Wikipedia page I think.

- Paddy.
Jan 9 '07 #2
"Paddy" <pa*******@nets cape.netwrote in message
news:11******** **************@ i15g2000cwa.goo glegroups.com.. .
>I was just perusing a Wikipedia entry on the "off side rule" at
http://en.wikipedia.org/wiki/Off-side_rule .
It says that the colon in Python is purely for readability, and cites
our FAQ entry
http://www.python.org/doc/faq/genera...ns-required-fo...
.
However, near the top of the Alternatives section, it states that for C
type, curly braces using languages:
"An advantage of this is that program code can be automatically
reformatted and neatly indented without fear of the block structure
changing".

Thinking about it a little, it seems that a colon followed by
non-indented code that has just been pasted in could also be used by a
Python-aware editor as a flag to re-indent the pasted code.

Tell me it is not so, or I will be editing the Wikipedia page I think.

- Paddy.
No, the ambiguity comes in when you have a nested construct within another
nested construct. Here is some (fake) code where all the indentation was
lost after pasting through a badly-behaved newsreader (this is NOT real
code, I know that it wont really run, I'm just trying to demonstrate the
indentation issue):

while x:
a = 100
if b 3:
a += 1
b += 1

Here are some valid indented versions:

while x:
a = 100
if b 3:
a += 1
b += 1

while x:
a = 100
if b 3:
a += 1
b += 1

while x:
a = 100
if b 3:
a += 1
b += 1

while x:
a = 100
if b 3:
a += 1
b += 1

The colons alone are not sufficient to tell us which is correct.

-- Paul
Jan 9 '07 #3

me********@aol. com wrote:
Paddy wrote:
I was just perusing a Wikipedia entry on the "off side rule" at
http://en.wikipedia.org/wiki/Off-side_rule .
It says that the colon in Python is purely for readability, and cites
our FAQ entry
http://www.python.org/doc/faq/genera...ns-required-fo...
.
However, near the top of the Alternatives section, it states that for C
type, curly braces using languages:
"An advantage of this is that program code can be automatically
reformatted and neatly indented without fear of the block structure
changing".

Thinking about it a little, it seems that a colon followed by
non-indented code that has just been pasted in could also be used by a
Python-aware editor as a flag to re-indent the pasted code.

How would you re-indent this?

if x>0: print x
If pasted as a line , after a line *ending with* a colon then indent it
w.r.t. previous line.
if pasting full lines after such a line then first pasted line cannot
be indented more than this line; if it is then flag for re-indenting
pasted block either equal too or less than this line.

- Paddy.

Jan 9 '07 #4

Paul McGuire wrote:
"Paddy" <pa*******@nets cape.netwrote in message
news:11******** **************@ i15g2000cwa.goo glegroups.com.. .
I was just perusing a Wikipedia entry on the "off side rule" at
http://en.wikipedia.org/wiki/Off-side_rule .
It says that the colon in Python is purely for readability, and cites
our FAQ entry
http://www.python.org/doc/faq/genera...ns-required-fo...
.
However, near the top of the Alternatives section, it states that for C
type, curly braces using languages:
"An advantage of this is that program code can be automatically
reformatted and neatly indented without fear of the block structure
changing".

Thinking about it a little, it seems that a colon followed by
non-indented code that has just been pasted in could also be used by a
Python-aware editor as a flag to re-indent the pasted code.

Tell me it is not so, or I will be editing the Wikipedia page I think.

- Paddy.
No, the ambiguity comes in when you have a nested construct within another
nested construct. Here is some (fake) code where all the indentation was
lost after pasting through a badly-behaved newsreader (this is NOT real
code, I know that it wont really run, I'm just trying to demonstrate the
indentation issue):

while x:
a = 100
if b 3:
a += 1
b += 1

Here are some valid indented versions:

while x:
a = 100
if b 3:
a += 1
b += 1

while x:
a = 100
if b 3:
a += 1
b += 1

while x:
a = 100
if b 3:
a += 1
b += 1

while x:
a = 100
if b 3:
a += 1
b += 1

The colons alone are not sufficient to tell us which is correct.

-- Paul
Won't the following rules work when pasting complete Python statements
and complete lines, after other lines in an editor:

lets call the line after which the block is to be pasted the paste
line, and the original indent of the first line of the copied block to
be pasted the copy indent.

If the paste line ends in a colon then the copy indent must be greater
than the paste line indent, or the copy block should be re-indented on
pasting to make it so.
If the paste line does not end in a colon then the copy block indent
should be equal too or less than the paste line indent. If this is not
the case then the user should be asked wether to re-indent the copy
block to be equal to, or de-dented w.r.t. the paste line indent prior
to pasting.

- Paddy.

Jan 9 '07 #5
Won't the following rules work when pasting complete Python statements
and complete lines, after other lines in an editor:

lets call the line after which the block is to be pasted the paste
line, and the original indent of the first line of the copied block to
be pasted the copy indent.

If the paste line ends in a colon then the copy indent must be greater
than the paste line indent, or the copy block should be re-indented on
pasting to make it so.
If the paste line does not end in a colon then the copy block indent
should be equal too or less than the paste line indent. If this is not
the case then the user should be asked wether to re-indent the copy
block to be equal to, or de-dented w.r.t. the paste line indent prior
to pasting.

Could be done that way - but is it a killer feature? I doubt it, but
maybe you convince somebody to implement it.

Yet it certainly doesn't justify a wikipedia edit - the FAQ entry is
right, and there is no way of auto-indenting python code that for some
reason suffered from indention mix-up.

Diez
Jan 9 '07 #6

OK, whilst colons are not sufficient to re-format a completely
mis-indented file. I'm thinking that they are sufficient for
reformatting most pasted code blocks when refactoring say?
- Paddy.

Jan 9 '07 #7
"Paddy" <pa*******@nets cape.netwrote in message
news:11******** **************@ 38g2000cwa.goog legroups.com...
>
If this is not
the case then the user should be asked wether to re-indent the copy
block to be equal to, or de-dented w.r.t. the paste line indent prior
to pasting.
How would the user know this? Every dedent is ambiguous, since there is no
punctuation to indicate it.

-- Paul
Jan 9 '07 #8
On 8 Jan 2007 23:57:29 -0800, Paddy <pa*******@nets cape.netwrote:
>
OK, whilst colons are not sufficient to re-format a completely
mis-indented file. I'm thinking that they are sufficient for
reformatting most pasted code blocks when refactoring say?
Let's put it this way: if the formatter can assume the original code is
valid (i.e. has the intended indentation) then it can do all kinds of nifty
things to it.

Personally, I'm happy with what Emacs' python-mode offers: suggested indents
as I type, and a command to indent/de-dent the highlighted block one step.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyn dns.org R'lyeh wgah'nagl fhtagn!
Jan 9 '07 #9

"Jorgen Grahn" <gr********@sni pabacken.dyndns .org>wrote:
On 8 Jan 2007 23:57:29 -0800, Paddy <pa*******@nets cape.netwrote:

OK, whilst colons are not sufficient to re-format a completely
mis-indented file. I'm thinking that they are sufficient for
reformatting most pasted code blocks when refactoring say?

Let's put it this way: if the formatter can assume the original code is
valid (i.e. has the intended indentation) then it can do all kinds of nifty
things to it.
This is true - and I think it will only fail if the "entry" point in the pasted
code is "further to the right" than where it has to fit in to the original
code - i.e. if you "run out of space" to the left. - but in that case
you really are hacking, and you are in urgent need of some slashing...

- Hendrik

Jan 10 '07 #10

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

Similar topics

21
6505
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help Workshop program: hcw.exe that's included with Visual Basic. This exact same file compiled perfectly with no notes, warnings or errors prior to...
0
1083
by: Draz | last post by:
I have some XML extended language that generates output. Everything works ok. I have created additional xsl templates for reformatting strings, mathematical calculations and reformatting numbers. It also works ok. Finally, I'm missing one step. I need to put all these results as values closed in quotes, everything in one line. Example...
3
1367
by: Chris Lane | last post by:
Hi, This is super annoying FrontPage like behavior. Does anybody know how I can stop the Visual Studio.NET IDE from reformatting my HTML? Thank You
7
1237
by: Lloyd Sheen | last post by:
I know I have seen a way to stop the terrible reformatting that VS 2003 IDE does. Beyond that is MS going to do a better (could not do a worse) job of auto formatting. Every change I make to the format of the page causes reformatting which totally gets rid of the idea that controls are nested and have some structure. Please help
6
2172
by: Abhishek Srivastava | last post by:
Hello All, I am developing a page which has a complex layout. This layout cannot be made using VS alone, so I have to go into the html view and edit the html myself. The problem is that everytime I save the file, (or add/remove any component to the page) VS.Net reformats and re-edits the whole page. I find this very irrating because...
2
1192
by: Paddy | last post by:
i was just perusing a Wikipedia entry on the "off side rule" at http://en.wikipedia.org/wiki/Off-side_rule . It says that the colon in Python is purely for readability, and cites our FAQ entry http://www.python.org/doc/faq/general.html#why-are-colons-required-for-the-if-while-def-class-statements
6
2217
by: Grant Robertson | last post by:
I am relatively new to XML, though I have taught myself a lot in the past month. I keep seeing namespace names with multiple colons in them such as: "urn:oasis:names:tc:entity:xmlns:xml:catalog" but I can't find any information on exactly what that means. Is that a namespace defined within a namespace defined within a namespace? Are they...
26
3018
by: machineghost | last post by:
First off, let me just say that as someone with no DBA training whatsoever, any help I can get with this issue will be very, very much appreciated. My company recently migrated our database from DB2 v7 to DB2 v9. We hired a consultant to help us, and things went pretty smoothly ... up until a few weeks after, when a co-worker tried to...
19
2087
by: Eric S. Johansson | last post by:
Almar Klein wrote: there's nothing like self interest to drive one's initiative. :-) 14 years with speech recognition and counting. I'm so looking to my 15th anniversary of being injured next year.... another initiative is exporting the speech recognition environment to the Linux context. In a nutshell, he dictated to application on...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.