diff --git a/bin/test_aperture.py b/bin/test_aperture.py index fee0995387dc26c004b7bb5c2053bb87296fac52..65efee1613c6246ee60ebab5b68de16d77bb520d 100644 --- a/bin/test_aperture.py +++ b/bin/test_aperture.py @@ -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, "..." diff --git a/pykat/components.py b/pykat/components.py index 6f89c3fd707429c624a7f13b3c2a1733e88e9112..93481203502a644e628fbd37cb202172d3ab4566 100644 --- a/pykat/components.py +++ b/pykat/components.py @@ -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): diff --git a/pykat/finesse.py b/pykat/finesse.py index 1df931cb7eeb949af9d4bebb85d00cff737e6d92..0582e5f97fce9eb62cda34847b24f5d8f4b26def 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -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) diff --git a/pykat/node_network.py b/pykat/node_network.py index 14d52e9123885756a84560ee0eb029adf4362507..f4067debe0e4f33eed8530e912be9c66b4053887 100644 --- a/pykat/node_network.py +++ b/pykat/node_network.py @@ -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):