visual indentation

Gary Herron gherron at islandtraining.com
Fri Aug 22 16:06:46 EDT 2003


On Friday 22 August 2003 11:28 am, achrist at easystreet.com 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







More information about the Python-list mailing list