parsing string to a list of objects - help!

Donn Ingle donn.ingle at gmail.com
Mon Nov 5 10:00:46 CET 2007


Hi, I really hope someone can help me -- I'm stuck.

 I have written three versions of code over a week and still can't get past
this problem, it's blocking my path to getting other code written.

This might be a little hairy, but I'll try to keep it short.

Situation: 
I want to pass a string to a function which will parse it and generate
objects in a list.
Each object is a "property" which keeps members like x,y,r,g,b etc.
Each element in the final list describes a frame in an animation.

I want to be able to pass only the properties of key moments (key frames) in
time to the function. Along with that is passed the *way* each moment
changes into the next.

The form I am using is like this:
t=Thing()
t.propLayout("#---#---#==_##_")
prop1 = Property(x=10,y=10,r=1,g=1,b=1,a=0)
prop2 = Property(x=20,y=10,r=0,g=0,b=1,a=1)
prop3 = Property(x=10,y=100,r=1,g=1,b=1,a=1)
prop4 = Property(x=100,y=200,r=1,g=1,b=1,a=1)
t.setProps(prop1,prop1,prop2,prop3,prop4)

So setProps is the parser and it creates a proplist within t.

My short-hand for this:
# is a property
- is a 'tween' (where the property before it influences it)
= is a property exactly the same as the one before it.
_ (underscore) is a blank frame

Things like this can be expressed:
1234567890123
#---#--#===_#
(4 keyframes, #'s. 13 frames, lifetime.)
In words that would say:
Frame 1, make a property.
Frame 2 to 4 tweening : copy previous property (from 1) and advance the
members by the average (calculated by getting the range between 1 and 5 and
going through each member variable (x,y,z,r,g,b) and dividing etc. This
means looking ahead.)
Frame 5, make a property
Frame 6 to 7 tweening.
Frame 8, make a property
Frame 9 to 11, copy property from 8
Frame 12, make a blank
Frame 13, make a property.

So, the animation will proceed: Start at 1, move/morph to 5, move/morph to
8, remain static to 11, go away for 12 and appear somewhere at 13.

It seems so simple, but I just cannot nail it down. Each time I end up with
a mess of string[index] and if else tests within while loops that seem to
work until I get to a test string that breaks it all.

I'm sure there must be a better way to formalize this little 'language' and
break it down into a list of property objects that exactly describes the
string -- catching user errors too.

The rules:
Tweens : must start and end in # signs: #----#
Static : must start with a # : #===
No other chars can be within a range: #==_= is bad. #--=--# is bad.

Odds:
Singles: things like ### are valid. This means each is a frame somewhere,
implying they will jump around (i.e. not be statics)
Blanks : are simple - they are properties for every underscore

I have glanced around at parsing and all the tech-speak confuses the heck
out of me. I am not too smart and would appreciate any help that steers
away from cold theory and simply hits at this problem.

Donn.




More information about the Python-list mailing list