473,505 Members | 16,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python AST preserving whitespace and comments

Hi,

I'm working on Pythoscope[1], a unit test generator for Python and
stumbled into the following problem. I need a way to analyze and
modify Python AST tree, but without loosing source code formatting and
comments. Standard library ast module discards those, so I started
looking for other solutions. I found a library used by the 2to3
script[2], which does different kinds of source code refactorings and
preserves the formatting of the original file. Sadly AST generated by
this library is not compatible with the standard one, so I can't use a
nice interface of compiler.visitor[3]. It isn't also as well
documented. I kind of liked the clean and descriptive list of standard
AST nodes[4].

So here are my questions to the list. Is there any library built on
top of lib2to3 which makes traversing it easier? The pattern matcher
is nice, but not always feasible for the higher level view of the
source code.

Are there any tutorials/documentation about usage of lib2to3 which I'm
not aware of? I basically read through HACKING, README and a lot of
source code, but would appreciate some more examples and insights.

Is the lib2to3 library meant to be used outside of the 2to3 script or
rather not? Are there any projects that will incorporate its features
(enriched AST in particular) in a more "official" library-like
package? Will lib2to3 be put on PyPI anytime?

[1] http://pythoscope.org/
[2] http://svn.python.org/view/sandbox/trunk/2to3/
[3] http://docs.python.org/lib/module-compiler.visitor.html
[4] http://docs.python.org/lib/module-compiler.ast.html

Cheers,
mk
Aug 28 '08 #1
1 2047


Michal Kwiatkowski wrote:
Hi,

I'm working on Pythoscope[1], a unit test generator for Python and
stumbled into the following problem. I need a way to analyze and
modify Python AST tree, but without loosing source code formatting and
comments. Standard library ast module discards those, so I started
looking for other solutions. I found a library used by the 2to3
script[2], which does different kinds of source code refactorings and
preserves the formatting of the original file. Sadly AST generated by
this library is not compatible with the standard one, so I can't use a
nice interface of compiler.visitor[3]. It isn't also as well
documented. I kind of liked the clean and descriptive list of standard
AST nodes[4].

So here are my questions to the list. Is there any library built on
top of lib2to3 which makes traversing it easier? The pattern matcher
is nice, but not always feasible for the higher level view of the
source code.

Are there any tutorials/documentation about usage of lib2to3 which I'm
not aware of? I basically read through HACKING, README and a lot of
source code, but would appreciate some more examples and insights.

Is the lib2to3 library meant to be used outside of the 2to3 script or
rather not? Are there any projects that will incorporate its features
(enriched AST in particular) in a more "official" library-like
package? Will lib2to3 be put on PyPI anytime?

[1] http://pythoscope.org/
[2] http://svn.python.org/view/sandbox/trunk/2to3/
[3] http://docs.python.org/lib/module-compiler.visitor.html
[4] http://docs.python.org/lib/module-compiler.ast.html
You found the appropriate library, but I am unaware of anything that you
missed. I think others have had the idea that 2to3 might grow into
something more, but the core developers are currently focues on getting
2.6/3.0 out by the end of September.

Aug 28 '08 #2

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

Similar topics

699
33325
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...
34
5084
by: Brandon J. Van Every | last post by:
What do you think of this Python logo? http://pythonology.org/logos Good, bad, indifferent, love it, hate it? -- Cheers, www.indiegamedesign.com Brandon Van Every ...
1
1441
by: Oleg Dulin | last post by:
Colleagues: Does anyone know if there are less-lossy XML parsers and serializers that can capture and reproduce the structure of the input XML file including tabulation,whitespace, etc. ? We...
8
6120
by: jerry.levan | last post by:
Hi, I have a file that contains a "tcl" list stored as a string. The list members are sql commands ex: { begin { select * from foo where baz='whatever'} {select * from gooble } end { insert...
9
2539
by: amattie | last post by:
Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0 webservice? This has been discussed before, but no one has...
29
2317
by: dbhbarton | last post by:
Had a thought that's grown on me. No idea if it's original or not- too inexperienced in programming- but I guess there's no harm floating it out there. Python wins big on readability, and...
2
2339
by: Fergus Mcgee | last post by:
I'm using C# and .NET 2 I am creating an XmlDocument object and setting PreserveWhiteSpace to True, I use loadXMl to load XML from a file but when I look at the XML of the document object some of...
0
7218
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,...
0
7103
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...
0
7307
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,...
0
7370
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...
1
7021
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...
1
5035
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...
0
4701
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...
0
3188
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.