self

Jeff Epler jepler at unpythonic.net
Wed Jun 5 08:34:40 EDT 2002


Nicola,
If you are going to always store the equation, never the value, what
happens when the user does
    c.y = 37
    c.y = 'y+1'
?  As far as I understand it, this would have to put the parser into an
infinite loop when
    c.y
is later referenced (since it must be determined by taking the value of
y and adding 1, but the value of y must be determined by ...)

You'd need to detect this problem somehow, and presumably not let
    c.y = 'y+1'
execute at all, but (especially if you allow dotted references like q.y)
this will be a bit of a pain to detect... regular expressions *might* be
up to the task (for a particular expression), but quite possibly not.
Then you're left with detecting stuff like
    c.x = "y"
    c.y = "x+1"
where it's a two-step process.  You have to determine that x uses
the value of y, and that y uses the value of x, to notice the cycle and
prevent the second assignment.

You're getting closer to the point where using the parser module to
parse the string as though it were an expression using
    >>> tree = parser.expr("x+1").totuple()
    >>> pprint.pprint(tree)
    (258,
     (313,
      (292,
       (293,
	(294,
	 (295,
	  (297,
	   (298,
	    (299,
	     (300,
	      (301,
	       (302, (303, (304, (305, (1, 'x'))))),
	       (14, '+'),
	       (302, (303, (304, (305, (2, '1'))))))))))))))),
     (4, ''),
     (0, ''))
you can use this form to both find the dependencies of the expression 
and to interpret the expression.  (btw I think the parser module doesn't
exist in jython)

Well, good luck with your project -- and don't spring any more
requirements on us, it's getting harder each time to solve your problem.

Jeff





More information about the Python-list mailing list