473,651 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What to use for adding syntax for hierarcical trees, metaclasses, tokenize.py or PLY?

Hi 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.

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.

bodyNode = body()

*!* bodyNode:
*=* color = blue
*=* bg = white
for i in headings:
*!* H1(heading[i]):
This would translate to something like this in python:
bodyNode = body()
if True:
bodyNode.attr['color'] = blue
bodyNode.attr['bg'] = white
for i in headings:
if True:
bodyNode.append (H1(heading[i]))

I think that with the added syntax you get better overview on how your
tree looks like.

But the thing is how to implement my added syntax. I dont want to mess
with Python source code to add the syntax. So I searched a bit on the
net and think there might be three alternatves.

1. MetaClasses. I tried to understand them but my head almost exploded
:-). But my conclusion was that it is not possible with metaclasses.
Since this is a real syntax change.

2. Tokenize.py. I modify tokenize.py to recognize my new operators
'#!#' and '#=#' and then I write a parser that exports the code. This
option I understand somewhat how to implement. But I would reduce my
own code for the parser, so is there any parser that can handle
tokenize.py input? So that I could somehow write rules for my code.

3. Use PLY or any other python parser. Write rules for my language. But
would I need to write rules that can handle the whole python
languager? Then this seems to be overkill. I would just
like to recognize my added syntax and convert that. So I would really
like to have a rule file that handles python and then add rules for my
syntax. Are there any Python rule file for the python language and a
python parser for it? Then I could just add my syntax to the rule file
and be able to create the output.

I would be very thankfull for any hints or thougts on how to do it.

Best regards,

G

May 10 '06 #1
2 1359
glomde wrote:
<questions on how to extend Python syntax>

You should look at the compiler package and at the new AST in Python
2.5.
For easy things tokenize could be enough, though. Notice that in Python
2.5 tokenize
grew a new 'untokenize' function which is pretty useful. See
http://docs.python.org/dev/lib/module-tokenize.html for examples.
Michele Simionato

May 10 '06 #2
"glomde" <tb****@yahoo.c om> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
Hi 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.

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.


It's not quite so syntactically sugary, but it looks like much of the
functionality you are looking for is in elementTree, which will be
incorporated into 2.5 (or you can download and install yourself from
http://www.effbot.org/downloads/#elementtree).

-- Paul
May 10 '06 #3

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

Similar topics

54
6542
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
699
33862
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
7
3554
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that you do not actually need to fork Python in order to implement prototypes. It seems to me that Python metaclasses + descriptors are more than powerful enough to implementing prototypes in pure Python. I wrote a module that implements part of what...
24
2397
by: Xah Lee | last post by:
in computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that } in advanced languages such as LISP family, it is not uncommon to define functions inside a function. For example:
16
2290
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the embedded comments I used the tokenize module. To my surprise the analysed output is different from the input (the last tuple element should exactly replicate the input line) The error comes out in correspondance of a triple string.
4
3120
by: Kelvin | last post by:
hi: in C, we can use strtok() to tokenize a char* but i can't find any similar member function of string that can tokenize a string so how so i tokenize a string in C++? do it the C way? thanks -- { Kelvin@!!! }
53
4063
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Can someone give me examples of expressions that "barely"...
1
2272
by: Tim | last post by:
I ran into a problem with a script i was playing with to check code indents and need some direction. It seems to depend on if tabsize is set to 4 in editor and spaces and tabs indents are mixed on consecutive lines. Works fine when editors tabsize was 8 regardless if indents are mixed. Below are how the 3 test files are laid out, the sample code and output I get. Any help on how to detect this correctly would be appreciated.
1
2504
by: Alan Isaac | last post by:
Forman's book is out of print. Is there a good substitute? Thanks, Alan Isaac
0
8361
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
8278
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
8807
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...
1
8466
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
4144
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
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
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.