473,698 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alternate indent proposal for python 3000

I was considering putting together a proposal for an alternate block
syntax for python, and I figured I'd post it here and see what the
general reactions are. I did some searching, and while I found a lot
of tab vs space debates, I didn't see anything like what I'm thinking
of, so forgive me if this is a very dead horse.

Generally speaking, I like the current block scheme just fine. I use
python on a daily basis for system administration and text parsing
tasks, and it works great for me.

From time to time, though, I find myself needing a language for server-
side includes in web pages. Because of the need to indent (and
terminate indents), python seems an awkward choice for this, and it's
easy for me to see why php and perl are more popular choices for this
kind of task. Perhaps this is just my perception though.

I feel that including some optional means to block code would be a big
step in getting wider adoption of the language in web development and
in general. I do understand though, that the current strict indenting
is part of the core of the language, so... thoughts?
Jun 27 '08
20 1398
On Apr 20, 1:29 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Sun, 20 Apr 2008 13:42:05 -0300, Matthew Woodcraft <matth...@chiar k.greenend.org. ukescribió:
An alternative scheme for describing the block structure could be
useful in other cases, though. For example, if you wanted to support
putting snippets of Python in configuration files, or spreadsheet
cells.
[...] If someone wrote a library for this and it proved popular, I expect it
would be considered for the standard library.

There is "pindent.py " in the Tools/scripts directory:

# ... When called as "pindent -r" it assumes its input is a
# Python program with block-closing comments but with its indentation
# messed up, and outputs a properly indented version.

# A "block-closing comment" is a comment of the form '# end <keyword>'
# where <keywordis the keyword that opened the block ...

def foobar(a, b):
if a == b:
a = a+1
elif a < b:
b = b-1
if b a: a = a-1
# end if
else:
print 'oops!'
# end if
# end def foobar

--
Gabriel Genellina
That's actually not a lot different than what you have to do now in a
web page.. It still seems overcomplicated though. I'm not sure why
this is worse:

def foobar(a, b):
if a == b:
a = a+1;
elif a < b:
b = b-1;
if b a:
a = a-1;
else:
print 'oops!';;

It's just ultimately whitespace insensitive. Whether that's a good or
bad design is a debate that can be argued either way, but other
languages do it, and it's handy sometimes. I agree that it makes it
much easier to produce illegible code. Developing for a browser is
arguably annoying and hackish enough, without having to stick in
comments and such to enforce indenting.

Jun 27 '08 #11
On 20 avr, 17:35, Eric Wertman <ewert...@gmail .comwrote:
I was considering putting together a proposal for an alternate block
syntax for python, and I figured I'd post it here and see what the
general reactions are. I did some searching, and while I found a lot
of tab vs space debates, I didn't see anything like what I'm thinking
of, so forgive me if this is a very dead horse.

Generally speaking, I like the current block scheme just fine. I use
python on a daily basis for system administration and text parsing
tasks, and it works great for me.

From time to time, though, I find myself needing a language for server-
side includes in web pages. Because of the need to indent (and
terminate indents), python seems an awkward choice for this, and it's
easy for me to see why php and perl are more popular choices for this
kind of task. Perhaps this is just my perception though.
The server-page scheme has long shown it's limitations and quirks -
mostly, you end up mixing application logic and presentation logic.
Even PHP programmers are slowly taking the MVC route.
I feel that including some optional means to block code would be a big
step in getting wider adoption of the language in web development and
in general. I do understand though, that the current strict indenting
is part of the core of the language, so... thoughts?
Python Server Page packages are nothing new, and didn't help making
Python more popular for web developpement. MVC frameworks like Django,
Pylons, Turbogears or web.py seems to draw way more attention, and we
start to see PHP coders switching to Django - which is the one with
the IMHO weakest templating language.

If you're looking for a templating system with Python syntax support,
you may want to take a look at Cheetah and (my favourite one) Mako.
Mako is the default template system for Pylons, and IIRC web.py
supports Cheetah (warning: never used web.py, and haven't followed
recent dev, so you'd better check by yourself).

HTH
Jun 27 '08 #12
On Apr 20, 12:34*pm, Eric Wertman <ewert...@gmail .comwrote:
Look into any of the dozen Python-based template engines that are
typically used for such tasks; they offer many more features than a
way to indent blocks.
George

I definitely will.. could you throw out some examples though?
Thanks!

Eric
Start out here: http://wiki.python.org/moin/Templating

As you can see there is no shortage of alternatives, which can be
overwhelming. Cheetah used to be the most popular and it's still
widely used, but these days Django templates, Genshi and Mako seem
more prominent for web development.

HTH,
George
Jun 27 '08 #13
On Apr 20, 11:42 am, Matthew Woodcraft
<matth...@chiar k.greenend.org. ukwrote:
Christian Heimes <li...@cheimes. dewrote:
I feel that including some optional means to block code would be a big
step in getting wider adoption of the language in web development and
in general. I do understand though, that the current strict indenting
is part of the core of the language, so... thoughts?
Why should Python repeat the mistakes other languages did with SSI or
<?php ?inline code? Python favors the MVC separation of code and layout.

An alternative scheme for describing the block structure could be
useful in other cases, though. For example, if you wanted to support
putting snippets of Python in configuration files, or spreadsheet
cells.

There's no need to support the new scheme in .py files, so it seems to
me that this doesn't have to be done in the core language. All that's
needed is a variant of 'eval' which expects the alternate scheme, and
that could be prototyped just using text manipulation and the normal
'eval'.
We wouldn't even need that. Just a new source encoding. Then we
could write:

# -*- coding: end-block -*-

def _itoa(num, base):
"""Return the string representation of a number in the given base."""
if num == 0:
return DIGITS[0]
end if
negative = num < 0
if negative:
num = -num
end if
digits = []
while num:
num, last_digit = divmod(num, base)
digits.append(D IGITS[last_digit])
end while
if negative:
digits.append('-')
end if
return ''.join(reverse d(digits))
end def
Jun 27 '08 #14
On Apr 20, 6:54*pm, Dan Bishop <danb...@yahoo. comwrote:
On Apr 20, 11:42 am, Matthew Woodcraft

<matth...@chiar k.greenend.org. ukwrote:
Christian Heimes *<li...@cheimes .dewrote:
>I feel that including some optional means to block code would be a big
>step in getting wider adoption of the language in web development and
>in general. *I do understand though, that the current strict indenting
>is part of the core of the language, so... thoughts?
Why should Python repeat the mistakes other languages did with SSI or
<?php ?inline code? Python favors the MVC separation of code and layout.
An alternative scheme for describing the block structure could be
useful in other cases, though. For example, if you wanted to support
putting snippets of Python in configuration files, or spreadsheet
cells.
There's no need to support the new scheme in .py files, so it seems to
me that this doesn't have to be done in the core language. All that's
needed is a variant of 'eval' which expects the alternate scheme, and
that could be prototyped just using text manipulation and the normal
'eval'.

We wouldn't even need that. *Just a new source encoding. *Then we
could write:

# -*- coding: end-block -*-

def _itoa(num, base):
"""Return the string representation of a number in the given base."""
if num == 0:
return DIGITS[0]
end if
negative = num < 0
if negative:
num = -num
end if
digits = []
while num:
num, last_digit = divmod(num, base)
digits.append(D IGITS[last_digit])
end while
if negative:
digits.append('-')
end if
return ''.join(reverse d(digits))
end def
A great example of why something like this would never fly in standard
Python.
Jun 27 '08 #15

"Matthew Woodcraft" <ma******@chiar k.greenend.org. ukwrote in message
news:hL*******@ news.chiark.gre enend.org.uk...
| Terry Reedy <tj*****@udel.e duwrote:
|
| But you do not really need a variant. Just define a preprocessor
| function 'blockify' which converts code in an alternate syntax to
| regular indented block syntax. Then
| >
| exec(blockify(a lt_code_string) )
|
| You can do it like that, but if it were to become part of the standard
| distribution it would be nice to avoid having to tokenise the code
| twice.

For the motivating example I was responding to -- short snippets of code in
html/xml/etc, that is completely a non-issue.

Any such scheme is very unlikely to become part of the stdlib and if it
were, it would have to first be tested and beat out competitors. A
preprocessor written in Python is the obvious way to test and gain
acceptance.

| (You could define the new block scheme in such a way that
| 'blockify' doesn't need to tokenise,

Off the top of my head: copy C and use {} to demarcate blocks and ';' to
end statements, so that '\n' is not needed and is just whitespace when
present. So, repeatedly scan for the next one of '{};'.

| but I think it would end up a bit ugly.)

For beautiful, stick with standard Python.

Terry Jan Reedy


Jun 27 '08 #16

"Dan Bishop" <da*****@yahoo. comwrote in message
news:48******** *************** ***********@m1g 2000pre.googleg roups.com...
| We wouldn't even need that. Just a new source encoding. Then we
| could write:
|
| # -*- coding: end-block -*-

Ummm.. source encoding refers to how unicode chars/codepoints are
represented as bytes. This syntax is copied from emacs and uses standard
terms. What would you expect an encoding aware editor to do with something
like the above?

Jun 27 '08 #17
On 21 Apr, 00:54, Dan Bishop <danb...@yahoo. comwrote:
>
We wouldn't even need that. Just a new source encoding. Then we
could write:

# -*- coding: end-block -*-
[...]

Someone at EuroPython 2007 did a lightning talk showing working code
which provided C-style block structuring using this mechanism. My
brother then jokingly suggested to Martijn Faassen that if someone
plugged the 2to3 converter in as a source file encoding handler, his
worries about migrating to Python 3 would disappear. I'm waiting to
see if anyone actually bothered to make that happen, albeit for
amusement purposes only.

Paul

P.S. EuroPython 2008 is now accepting talks, especially ones on the
language, Python 3000, and other implementations . See http://www.europython.org/
for details!
Jun 27 '08 #18
Terry Reedy <tj*****@udel.e duwrote:
Off the top of my head: copy C and use {} to demarcate blocks and ';' to
end statements, so that '\n' is not needed and is just whitespace when
present. So, repeatedly scan for the next one of '{};'.
That would break if those characters appear in string literals or
comments. That's why it's nicer if you can do the transformation after
tokenising.

(Also, '{' and '}' have rather useful meanings in Python already.)

-M-
Jun 27 '08 #19
On Apr 21, 4:01 am, Paul Boddie <p...@boddie.or g.ukwrote:
On 21 Apr, 00:54, Dan Bishop <danb...@yahoo. comwrote:
We wouldn't even need that. Just a new source encoding. Then we
could write:
# -*- coding: end-block -*-

[...]

Someone at EuroPython 2007 did a lightning talk showing working code
which provided C-style block structuring using this mechanism.
Yes, I saw an example of that: It's what inspired my post.
Jun 27 '08 #20

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

Similar topics

2
1704
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason to accept it would be to pacify the supporters of the proposal, and that just isn't a good enough reason in language design. However, it got pretty darn close! I'm impressed with how the community managed to pull together and face the enormous...
12
1471
by: John Salerno | last post by:
Is 'Python 3000' just a code name for version 3.0, or will it really be called that when it's released?
1
1228
by: gangesmaster | last post by:
typing "help(type)" gives the following documentation: >>> help(type) Help on class type in module __builtin__: class type(object) | type(object) -> the object's type | type(name, bases, dict) -> a new type "type" behaves both as a function, that reports the type of an object, and as a factory type for creating types, as used mainly with metaclasses.
14
1634
by: beliavsky | last post by:
At http://www-03.ibm.com/developerworks/blogs/page/davidmertz David Mertz writes "Presumably with 2.7 (and later 2.x versions), there will be a means of warning developers of constructs that are likely to cause porting issues . In the simplest case, this will include deprecated functions and syntax constructs. But presumably the warnings may cover "potential problems" like the above example." The current beta version of Python is 2.5...
1
2082
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules written using other languages. Extensions of Python could be done via special interpreter extensions. From Python sources, the special modules would look like other modules, with the Python API (the key feature from
34
3674
by: Anthony Irwin | last post by:
Hi All, I am currently trying to decide between using python or java and have a few quick questions about python that you may be able to help with. #1 Does python have something like javas .jar packages. A jar file contains all the program files and you can execute the program with java -jar program.jar I am sort of hoping python has something like this because I feel it
0
1150
by: Guido van Rossum | last post by:
python-list@python.org] The first Python 3000 release is out -- Python 3.0a1. Be the first one on your block to download it! http://python.org/download/releases/3.0/ Excerpts: Python 3000 (a.k.a. "Py3k", and released as Python 3.0) is a new
25
3028
by: Lennart Benschop | last post by:
Python has had the Decimal data type for some time now. The Decimal data type is ideal for financial calculations. Using this data type would be more intuitive to computer novices than float as its rounding behaviour matches more closely what humans expect. More to the point: 0.1 and 0.01 are exact in Decimal and not exact in float. Unfortunately it is not very easy to access the Decimal data type. To obtain the decimal number 12.34 one...
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5861
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
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
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.