Ric*_*een 5 python metaprogramming deferred-execution
这是一个由两部分组成的Python问题。第一部分是关于美学,第二部分是关于实施。到目前为止我所使用的代码示例附在底部。
我正在尝试设计一个 python 模块/类,它使我能够进行惰性评估/延迟计算。设置这些计算后,我希望能够准确确定答案为何如此,并修改函数的输入并重新计算输出,而无需重新定义中间变量。
本质上,我希望能够做尽可能接近的事情:
>>> a=3
>>> b=5
>>> c=a+b
>>> c
8
>>> a = 6
>>> c
11
Run Code Online (Sandbox Code Playgroud)
和类似的东西
>>> c.calculation
'a [ 6 ] + b [ 5 ] = 11'
Run Code Online (Sandbox Code Playgroud)
现在我对循环依赖关系还没有那么大惊小怪,我最接近上面的是:
a = constant(2)
b = constant(3)
c = a + b
d = a * b
Run Code Online (Sandbox Code Playgroud)
这给了我这样的东西:
c is 5 [ 2 <built-in function __add__> 3 ]
d is 6 [ 2 <built-in function __mul__> 3 ]
Run Code Online (Sandbox Code Playgroud)
这并不完美,但已经很接近了。然后我更改 a 和 b 的值以重新计算 c,d 的值
我知道我不能使用赋值来修改 a,b 的值而不完全覆盖它,所以像 a = 2, b = 3 这样的东西就可以了。
接下来,如果您查看下面的代码,您会发现我刚刚实现了 add 和 mul 的运算符,但我想为(最好)所有内置运算符重现此操作。
最终,我希望能够使用这个库进行一些矩阵乘积/求和。
我知道这会占用大量内存 - 但对我来说,获得“这个数字最终如何成为 x”的解释对我来说比内存使用/CPU 周期更重要。
import operator
class operable(object):
def __add__(self,other):
if isinstance(other,operable):
d = calculation()
d.operator = operator.__add__
d.operands = [self,other]
return d
def __mul__(self,other):
if isinstance(other,operable):
d = calculation()
d.operator = operator.__mul__
d.operands = [self,other]
return d
class calculation(operable):
def __init__(self):
self.operands = []
self.operator = None
@property
def value(self):
return reduce(self.operator, [x.value for x in self.operands])
@property
def calculation(self):
return (" %s " % str(self.operator)).join([x.__repr__() for x in self.operands])
def __repr__(self):
return "%d [ %s ] " % ( self.value, self.calculation )
class constant(operable):
def __init__(self, x = 0):
self._value = x
def __repr__(self):
return "%d" %( self.value)
@property
def value(self):
return self._value
@value.setter
def value(self,new_val):
self._value = new_val
def test_1():
a = constant(2)
b = constant(3)
c = a + b
d = a * b
z = a + b + c + d
print "c is",c
print "d is",d
print "z is ",z
b.value = 5
print "c is now",c
print "d is now",d
print "z is now ",z
if __name__ == "__main__":
test_1()
Run Code Online (Sandbox Code Playgroud)
我非常喜欢 Raymond Hettinger 解决类似问题的方法:
class SpreadSheet:
_cells = {}
tools = {}
def __setitem__(self, key, formula):
self._cells[key] = formula
def getformula(self, key):
return self._cells[key]
def __getitem__(self, key ):
return eval(self._cells[key], SpreadSheet.tools, self)
>>> from math import sin, pi
>>> SpreadSheet.tools.update(sin=sin, pi=pi, len=len)
>>> ss = SpreadSheet()
>>> ss['a1'] = '5'
>>> ss['a2'] = 'a1*6'
>>> ss['a3'] = 'a2*7'
>>> ss['a3']
210
>>> ss['b1'] = 'sin(pi/4)'
>>> ss['b1']
0.70710678118654746
>>> ss.getformula('b1')
'sin(pi/4)'
Run Code Online (Sandbox Code Playgroud)
摘自: http: //code.activestate.com/recipes/355045-spreadsheet/