Skip to content
Snippets Groups Projects
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
Branches
No related tags found
No related merge requests found
...@@ -2,14 +2,13 @@ from pykat import finesse ...@@ -2,14 +2,13 @@ from pykat import finesse
from pykat.commands import xaxis from pykat.commands import xaxis
import pylab as pl import pylab as pl
import numpy as np import numpy as np
import math
code = """ code = """
l l1 1 0 0 n1 l l1 1 0 0 n1
s s1 10 1 n1 n2 s s1 10 1 n1 n2
m m1 1 0 0 n2 n3 m m1 1 0 0 n2 n3
#gauss g1 m1 n2 1e-3 0
pd refl n2 pd refl n2
xaxis m1 r_ap lin 0.1e-3 2e-3 10 xaxis m1 r_ap lin 0.1e-3 2e-3 10
...@@ -20,7 +19,7 @@ kat.parseCommands(code) ...@@ -20,7 +19,7 @@ kat.parseCommands(code)
maxtem = np.arange(0, 2, 2) 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: for tem in maxtem:
print "Calculating maxtem ", tem, "..." print "Calculating maxtem ", tem, "..."
......
...@@ -17,38 +17,36 @@ from pykat.SIfloat import * ...@@ -17,38 +17,36 @@ from pykat.SIfloat import *
next_component_id = 1 next_component_id = 1
class NodeGaussSetter: class NodeGaussSetter(object):
def __init__(self, component, node): 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")
self.__comp = component self.__comp = component
self.__node = node self.__node = node
@property @property
def node(self): return self.__node def node(self):
return self.__node
@property @property
def q(self): return self.__node.qx def q(self):
return self.__node.qx
@q.setter @q.setter
def q(self, value): def q(self, value):
node.setGauss(self.__comp, value) self.__node.setGauss(self.__comp, value)
@property @property
def qx(self): return self.__node.qx def qx(self):
return self.__node.qx
@qx.setter @qx.setter
def qx(self, value): def qx(self, value):
node.setGauss(self.__comp, value) self.__node.setGauss(self.__comp, value)
@property @property
def qy(self): return self.__node.qy def qy(self):
return self.__node.qy
@qy.setter @qy.setter
def qy(self, value): def qy(self, value):
node.setGauss(self.__comp, self.qx, value) self.__node.setGauss(self.__comp, self.qx, value)
class Component(object) : class Component(object) :
def __init__(self, name): def __init__(self, name):
...@@ -63,6 +61,12 @@ class Component(object) : ...@@ -63,6 +61,12 @@ class Component(object) :
self.__id = next_component_id self.__id = next_component_id
next_component_id += 1 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): def _on_kat_add(self, kat):
""" """
Called when this component has been added to a kat object. Called when this component has been added to a kat object.
...@@ -106,7 +110,7 @@ class Component(object) : ...@@ -106,7 +110,7 @@ class Component(object) :
name = ns.node.name name = ns.node.name
fget = lambda self: self.__get_node_setter(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) setattr(self, '__nodesetter_' + name, ns)
def __get_node_setter(self, name): def __get_node_setter(self, name):
......
...@@ -108,6 +108,9 @@ class kat(object): ...@@ -108,6 +108,9 @@ class kat(object):
if kat_file != None: if kat_file != None:
self.loadKatFile(kat_file) self.loadKatFile(kat_file)
cls = type(self)
self.__class__ = type(cls.__name__, (cls,), {})
@property @property
def maxtem(self): return self.__maxtem def maxtem(self): return self.__maxtem
@maxtem.setter @maxtem.setter
...@@ -202,7 +205,7 @@ class kat(object): ...@@ -202,7 +205,7 @@ class kat(object):
obj = line obj = line
# manually add the line to the block contents # manually add the line to the block contents
self.__blocks[self.__currentTag].contents.append(line) self.__blocks[self.__currentTag].contents.append(line)
print obj
if not isinstance(obj, str): if not isinstance(obj, str):
self.add(obj) self.add(obj)
......
...@@ -19,6 +19,9 @@ class NodeNetwork(object): ...@@ -19,6 +19,9 @@ class NodeNetwork(object):
self.__componentCallback = {} self.__componentCallback = {}
self.__node_id = 1 self.__node_id = 1
cls = type(self)
self.__class__ = type(cls.__name__, (cls,), {})
def registerComponentNodes(self, comp, node_names, change_callback): def registerComponentNodes(self, comp, node_names, change_callback):
""" """
For a given component we create some nodes or get existing ones and For a given component we create some nodes or get existing ones and
...@@ -141,7 +144,7 @@ class NodeNetwork(object): ...@@ -141,7 +144,7 @@ class NodeNetwork(object):
name = node.name name = node.name
fget = lambda self: self.__get_node_attr(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) setattr(self, '__node_' + name, node)
def __remove_node_attr(self, node): def __remove_node_attr(self, node):
...@@ -149,7 +152,7 @@ class NodeNetwork(object): ...@@ -149,7 +152,7 @@ class NodeNetwork(object):
raise exceptions.ValueError("Argument is not of type Node") raise exceptions.ValueError("Argument is not of type Node")
name = node.name name = node.name
detattr(self, '__node_' + name) detattr(self.__class__, '__node_' + name)
delattr(self, name) delattr(self, name)
def __get_node_attr(self, name): def __get_node_attr(self, name):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment