Parsing files in python

Chris Angelico rosuav at gmail.com
Sun Dec 23 19:32:17 EST 2012


On Mon, Dec 24, 2012 at 4:19 AM, Kene Meniru <kene.meniru at illom.org> wrote:
> Hello: I am writing a program that is made up of a collection of POV-Ray macros. POV-Ray is available at povray.org. It is a ray-tracing program that reads a scene description language (SDL) to create photo-realistic images. At this time my program (for modeling building information) is so huge that I am finding it difficult managing the macros and I am not even near completion.

I love POV-Ray! Great software, but the input language does at times
lack something, so I'm not surprised that you're wanting a pre-parser.

> ------------possible user file content for parsing ------------
> // in the following the python interface program reads
> //+ the contents of the file "other.file" as if its content
> //+ were located at this point.
> include other.file
>
> //In the following the python interface makes "snap_size" a
> //+  global parameter
> declare snap_size = 10
>
>
> // In the following "buildingLevel" is a class that is
> //+  called and passed the parameters in parenthesis.
> buildingLevel("FirstLevel", 3000)
>
> // In the following "snapOffset" is a class that is
> //+  called and passed the parameters in parenthesis.
> snapOffset("Closet-S1_r1", "Closet-S2_r3", <0,0,0>)
> ------------end of user file content
>
> It should also be possible to include comments using double-slashes, etc.

Hmm. That's a fairly complex file format you have there. I wonder if
it'd be possible to use an actual language parser for it - for
instance, to make this a real Python program. You'd have to use # for
a comment rather than //, and vector syntax may be a problem, but for
the rest, you should be able to do it all with just one extra line at
the top:

from povray_macros import *

You then write all your macros in a file called povray_macros.py and
they'll be conveniently available. For instance:

def buildingLevel(name, altitude):
    print("... whatever POV-Ray code is needed ...")

Unfortunately POV-Ray doesn't seem to support reading from stdin, so
you can't simply pipe your program into the renderer. But you can do
it this way:

my_file_whatever_it_is.py >temp.pov
povray +Itemp.pov

ChrisA



More information about the Python-list mailing list