Commit ea592218 authored by Daniel Brown's avatar Daniel Brown
Browse files

tidying up using a decorator instead for freezing objects, adding to fsig too

parent 54f2c5bf
Pipeline #1774 failed with stage
in 13 seconds
...@@ -20,7 +20,7 @@ except ImportError: ...@@ -20,7 +20,7 @@ except ImportError:
import pykat.exceptions as pkex import pykat.exceptions as pkex
NoGUIException = pkex.BasePyKatException("No PyQt4 module was found so cannot open a GUI") NoGUIException = pkex.BasePyKatException("No PyQt4 module was found so cannot open a GUI")
import pykat.finesse as finesse import pykat.finesse as finesse
import pykat.components as components import pykat.components as components
import pykat.detectors as detectors import pykat.detectors as detectors
...@@ -30,4 +30,3 @@ from pykat.optics.gaussian_beams import BeamParam ...@@ -30,4 +30,3 @@ from pykat.optics.gaussian_beams import BeamParam
from pykat.plotting import init_pykat_plotting from pykat.plotting import init_pykat_plotting
...@@ -11,6 +11,7 @@ from __future__ import unicode_literals ...@@ -11,6 +11,7 @@ from __future__ import unicode_literals
import numpy import numpy
import warnings import warnings
import pykat
import pykat.external.six as six import pykat.external.six as six
import pykat.exceptions as pkex import pykat.exceptions as pkex
...@@ -23,8 +24,9 @@ from numpy import min, max ...@@ -23,8 +24,9 @@ from numpy import min, max
from pykat.param import Param, putter from pykat.param import Param, putter
from collections import namedtuple from collections import namedtuple
from pykat.optics.gaussian_beams import BeamParam from pykat.optics.gaussian_beams import BeamParam
from pykat.freeze import canFreeze
@canFreeze
class Command(object): class Command(object):
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
...@@ -54,15 +56,6 @@ class Command(object): ...@@ -54,15 +56,6 @@ class Command(object):
result._freeze() result._freeze()
return result return result
def _freeze(self): self.__dict__["____FROZEN____"] = True
def _unfreeze(self): self.__dict__["____FROZEN____"] = False
def __setattr__(self, name, value):
if "____FROZEN____" in self.__dict__ and self.__dict__["____FROZEN____"] and not hasattr(self, name):
warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2)
super(Command, self).__setattr__(name, value)
def getFinesseText(self): def getFinesseText(self):
""" Base class for individual finesse optical components """ """ Base class for individual finesse optical components """
raise NotImplementedError("This function is not implemented") raise NotImplementedError("This function is not implemented")
......
...@@ -37,6 +37,7 @@ from pykat.param import Param, AttrParam ...@@ -37,6 +37,7 @@ from pykat.param import Param, AttrParam
import weakref import weakref
import pykat.exceptions as pkex import pykat.exceptions as pkex
from copy import deepcopy from copy import deepcopy
from pykat.freeze import canFreeze
next_component_id = 1 next_component_id = 1
...@@ -86,7 +87,8 @@ class NodeGaussSetter(object): ...@@ -86,7 +87,8 @@ class NodeGaussSetter(object):
self.__node().setGauss(self.__comp(), self.qx, complex(value)) self.__node().setGauss(self.__comp(), self.qx, complex(value))
id_____pykat_class = 0 id_____pykat_class = 0
@canFreeze
class Component(object): class Component(object):
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
...@@ -121,15 +123,6 @@ class Component(object): ...@@ -121,15 +123,6 @@ class Component(object):
global next_component_id global next_component_id
self.__id = next_component_id self.__id = next_component_id
next_component_id += 1 next_component_id += 1
def _freeze(self): self.__dict__["____FROZEN____"] = True
def _unfreeze(self): self.__dict__["____FROZEN____"] = False
def __setattr__(self, name, value):
if "____FROZEN____" in self.__dict__ and self.__dict__["____FROZEN____"] and not hasattr(self, name):
warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2)
super(Component, self).__setattr__(name, value)
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
""" """
......
...@@ -26,6 +26,7 @@ import warnings ...@@ -26,6 +26,7 @@ import warnings
import copy import copy
from pykat import USE_GUI, NoGUIException from pykat import USE_GUI, NoGUIException
from pykat.freeze import canFreeze
if USE_GUI: if USE_GUI:
import pykat.gui.resources import pykat.gui.resources
...@@ -33,6 +34,7 @@ if USE_GUI: ...@@ -33,6 +34,7 @@ if USE_GUI:
id_____pykat_class = 0 id_____pykat_class = 0
@canFreeze
class BaseDetector(object) : class BaseDetector(object) :
""" """
This is a base class for all detectors. Classes Detector1 and Detector2 should be used directly. This is a base class for all detectors. Classes Detector1 and Detector2 should be used directly.
...@@ -100,15 +102,6 @@ class BaseDetector(object) : ...@@ -100,15 +102,6 @@ class BaseDetector(object) :
else: else:
raise pkex.BasePyKatException("Nodes should be a list or tuple of node names or a singular node name as a string.") raise pkex.BasePyKatException("Nodes should be a list or tuple of node names or a singular node name as a string.")
def _freeze(self): self.__dict__["____FROZEN____"] = True
def _unfreeze(self): self.__dict__["____FROZEN____"] = False
def __setattr__(self, name, value):
if "____FROZEN____" in self.__dict__ and self.__dict__["____FROZEN____"] and not hasattr(self, name):
warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2)
super(BaseDetector, self).__setattr__(name, value)
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
""" """
When deep copying a kat object we need to take into account When deep copying a kat object we need to take into account
......
...@@ -87,14 +87,13 @@ from pykat.commands import Command, xaxis ...@@ -87,14 +87,13 @@ from pykat.commands import Command, xaxis
from pykat.SIfloat import * from pykat.SIfloat import *
from pykat.param import Param, AttrParam from pykat.param import Param, AttrParam
from pykat.external import progressbar from pykat.external import progressbar
from pykat.freeze import canFreeze
import pykat.external.six as six import pykat.external.six as six
import pykat.exceptions as pkex import pykat.exceptions as pkex
from pykat import USE_GUI, HAS_OPTIVIS, NoGUIException from pykat import USE_GUI, HAS_OPTIVIS, NoGUIException
if HAS_OPTIVIS: if HAS_OPTIVIS:
from optivis.bench.labels import Label as optivis_label from optivis.bench.labels import Label as optivis_label
from optivis.geometry import Coordinates as optivis_coord from optivis.geometry import Coordinates as optivis_coord
...@@ -342,18 +341,24 @@ def GUILength(L): ...@@ -342,18 +341,24 @@ def GUILength(L):
Should scale the lengths in some way to handle km and mm for time being Should scale the lengths in some way to handle km and mm for time being
""" """
return L # * ( 40 * erfc(L/400.0) + 0.01) return L # * ( 40 * erfc(L/400.0) + 0.01)
@canFreeze
class KatRun(object): class KatRun(object):
def __init__(self): def __init__(self):
self._unfreeze()
self.runtime = None self.runtime = None
self.StartDateTime = datetime.datetime.now() self.StartDateTime = datetime.datetime.now()
self.x = None self.x = None
self.stdout = None
self.stderr = None
self.runDateTime = None
self.y = None self.y = None
self.xlabel = None self.xlabel = None
self.ylabels = None self.ylabels = None
self.katScript = None self.katScript = None
self.katVersion = None self.katVersion = None
self.yaxis = None self.yaxis = None
self._freeze()
def info(self): def info(self):
...@@ -635,9 +640,11 @@ class KatRun(object): ...@@ -635,9 +640,11 @@ class KatRun(object):
return out.squeeze() return out.squeeze()
else: else:
raise pkex.BasePyKatException("No output by the name '{0}' found in the output".format(str(value))) raise pkex.BasePyKatException("No output by the name '{0}' found in the output".format(str(value)))
@canFreeze
class KatRun2D(object): class KatRun2D(object):
def __init__(self): def __init__(self):
self._unfreeze()
self.runtime = None self.runtime = None
self.startDateTime = datetime.datetime.now() self.startDateTime = datetime.datetime.now()
self.x = None self.x = None
...@@ -650,6 +657,7 @@ class KatRun2D(object): ...@@ -650,6 +657,7 @@ class KatRun2D(object):
self.katVersion = None self.katVersion = None
self.stderr = None self.stderr = None
self.stdout = None self.stdout = None
self._freeze()
def saveKatRun(self, filename): def saveKatRun(self, filename):
with open(filename,'w') as outfile: with open(filename,'w') as outfile:
...@@ -670,10 +678,13 @@ class KatRun2D(object): ...@@ -670,10 +678,13 @@ class KatRun2D(object):
else: else:
raise pkex.BasePyKatException("No output by the name {0} found".format(str(value))) raise pkex.BasePyKatException("No output by the name {0} found".format(str(value)))
@canFreeze
class Signals(object): class Signals(object):
@canFreeze
class fsig(object): class fsig(object):
def __init__(self, param, name, amplitude, phase, signal): def __init__(self, param, name, amplitude, phase, signal):
self._unfreeze()
self._params = [] self._params = []
self.__target = param self.__target = param
self.__name = name self.__name = name
...@@ -681,6 +692,7 @@ class Signals(object): ...@@ -681,6 +692,7 @@ class Signals(object):
self.__phase = Param("phase", self, SIfloat(phase)) self.__phase = Param("phase", self, SIfloat(phase))
self.__removed = False self.__removed = False
self.__signal = signal self.__signal = signal
self._freeze()
# unfortunatenly the target names for fsig are not the same as the # unfortunatenly the target names for fsig are not the same as the
# various parameter names of the components, e.g. mirror xbeta is x # various parameter names of the components, e.g. mirror xbeta is x
...@@ -766,11 +778,13 @@ class Signals(object): ...@@ -766,11 +778,13 @@ class Signals(object):
self.__f.value = SIfloat(value) self.__f.value = SIfloat(value)
def __init__(self, kat): def __init__(self, kat):
self._unfreeze()
self._default_name = "fsignal" self._default_name = "fsignal"
self.targets = [] self.targets = []
self._params = [] self._params = []
self.__f = Param("f", self, None) self.__f = Param("f", self, None)
self._kat = kat self._kat = kat
self._freeze()
def _register_param(self, param): def _register_param(self, param):
self._params.append(param) self._params.append(param)
...@@ -823,6 +837,7 @@ Constant = namedtuple('Constant', 'name, value, usedBy') ...@@ -823,6 +837,7 @@ Constant = namedtuple('Constant', 'name, value, usedBy')
id___ = 0 id___ = 0
@canFreeze
class kat(object): class kat(object):
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
...@@ -891,15 +906,6 @@ class kat(object): ...@@ -891,15 +906,6 @@ class kat(object):
self._freeze() self._freeze()
def _freeze(self): self.__dict__["____FROZEN____"] = True
def _unfreeze(self): self.__dict__["____FROZEN____"] = False
def __setattr__(self, name, value):
if "____FROZEN____" in self.__dict__ and self.__dict__["____FROZEN____"] and not hasattr(self, name):
warnings.warn("'%s' does not have attribute called '%s'" % (self.__class__.__name__, name), stacklevel=2)
super(kat, self).__setattr__(name, value)
def deepcopy(self): def deepcopy(self):
return copy.deepcopy(self) return copy.deepcopy(self)
......
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