473,789 Members | 2,876 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 #1
34 1884
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.


Given that python doesn't have syntactic sugar for something as
prevalent as regular expressions, it's not that hard to predict the
luck of this proposal. Good luck pushing it forward.

George

May 17 '06 #2
glomde schrieb:
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?


It's ugly, and could easily achieved using the built-in tupels, lists
and dictionaries together with a simple traversing function.

Like this (untested):

class Node(object):
def __init__(self, value):
self.value = vallue
self._childs = []
def append(self, child):
self._childs.ap pend(child)
t = ('root',
('child1,
('grandchild', ()),
'child2',
()
)
)

def create_node(t):
value, childs = t
n = Node(value)
if childs:
for ch in childs:
n.append(create _node(ch))
return n
IMHO that tuple-based tree is waaay more readable than your proposal -
and no need to come up with new syntax.

Diez
May 17 '06 #3
"glomde" <tb****@yahoo.c om> writes:
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!"
We already have syntax for building hierarchical data structures:
lists and/or tuples. If you want to define Node and Attribute classes,
you can already do so without adding new syntax.
I think that with the added syntax you get better view of the html
page.
I think indenting our existing data type syntax can do the same thing:

root = Node("html", children=[
Node("head", children=[
Node("title", children=[
"Page Title",
])
]),
Node("body", children=[
Attribute("bgco lor", "white"),
"Hello, World!",
]),
])

Set up the __init__ for those classes to do whatever you would have
done with the syntax you proposed.
Repeating things dissapears and you get indentation that corresponds
to the tree.
Indeed.
I think it is very pythonic IMHO.


Adding new punctuation to deal with a particular type is extremely
un-Pythonic.

--
\ "The cost of a thing is the amount of what I call life which is |
`\ required to be exchanged for it, immediately or in the long |
_o__) run." -- Henry David Thoreau |
Ben Finney

May 17 '06 #4
glomde wrote:
i I would like to extend python so that you could create hiercical
tree structures (XML, HTML etc) easier ...
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!"


Hunt up PyYAML. It might be what you want.

--Scott David Daniels
sc***********@a cm.org
May 18 '06 #5
There are some difference which are quite essential.

First of all I dont se how you set attributes with that and
then I dont see how you can mix python code with the creation.

So how do you do this:

root = ET.Element("htm l")
*!*root:
*!*head("head") :
*!*title("title ):
for i in sections:
!*! section():
*=*Text = section[i]

May 18 '06 #6
But you cant mix python code in there when creating the nodes.
That is when it gets quite ugly an unreadable according to me.

But I also relly do think that this:

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

Especially if you start having deeper hierachies like. But the big
other plus is that you can
have any python code in beetween.

May 18 '06 #7
Actually we did start of with YAML, but realised that we need to have
the power
of a programming language aswell. But I wanted to come up with
something that looked
very clos to YAML since I find it quite readable.

I have implemented the syntax, but as a preprocessor for python and it
works quite nice.

Cheers,

T

May 18 '06 #8
glomde wrote:
There are some difference which are quite essential.

First of all I dont se how you set attributes with that and
Use dicts as childs.
then I dont see how you can mix python code with the creation.


You can put any python expression in there, and in the Node class you can do
what you 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.

Counterquestion : how do you create this:

<?xml version='1.0' encoding='utf-8'?>
<?python
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')
?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://purl.org/kid/ns#">
<head>
<title py:content="tit le" />
</head>
<body bgcolor="blue" text="yellow">
<h1 py:content="tit le" />
<table cellpadding="4" cellspacing="4" >
<tr py:for="i, entry in enumerate(root) "
py:if="entry.ta g == ns + 'entry'">
<td py:attrs="bgcol or=('white', 'yellow')[i % 2]">
<a py:attrs="href= entry.find(ns + 'link').attrib['href']"
py:content="ent ry.findtext(ns + 'title')" />
</td>
</tr>
</table>
</body>
</html>
Diez
May 18 '06 #9
glomde wrote:
i I would like to extend python so that you could create hiercical
[...]
# 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?


I personally dislike the nested-indented-brackets type of code given in
other replies, and i think your suggestion has a certain appeal - 'What
you see is what you mean' . But it's not Python.

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!"

mmm...yamlthon? yython...:-)

All the best

Gerard

May 18 '06 #10

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

Similar topics

9
2036
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
2518
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
3312
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
3397
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
2104
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
2662
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
10199
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
10139
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
9983
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
6768
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
5417
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
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
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.