extracting variables accessed and written from function / rule-basedfunction calls

Discussion in 'Python' started by Daniel, Nov 1, 2010.

  1. Daniel

    Daniel Guest

    Hello,


    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 +
    math.sin(self.parent.c)

    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,)


    Dan
     
    Daniel, Nov 1, 2010
    #1
    1. Advertising

  2. Daniel

    MRAB Guest

    Re: extracting variables accessed and written from function /rule-based function calls

    On 01/11/2010 22:44, Daniel wrote:
    > Hello,
    >
    >
    > 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 +
    > math.sin(self.parent.c)
    >
    > 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/
     
    MRAB, Nov 1, 2010
    #2
    1. Advertising

  3. Daniel

    James Mills Guest

    Re: extracting variables accessed and written from function /rule-based function calls

    On Tue, Nov 2, 2010 at 9:23 AM, MRAB <> wrote:
    > You might be interested by the story of how AstraZeneca tackled that
    > kind of problem in PyDrone: http://www.python.org/about/success/astra/


    This might be a good use-case (if I'm reading the post correctly) for
    "Traits" (1)

    cheers
    James

    1. http://pypi.python.org/pypi/Traits/3.5.0

    --
    -- James Mills
    --
    -- "Problems are solved by method"
     
    James Mills, Nov 1, 2010
    #3
  4. Daniel

    MRAB Guest

    Re: extracting variables accessed and written from function/ rule-based function calls

    On 01/11/2010 23:38, James Mills wrote:
    > On Tue, Nov 2, 2010 at 9:23 AM, MRAB<> wrote:
    >> You might be interested by the story of how AstraZeneca tackled that
    >> kind of problem in PyDrone: http://www.python.org/about/success/astra/

    >
    > This might be a good use-case (if I'm reading the post correctly) for
    > "Traits" (1)
    >
    > cheers
    > James
    >
    > 1. http://pypi.python.org/pypi/Traits/3.5.0
    >

    There's a difference between "Traits" and what PyDrone does.

    In Traits, when a value changes, it notifies other values which depend
    on it.

    In PyDrone, there are constants, some of which depend on other
    constants and need to be calculated. When a value is first requested it
    is calculated, which might trigger the calculation of other values if
    it's the first time that they've been requested, so no calculation
    (work) is done until it's needed.
     
    MRAB, Nov 2, 2010
    #4
  5. Daniel

    Daniel Guest

    Re: extracting variables accessed and written from function /rule-based function calls


    > >> You might be interested by the story of how AstraZeneca tackled that
    > >> kind of problem in PyDrone:http://www.python.org/about/success/astra/

    that is interesting! So it seems they store the values in a
    dictionary.
    For each value they associate a function that gets called when the
    value is not available.
    This function uses the same dictionary to access the other values,
    which might trigger more evaluations.
    I like this approach. Unfortunately in my case it would not work,
    because I might have more than one rule associated with each function,
    depending on which values are available. If the wrong rule gets called
    which needs to call other (wrong) rules I could end up in a loop, even
    though everything
    could be computed in principle.
    lets say a follows from b or c (r1, r2) , b follows from a or c (r3,
    r4) and c is given. Using this approach I would end up in a loop of
    rules r1 and r3 to compute a.
    I don't see how this approach could be made to work the other way
    round, each value could have a rule attached that, given the
    availability of other dependencies computes the
    dependent values. That seems complicated, because one rule would need
    to be associated with several values.

    One possibility I thought of was to write the rules as python
    functions and use the ast module to extract all the variables that are
    being referenced. In principle this should be
    possible. Is there any library around that does that?


    Dan
     
    Daniel, Nov 2, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page