param.py 4.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from pykat.SIfloat import SIfloat
import abc
import pykat.exceptions as pkex

class putable(object):
    """
    Objects that inherit this should be able to have something `put` to it.
    Essentially this means you could write Finesse commands like
    
    put this parameter value
    """
    __metaclass__ = abc.ABCMeta
    
    def __init__(self, component_name, parameter_name, isPutable=True):
        self._parameter_name = parameter_name
        self._component_name = component_name
        self._putter = None
        self._isPutable  = isPutable
    
    @property
    def isPutable(self): return self._isPutable
    
    def put(self, var):
24
    
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        if not isinstance(var, putter):
            raise pkex.BasePyKatException("var was not something that can be `put` as a value")
        
        if self._putter != None:
            self._putter.put_count -= 1
        
        self._putter = var
        self._putter.put_count += 1
        
    def _getPutFinesseText(self):
        rtn = []
        # if something is being put to this 
        if self._putter != None:
            rtn.append("put {comp} {param} ${value}".format(comp=self._component_name, param=self._parameter_name, value=self._putter.put_name()))
        
        return rtn
            
class putter(object):
    """
    If an object can be put to something that is putable it should inherit this
    object.
    """
    
    def __init__(self, put_name, isPutter=True):
        self._put_name = put_name
        self.put_count = 0
        self._isPutter = isPutter
    
    @property
    def isPutter(self): return self._isPutter
    
    def put_name(self): return self._put_name
    
        
class Param(putable, putter):

    def __init__(self, name, owner, value, isPutable=True, isPutter=True, isTunable=True, var_name=None):
        self._name = name
        self._owner = owner
        self._value = value
        self._isPutter = isPutter
        self._isTunable = isTunable
        self._owner._register_param(self)
        
        if isPutter:
            if var_name == None:
                var_name = "var_{0}_{1}".format(owner.name, name)
                
            putter.__init__(self, var_name, isPutter)
            
        if isPutable:
            putable.__init__(self, owner.name, name, isPutable)
        
    @property
    def name(self): return self._name
    
    @property
    def isTuneable(self): return self._isTunable
    
    @property
    def value(self): return self._value
    @value.setter
    def value(self, value):
        self._value = value
    
    def __str__(self): return str(self.value)
    def __float__(self): return self.value
        
    def getFinesseText(self):
        rtn = []
        
        if self.isPutable: rtn.extend(self._getPutFinesseText())
        
        # if this parameter is being put somewhere then we need to
        # set it as a variable
        if self.isPutter and self.put_count > 0:
            rtn.append("set {put_name} {comp} {param}".format(put_name=self.put_name(), comp=self._owner.name, param=self.name))
        
        return rtn
        
    def __mul__(self, a):
106
        return self.value * a
107
108
    
    def __imul__(self, a):
109
        return self.value * (a)
110
111
112
113
        
    __rmul__ = __mul__
    
    def __add__(self, a):
114
        return self.value + (a)
115
116
    
    def __iadd__(self, a):
117
        return self.value + (a)
118
119
120
121
        
    __radd__ = __add__
    
    def __sub__(self, a):
122
        return self.value - (a)
123
124
    
    def __isub__(self, a):
125
        return self.value - (a)
126
127
128
129
        
    __rsub__ = __sub__
    
    def __div__(self, a):
130
        return self.value / (a)
131
132
133
134
135
136
137
138
139
140
141
    
    def __idiv__(self, a):
        return self.value / complex(a)
        
    def __pow__(self, q):
        return  self.value**q

    def __neg__(self):
        return -self.value
        
    def __eq__(self, q):
142
        return (q) == self.value
143
    def __ne__(self, q):
144
        return (q) != self.value
145
    def __lt__(self, q):
146
        return (q) > self.value
147
    def __gt__(self, q):
148
        return (q) < self.value        
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        
class AttrParam(Param):
    """
    Certain parameters of a component are set using the Finesse `attr` command.
    
    This inherits directly from a Param object so can be set whether this attribute
    is putable or a putter.
    
    If the value pf the parameter is not 0 the attr command will be printed.
    """
    def getFinesseText(self):
        rtn = []
        
        if self.value != 0:
            rtn.append("attr {0} {1} {2}".format(self._owner.name, self.name, self.value))
            
        rtn.extend(super(AttrParam, self).getFinesseText())
        
        return rtn