Python 中具有依赖关系的惰性数据流(类似电子表格)属性
我的问题如下:我有一些 python 类,它们具有从其他属性派生的属性;一旦计算完毕,就应该缓存它们,并且每次更改基本属性时,缓存的结果都应该失效。
我可以手动完成,但如果属性数量增加,维护似乎相当困难。因此,我希望在我的对象中添加类似 Makefile 规则的内容,以自动跟踪需要重新计算的内容。
所需的语法和行为应该是这样的:
# 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
那么,是否有类似的东西已经可用,或者我应该开始实现自己的?对于第二种情况,欢迎提出建议:-)
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.
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 :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里,这应该可以解决问题。
描述符机制(语言通过它实现“属性”)是
足够你想要的了。
如果下面的代码在某些极端情况下不起作用,请写信给我。
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.
,然后使用一个!你可以考虑这样的模型:
我们公司的硬件测试团队使用这样的框架密集的探索性测试:
(大)这种方法的缺点是您必须放弃 python
import
关键字,因为它会创建隐式(且未跟踪)依赖项(对此有解决方法)。then use one! You may consider this model:
The hardware test team in our company use such a framework for intensive exploratory tests:
the (big) downside to this method is that you have to give up python
import
keyword because it creates an implicit (and untracked) dependency (there are workarounds for this).这些是我用来验证其行为的单元测试。
These are the unit tests I used to verify its behavior.