reifying indent and dedent into braces

Discussion in 'Python' started by Rustom Mody, Oct 13, 2009.

  1. Rustom Mody

    Rustom Mody Guest

    At http://www.secnetix.de/olli/Python/block_indentation.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!
    Rustom Mody, Oct 13, 2009
    #1
    1. Advertising

  2. 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
    Stefan Behnel, Oct 13, 2009
    #2
    1. Advertising

  3. On Tue, 13 Oct 2009 12:35:13 +0530, Rustom Mody wrote:

    > At http://www.secnetix.de/olli/Python/block_indentation.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
    Steven D'Aprano, Oct 13, 2009
    #3
  4. Rustom Mody

    rustom Guest

    rustom, Oct 13, 2009
    #4
  5. Rustom Mody

    greg Guest

    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
    greg, Oct 14, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    532
    Franz Steinhaeusler
    Oct 5, 2005
  2. Adriaan Renting
    Replies:
    1
    Views:
    358
    Franz Steinhaeusler
    Oct 12, 2005
  3. Steven Bethard
    Replies:
    3
    Views:
    303
    Peter Hansen
    Nov 19, 2005
  4. dt
    Replies:
    4
    Views:
    468
    CBFalconer
    Dec 31, 2006
  5. Alexander Schmolck

    Re: how to indent/dedent a region in emacs?

    Alexander Schmolck, Jun 11, 2008, in forum: Python
    Replies:
    0
    Views:
    454
    Alexander Schmolck
    Jun 11, 2008
Loading...

Share This Page