Skip to content

Instantly share code, notes, and snippets.

@vedgar
Created August 25, 2015 14:06
Show Gist options
  • Select an option

  • Save vedgar/d0124366bc1358f77b83 to your computer and use it in GitHub Desktop.

Select an option

Save vedgar/d0124366bc1358f77b83 to your computer and use it in GitHub Desktop.
import operator, types
def value(x):
return getattr(x, 'value', x)
class Cell(types.SimpleNamespace):
@property
def value(self):
return self.op(value(self.left), value(self.right)) if self.op else self.left
def __repr__(self):
return str(self.value)
namespace = locals()
for name in "add sub mul truediv".split():
op = getattr(operator, name)
def operation(self, other, op=op):
return Cell(left=self, op=op, right=other)
def roperation(self, other, op=op):
return Cell(right=self, op=op, left=other)
namespace['__{}__'.format(name)] = operation
namespace['__r{}__'.format(name)] = roperation
class Spreadsheet:
def __setattr__(self, attr, value):
if value == ...:
value = getattr(self, attr)
if not isinstance(value, Cell):
value = Cell(left=value, op=None, right=None)
try:
vars(getattr(self, attr)).update(vars(value))
except AttributeError:
super().__setattr__(attr, value)
print(attr, '=', value)
def webtest():
jtag = Spreadsheet()
jtag.lo = 640000
jtag.hi = 640000
jtag.period = jtag.lo + jtag.hi
jtag.idle = 500
jtag.lo2 = 1300
jtag.hi2 = 100
jtag.period2 = jtag.lo2 + jtag.hi2
jtag.idle2 = jtag.idle * jtag.period / jtag.period2
jtag.idle = 50
jtag.idle2 = ...
def othertest():
a = Spreadsheet()
a.b = 2
a.c = 3 + a.b
a.b = 8
a.c = ...
a.d = a.b * 2 + 5
a.c = 17
a.d = ...
a.b = 12
a.d = ...
if __name__ == '__main__':
webtest()
print('-' * 20)
othertest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment