visual indentation

H

Hilbert

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

(e-mail address removed)
 
A

achrist

Hilbert said:
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
 
S

Skip Montanaro

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
 
H

Hans Nowak

Hilbert said:
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...?
 
C

Cliff Wells

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,
 
G

Gary Herron

Hilbert said:
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
 
G

Gary Herron

Hilbert said:
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
 
E

Evan Simpson

Hilbert said:
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
 
P

Peter Otten

Hilbert said:
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
 
B

Ben Finney

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

Greg Krohn

Hilbert said:
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

(e-mail address removed)

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

Dan Bishop

Hilbert said:
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()
)
 
C

Cliff Wells

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
 
H

Hilbert

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

(e-mail address removed)
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top