reifying indent and dedent into braces

R

Rustom Mody

At http://www.secnetix.de/olli/Python/block_indentation.hawk
I find that the python code
.... if bar:
.... x = 42
.... else:
.... print foo
....

has its indentation structure made explicit as

<if> <foo> <:> [0]
<INDENT> <if> <bar> <:> [0, 4]
<INDENT> <x> <=> <42> [0, 4, 8]
<DEDENT> <DEDENT> <else> <:> [0]
<INDENT> <print> <foo> [0, 2]
<DEDENT> [0]

Is there any api in python for going from 1st to 2nd and back?

Context: I am trying to generate some python code and its
indentation=structure is giving me a headache!
 
S

Stefan Behnel

Rustom said:
I am trying to generate some python code and its
indentation=structure is giving me a headache!

Have you considered searching the web for "Python code generator"?

Stefan
 
G

greg

Rustom said:
Context: I am trying to generate some python code and its
indentation=structure is giving me a headache!

When I generate Python code (or anything else with an
indented structure) I usually define myself a class with
a method for writing out a line, and a pair of methods
for increasing and decreasing the indentation level.
It keeps track of the current indentation level and
automatically prepends the appropriate amount of
whitespace to each line written out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top