title: Lazy data-flow (spreadsheet like) properties with dependencies in Python
body: <p>My problem is the following: I have some python classes that have properties that are derived from other properties; and those should be cached once they are calculated, and the cached results should be invalidated each time the base properties are changed.</p>
  
  <p>I could do it manually, but it seems quite difficult to maintain if the number of properties grows. So I would like to have something like <em>Makefile</em> rules inside my objects to automatically keep track of what needs to be recalculated.</p>
  
  <p>The desired syntax and behaviour should be something like that:</p>
  
  <pre><code># this does dirty magic, like generating the reverse dependency graph,
  # and preparing the setters that invalidate the cached values
  @dataflow_class
  class Test(object):
  
      def calc_a(self):
          return self.b + self.c
  
      def calc_c(self):
          return self.d * 2
  
      a = managed_property(calculate=calc_a, depends_on=('b', 'c'))
      b = managed_property(default=0)
      c = managed_property(calculate=calc_c, depends_on=('d',))
      d = managed_property(default=0)
  
  
  t = Test()
  
  print t.a
  # a has not been initialized, so it calls calc_a
  # gets b value
  # c has not been initialized, so it calls calc_c
  # c value is calculated and stored in t.__c
  # a value is calculated and stored in t.__a
  
  t.b = 1
  # invalidates the calculated value stored in self.__a
  
  print t.a
  # a has been invalidated, so it calls calc_a
  # gets b value
  # gets c value, from t.__c
  # a value is calculated and stored in t.__a
  
  print t.a
  # gets value from t.__a
  
  t.d = 2
  # invalidates the calculated values stored in t.__a and t.__c
  </code></pre>
  
  <p>So, is there something like this already available or should I start implementing my own? In the second case, suggestions are welcome :-)</p>