473,803 Members | 3,518 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:
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.


What is a 'non-hierarchical treestructure' ? A list ?-)

But it would work for any package that
manipulates,
creates hierarchical data.
FWIW, filesystems are trees, most RDBMS use BTrees, and almost any OO
program is a graph of objects - trees being a subset of graphs..

Strange enough, working with trees is nothing new, and it seems that
almost anyone managed to get by without cryptic 'operators' stuff.
I used HTML as example since it is a good
example and
most people would understand the intention.
Sorry for being dumb.
But could you elaborate on your comment that it is unusable.
Ask all the coders that switched from Perl to Python why they did so...
Do you
think all template systems are unusable


Nope - I use template systems everyday.

Please don't take it wrong: there's surely something to do to ease
declarative XML-like (including JSON, Yaml etc...) datastructure
construction. But I think the pythonic way to go would rely on
metaprogramming - not on ugly perlish syntax.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 18 '06 #21
glomde wrote:
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


Nor is Cheetah. Nor is Django's templating system. Nor are some other
Python templating systems.
it can be used for all hieracical data.
Example:

myList = []
*+* myList:
*+* []:
for i in range(10):
*+* i
*+* {}:
for i in range(10):
*=* i = i
Sweet Lord, have mercy !
Which should create myList = [[0..9], {0:0, ... 9:9}]
myList = [
range(10),
dict((i, i) for i in range(10))
]

Let's talk about readability....
I do think I solve something and make it more
readable.


Lol. Any Perl coder around ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 18 '06 #22
Am Donnerstag 18 Mai 2006 15:17 schrieb glomde:
> 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.


Sure, a list comprehension solves something general. It's syntactic sugar for
a recurring pattern in a for loop.

What you are trying to achieve is to make syntactic sugar for making namespace
definitions look nicer. But: the way you are trying to do so isn't pythonic,
because there isn't one obvious way how your proposal works; you're not even
specifying a proper semantic interpretation of your syntax (and use "magic"
markers, which is even more a NoNo).

For a better thought out proposal (IMHO) for stacking and defining namespaces
(based on the current metaclass behaviour), look for the PEP on the "make"
keyword (which was sent to Py-Dev some weeks ago).
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.


Sure it does.

In case you implement a templating language that simply contains the "full"
power of the Python language, you create a (somewhat) cripled PHP, basically.
The View part of MVC shouldn't do branching (well, in the strict sense of
MVC), shouldn't do looping, shouldn't do modification of the data, all that
stuff is up to the controller.

If you empower the template writer with the full power of the Python
programming language by integrating your proposal with Python itself, you're
bound to water down the split between the three, and to create a maintenance
nightmare (as I said, PHP comes to mind, where you basically can't split
controller from model properly).

For a proper (and pretty strict) MVC templating language, have a look at
Nevow.

--- Heiko.
May 18 '06 #23
I'm answering two of you posts here...
Sweet Lord, have mercy !
> Which should create myList = [[0..9], {0:0, ... 9:9}]
myList = [
range(10),
dict((i, i) for i in range(10))
]

Let's talk about readability....
My code was just to show that the proposal is not only for HTML
generation but could be used whenever you want to create COMPLEX
hierarcical datastructures.
In the above example I would of course use what you wrote.

Do I need to show you a example where you cant use the style you
showed?
Strange enough, working with trees is nothing new, and it seems that
almost anyone managed to get by without cryptic 'operators' stuff. Strange enough once almost anyone managed to get by without Python...
:-)
I used HTML as example since it is a good
example and
most people would understand the intention.


Sorry for being dumb.

It not your fault :-)
But could you elaborate on your comment that it is unusable.


Ask all the coders that switched from Perl to Python why they did so...


You seem to really have a thing for Perl...
From what you written I assume you mean that it is no good because you

find the syntax cryptic. For me cryptic is for example how you in Perl
create list of lists, ie
it takes a while to understand and when you havent done in a while
youyou have to relearn it. Python has a few of those aswell... but you
really
only need them when doing something cryptic...

IMO what I propose isnt cryptic, because of
* I think after it has been explained it is no problem to understand
how it works.
* It doesnt have strange sideeffects
* Doesnt break any old code.
* You dont have to relearn if you havent done it a while.

Also if you never would have seen such code before, I think you would
understand
what is meant and even be able to modify it.

May 18 '06 #24
> What you are trying to achieve is to make syntactic sugar for making namespace
definitions look nicer. But: the way you are trying to do so isn't pythonic,
because there isn't one obvious way how your proposal works; you're not even
specifying a proper semantic interpretation of your syntax (and use "magic"
markers, which is even more a NoNo). I used 'magic' markers, since I am lousy at coming up with new
keywords....

For a better thought out proposal (IMHO) for stacking and defining namespaces
(based on the current metaclass behaviour), look for the PEP on the "make"
keyword (which was sent to Py-Dev some weeks ago).


It might be that my proposal the same as the 'make' pep but not so
general...
But I'll try to formalize it a little bit more.

I am proposing two new keywords 'node' and 'attr'. Which have the
syntax:

node [callable|name][:]

This will append itself to the current node if it is a callable. If it
is a name it
will become the new current node. The current node has the scope of the
block
or until a new node is created.

attr name = expression
This will set the attribute name to expression on the current node.

I dont think it is the same as the 'make' but I wouldnt rule it
out.....

May 18 '06 #25
glomde wrote:
I'm answering two of you posts here...

Sweet Lord, have mercy !
> Which should create myList = [[0..9], {0:0, ... 9:9}]
myList = [
range(10),
dict((i, i) for i in range(10))
]


Let's talk about readability....

My code was just to show that the proposal is not only for HTML
generation but could be used whenever you want to create COMPLEX
hierarcical datastructures.


It's enough to see why no-one in it's own mind would ever want to use
such a syntax.
Do I need to show you a example where you cant use the style you
showed?
Please, don't. Let's protect innocent eyes.
Strange enough, working with trees is nothing new, and it seems that
almost anyone managed to get by without cryptic 'operators' stuff.
Strange enough once almost anyone managed to get by without Python...
:-)

I used HTML as example since it is a good
example and
most people would understand the intention.


Sorry for being dumb.


It not your fault :-)

But could you elaborate on your comment that it is unusable.


Ask all the coders that switched from Perl to Python why they did so...

You seem to really have a thing for Perl...


Yes : readability. Being dumb, I need a readable, cryptic-operator free
language.
From what you written I assume you mean that it is no good because you

find the syntax cryptic.


cryptic *and* utterly ugly FWIW.

Also, you failed to address the fact that there may be much better ways
to do so, even probably without syntax change.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 18 '06 #26
glomde wrote:
What you are trying to achieve is to make syntactic sugar for making namespace
definitions look nicer. But: the way you are trying to do so isn't pythonic,
because there isn't one obvious way how your proposal works; you're not even
specifying a proper semantic interpretation of your syntax (and use "magic"
markers, which is even more a NoNo).
I used 'magic' markers, since I am lousy at coming up with new
keywords....


This is an understatement.

(snip) But I'll try to formalize it a little bit more.

I am proposing two new keywords 'node' and 'attr'. Which have the
syntax:

node [callable|name][:]

This will append itself to the current node if it is a callable. If it
is a name it
will become the new current node. The current node has the scope of the
block
or until a new node is created.

attr name = expression
This will set the attribute name to expression on the current node.
We already have this (quite close to) this syntax:

class <name>(super) :
<name>=<attr_va lue> *
[class *]

There's also the Zope3 'implements' trick, that allow to modify the
class namespace without assignement:

class <name>:
<callable>(*arg s, **kw)*

So what you want is very certainly doable without any syntax change.

I dont think it is the same as the 'make' but I wouldnt rule it
out.....


It has already been ruled out IIRC.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
May 18 '06 #27
> We already have this (quite close to) this syntax:

class <name>(super) :
<name>=<attr_va lue> *
[class *]

There's also the Zope3 'implements' trick, that allow to modify the
class namespace without assignement:

class <name>:
<callable>(*arg s, **kw)*

So what you want is very certainly doable without any syntax change.


It's not obvious me how you would do it it with the above syntax.
Could you give me an example?
For example how would you do:

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

May 18 '06 #28
glomde a écrit :
We already have this (quite close to) this syntax:

class <name>(super) :
<name>=<attr_va lue> *
[class *]

There's also the Zope3 'implements' trick, that allow to modify the
class namespace without assignement:

class <name>:
<callable>(*arg s, **kw)*

So what you want is very certainly doable without any syntax change.

It's not obvious me how you would do it it with the above syntax.
Could you give me an example?
For example how would you do:

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


Your snippet doesn't follow the syntax you described.
But FWIW:

class Root(Node):
class Head(Node):
class Title(Node):
content="page title"
for section in sections:
class Section(Node):
content=section

Ok. Get it. Doh :(

Can't work, because we can't neither reuse the same name nor dynamically
create it (or if it's possible, I fail to see how without really dirty
code).

Point taken.

We'd need the make: statement, but the BDFL has pronounced against.

I'm still -2 against your proposition, but it could make a good use case
for the make statement. I gave an eye at the new 'with' statement, but
I'm not sure it could be used to solve this.
May 18 '06 #29
Am Freitag 19 Mai 2006 02:08 schrieb Bruno Desthuilliers:
We'd need the make: statement, but the BDFL has pronounced against.

I'm still -2 against your proposition, but it could make a good use case
for the make statement. I gave an eye at the new 'with' statement, but
I'm not sure it could be used to solve this.


Couldn't. "with" is a blatant misnomer for that it's functionality is
(basically a "protected" generator), at least if you know what with does in
VB (god, am I really comparing VB with Python? And I've never even programmed
in the former...)

--- Heiko.
May 18 '06 #30

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...
0
9119
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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
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.