473,756 Members | 8,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parsing string to a list of objects - help!

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(prop 1,prop1,prop2,p rop3,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.

Nov 5 '07 #1
7 2377
On Nov 5, 3:00 am, Donn Ingle <donn.in...@gma il.comwrote:
>
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 -

Here is a pyparsing version that I hope is fairly easy-to-read and
tech-speak free:

from pyparsing import *

frame = Literal("#")
tween = Word("-") # that is, it is a "word" composed of 1 or more -'s
copy = Literal("=")
blank = Literal("_")

animation = OneOrMore((fram e + Optional(
(tween + FollowedBy(fram e)) |
OneOrMore(copy | blank) ) ) )

test = "#---#--#===_#"

print animation.parse String(test)

This prints the following:

['#', '---', '#', '--', '#', '=', '=', '=', '_', '#']

>From here, the next step would be to define classes that these matched
tokens could be converted to. Pyparsing allows you to attach a method
to individual expressions using setParseAction - if you pass a class
instead of a method, then class instances are returned. For these
tokens, you could write:

class Prop(object):
def __init__(self,t okens):
pass

class Tween(object):
def __init__(self,t okens):
self.tween = tokens[0]
self.tweenLengt h = len(self.tween)

class CopyPrevious(ob ject):
def __init__(self,t okens):
pass

class Blank(object):
def __init__(self,t okens):
pass

frame.setParseA ction(Prop)
tween.setParseA ction(Tween)
copy.setParseAc tion(CopyPrevio us)
blank.setParseA ction(Blank)

And now calling animation.parse String(test) returns a sequence of
objects representing the input string's frames, tweens, etc.:

[<__main__.Pro p object at 0x00B9D8F0>, <__main__.Twe en object at
0x00BA5390>, <__main__.Pro p object at 0x00B9D9B0>, <__main__.Twe en
object at 0x00BA55F0>, <__main__.Pro p object at 0x00BA5230>,
<__main__.CopyP revious object at 0x00BA58F0>, <__main__.CopyP revious
object at 0x00BA59D0>, <__main__.CopyP revious object at 0x00BA5AB0>,
<__main__.Bla nk object at 0x00BA5B70>, <__main__.Pro p object at
0x00BA5510>]

Now you can implement behavior in these 4 classes to implement the
tweening, copying, etc. behavior that you want, and then walk through
this sequence invoking this animation logic.

-- Paul
Nov 5 '07 #2
Wow Paul, you have given me much to chew. I'll start testing it in my slow
way -- it looks so simple!

Thank you.
\d

Nov 5 '07 #3
Paul,
I quickly slapped your example into a py file for a first-glance test and
the first part works, but the second gives me the error below. Could I have
an old version of pyparser? I will dig into it anyway, just being lazy :)

code:
from pyparsing import *

frame = Literal("#")
tween = Word("-") # that is, it is a "word" composed of 1 or more -'s
copy = Literal("=")
blank = Literal("_")

animation = OneOrMore((fram e + Optional(
(tween + FollowedBy(fram e)) |
OneOrMore(copy | blank) ) ) )

test = "#---#--#===_#"

print animation.parse String(test)

class Prop(object):
def __init__(self,t okens):
pass

class Tween(object):
def __init__(self,t okens):
self.tween = tokens[0]
self.tweenLengt h = len(self.tween)

class CopyPrevious(ob ject):
def __init__(self,t okens):
pass

class Blank(object):
def __init__(self,t okens):
pass

frame.setParseA ction(Prop)
tween.setParseA ction(Tween)
copy.setParseAc tion(CopyPrevio us)
blank.setParseA ction(Blank)
animation.parse String(test)

result:
containers:$ python parser.py
['#', '---', '#', '--', '#', '=', '=', '=', '_', '#']
Traceback (most recent call last):
File "parser.py" , line 39, in ?
animation.parse String(test)
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 620, in
parseString
loc, tokens = self.parse( instring.expand tabs(), 0 )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 562, in parse
loc,tokens = self.parseImpl( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 1768, in
parseImpl
loc, tokens = self.expr.parse ( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 562, in parse
loc,tokens = self.parseImpl( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 1390, in
parseImpl
loc, resultlist = self.exprs[0].parse( instring, loc, doActions )
File "/usr/lib/python2.4/site-packages/pyparsing.py", line 588, in parse
tokens = self.parseActio n( instring, tokensStart, retTokens )
TypeError: __init__() takes exactly 2 arguments (4 given)
Nov 5 '07 #4
Wow Paul, you have given me much to chew. I'll start testing it in my slow
way -- it looks so simple!

Thank you.
\d

Nov 5 '07 #5
Paul,
frame = Literal("#")
tween = Word("-") # that is, it is a "word" composed of 1 or more -'s
copy = Literal("=")
blank = Literal("_")

animation = OneOrMore((fram e + Optional(
(tween + FollowedBy(fram e)) |
OneOrMore(copy | blank) ) ) )
I found that this form insists on having a # in char 0. How could it be
changed to take a bunch of blanks before the first frame?
____# ?

I have tried:
animation = OneOrMore(
Optional ( blank | frame ) +
Optional( (tween + FollowedBy(fram e) ) | OneOrMore(copy | blank) )
)

And permutations thereof, but it goes into a black hole.

\d

Nov 5 '07 #6
Donn -

The exception you posted is from using an old version of pyparsing.
You can get the latest from SourceForge - if you download the Windows
binary install, please get the docs package too. It has a full doc
directory (generated with epydoc), plus example scripts for a variety
of applications. There is also an e-book available from O'Reilly that
just came out in early October.

To add an optional lead-in of one or more blanks,just change animation
from:

animation = OneOrMore((fram e + Optional(
(tween + FollowedBy(fram e)) |
OneOrMore(copy | blank) ) ) )

to:

animation = Optional(OneOrM ore(blank)) + \
OneOrMore((fram e + Optional(
(tween + FollowedBy(fram e)) |
OneOrMore(copy | blank) ) ) )

I modified your test, and there were no problems parsing strings with
and without the leading '_'s.

-- Paul

Nov 5 '07 #7
Here is a first cut at processing the parsed objects, given a list of
Property objects representing the frames at each '#':

class Property(object ):
def __init__(self,* *kwargs):
self.__dict__.u pdate(kwargs)

def copy(self):
return Property(**self .__dict__)

@staticmethod
def tween(prev,next ,n,i):
dt = 1.0/(n+1)
x = prev.x + (next.x-prev.x)*dt*(i+1 )
y = prev.y + (next.y-prev.y)*dt*(i+1 )
r = prev.r + (next.r-prev.r)*dt*(i+1 )
g = prev.g + (next.g-prev.g)*dt*(i+1 )
b = prev.b + (next.b-prev.b)*dt*(i+1 )
a = prev.a + (next.a-prev.a)*dt*(i+1 )
return Property(x=x,y= y,r=r,g=g,b=b,a =a)

@staticmethod
def makeBlank():
return Property(x=0,y= 0,r=0,g=0,b=0,a =0)

def __repr__(self):
return "Property(x=%(x ).3f, y=%(y).3f, r=%(r).3f, g=%(g).3f, b=
%(b).3f, a=%(a).3f)" % self.__dict__
def makeAllFrames(p ropFrameList, animationList):
ret = []
propFrameIndex = 0
for animIndex,animO bj in enumerate(anima tionList):
if isinstance(anim Obj, Prop):
ret.append( propFrameList[propFrameIndex] )
propFrameIndex += 1
if isinstance(anim Obj, Blank):
ret.append( Property.makeBl ank() )
if isinstance(anim Obj, CopyPrevious):
ret.append( ret[-1].copy() )
if isinstance(anim Obj, Tween):
# compute delta x,y,r,g,b,a between prev frame and next
frame
prev = propFrameList[propFrameIndex-1]
next = propFrameList[propFrameIndex]
numFrames = animObj.tweenLe ngth
print `prev`, `next`
tweens = [ Property.tween( prev,next,numFr ames,i) for i in
range(numFrames ) ]
ret += tweens
return ret

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)
propFrames = [ prop1, prop2, prop3, prop4 ]

allFramesList = makeAllFrames( propFrames, animation.parse String("#---
#--#===_#") )
from pprint import pprint
pprint( allFramesList )

Gives:
[Property(x=10.0 00, y=10.000, r=1.000, g=1.000, b=1.000, a=0.000),
Property(x=12.5 00, y=10.000, r=0.750, g=0.750, b=1.000, a=0.250),
Property(x=15.0 00, y=10.000, r=0.500, g=0.500, b=1.000, a=0.500),
Property(x=17.5 00, y=10.000, r=0.250, g=0.250, b=1.000, a=0.750),
Property(x=20.0 00, y=10.000, r=0.000, g=0.000, b=1.000, a=1.000),
Property(x=16.6 67, y=40.000, r=0.333, g=0.333, b=1.000, a=1.000),
Property(x=13.3 33, y=70.000, r=0.667, g=0.667, b=1.000, a=1.000),
Property(x=10.0 00, y=100.000, r=1.000, g=1.000, b=1.000, a=1.000),
Property(x=10.0 00, y=100.000, r=1.000, g=1.000, b=1.000, a=1.000),
Property(x=10.0 00, y=100.000, r=1.000, g=1.000, b=1.000, a=1.000),
Property(x=10.0 00, y=100.000, r=1.000, g=1.000, b=1.000, a=1.000),
Property(x=0.00 0, y=0.000, r=0.000, g=0.000, b=0.000, a=0.000),
Property(x=100. 000, y=200.000, r=1.000, g=1.000, b=1.000, a=1.000)]
-- Paul

Nov 5 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
9444
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $ Last-Modified: $Date: 2003/10/28 19:48:44 $ Author: A.M. Kuchling <amk@amk.ca> Status: Draft Type: Standards Track
0
1412
by: bj0011 | last post by:
I am trying to parse an XML file obtained from a public WebService (see below). This file does not seem to be loading into a .NET XmlDocument right (I think it is because of all of the <Item Name="" Type=""></Item> elements). I need to get out the data for the <Id/>, Name="Authors", Name="Title", and Name="PubDate" elements. Anyone have an idea how to do this (I have tried to get them out short of dumping the doc out to a string and parsing it...
3
1889
by: Zach | last post by:
Say I have a string which is of the format {A, B, C, D} for some variable number of objects. I want a regular expression that will put each of A, B, C, and D into its own separate capture group without any commas. If the string is {}
4
1910
by: Jim Langston | last post by:
In my program I am accepting messages over the network and parsing them. I find that the function that does this has gotten quite big, and so want to break the if else code into functions. I started thinking of how to do this, but came up with a number of ways and don't know what would be best, and fit into the C++ idoism. This is what I have now: if ( ThisPlayer.Character.GMLevel == 100 && ( StrMessage == "/debugserver" ||...
3
4386
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
9
1990
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an ArrayList? I have tried looping through the logfile using regex, if statements and flags to find the start and end of each message but I do not see a good time in this process to create a new instance of my Message object. While messing around with...
2
3066
by: nedelm | last post by:
My problem's with parsing. I have this (arbitrary, from a file) string, lets say: "Directory: /file{File:/filename(/size) }" I would like it to behave similar to LaTeX. I parse it, and then I write it out for diferent variables, like:
7
1974
by: Eric Wertman | last post by:
I have a set of files with this kind of content (it's dumped from WebSphere): ]
6
1390
by: Paul Wilson | last post by:
Hi all, I'd like to be able to do the following to a python source file programmatically: * Read in a source file * Add/Remove/Edit Classes, methods, functions * Add/Remove/Edit Decorators * List the Classes * List the imported modules * List the functions
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.