extracting variables accessed and written from function / rule-based function calls
python at mrabarnett.plus.com
Tue Nov 2 00:23:47 CET 2010
On 01/11/2010 22:44, Daniel wrote:
> I have a class with some members that depend on others. Initially most
> of them are None.
> For each one there is a function to calculate it as soon as some other
> dependencies become available.
> In the end, all values can be computed by the right sequence of
> function applications.
> class A:
> def __init__(self):
> self.a = None
> self.b = None
> self.c = none
> def f1(self): # compute a given b and c
> self.a = 2*self.b + math.sin(self.c)
> Now I am looking for a way to call these functions only when the
> preconditions are met (in this case b and c are not None) and when the
> result is needed (a is None).
> I could wrap each function in a class like this:
> class rule_a_from_b_and_c:
> def __init__(self, parent): self.parent = parent
> def pre(self): return self.parent.b is not None and self.parent.c
> is not None
> def needed(self): return self.parent.a is None
> def rule(self): self.parent.a = 2*self.parent.b +
> This way I have to replicate the inputs an the output of the function
> for each rule, which is a lot of work.
> Is there a way to access the values read by the function and written
> to by the function f?
> Like values _read(f) returns (self.b, self.c), values_written(f)
> returns (self.a,)
You might be interested by the story of how AstraZeneca tackled that
kind of problem in PyDrone: http://www.python.org/about/success/astra/
More information about the Python-list