Commit 7141968c authored by Daniel Brown's avatar Daniel Brown
Browse files

objects that have properties dynamically added to themselves need to declare...

objects that have properties dynamically added to themselves need to declare themselves as new classes, with a base class of the original. Otherwise when we add properties we add them to all instances.
parent 61d5c32d
......@@ -2,14 +2,13 @@ from pykat import finesse
from pykat.commands import xaxis
import pylab as pl
import numpy as np
import math
code = """
l l1 1 0 0 n1
s s1 10 1 n1 n2
m m1 1 0 0 n2 n3
#gauss g1 m1 n2 1e-3 0
pd refl n2
xaxis m1 r_ap lin 0.1e-3 2e-3 10
......@@ -20,7 +19,7 @@ kat.parseCommands(code)
maxtem = np.arange(0, 2, 2)
kat.nodes.n2.gauss_w0_z(1e-3, 0)
kat.m1.n2.q = 1j*(math.pi * 1e-3)**2/1064e-9
for tem in maxtem:
print "Calculating maxtem ", tem, "..."
......
......@@ -17,38 +17,36 @@ from pykat.SIfloat import *
next_component_id = 1
class NodeGaussSetter:
def __init__(self, component, node):
if not isinstance(component, Component):
raise pkex.BasePyKatException("Value passed is not a Component")
if not isinstance(node, pykat.node_network.Node):
raise pkex.BasePyKatException("Value passed is not a Node")
class NodeGaussSetter(object):
def __init__(self, component, node):
self.__comp = component
self.__node = node
@property
def node(self): return self.__node
def node(self):
return self.__node
@property
def q(self): return self.__node.qx
def q(self):
return self.__node.qx
@q.setter
def q(self, value):
node.setGauss(self.__comp, value)
self.__node.setGauss(self.__comp, value)
@property
def qx(self): return self.__node.qx
def qx(self):
return self.__node.qx
@qx.setter
def qx(self, value):
node.setGauss(self.__comp, value)
self.__node.setGauss(self.__comp, value)
@property
def qy(self): return self.__node.qy
def qy(self):
return self.__node.qy
@qy.setter
def qy(self, value):
node.setGauss(self.__comp, self.qx, value)
self.__node.setGauss(self.__comp, self.qx, value)
class Component(object) :
def __init__(self, name):
......@@ -63,6 +61,12 @@ class Component(object) :
self.__id = next_component_id
next_component_id += 1
# This creates an instance specific class for the component
# this enables us to add properties to instances rather than
# all classes
cls = type(self)
self.__class__ = type(cls.__name__, (cls,), {})
def _on_kat_add(self, kat):
"""
Called when this component has been added to a kat object.
......@@ -106,7 +110,7 @@ class Component(object) :
name = ns.node.name
fget = lambda self: self.__get_node_setter(name)
setattr(self, name, property(fget))
setattr(self.__class__, name, property(fget))
setattr(self, '__nodesetter_' + name, ns)
def __get_node_setter(self, name):
......
......@@ -107,7 +107,10 @@ class kat(object):
if kat_file != None:
self.loadKatFile(kat_file)
cls = type(self)
self.__class__ = type(cls.__name__, (cls,), {})
@property
def maxtem(self): return self.__maxtem
@maxtem.setter
......@@ -202,7 +205,7 @@ class kat(object):
obj = line
# manually add the line to the block contents
self.__blocks[self.__currentTag].contents.append(line)
print obj
if not isinstance(obj, str):
self.add(obj)
......
......@@ -19,6 +19,9 @@ class NodeNetwork(object):
self.__componentCallback = {}
self.__node_id = 1
cls = type(self)
self.__class__ = type(cls.__name__, (cls,), {})
def registerComponentNodes(self, comp, node_names, change_callback):
"""
For a given component we create some nodes or get existing ones and
......@@ -141,7 +144,7 @@ class NodeNetwork(object):
name = node.name
fget = lambda self: self.__get_node_attr(name)
setattr(self, name, property(fget))
setattr(self.__class__, name, property(fget))
setattr(self, '__node_' + name, node)
def __remove_node_attr(self, node):
......@@ -149,7 +152,7 @@ class NodeNetwork(object):
raise exceptions.ValueError("Argument is not of type Node")
name = node.name
detattr(self, '__node_' + name)
detattr(self.__class__, '__node_' + name)
delattr(self, name)
def __get_node_attr(self, name):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment