<small id='93sTk'></small><noframes id='93sTk'>

  • <legend id='93sTk'><style id='93sTk'><dir id='93sTk'><q id='93sTk'></q></dir></style></legend>

    1. <tfoot id='93sTk'></tfoot>
      <i id='93sTk'><tr id='93sTk'><dt id='93sTk'><q id='93sTk'><span id='93sTk'><b id='93sTk'><form id='93sTk'><ins id='93sTk'></ins><ul id='93sTk'></ul><sub id='93sTk'></sub></form><legend id='93sTk'></legend><bdo id='93sTk'><pre id='93sTk'><center id='93sTk'></center></pre></bdo></b><th id='93sTk'></th></span></q></dt></tr></i><div id='93sTk'><tfoot id='93sTk'></tfoot><dl id='93sTk'><fieldset id='93sTk'></fieldset></dl></div>
        <bdo id='93sTk'></bdo><ul id='93sTk'></ul>

        在 Python 中具有依赖关系的惰性数据流(类似电子表格)属性

        时间:2023-07-06
        • <small id='k9b4M'></small><noframes id='k9b4M'>

        • <legend id='k9b4M'><style id='k9b4M'><dir id='k9b4M'><q id='k9b4M'></q></dir></style></legend>
            <bdo id='k9b4M'></bdo><ul id='k9b4M'></ul>

              <tbody id='k9b4M'></tbody>
            <i id='k9b4M'><tr id='k9b4M'><dt id='k9b4M'><q id='k9b4M'><span id='k9b4M'><b id='k9b4M'><form id='k9b4M'><ins id='k9b4M'></ins><ul id='k9b4M'></ul><sub id='k9b4M'></sub></form><legend id='k9b4M'></legend><bdo id='k9b4M'><pre id='k9b4M'><center id='k9b4M'></center></pre></bdo></b><th id='k9b4M'></th></span></q></dt></tr></i><div id='k9b4M'><tfoot id='k9b4M'></tfoot><dl id='k9b4M'><fieldset id='k9b4M'></fieldset></dl></div>

                1. <tfoot id='k9b4M'></tfoot>
                  本文介绍了在 Python 中具有依赖关系的惰性数据流(类似电子表格)属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的问题如下:我有一些 python 类具有从其他属性派生的属性;并且这些应该在计算后被缓存,并且每次更改基本属性时都应该使缓存的结果无效.

                  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.

                  我可以手动完成,但如果属性数量增加,似乎很难维护.所以我想在我的对象中加入类似 Makefile 的规则,以自动跟踪需要重新计算的内容.

                  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 Makefile rules inside my objects to automatically keep track of what needs to be recalculated.

                  所需的语法和行为应该是这样的:

                  The desired syntax and behaviour should be something like that:

                  # 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
                  

                  那么,有没有类似的东西已经可用,或者我应该开始自己实现吗?在第二种情况下,欢迎提出建议:-)

                  So, is there something like this already available or should I start implementing my own? In the second case, suggestions are welcome :-)

                  推荐答案

                  在这里,这应该可以解决问题.描述符机制(语言通过它实现属性")是足够你想要的了.

                  Here, this should do the trick. The descriptor mechanism (through which the language implements "property") is more than enough for what you want.

                  如果下面的代码在某些极端情况下不起作用,请写信给我.

                  If the code bellow does not work in some corner cases, just write me.

                  class DependentProperty(object):
                      def __init__(self, calculate=None, default=None, depends_on=()):
                          # "name" and "dependence_tree" properties are attributes
                          # set up by the metaclass of the owner class
                          if calculate:
                              self.calculate = calculate
                          else:
                              self.default = default
                          self.depends_on = set(depends_on)
                  
                      def __get__(self, instance, owner):
                          if hasattr(self, "default"):
                              return self.default
                          if not hasattr(instance, "_" + self.name):
                              setattr(instance, "_" + self.name,
                                  self.calculate(instance, getattr(instance, "_" + self.name + "_last_value")))
                          return getattr(instance, "_" + self.name)
                  
                      def __set__(self, instance, value):
                          setattr(instance, "_" + self.name + "_last_value", value)
                          setattr(instance, "_" + self.name, self.calculate(instance, value))
                          for attr in self.dependence_tree[self.name]:
                              delattr(instance, attr)
                  
                      def __delete__(self, instance):
                          try:
                              delattr(instance, "_" + self.name)
                          except AttributeError:
                              pass
                  
                  
                  def assemble_tree(name,  dict_, all_deps = None):
                      if all_deps is None:
                          all_deps = set()
                      for dependance in dict_[name].depends_on:
                          all_deps.add(dependance)
                          assemble_tree(dependance, dict_, all_deps)
                      return all_deps
                  
                  def invert_tree(tree):
                      new_tree = {}
                      for key, val in tree.items():
                          for dependence in val:
                              if dependence not in new_tree:
                                  new_tree[dependence] = set()
                              new_tree[dependence].add(key)
                      return new_tree
                  
                  class DependenceMeta(type):
                      def __new__(cls, name, bases, dict_):
                          dependence_tree = {}
                          properties = []
                          for key, val in dict_.items():
                              if not isinstance(val, DependentProperty):
                                  continue
                              val.name = key
                              val.dependence_tree = dependence_tree
                              dependence_tree[key] = set()
                              properties.append(val)
                          inverted_tree = {}
                          for property in properties:
                              inverted_tree[property.name] = assemble_tree(property.name, dict_)
                          dependence_tree.update(invert_tree(inverted_tree))
                          return type.__new__(cls, name, bases, dict_)
                  
                  
                  if __name__ == "__main__":
                      # Example and visual test:
                  
                      class Bla:
                          __metaclass__ = DependenceMeta
                  
                          def calc_b(self, x):
                              print "Calculating b"
                              return x + self.a
                  
                          def calc_c(self, x):
                              print "Calculating c"
                              return x + self.b
                  
                          a = DependentProperty(default=10)    
                          b = DependentProperty(depends_on=("a",), calculate=calc_b)
                          c = DependentProperty(depends_on=("b",), calculate=calc_c)
                  
                  
                  
                  
                      bla = Bla()
                      bla.b = 5
                      bla.c = 10
                  
                      print bla.a, bla.b, bla.c
                      bla.b = 10
                      print bla.b
                      print bla.c
                  

                  这篇关于在 Python 中具有依赖关系的惰性数据流(类似电子表格)属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:具有冲突依赖项的 Python 包 下一篇:相当于 `pip` 的 `package.json' 和 `package-lock.json`

                  相关文章

                  <small id='1wosw'></small><noframes id='1wosw'>

                  <tfoot id='1wosw'></tfoot>
                    <bdo id='1wosw'></bdo><ul id='1wosw'></ul>
                    <legend id='1wosw'><style id='1wosw'><dir id='1wosw'><q id='1wosw'></q></dir></style></legend>

                    <i id='1wosw'><tr id='1wosw'><dt id='1wosw'><q id='1wosw'><span id='1wosw'><b id='1wosw'><form id='1wosw'><ins id='1wosw'></ins><ul id='1wosw'></ul><sub id='1wosw'></sub></form><legend id='1wosw'></legend><bdo id='1wosw'><pre id='1wosw'><center id='1wosw'></center></pre></bdo></b><th id='1wosw'></th></span></q></dt></tr></i><div id='1wosw'><tfoot id='1wosw'></tfoot><dl id='1wosw'><fieldset id='1wosw'></fieldset></dl></div>