473,574 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allowing braces around suites

often when re-factoring code, I need to change the indent level of
some chunk of code. due to the lack of an end marker, my Emacs has to
use heuristics when re-indenting, and this will occasionally lead to
mistakes. of course this is really _my_ fault, not Emacs', but it's
annoying all the same.

a colleague uses #fi, #yrt etc. to mark the end of blocks, but I don't
find this satisfactory since neither Python nor Emacs has any
knowledge of what the magic comment means.

my suggestion is to allow braces to mark the suites. Python is still
strictly enforcing indentation, in fact, in some ways it's stricter
then before. code which uses braces and is wrongly re-indented will
always cause a syntax error during compilation.

code could look like this:

def appraise_insura nce(rental_cars ):
{
assert isinstance(rent al_cars, (list, tuple))

for car in rental_cars:
{
if car.make == "Lamborghin i":
{
car.insurance = 1000.00
}
elif car.make == "Lada":
{
car.insurance = 50.00
}
else:
{
car.insurance = 100.00
}
}
logger.debug("I nsurance values updated")
}

now, I don't suggest to write code like that, that has far too much
air between actual codelines. I'd only add the braces where there is
a risk re-indentation changing the meaning of the program, like this:

def appraise_insura nce(rental_cars ):
assert isinstance(rent al_cars, (list, tuple))

for car in rental_cars:
{
if car.make == "Lamborghin i":
car.insurance = 1000.00
elif car.make == "Lada":
car.insurance = 50.00
else:
car.insurance = 100.00
}
logger.debug("I nsurance values updated")

the single pair of braces is enough. there is now no risk of Emacs
including the debug line into the else branch unnoticed.

the patch to implement this is very simple and restricted to the
tokenizer. as it stands, it requires the braces to be placed as in
the above examples, you _can't_ write

if True: {
code
} else {
code
}

a single braces style improves readability and style consistency among
software projects. my patch does allow two styles, though: to reduce
line count you can put the else: on the same line as the closing
brace.

if True:
{
code
} else:
{
code
}

in both styles, the braces line up and IMHO this makes it easier to
identify where a medium sized block begins and ends.

there can be no code after an opening brace, but it's a good place to
put a comment, so in practice the number of added lines may not be so
large.
a small caveat: it breaks code which looks like this:

def test_varargs1(s elf):
"""some text"""
{}.has_key(0)

this function is taken from Lib/test/test_call.py, but the doc string
was added to make my patched Python reject it with a syntax error.
the problem is that a brace at the same indent level as the line above
will always mark the beginning of a new suite. I don't think such
code is widespread, but would be interested in hearing contrary
experiences. one fix for this issue is to have the parser tell the
tokenizer whether "a suite may follow" when it asks for the next
token, but I haven't persued writing such a patch.

a patch relative to Python 2.4a2 is available at

http://heim.ifi.uio.no/~kjetilho/hac...on-2.4a2.patch

what do you think? should I write a PEP?
--
Kjetil T.
Jul 18 '05 #1
97 4380
Kjetil Torgrim Homme wrote:
...
what do you think? should I write a PEP?


It's more fun to ask python what it thinks:

michaels@pc474: ~/Documents> python
Python 2.3.3 (#1, Apr 6 2004, 01:47:39)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
from __future__ import braces File "<stdin>", line 1
SyntaxError: not a chance

^^^^^^^^^^^^

I suspect you might have a hard time getting anywhere with this ;)

Regards,
Michael.
--
Mi************@ rd.bbc.co.uk
British Broadcasting Corporation, Research and Development
Kingswood Warren, Surrey KT20 6NP

This message (and any attachments) may contain personal views
which are not the views of the BBC unless specifically stated.
Jul 18 '05 #2
[Michael Sparks]:

Kjetil Torgrim Homme wrote:
> what do you think? should I write a PEP?


I suspect you might have a hard time getting anywhere with this ;)


well, in the previous discussions I've read, the proponents of braces
want them as an alternative to strict indentation. that's not the
case here, so the code is still kept pythonic, IMO.

--
Kjetil T.
Jul 18 '05 #3
On Fri, 27 Aug 2004 12:40:02 +0100, Michael Sparks
<mi******@rd.bb c.co.uk> wrote:
Kjetil Torgrim Homme wrote:
...
what do you think? should I write a PEP?


The whole topic of explicit[*] end-of-block markers was brought up and
discussed at great length in May 1994:

http://groups.google.com/groups?hl=e...8%40vicorp.com

(I was looking at the old archives of c.l.py to try and find info on
the origins of triple-quoted strings and docstrings, both of which
were born around April 1994)

Regardless of history, it looks to me like your problem could be
better solved by modifying your emacs mode to consider whitespace more
correctly -- AFAICT there is no reason why it should ever pick up the
logging line in your example and indent it to the same level as the
else: block.
Jul 18 '05 #4
[Andrew Durdin]:

The whole topic of explicit[*] end-of-block markers was brought up
and discussed at great length in May 1994:

http://groups.google.com/groups?thre...8%40vicorp.com
thank you for the reference! I'm happy to see that Guido wasn't
strictly opposed to such a feature 10 years ago.
Regardless of history, it looks to me like your problem could be
better solved by modifying your emacs mode to consider whitespace
more correctly -- AFAICT there is no reason why it should ever
pick up the logging line in your example and indent it to the same
level as the else: block.


how can Emacs tell? if I press TAB and thus ask Emacs to re-indent
the line, that's what it will do. arguably, the correct fix is to
disable the "clever" re-indentation code of Emacs since it's too easy
to fool it, but this makes coding more awkward in other cases. it
also doesn't solve cut-n-paste errors.
--
Kjetil T.
Jul 18 '05 #5
Kjetil Torgrim Homme wrote:
[...]
Regardless of history, it looks to me like your problem could be
better solved by modifying your emacs mode to consider whitespace
more correctly -- AFAICT there is no reason why it should ever
pick up the logging line in your example and indent it to the same
level as the else: block.

how can Emacs tell? if I press TAB and thus ask Emacs to re-indent
the line, that's what it will do. arguably, the correct fix is to
disable the "clever" re-indentation code of Emacs since it's too easy
to fool it, but this makes coding more awkward in other cases. it
also doesn't solve cut-n-paste errors.


You can use C-c < or C-c > in Emacs to (de-)indent regions.

Wolfram
Jul 18 '05 #6
On Fri, 27 Aug 2004 14:55:26 +0200, Kjetil Torgrim Homme
thank you for the reference! I'm happy to see that Guido wasn't
strictly opposed to such a feature 10 years ago.


Regardless of what he thought back then, I think I can say with
absolutely no fear of contradiction that there is no chance in hell of
braces appearing in Python... ever.

If your tool gets something wrong, fix the tool, don't try and change
the language, just for the convenience of a broken tool.
Jul 18 '05 #7
>>>>> "Kjetil" == Kjetil Torgrim Homme <kj******@yksi. ifi.uio.no> writes:

Kjetil> how can Emacs tell? if I press TAB and thus ask Emacs to
Kjetil> re-indent the line, that's what it will do.

If the aim is simply to do a local change that is sufficient to deal
with the problem just in your computer, then the simplest answer might
be to use "pass": if your code contains

if t1 == t2:
t1 = t3
else:
t2 = t3
pass
t = t2

and you tab at the last line, Emacs won't try to indent the t=t2 line
further to after the pass line where it is impossible to execute.

If you want something that is done globally and everybody will use
it... not a chance.

Kjetil> arguably, the correct fix is to disable the "clever"
Kjetil> re-indentation code of Emacs since it's too easy to fool
Kjetil> it, but this makes coding more awkward in other cases.

The indent-region command of Emacs (M-C-\) is really disabled in
Python mode, I think it is done by nullifying the
indent-region-function. Anyway, reindent makes sense only when the
indentation does not constitute the meaning of a program and thus the
editor can freely change it. If indentation does mean something, it
doesn't make sense at all.

Kjetil> it also doesn't solve cut-n-paste errors.

Use C-c > and C-c < might work for you.

Regards,
Isaac.
Jul 18 '05 #8
Wolfram Kraus wrote:
Kjetil Torgrim Homme wrote:
[...]
how can Emacs tell? if I press TAB and thus ask Emacs to re-indent
the line, that's what it will do. arguably, the correct fix is to
disable the "clever" re-indentation code of Emacs since it's too easy
to fool it, but this makes coding more awkward in other cases. it
also doesn't solve cut-n-paste errors.


You can use C-c < or C-c > in Emacs to (de-)indent regions.


Or C-M-\ to re-indent the region "correctly" with respect to its
context. This works far more reliably the using TAB to re-indent single
lines.

--
--------------------------------------------------------------------
Aaron Bingham
Application Developer
Cenix BioScience GmbH
--------------------------------------------------------------------

Jul 18 '05 #9
[Isaac To]:

If the aim is simply to do a local change that is sufficient to
deal with the problem just in your computer, then the simplest
answer might be to use "pass": if your code contains

if t1 == t2:
t1 = t3
else:
t2 = t3
pass
t = t2

and you tab at the last line, Emacs won't try to indent the t=t2
line further to after the pass line where it is impossible to
execute.
interesting idea, but Python won't discover inconsistencies . if only
an Emacs solution is sought, a "# end" convention is preferable since
it's more explicit. an explicit convention that Python recognises
would be even better.
If you want something that is done globally and everybody will use
it... not a chance.


I don't expect everybody to use it. miscellaneous projects where the
developers are comfortable with it might want to add it to their
coding standards.
--
Kjetil T.
Jul 18 '05 #10

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

Similar topics

16
3326
by: Jason | last post by:
Programming recently I was the habit of leaving out the braces on if statements if the branch only applied to a single line, assuming it would all work and be equivalent to "if(condition) { statement; }" I implemented an algorithm and it did not work for a while and when i tracked the problem down it was as if a certain action was not being...
0
7741
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
8085
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
8259
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...
0
8126
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
6491
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
5328
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3763
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
2257
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
0
1090
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.