473,803 Members | 3,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proposal for new operators to python that add syntactic sugar for hierarcical data.

i I would like to extend python so that you could create hiercical
tree structures (XML, HTML etc) easier and that resulting code would be
more readable than how you write today with packages like elementtree
and xist.
I dont want to replace the packages but the packages could be used with
the
new operators and the resulting IMHO is much more readable.

The syntax i would like is something like the below:

# Example creating html tree

'*!*' is an operator that creates an new node,
'*=*' is an operator that sets an attribute.

So if you take an example to build a smalle web page
and compare with how it looks with in element tree now
and how it would look like when the abover operators would exist.

With element tree package.

# build a tree structure
root = ET.Element("htm l")
head = ET.SubElement(r oot, "head")
title = ET.SubElement(h ead, "title")
title.text = "Page Title"
body = ET.SubElement(r oot, "body")
body.set("bgcol or", "#ffffff")
body.text = "Hello, World!"

With syntactical sugar:

# build a tree structure
root = ET.Element("htm l")
*!*root:
*!*head("head") :
*!*title("title ):
*=*text = "Page Title"
*!*body("body") :
*=*bgcolor = "#ffffff"
*=*text = "Hello, World!"

I think that with the added syntax you get better view of the html
page.
Repeating things dissapears and you get indentation that corresponds to
the tree.
I think it is very pythonic IMHO.

It could be done quite generic. If the variable, object after '*!*'
must support append
method and if you use '*=*' it must support __setitem__

Any comments?

May 17 '06
34 1889
glomde wrote:
i I would like to extend python so that you could create hiercical
tree structures (XML, HTML etc) easier and that resulting code would be
more readable than how you write today with packages like elementtree
and xist.
I dont want to replace the packages but the packages could be used with
the
new operators and the resulting IMHO is much more readable.

The syntax i would like is something like the below:

# Example creating html tree
'*!*' is an operator that creates an new node,
'*=*' is an operator that sets an attribute.
(snip)
With syntactical sugar:

# build a tree structure
root = ET.Element("htm l")
*!*root:
*!*head("head") :
*!*title("title ):
*=*text = "Page Title"
*!*body("body") :
*=*bgcolor = "#ffffff"
*=*text = "Hello, World!"

What about using <XXX>data</XXX> for nodes and '=' for attributes ?
Would look like:

<html>
<head>
<title>Page Title</title>
</head>
<body bgcolor='#fffff f'>
Hello World
</body>
</html>

I think that with the added syntax you get better view of the html
page.


indeed !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 18 '06 #11
[glomde]
i I would like to extend python so that you could create hiercical
tree structures (XML, HTML etc) easier and that resulting code would be
more readable than how you write today with packages like elementtree
and xist. Any comments?


Yes: it's ugly and unnecessary.

Why would you want to change the language syntax just to make it easier
to express markup directly in program code? Javascript just made that
mistake with E4X: IMHO, it makes for some of the ugliest code. E4X
reminds me of that Microsoft b*stardisation, XML Data Islands.

http://www.w3schools.com/e4x/e4x_howto.asp
http://en.wikipedia.org/wiki/E4X

For a nice pythonic solution to representing markup directly in python
code, you should check out stan.

http://divmod.org/users/exarkun/nevo...an-module.html

Here's a nice stan example, taken from Kieran Holland's tutorial

http://www.kieranholland.com/code/do...on/nevow-stan/

aDocument = tags.html[
tags.head[
tags.title["Hello, world!"]
],
tags.body[
tags.h1[ "This is a complete XHTML document modeled in Stan." ],
tags.p[ "This text is inside a paragraph tag." ],
tags.div(style= "color: blue; width: 200px; background-color:
yellow;")
[
"And this is a coloured div."
]
]
]

That looks nice and simple, and no need to destroy the elegance of
python to do it.

regards,

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan

May 18 '06 #12
> You can put any python expression in there, and in the Node class you can do
what you want.
But the main advantage is any python code not only expressions. In
addition to that
you get nice flow style. Can set variables which you later use as want.
Seems that you are after a templating-system. Well, there are plenty of them
available, including yours. A preprocessor is nothing but a template system
- see TurboGears-utilized KID (http://kid.lesscode.org/) for example, it
generates python files. And way more readable, because you are working in
the domain of your application (HTML) instead of some crude syntax that is
neither fish or flesh.


What I propose is for all data that is hierarcical. Not only HTML. I
dont use it for creating html. Templating systems have the drawback IMO
that the more code you get the more undreadable it gets. If the code
part is small they are very nice.

The other part is that indentation, syntax highlighting and so on works
not so well when working with such code.
I would translate your example to the below. Which I think is much more
readable, since you see the python code flow much easier. I realised
that you realy should do Element("html") and not html("html") when
adding nodes in my syntax and using elementtree.

from urllib import urlopen
from elementtree.Ele mentTree import parse, tostring
feed = 'http://naeblis.cx/rtomayko/weblog/index.atom'
root = parse(urlopen(f eed)).getroot()
ns = '{http://purl.org/atom/ns#}'
title = root.findtext(n s + 'title')

root = ET.Element("htm l")
*+* root:
*+* Element("head") :
*+* Element("title" )
*+* Element("body") :
*=* bgcolor = "blue"
*=* text = "yellow"
*+* Element("table" ):
*=* cellpadding="4"
*=* cellspacing="4"
for i, entry in enumerate(root) :
*+* Element("tr"):
if entry.tag==ns + 'entry':
*+* Element("td"):
*=* bgcolor = ('white', 'yellow')[i %
2]
*+* Element("a"):
*=* href = entry.find(ns +
'link').attrib['href']
*=* text = "entry.findtext (ns +
'title')"
With the above you can use your favorite editor to do the coding and
get indentation, syntax highlightning and so on. Which I find can be
quite a burden with a templating system especially the more code that
is in there.

It is also easier to debug in comparison when using a templating system.

May 18 '06 #13
> You also repeat yourself: head("head"), title("title"), body("body")

What about this:

# build a tree structure
root = ET.Element("htm l")
!root
!head
!title
if A is True:
&text = "Page A"
else:
&text = "Page B"
!body
&bgcolor = "#ffffff"
&text = "Hello, World!"


Yes this is how it should be. But It depends on which package would be
used. If you used xist it would look like that since they have one
class for each html tag. I really propose
a generic solution that any package that support the method append and
__settiem__
could use the new syntax.

But minor isssues but it would look something like this with xist:

!root
!head():
!title():
if A is True:
&text = "Page A"
else:
&text = "Page B"
!body()
&bgcolor = "#ffffff"
&text = "Hello, World!"
mmm...yamlthon? yython...:-)

Guess what I have for suffix on the files before preprocessing?
.....pyml ofcourse.

May 18 '06 #14
> What about using <XXX>data</XXX> for nodes and '=' for attributes ?
Would look like:

<html>
<head>
<title>Page Title</title>
</head>
<body bgcolor='#fffff f'>
Hello World
</body>
</html>
I think that with the added syntax you get better view of the html
page.


indeed !-)


I dont think it is very pythonic :-). You dont use endtags and then you
dont use ':' to
mark that you nest a level. :-)
And I dont see where it is allowed to put real python code.
So could you show an example with a for loop? :-)

May 18 '06 #15
> Here's a nice stan example, taken from Kieran Holland's tutorial
http://www.kieranholland.com/code/do...on/nevow-stan/


I took a quick look and it looks nice as long as your page is static.
But I couldnt
se how you could mix in python code seamlessy when creating your tree.

But I might be wrong if you could write:

tags.body[
for i in range(10):
tags.h1[ "Heading %s." %(i) ],
tags.p[ "This text is inside a paragraph tag." ],
]

I think that this is close enough to my syntax. But I dont think you
can do it.

May 18 '06 #16
glomde wrote:
What about using <XXX>data</XXX> for nodes and '=' for attributes ?
Would look like:

<html>
<head>
<title>Page Title</title>
</head>
<body bgcolor='#fffff f'>
Hello World
</body>
</html>
I think that with the added syntax you get better view of the html
page.


indeed !-)

I dont think it is very pythonic :-).


Adding ugly and unintuitive "operators" to try to turn a general purpose
programming language into a half-backed unusable HTML templating
language is of course *much* more pythonic...

I think you would have much more success trying to get this added to
Perl !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 18 '06 #17
Am Donnerstag 18 Mai 2006 13:27 schrieb bruno at modulix:
Adding ugly and unintuitive "operators" to try to turn a general purpose
programming language into a half-backed unusable HTML templating
language is of course *much* more pythonic...


What about writing a mini-language that gets translated to Python? Think of
Cheetah, which does exactly this (albeit not being limited to templating HTML
data).

Adding these kind of operators to Python is an absolute NoNo, because it's
nothing general the OP is trying to achieve here. Creating a small wrapper
language: why not? (it's not that we have enough templating languages
already ;-))

By the way: the language you (the OP) are trying to implement here goes
strictly against the MVC model of application programming. You know that,
right?

--- Heiko.
May 18 '06 #18
> Adding ugly and unintuitive "operators" to try to turn a general purpose
programming language into a half-backed unusable HTML templating
language is of course *much* more pythonic...


IT is not only for HTML. I do think html and xml are the biggest
creators of
hierarcical treestructures. But it would work for any package that
manipulates,
creates hierarchical data. I used HTML as example since it is a good
example and
most people would understand the intention.

But could you elaborate on your comment that it is unusable. Do you
think all template systems are unusable or what is the specific reason
you think what i propose is unusable?

IMO it is a mix between yml and python. Yml has the advantage that it
can be read by many programming languages but the disadvantage is that
it is static.

May 18 '06 #19
> What about writing a mini-language that gets translated to Python? Think of
Cheetah, which does exactly this (albeit not being limited to templating HTML
data). I have implemented my proposal as preprocessor. And it works fine. But
my
proposal in not only for HTML it can be used for all hieracical data.
Example:

myList = []
*+* myList:
*+* []:
for i in range(10):
*+* i
*+* {}:
for i in range(10):
*=* i = i
Which should create myList = [[0..9], {0:0, ... 9:9}]

So it adds the power of for loops etc when creating data structures.
ANY datastructure.
nothing general the OP is trying to achieve here Define general :-). I do think I solve something and make it more
readable.
You could also argue that list comprehension doesnt solve anything
general.
By the way: the language you (the OP) are trying to implement here goes
strictly against the MVC model of application programming. You know that,
right?


???. I cant see how this breaks MVC. MVC depends on how you parition
your application
this doesnt put any constraint on how you should do your application.

May 18 '06 #20

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

Similar topics

9
2037
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more pythonic approach. One thing I've been seeing suggested a lot lately is that running jobs in separate processes, to make it easy to use the latest multiprocessor machines. Makes a lot of sense to me, those processors are going to be more and...
13
2519
by: Sam Kong | last post by:
Hi, While discussing C#'s using statement, a guy and I had an argument. In C# spec (15.13), there's an explanation like the following. using (R r1 = new R()) { r1.F(); } is precisely equivalent to
10
3315
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add signed/unsigned modifier to class declarations to next revision of C++ programming language
57
3401
by: Alan Isaac | last post by:
Is there any discussion of having real booleans in Python 3000? Say something along the line of the numpy implementation for arrays of type 'bool'? Hoping the bool type will be fixed will be fixed, Alan Isaac
11
2106
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
70
2668
by: TheFlyingDutchman | last post by:
Python user and advocate Bruce Eckel is disappointed with the additions (or lack of additions) in Python 3: http://www.artima.com/weblogs/viewpost.jsp?thread=214112
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
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
10309
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
10289
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
6840
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();...
0
5496
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
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
3
2968
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.