473,780 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing Python parse trees

Hi.
With module parser it is possible to access Python parse trees.
But this only works for 'external' source.
I would like to known if, at least in theory, it can be possible to
access Python parse trees from 'inside' a script.
As a simple example:
def on_parsing(ast) :
...
@parsing -> on_parsing
some statement

This will call on_parsing function with the AST object generated from
the next statement:

on_parsing(pars er.suit(somesta tement))

Thanks and regards Manlio Perillo

Jul 18 '05 #1
5 1337
No. I don't think it's possible to read the parse tree used by the
interpreter, especially as it is being created. Here are a couple of
kludgy ideas that might come close, though:

1. Use introspection to have your on_parsing function locate the and
parse the code being executed. I'm not sure if you'll be able to
figure out _exactly_ where on_parsing is being called from (e.g. you
can probably only narrow it down to the statement, not the expression),
but you should be able to come close enough to be useful. The inspect
module and sys._getframe will come in handy. Note that this won't work
if you can't retrieve the source (as is the case with exec/eval)

2. Or, using the code module, start up your own interpreter inside of
the interpreter. Intercept and parse lines of source as they are fed
to this interpreter.

On a side note, check out the compiler module. You might find it to be
friendlier and more useful than parser.

Jul 18 '05 #2
On 3 Mar 2005 11:15:28 -0800, "Lonnie Princehouse"
<fi************ **@gmail.com> wrote:
No. I don't think it's possible to read the parse tree used by the
interpreter, especially as it is being created. Here are a couple of
kludgy ideas that might come close, though:
Is this a 'limitation' of the current version or it is impossible for
the architecture of CPython?
What about pypy?

On a side note, check out the compiler module. You might find it to be
friendlier and more useful than parser.


Thanks for the hint. It is what I want.
Unfortunately is seem to be not well documented.
Anyway, here is an example of what I would like to do:

#begin
def foo(**kwargs): print kwargs

foo(a = 1, b = 2, c = 3)
#end
In the current implementation kwargs is a dict, but I need to have the
keyword argument sorted.
Unfortunately subclassing fron dict and installing the class in the
__builtin__ module (with the name 'dict') does not work, CPython uses
only builtin types.

With the compiler module I can obtain the keyword arguments in the
order the were specified.
The problem is how to do this for every call to foo!


Thanks and regards Manlio Perillo
Jul 18 '05 #3
Manlio Perillo wrote:
Anyway, here is an example of what I would like to do:

#begin
def foo(**kwargs): print kwargs

foo(a = 1, b = 2, c = 3)
#end
In the current implementation kwargs is a dict, but I need to have the
keyword argument sorted.
Unfortunately subclassing fron dict and installing the class in the
__builtin__ module (with the name 'dict') does not work, CPython uses
only builtin types.

With the compiler module I can obtain the keyword arguments in the
order the were specified.
The problem is how to do this for every call to foo!


Why not just pass the kind of argument you want? What is it you really need to do?

def foo(kwds): print kwds

foo(MyDict(a = 1, b = 2, c = 3))

Kent
Jul 18 '05 #4
Manlio Perillo wrote:
On 3 Mar 2005 11:15:28 -0800, "Lonnie Princehouse"
<fi************ **@gmail.com> wrote:

No. I don't think it's possible to read the parse tree used by the
interpreter , especially as it is being created. Here are a couple of
kludgy ideas that might come close, though:

Is this a 'limitation' of the current version or it is impossible for
the architecture of CPython?
What about pypy?

On a side note, check out the compiler module. You might find it to be
friendlier and more useful than parser.

Thanks for the hint. It is what I want.
Unfortunately is seem to be not well documented.
Anyway, here is an example of what I would like to do:

#begin
def foo(**kwargs): print kwargs

foo(a = 1, b = 2, c = 3)
#end
In the current implementation kwargs is a dict, but I need to have the
keyword argument sorted.
Unfortunately subclassing fron dict and installing the class in the
__builtin__ module (with the name 'dict') does not work, CPython uses
only builtin types.

With the compiler module I can obtain the keyword arguments in the
order the were specified.
The problem is how to do this for every call to foo!

The nature of the interpreter is that the C code implementing function
calls specifically uses a dict created in C rather than using the
mechanisms that would be used to create a dict from within a python
program, so you have no way to "hook" your own implementation in to the
interpreter without modifying the C code.

Introspection does have its limits, and unfortunately that's one of
them. Sine the code that interprets the byte codes is pretty much all
written in C, there's no way to affect something that does not already
have Python run-time hooks provided (such as the __add__ method that
allows you to implement a specific response to the binary "+" operator).

What you probably need is a specific magic hook for the "**" operator,
but I'm not sure that's going to happen any time soon ...

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #5
On Sat, 05 Mar 2005 08:52:38 -0500, Kent Johnson <ke****@tds.net >
wrote:
Manlio Perillo wrote:
Anyway, here is an example of what I would like to do:

#begin
def foo(**kwargs): print kwargs

foo(a = 1, b = 2, c = 3)
#end
In the current implementation kwargs is a dict, but I need to have the
keyword argument sorted.
Unfortunately subclassing fron dict and installing the class in the
__builtin__ module (with the name 'dict') does not work, CPython uses
only builtin types.

With the compiler module I can obtain the keyword arguments in the
order the were specified.
The problem is how to do this for every call to foo!


Why not just pass the kind of argument you want? What is it you really need to do?

def foo(kwds): print kwds

foo(MyDict(a = 1, b = 2, c = 3))

Kent


I don't understand your code.
Here an example using OrderedDict from twisted:
import twisted.python. util as util foo(util.Ordere dDict(a = 1, b = 2, c = 3))

{'a': 1, 'c': 3, 'b': 2}
Simply I can't use a dict.

I have to do, as an example example:
foo('a', 1, 'b', 2, 'c', 3)

or

foo(['a', 'b', 'c'], [1, 2, 3])
Thanks and regards Manlio Perillo
Jul 18 '05 #6

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

Similar topics

12
2851
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the Python tutorial and docs leave a touch murky to my understanding. Dictionaries/"dict" types... Am I understanding/interpreting correctly when I go with the idea that a "dict" variable can be looked at as (effectively) two parallel arrays?...
699
34238
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...
6
1792
by: Kenneth McDonald | last post by:
I can't see anything about this in the notes on the upcoming 2.4 on python.org, but for some reason I thought I remembered seeing that a balanced tree sequence type would be included in Python in the near future. Is this correct? Thanks, Ken
3
1920
by: Oleg Paraschenko | last post by:
Hello, maybe of some interest: http://pysch.sourceforge.net/ast.html -- Oleg
63
5178
by: Davor | last post by:
Is it possible to write purely procedural code in Python, or the OO constructs in both language and supporting libraries have got so embedded that it's impossible to avoid them? Also, is anyone aware of any scripting language that could be considered as "Python minus OO stuff"? (As you can see I'm completely new to Python and initially believed it's a nice&simple scripting language before seeing all this OO stuff that was added in over...
19
3310
by: gaudetteje | last post by:
I've been searching high and low for a way to simply convert a small XML configuration file to Python data structures. I came across gnosis XML tools, but need a built-in option for doing something similar. My knowledge of DOM and anything beyond simple XML structures is rudimentary at best. Is there a simple way to use SAX handlers to pull attributes and elements into Python lists/dictionaries?
11
2511
by: M1st0 | last post by:
where I can find the grammar of python bytecode ? ( better if is in BCF ).
29
16580
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what I *want* at the end of this; I just have never taken on a programming task of this magnitude. I've seen that some are using python as a utility language for existing CAD environments, and I've also found some guy who's writing a 2d drafting...
852
28731
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for my general education. Mark
0
9474
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
10139
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
10075
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
8961
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
7485
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
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
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
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
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.