advise on using ply

329 views
Skip to first unread message

Kene Meniru

unread,
Dec 24, 2012, 7:16:40 AM12/24/12
to ply-...@googlegroups.com
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 am hoping to move this program but first I was considering creating a file
format for input and am wondering the best way to approach this.

Basically the user writes a text file using certain key words and numbers
and my proposed python program will read this file and call the classes that
will then work together to calculate the information that is needed to
create an accurate model. The result of this calculation will be an output
to another text file in the appropriate format such as POV-Ray SDL, OpenSCAD
script, etc. This file output can then be rendered by the corresponding
program to produce the actual 3D model. The macros I have now currently does
this but like I said it is getting tedious and most importantly the fun
factor is losing its strength for me.

I have been advised to check out python-ply but I was wondering if the
format is something I can do with ply or if I am better off using pure
python. Also considering that the text files the proposed program will be
parsing may get quite big, will there be any performance issues? The
following is a sample of what the text file that will be processed by this
proposed system will contain. I appreciate any pointers and suggestions.
Thank you very much.

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

Sincerely,
Kene

Joseph S. Tate

unread,
Dec 24, 2012, 1:49:20 PM12/24/12
to ply-...@googlegroups.com

You could do this in pure python, but this kind of thing is perfect for PLY.

--
You received this message because you are subscribed to the Google Groups "ply-hack" group.
To post to this group, send email to ply-...@googlegroups.com.
To unsubscribe from this group, send email to ply-hack+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Kene Meniru

unread,
Dec 24, 2012, 4:30:52 PM12/24/12
to ply-...@googlegroups.com
Joseph S. Tate wrote:

> You could do this in pure python, but this kind of thing is perfect for
> PLY.

I thought as much. I am willing to do the work but I can't seem to wrap my
mind around the PLY examples I have not seen examples on exactly what I am
proposing. This is why I am posting here. Can you give me a very simple
example of reading a single line of text and calling a python class with the
parameters?

I am not knowledgeable at all in this area so I need some help. I may be
wrong but I do not think it is like compiling but more like interpreting.
Thanks.

Joseph S. Tate

unread,
Dec 24, 2012, 4:54:18 PM12/24/12
to ply-...@googlegroups.com
There are lots of examples to crib from.  Looking at those is how I learned how to use PLY:

This is a very very simple example, but is an end to end PLY lexer and parser.  No matter how big your system gets it still looks like this: http://www.dalkescientific.com/writings/NBN/parsing_with_ply.html .  It's an almost trivial example, but you can probably see how you would extend the singular "Atom" into different statement objects that you would define.  These statement objects then would call your python class with the parameters.  Or if you don't need to parse separately from the execution, you could just make your calls inside p_*.

Definitely take a look at the examples in the source repo: https://github.com/dabeaz/ply/tree/master/example .  They range from simple to extremely advanced, but they don't take very long to read over.

That should get you where you need to go.


--
You received this message because you are subscribed to the Google Groups "ply-hack" group.
To post to this group, send email to ply-...@googlegroups.com.
To unsubscribe from this group, send email to ply-hack+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
Joseph Tate
Personal e-mail: jtate AT dragonstrider DOT com
Web: http://www.dragonstrider.com

Kene Meniru

unread,
Dec 24, 2012, 5:33:41 PM12/24/12
to ply-...@googlegroups.com
Joseph S. Tate wrote:

> There are lots of examples to crib from. Looking at those is how I
> learned how to use PLY:
>
> This is a very very simple example, but is an end to end PLY lexer and
> parser. No matter how big your system gets it still looks like this:
> http://www.dalkescientific.com/writings/NBN/parsing_with_ply.html . It's
> an almost trivial example, but you can probably see how you would extend
> the singular "Atom" into different statement objects that you would
> define.

Looks like a good example and shows a manual option for comparison. Exactly
what I would like to play with. Thanks.

Rob Fowler

unread,
Dec 25, 2012, 4:22:40 AM12/25/12
to ply-...@googlegroups.com
I have used POVray extensively in the past myself. I still have A1
printed images on the walls around me this very minute.
I have also used yacc and ply in projects.

Although I think making an intermediate language and using ply to parse
it would be a good way to go I think, if you are not concerned about
security it would probably be simpler to make the description file
readable by python itself:
building({'firstLevel' : 123, 'ceiling' : 200, 'levels' : 20})
then have a 'building' object generator,
already things comments, includes, compilation and in-line maths all
work.
That style reminds me of python 'sqlalchemy' if you know what I mean.

That said, if you do a few tutorials you will find 'ply' quite easy if
you have used yacc.
Python, because of the native lists and dictionaries lends itself to
much smaller parsers than even yacc and c++ with STL.

A.T.Hofkamp

unread,
Jan 2, 2013, 3:54:04 AM1/2/13
to ply-...@googlegroups.com
On 12/24/2012 01:16 PM, Kene Meniru 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.

I know that program :)

> 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 never got much further than simple solidly coloured images, but yeah, doing that in native SDL is
not much fun.
Even for the simple things I make, I found using a Python program to generate the SDL file much nicer.

> I am hoping to move this program but first I was considering creating a file
> format for input and am wondering the best way to approach this.

The usual apporach is to write first some example files, if possible together with the expected
output so you get an idea of what the input language should look like, and what your generator
should produce.

In your example, it seems that classes get magically defined, which seems a next step to look into.

> Basically the user writes a text file using certain key words and numbers
> and my proposed python program will read this file and call the classes that

My suggestion would be to first do this in pure Python. Python is very good at reading some data
files, so branching out towards loading stuff from a file is easy if you need it.
Your example is not exactly Python, but quite close (ie "Vector(0,0,0)" instead of "<0,0,0>" etc).

Doing it like this give you again more experience in what you actually want from the input format,
so you can first settle what you want to have without needing to write a PLY parser + input file
processor.

On the other hand, if you have never done anything with parsing, spending some time on it is good
too, as it gives you an idea of what these tools can do for you.

> to another text file in the appropriate format such as POV-Ray SDL, OpenSCAD
> script, etc. This file output can then be rendered by the corresponding
> program to produce the actual 3D model. The macros I have now currently does

Yep, that's much easier to do :)

An alternative could be to use a better front-end, eg Blender, which can afaik also generate such
output.

> I have been advised to check out python-ply but I was wondering if the
> format is something I can do with ply or if I am better off using pure
> python. Also considering that the text files the proposed program will be
> parsing may get quite big, will there be any performance issues? The

PLY works well when you understand what the language should look like, and what it should do.
Getting there is imo better done by doing experiments in generating output eg with Python.
(Obviously, you can use PLY when you find patterns in your input that you want to move to an input
file.)

As for speed issues, don't bother until you have a real problem.
It is good to keep in mind that you should not do computations that you can avoid, but otherwise, it
is better to write clean code than it is to write fast code which is unreadable and/or unmaintainable.
In my experience, you cannot predict where the problem is going to surface, so it is not useful to
anticipate for it, as it will surface at a place where you didn't expect it.

> following is a sample of what the text file that will be processed by this
> proposed system will contain. I appreciate any pointers and suggestions.
> Thank you very much.
>
> ------------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
> 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

Instantiating some classes is the simple part. How do you define "buildingLevel" ?
What kind of expressions do you allow, what kind of types do you have, what operations can you
perform? (eg "0+true": is that ok, if so what's the result, if not what error do you raise?)

Parsing is just the first step. The real problem is not parsing, it is what you intend to do with
the text that you parsed from the input file.

> It should also be possible to include comments using double-slashes, etc.

This is trivial (just one definition in PLY).


Albert

Kene Meniru

unread,
Jan 2, 2013, 11:34:37 AM1/2/13
to ply-...@googlegroups.com
OK thanks for the replies. It kinda surprised me but I realized suddenly I
can just use python for both the classes and the scene description language
for now. When I have fully ported the code I can then work on a custom
language if necessary.

For those of you that know POV-Ray already and want to try out this program
in its current incarnation called Kobldes, visit
http://sourceforge.net/projects/kobldes/

Reply all
Reply to author
Forward
0 new messages