visual indentation

Discussion in 'Python' started by Hilbert, Aug 22, 2003.

  1. Hilbert

    Hilbert Guest

    Hello,

    I'm using python to output RIB streams for Renderman.
    The RIB stream is a bunch of statements which describes
    a 3d image. The Rib standard allows for blocks which we
    usually indent for better visualization for example:

    WorldBegin
    Color [1 1 1]
    Surface "constant"
    Sphere(1.0, -1.0, 1.0, 360)
    WorldEnd

    I'm using CGKit in python which has a Renderman binding,
    so to output the same RIB I'd write:

    RiWorldBegin()
    RiColor(1.0,1.0,1.0)
    RiSurface('constant')
    RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()

    But I get an error, because python interprets my indentation
    as a block in the python code. So the only way to write this
    is without the indentation:

    RiWorldBegin()
    RiColor(1.0,1.0,1.0)
    RiSurface('constant')
    RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()

    But this is a lot harder to read.

    Is there any way to use such "visual" indentation in python?

    Thanks,
    Hilbert

     
    Hilbert, Aug 22, 2003
    #1
    1. Advertising

  2. Hilbert

    Guest

    Hilbert wrote:
    >
    > Hello,
    >
    > I'm using python to output RIB streams for Renderman.
    > The RIB stream is a bunch of statements which describes
    > a 3d image. The Rib standard allows for blocks which we
    > usually indent for better visualization for example:
    >
    > WorldBegin
    > Color [1 1 1]
    > Surface "constant"
    > Sphere(1.0, -1.0, 1.0, 360)
    > WorldEnd
    >
    > I'm using CGKit in python which has a Renderman binding,
    > so to output the same RIB I'd write:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But I get an error, because python interprets my indentation
    > as a block in the python code. So the only way to write this
    > is without the indentation:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But this is a lot harder to read.
    >
    > Is there any way to use such "visual" indentation in python?
    >


    If the code is purely sequential, how about something like this:

    import string
    def RunTheseStatements(s):
    stmts = map(string.strip, s.split("\n"))
    for stmt in stmts:
    eval(stmt)

    RunTheseStatements("""


    RiWorldBegin()
    RiColor(1.0,1.0,1.0)
    RiSurface('constant')
    RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()


    """)


    Al
     
    , Aug 22, 2003
    #2
    1. Advertising

  3. Hilbert> I'm using CGKit in python which has a Renderman binding, so to
    Hilbert> output the same RIB I'd write:

    Hilbert> RiWorldBegin()
    Hilbert> RiColor(1.0,1.0,1.0)
    Hilbert> RiSurface('constant')
    Hilbert> RiSphere(1.0,-1.0,1.0,360)
    Hilbert> RiWorldEnd()

    Hilbert> But I get an error, because python interprets my indentation as
    Hilbert> a block in the python code.

    As it should, because whitespace is significant in Python.

    Hilbert> So the only way to write this is without the indentation:

    Hilbert> RiWorldBegin()
    Hilbert> RiColor(1.0,1.0,1.0)
    Hilbert> RiSurface('constant')
    Hilbert> RiSphere(1.0,-1.0,1.0,360)
    Hilbert> RiWorldEnd()

    Hilbert> But this is a lot harder to read.

    Hilbert> Is there any way to use such "visual" indentation in python?

    In this case, just use "if 1:"

    RiWorldBegin()
    if 1:
    RiColor(1.0,1.0,1.0)
    RiSurface('constant')
    RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()

    Skip
     
    Skip Montanaro, Aug 22, 2003
    #3
  4. Hilbert

    Hans Nowak Guest

    Hilbert wrote:
    > Hello,
    >
    > I'm using python to output RIB streams for Renderman.
    > The RIB stream is a bunch of statements which describes
    > a 3d image. The Rib standard allows for blocks which we
    > usually indent for better visualization for example:
    >
    > WorldBegin
    > Color [1 1 1]
    > Surface "constant"
    > Sphere(1.0, -1.0, 1.0, 360)
    > WorldEnd
    >
    > I'm using CGKit in python which has a Renderman binding,
    > so to output the same RIB I'd write:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But I get an error, because python interprets my indentation
    > as a block in the python code. So the only way to write this
    > is without the indentation:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But this is a lot harder to read.
    >
    > Is there any way to use such "visual" indentation in python?


    I can think of various ugly solutions, none of them very satisfying...

    First, you can fake an indented block, e.g. like this:

    RiWorldBegin()
    if 1:
    RiColor(1.0, 1.0, 1.0)
    # etc...
    RiWorldEnd()

    Or you can put the function calls in a dummy list/tuple:

    RiWorldBegin()
    [
    RiColor(1.0, 1.0, 1.0),
    RiSurface('constant'),
    RiSphere(1.0,-1.0,1.0,360),
    ]
    RiWorldEnd()

    Or you can put some dummy statements in front...

    RiWorldBegin()
    (); RiColor(1.0, 1.0, 1.0)
    (); RiSurface('constant')
    (); RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()

    I warned you they were ugly... :)

    Maybe the best solution would be judicious use of comments, e.g.

    RiWorldBegin()
    #
    RiColor(1.0, 1.0, 1.0)
    RiSurface('constant')
    RiSphere(1.0,-1.0,1.0,360)
    #
    RiWorldEnd()

    This doesn't give you indentation, but at least the statements in the "block"
    stand out more.

    But maybe somebody else has a better solution...?

    --
    Hans ()
    http://zephyrfalcon.org/
     
    Hans Nowak, Aug 22, 2003
    #4
  5. Hilbert

    Cliff Wells Guest

    On Fri, 2003-08-22 at 11:10, Hilbert wrote:
    > Hello,
    >
    > I'm using python to output RIB streams for Renderman.
    > The RIB stream is a bunch of statements which describes
    > a 3d image. The Rib standard allows for blocks which we
    > usually indent for better visualization for example:
    >
    > WorldBegin
    > Color [1 1 1]
    > Surface "constant"
    > Sphere(1.0, -1.0, 1.0, 360)
    > WorldEnd
    >
    > I'm using CGKit in python which has a Renderman binding,
    > so to output the same RIB I'd write:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But I get an error, because python interprets my indentation
    > as a block in the python code. So the only way to write this
    > is without the indentation:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But this is a lot harder to read.
    >
    > Is there any way to use such "visual" indentation in python?


    How about this? It creates a bit of unnecessary overhead (a single
    tuple creation), but looks okay visually (and Emacs correctly indents
    it):

    RiWorldBegin()
    (
    RiColor(1.0,1.0,1.0),
    RiSurface('constant'),
    RiSphere(1.0,-1.0,1.0,360),
    )
    RiWorldEnd()


    Regards,

    --
    Cliff Wells, Software Engineer
    Logiplex Corporation (www.logiplex.net)
    (503) 978-6726 (800) 735-0555
     
    Cliff Wells, Aug 22, 2003
    #5
  6. Hilbert

    Gary Herron Guest

    On Friday 22 August 2003 11:28 am, wrote:
    > Hilbert wrote:
    > > Hello,
    > >
    > > I'm using python to output RIB streams for Renderman.
    > > The RIB stream is a bunch of statements which describes
    > > a 3d image. The Rib standard allows for blocks which we
    > > usually indent for better visualization for example:
    > >
    > > WorldBegin
    > > Color [1 1 1]
    > > Surface "constant"
    > > Sphere(1.0, -1.0, 1.0, 360)
    > > WorldEnd
    > >
    > > I'm using CGKit in python which has a Renderman binding,
    > > so to output the same RIB I'd write:
    > >
    > > RiWorldBegin()
    > > RiColor(1.0,1.0,1.0)
    > > RiSurface('constant')
    > > RiSphere(1.0,-1.0,1.0,360)
    > > RiWorldEnd()
    > >
    > > But I get an error, because python interprets my indentation
    > > as a block in the python code. So the only way to write this
    > > is without the indentation:
    > >
    > > RiWorldBegin()
    > > RiColor(1.0,1.0,1.0)
    > > RiSurface('constant')
    > > RiSphere(1.0,-1.0,1.0,360)
    > > RiWorldEnd()
    > >
    > > But this is a lot harder to read.
    > >
    > > Is there any way to use such "visual" indentation in python?

    >
    > If the code is purely sequential, how about something like this:
    >
    > import string
    > def RunTheseStatements(s):
    > stmts = map(string.strip, s.split("\n"))
    > for stmt in stmts:
    > eval(stmt)
    >
    > RunTheseStatements("""
    >
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    >
    > """)


    Here's another way -- inside a [..] or (...) construct

    [
    RiWorldBegin(),
    RiColor(1.0,1.0,1.0),
    RiSurface('constant'),
    RiSphere(1.0,-1.0,1.0,360),
    RiWorldEnd(),
    ]


    Or try this (this might be too ugly)

    RiWorldBegin()
    if 1:
    RiColor(1.0,1.0,1.0)
    RiSurface('constant')
    RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()

    or make these all member functions of a class and try

    c. RiWorldBegin(),
    c. RiColor(1.0,1.0,1.0),
    c. RiSurface('constant'),
    c. RiSphere(1.0,-1.0,1.0,360),
    c. RiWorldEnd(),

    or

    Hmmmm -- I'll bet there's more possibilities.

    Gary Herron
     
    Gary Herron, Aug 22, 2003
    #6
  7. Hilbert

    Gary Herron Guest

    On Friday 22 August 2003 11:28 am, wrote:
    > Hilbert wrote:
    > > Hello,
    > >
    > > I'm using python to output RIB streams for Renderman.
    > > The RIB stream is a bunch of statements which describes
    > > a 3d image. The Rib standard allows for blocks which we
    > > usually indent for better visualization for example:
    > >
    > > WorldBegin
    > > Color [1 1 1]
    > > Surface "constant"
    > > Sphere(1.0, -1.0, 1.0, 360)
    > > WorldEnd
    > >
    > > I'm using CGKit in python which has a Renderman binding,
    > > so to output the same RIB I'd write:
    > >
    > > RiWorldBegin()
    > > RiColor(1.0,1.0,1.0)
    > > RiSurface('constant')
    > > RiSphere(1.0,-1.0,1.0,360)
    > > RiWorldEnd()
    > >
    > > But I get an error, because python interprets my indentation
    > > as a block in the python code. So the only way to write this
    > > is without the indentation:
    > >
    > > RiWorldBegin()
    > > RiColor(1.0,1.0,1.0)
    > > RiSurface('constant')
    > > RiSphere(1.0,-1.0,1.0,360)
    > > RiWorldEnd()
    > >
    > > But this is a lot harder to read.
    > >
    > > Is there any way to use such "visual" indentation in python?

    >
    > If the code is purely sequential, how about something like this:
    >
    > import string
    > def RunTheseStatements(s):
    > stmts = map(string.strip, s.split("\n"))
    > for stmt in stmts:
    > eval(stmt)
    >
    > RunTheseStatements("""
    >
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    >
    > """)


    Here's another way -- inside a [..] or (...) construct

    [
    RiWorldBegin(),
    RiColor(1.0,1.0,1.0),
    RiSurface('constant'),
    RiSphere(1.0,-1.0,1.0,360),
    RiWorldEnd(),
    ]


    Or try this (this might be too ugly)

    RiWorldBegin()
    if 1:
    RiColor(1.0,1.0,1.0)
    RiSurface('constant')
    RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()

    or make these all member functions of a class and try

    c. RiWorldBegin(),
    c. RiColor(1.0,1.0,1.0),
    c. RiSurface('constant'),
    c. RiSphere(1.0,-1.0,1.0,360),
    c. RiWorldEnd(),

    or

    Hmmmm -- I'll bet there's more possibilities.

    Gary Herron
     
    Gary Herron, Aug 22, 2003
    #7
  8. Hilbert

    Evan Simpson Guest

    Hilbert wrote:
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > Is there any way to use such "visual" indentation in python?


    If all of the lines that you want to indent in this fashion are
    expressions, not statments, then you could do this:

    RiWorldBegin()
    ( RiColor(1.0,1.0,1.0) )
    ( RiSurface('constant') )
    ( RiSphere(1.0,-1.0,1.0,360) )
    RiWorldEnd()

    If there are statements involved, you could do this:

    RiWorldBegin()
    1; RiColor(1.0,1.0,1.0)
    1; RiSurface('constant')
    1; RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()

    Both are ugly, of course.

    Cheers,

    Evan @ 4-am
     
    Evan Simpson, Aug 22, 2003
    #8
  9. Hilbert

    Peter Otten Guest

    Hilbert wrote:

    > Is there any way to use such "visual" indentation in python?


    Whitespace *is* significant in Python, and you should not try to circumvent
    this using dirty tricks. Why not structuring your world definitions in the
    standard way, putting the things that belong together in separate functions
    or classes?

    #disclaimer: no knowledge of CGKit involved in the following code
    WHITE = (1.0,1.0,1.0)

    def whitesurface():
    RiColor(*WHITE)
    RiSurface('constant')

    def firstworld():
    whitesurface()
    RiSphere(1.0,-1.0,1.0,360)

    def secondworld():
    whitesurface()
    RiSphere(1.0,-1.0,1.0,360)

    for world in [firstworld, secondworld]:
    RiWorldBegin()
    world()
    RiWorldEnd()

    I think the above is pretty readable, and you can always factor out
    repeating chunks of code

    Peter
     
    Peter Otten, Aug 22, 2003
    #9
  10. Hilbert

    Ben Finney Guest

    On Fri, 22 Aug 2003 13:06:46 -0700, Gary Herron wrote:
    > Hmmmm -- I'll bet there's more possibilities.


    All of which, like all possibilities for forcing different indentation
    on Python, presented so far, *reduce* readability instead of enhancing
    it.

    --
    \ "One time a cop pulled me over for running a stop sign. He |
    `\ said, 'Didn't you see the stop sign?' I said, 'Yeah, but I |
    _o__) don't believe everything I read.'" -- Steven Wright |
    Ben Finney <http://bignose.squidly.org/>
     
    Ben Finney, Aug 22, 2003
    #10
  11. Hilbert

    Greg Krohn Guest

    "Hilbert" <> wrote in message
    news:...
    > Hello,
    >
    > I'm using python to output RIB streams for Renderman.
    > The RIB stream is a bunch of statements which describes
    > a 3d image. The Rib standard allows for blocks which we
    > usually indent for better visualization for example:
    >
    > WorldBegin
    > Color [1 1 1]
    > Surface "constant"
    > Sphere(1.0, -1.0, 1.0, 360)
    > WorldEnd
    >
    > I'm using CGKit in python which has a Renderman binding,
    > so to output the same RIB I'd write:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But I get an error, because python interprets my indentation
    > as a block in the python code. So the only way to write this
    > is without the indentation:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But this is a lot harder to read.
    >
    > Is there any way to use such "visual" indentation in python?
    >
    > Thanks,
    > Hilbert
    >
    >
    >
    >


    What about an if statement:

    RiWorldBegin()
    if True:
    RiColor(1.0,1.0,1.0)
    RiSurface('constant')
    RiSphere(1.0,-1.0,1.0,360)
    RiWorldEnd()

    I realize it's ugly, but it's easy.
     
    Greg Krohn, Aug 22, 2003
    #11
  12. Hilbert

    Dan Bishop Guest

    Hilbert <> wrote in message news:<>...
    > Hello,
    >
    > I'm using python to output RIB streams for Renderman.
    > The RIB stream is a bunch of statements which describes
    > a 3d image. The Rib standard allows for blocks which we
    > usually indent for better visualization for example:
    >
    > WorldBegin
    > Color [1 1 1]
    > Surface "constant"
    > Sphere(1.0, -1.0, 1.0, 360)
    > WorldEnd

    ....
    > Is there any way to use such "visual" indentation in python?


    If the code consists of nothing but expressions (which yours does), you can write:

    (RiWorldBegin())
    ( RiColor(1.0,1.0,1.0))
    ( RiSurface('constant'))
    ( RiSphere(1.0,-1.0,1.0,360))
    (RiWorldEnd())

    or

    (
    RiWorldBegin(),
    RiColor(1.0,1.0,1.0),
    RiSurface('constant'),
    RiSphere(1.0,-1.0,1.0,360),
    RiWorldEnd()
    )
     
    Dan Bishop, Aug 23, 2003
    #12
  13. Hilbert

    Cliff Wells Guest

    On Fri, 2003-08-22 at 11:10, Hilbert wrote:
    > Hello,
    >
    > I'm using python to output RIB streams for Renderman.
    > The RIB stream is a bunch of statements which describes
    > a 3d image. The Rib standard allows for blocks which we
    > usually indent for better visualization for example:
    >
    > WorldBegin
    > Color [1 1 1]
    > Surface "constant"
    > Sphere(1.0, -1.0, 1.0, 360)
    > WorldEnd
    >
    > I'm using CGKit in python which has a Renderman binding,
    > so to output the same RIB I'd write:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But I get an error, because python interprets my indentation
    > as a block in the python code. So the only way to write this
    > is without the indentation:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But this is a lot harder to read.
    >
    > Is there any way to use such "visual" indentation in python?


    I'm sending this again as the message I sent earlier apparently never
    made it.

    RiWorldBegin()
    (
    RiColor(1.0,1.0,1.0),
    RiSurface('constant'),
    RiSphere(1.0,-1.0,1.0,360),
    )
    RiWorldEnd()

    There's a bit of added overhead (superfluous tuple creation) but it does
    what you ask.

    Regards,
    Cliff

    --
    Should I stand midst the breakers, Should I lie with Death my bride?
    -This Mortal Coil
     
    Cliff Wells, Aug 23, 2003
    #13
  14. Hilbert

    Hilbert Guest

    Thanks for all the suggestions!

    I like both the tuple and the if 1: approach.

    I don't want to define blocks in functions because that
    would actually differ from how the RIB stream works.
    I'd like to have my code reflect a real RIB stream.

    Thanks again,
    Hilbert





    In article <>, Hilbert wrote:
    > Hello,
    >
    > I'm using python to output RIB streams for Renderman.
    > The RIB stream is a bunch of statements which describes
    > a 3d image. The Rib standard allows for blocks which we
    > usually indent for better visualization for example:
    >
    > WorldBegin
    > Color [1 1 1]
    > Surface "constant"
    > Sphere(1.0, -1.0, 1.0, 360)
    > WorldEnd
    >
    > I'm using CGKit in python which has a Renderman binding,
    > so to output the same RIB I'd write:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But I get an error, because python interprets my indentation
    > as a block in the python code. So the only way to write this
    > is without the indentation:
    >
    > RiWorldBegin()
    > RiColor(1.0,1.0,1.0)
    > RiSurface('constant')
    > RiSphere(1.0,-1.0,1.0,360)
    > RiWorldEnd()
    >
    > But this is a lot harder to read.
    >
    > Is there any way to use such "visual" indentation in python?
    >
    > Thanks,
    > Hilbert
    >
    >
    >
    >
     
    Hilbert, Aug 24, 2003
    #14
  15. Hilbert

    Chad Netzer Guest

    On Fri, 2003-08-22 at 12:13, Hans Nowak wrote:
    > Hilbert wrote:


    > But maybe somebody else has a better solution...?



    Another kinda ugly one:


    RiWorldBegin() ;\
    RiColor(1.0,1.0,1.0) ;\
    RiSurface('constant') ;\
    RiSphere(1.0,-1.0,1.0,360) ;\
    RiWorldEnd()


    --

    Chad Netzer
     
    Chad Netzer, Aug 25, 2003
    #15
    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. SJ

    DataList Indentation

    SJ, Nov 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,089
    Patrick.O.Ige
    Nov 4, 2005
  2. jacov

    [Fwd: Indentation]

    jacov, Jul 22, 2004, in forum: Java
    Replies:
    0
    Views:
    351
    jacov
    Jul 22, 2004
  3. Daan

    Unwanted indentation

    Daan, Dec 9, 2003, in forum: HTML
    Replies:
    2
    Views:
    483
  4. Jay

    List Indentation

    Jay, Apr 20, 2004, in forum: HTML
    Replies:
    5
    Views:
    551
  5. Jesse B.
    Replies:
    2
    Views:
    223
    Josh Cheek
    Mar 27, 2010
Loading...

Share This Page