Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > reifying indent and dedent into braces

Reply
Thread Tools

reifying indent and dedent into braces

 
 
Rustom Mody
Guest
Posts: n/a
 
      10-13-2009
At http://www.secnetix.de/olli/Python/b...dentation.hawk
I find that the python code

>>> if foo:

.... 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!
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      10-13-2009
Rustom Mody wrote:
> 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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-13-2009
On Tue, 13 Oct 2009 12:35:13 +0530, Rustom Mody wrote:

> At http://www.secnetix.de/olli/Python/b...dentation.hawk I find that
> the python code
>
>>>> if foo:

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



Have you looked at the tokenize module?

http://docs.python.org/library/tokenize.html




--
Steven
 
Reply With Quote
 
rustom
Guest
Posts: n/a
 
      10-13-2009
On Oct 13, 12:45*pm, Steven D'Aprano
<(E-Mail Removed)> wrote:
>
> Have you looked at the tokenize module?
>
> http://docs.python.org/library/tokenize.html


Thanks Steven -- that was what I was looking for.
 
Reply With Quote
 
greg
Guest
Posts: n/a
 
      10-14-2009
Rustom Mody wrote:

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

--
Greg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: how to indent/dedent a region in emacs? Alexander Schmolck Python 0 06-11-2008 10:49 PM
gnu indent: how to avoid extra indent? dt C Programming 4 12-31-2006 11:31 AM
textwrap.dedent() drops tabs - bug or feature? Steven Bethard Python 3 11-19-2005 09:57 PM
Re: IDLE dedent/unindent key bindings for non-us keybord? Adriaan Renting Python 1 10-12-2005 01:10 PM
IDLE dedent/unindent key bindings for non-us keybord? gjohannes@gmail.com Python 1 10-05-2005 08:50 AM



Advertisments