Esempio n. 1
0
    def testBadCode(self):
        """Test execution of not allowed code"""
        testCode = "import os"
        x = SLRC(testCode)
        with self.assertRaises(ImportError):
            x()

        testCode = "return globals()"
        x = SLRC(testCode)
        with self.assertRaises(NameError):
            x()
Esempio n. 2
0
    def testConvertCodeToAnything(self):
        testCode = "return 0"
        x = SLRC(testCode)
        x.kind = "scalar"
        self.assertEqual(x.value, 0)

        x = SLRC(testCode)
        x.kind = "list"
        self.assertEqual(x.value, [0])

        x = SLRC(testCode)
        x.kind = "range"
        self.assertEqual(x.value, [0, 1])
Esempio n. 3
0
 def testCode(self):
     testCode = "x = args[0]; return 2 * x"
     x = SLRC(testCode)
     f = x.value
     self.assertEqual(f(2), 4)
     self.assertEqual(len(x), 1)
     f = x[0]
     self.assertEqual(f(1), 2)
     self.assertEqual(x(3), 6)  # test __call__
Esempio n. 4
0
    def __init__(self, parent=None, f=QtCore.Qt.Widget):
        QtGui.QFrame.__init__(self, parent, f)
        SLRC.__init__(self)
        self.scale = 1.

        self.innerLayout = QtGui.QHBoxLayout()

        self._initTypeButton()
        layout = QtGui.QHBoxLayout()
        layout.addLayout(self.innerLayout)
        layout.addWidget(self.typeButton)
        self.setLayout(layout)

        self._initScalarControl()
        self._initListControl()
        self._initRangeControl()
        self._initCodeControl()

        self.setDecimals(5)
Esempio n. 5
0
 def testScalar(self):
     x = SLRC(3.14)
     self.assertEqual(x.value, 3.14)
     self.assertEqual(x.kind, 'scalar')
     self.assertEqual(len(x), 1)
     self.assertEqual(x[0], 3.14)
     visited = False
     for v in x:
         self.assertEqual(v, 3.14)
         visited = True
     self.assertTrue(visited)
     self.assertEqual(x(), 3.14)
Esempio n. 6
0
    def set_wavelengths(self, value):
        """Set the list of wavelengths.

        It can be used if this was not done in the constructor, or to modify
        the current list of wavelengths.

        Args:
            value(list): List of wavelengths (in meters)

        """
        self._wavelengths = tuple(Wavelength(w) for w in iter(SLRC(value)))
        self._build_fsims()
Esempio n. 7
0
 def testList(self):
     testList = [1, 2, 3]
     x = SLRC(testList)
     self.assertEqual(x.value, testList)
     self.assertEqual(x.kind, 'list')
     self.assertEqual(len(x), 3)
     for i, v in enumerate(testList):
         self.assertEqual(x[i], v)
     visited = False
     for v, t in zip(x, testList):
         self.assertEqual(v, t)
         visited = True
     self.assertTrue(visited)
     self.assertEqual(x(), testList)
Esempio n. 8
0
 def testUnorderedList(self):
     testList = [1, 4, 2, 5, 3]
     x = SLRC(testList)
     self.assertEqual(x.value, sorted(testList))
     self.assertEqual(x.kind, 'list')
     self.assertEqual(len(x), 5)
     for i, v in enumerate(sorted(testList)):
         self.assertEqual(x[i], v)
     visited = False
     for v, t in zip(x, sorted(testList)):
         self.assertEqual(v, t)
         visited = True
     self.assertTrue(visited)
     self.assertEqual(x(), sorted(testList))
Esempio n. 9
0
 def testRange(self):
     testRange = {'start': 0, 'end': 5, 'num': 6}
     testList = [0, 1, 2, 3, 4, 5]
     x = SLRC(testRange)
     self.assertEqual(x.value, testList)
     self.assertEqual(x.kind, 'range')
     self.assertEqual(len(x), testRange['num'])
     for i, v in enumerate(testList):
         self.assertEqual(x[i], v)
     visited = False
     for v, t in zip(x, testList):
         self.assertEqual(v, t)
         visited = True
     self.assertTrue(visited)
     self.assertEqual(x(), testList)
Esempio n. 10
0
    def testConvertCodeToAnything(self):
        testCode = "return 0"
        x = SLRC(testCode)
        x.kind = "scalar"
        self.assertEqual(x.value, 0)

        x = SLRC(testCode)
        x.kind = "list"
        self.assertEqual(x.value, [0])

        x = SLRC(testCode)
        x.kind = "range"
        self.assertEqual(x.value, [0, 1])
Esempio n. 11
0
 def testConvertListToScalar(self):
     x = SLRC([1, 2, 3])
     x.kind = 'scalar'
     self.assertEqual(x.value, 1)
Esempio n. 12
0
 def testConvertScalarToRange(self):
     x = SLRC(5)
     x.kind = 'range'
     self.assertEqual(x.value, [5])
Esempio n. 13
0
 def testConvertScalarToCode(self):
     x = SLRC(5)
     x.kind = 'code'
     self.assertEqual(x(), 5)
Esempio n. 14
0
 def testConvertRangeToList(self):
     testRange = {'start': 0, 'end': 5, 'num': 6}
     x = SLRC(testRange)
     x.kind = 'list'
     self.assertEqual(x.value, [0, 1, 2, 3, 4, 5])
Esempio n. 15
0
 def testConvertScalarToList(self):
     x = SLRC(5)
     x.kind = 'list'
     self.assertEqual(x.value, [5])
Esempio n. 16
0
 def testConvertRangeToScalar(self):
     testRange = {'start': 0, 'end': 5, 'num': 6}
     x = SLRC(testRange)
     x.kind = 'scalar'
     self.assertEqual(x.value, 0)
Esempio n. 17
0
 def testConvertRangeToCode(self):
     testRange = {'start': 0, 'end': 5, 'num': 6}
     x = SLRC(testRange)
     x.kind = 'code'
     self.assertEqual(x(), 0)
Esempio n. 18
0
 def testMathCode(self):
     """Test if math module is available"""
     testCode = "return math.pi"
     x = SLRC(testCode)
     self.assertAlmostEqual(x(), 3.141592653589793)
Esempio n. 19
0
 def testConvertScalarToCode(self):
     x = SLRC(5)
     x.kind = 'code'
     self.assertEqual(x(), 5)
Esempio n. 20
0
 def testConvertListToScalar(self):
     x = SLRC([1, 2, 3])
     x.kind = 'scalar'
     self.assertEqual(x.value, 1)
Esempio n. 21
0
    def _buildFiber(self, indexes):
        """Build Fiber object from list of indexes"""

        r = []
        f = []
        fp = []
        m = []
        mp = []
        names = []

        # Get parameters for selected fiber
        ii = 0
        for i, layer in enumerate(self._fibers["layers"], 1):
            name = layer["name"] if layer["name"] else "layer {}".format(i + 1)
            names.append(name)

            if i < len(self._fibers["layers"]):
                rr = SLRC(layer["tparams"][0])
                rr.codeParams = ["r", "fp", "mp"]
                r.append(rr[indexes[ii]])
            ii += 1  # we count radius of cladding, even if we don't use it

            f.append(layer["type"])
            fp_ = []
            for p in layer["tparams"][1:]:
                ff = SLRC(p)
                ff.codeParams = ["r", "fp", "mp"]
                fp_.append(ff[indexes[ii]])
                ii += 1
            fp.append(fp_)

            m.append(layer["material"])
            mp_ = []
            for p in layer["mparams"]:
                mm = SLRC(p)
                mm.codeParams = ["r", "fp", "mp"]
                mp_.append(mm[indexes[ii]])
                ii += 1
            mp.append(mp_)

        # Execute code parts
        for i, p in enumerate(r):
            if callable(p):
                r[i] = float(p(r, fp, mp))
        for i, pp in enumerate(fp):
            for j, p in enumerate(pp):
                if callable(p):
                    fp[i][j] = float(p(r, fp, mp))
            fp[i] = tuple(fp[i])
        for i, pp in enumerate(mp):
            for j, p in enumerate(pp):
                if callable(p):
                    mp[i][j] = float(p(r, fp, mp))
            mp[i] = tuple(mp[i])

        # Remove unneeded layers
        i = len(m) - 2
        while i >= 0 and len(m) > 1:
            if (r[i] == 0 or (i > 0 and r[i] <= r[i - 1])
                    or (f[i] == f[i + 1] == 'StepIndex' and m[i] == m[i + 1]
                        and mp[i] == mp[i + 1])):
                del r[i]
                del f[i]
                del fp[i]
                del m[i]
                del mp[i]
                del names[i]
            i -= 1

        return Fiber(r, f, fp, m, mp, names, self._Cutoff, self._Neff)
Esempio n. 22
0
 def testConvertListToRange(self):
     x = SLRC([1, 4, 5])
     x.kind = 'range'
     self.assertEqual(x.value, [1, 3, 5])
Esempio n. 23
0
 def testConvertListToCode(self):
     x = SLRC([1, 2, 3])
     x.kind = 'code'
     self.assertEqual(x(), 1)
Esempio n. 24
0
 def testConvertRangeToScalar(self):
     testRange = {'start': 0, 'end': 5, 'num': 6}
     x = SLRC(testRange)
     x.kind = 'scalar'
     self.assertEqual(x.value, 0)
Esempio n. 25
0
 def testConvertListToRange(self):
     x = SLRC([1, 4, 5])
     x.kind = 'range'
     self.assertEqual(x.value, [1, 3, 5])
Esempio n. 26
0
 def testConvertScalarToRange(self):
     x = SLRC(5)
     x.kind = 'range'
     self.assertEqual(x.value, [5])
Esempio n. 27
0
 def testConvertListToCode(self):
     x = SLRC([1, 2, 3])
     x.kind = 'code'
     self.assertEqual(x(), 1)
Esempio n. 28
0
    def _buildFiber(self, indexes):
        """Build Fiber object from list of indexes"""

        r = []
        f = []
        fp = []
        m = []
        mp = []
        names = []

        # Get parameters for selected fiber
        ii = 0
        for i, layer in enumerate(self._fibers["layers"], 1):
            name = layer["name"] if layer["name"] else "layer {}".format(i+1)
            names.append(name)

            if i < len(self._fibers["layers"]):
                rr = SLRC(layer["tparams"][0])
                rr.codeParams = ["r", "fp", "mp"]
                r.append(rr[indexes[ii]])
            ii += 1  # we count radius of cladding, even if we don't use it

            f.append(layer["type"])
            fp_ = []
            for p in layer["tparams"][1:]:
                ff = SLRC(p)
                ff.codeParams = ["r", "fp", "mp"]
                fp_.append(ff[indexes[ii]])
                ii += 1
            fp.append(fp_)

            m.append(layer["material"])
            mp_ = []
            for p in layer["mparams"]:
                mm = SLRC(p)
                mm.codeParams = ["r", "fp", "mp"]
                mp_.append(mm[indexes[ii]])
                ii += 1
            mp.append(mp_)

        # Execute code parts
        for i, p in enumerate(r):
            if callable(p):
                r[i] = float(p(r, fp, mp))
        for i, pp in enumerate(fp):
            for j, p in enumerate(pp):
                if callable(p):
                    fp[i][j] = float(p(r, fp, mp))
            fp[i] = tuple(fp[i])
        for i, pp in enumerate(mp):
            for j, p in enumerate(pp):
                if callable(p):
                    mp[i][j] = float(p(r, fp, mp))
            mp[i] = tuple(mp[i])

        # Remove unneeded layers
        i = len(m)-2
        while i >= 0 and len(m) > 1:
            if (r[i] == 0 or
                    (i > 0 and r[i] <= r[i-1]) or
                    (f[i] == f[i+1] == 'StepIndex' and
                     m[i] == m[i+1] and
                     mp[i] == mp[i+1])):
                del r[i]
                del f[i]
                del fp[i]
                del m[i]
                del mp[i]
                del names[i]
            i -= 1

        return Fiber(r, f, fp, m, mp, names, self._Cutoff, self._Neff)
Esempio n. 29
0
 def testConvertRangeToList(self):
     testRange = {'start': 0, 'end': 5, 'num': 6}
     x = SLRC(testRange)
     x.kind = 'list'
     self.assertEqual(x.value, [0, 1, 2, 3, 4, 5])
Esempio n. 30
0
 def testConvertScalarToList(self):
     x = SLRC(5)
     x.kind = 'list'
     self.assertEqual(x.value, [5])
Esempio n. 31
0
 def _buildFiberList(self):
     self._nitems = []
     for layer in self._fibers["layers"]:
         for key in ("tparams", "mparams"):
             for tp in layer[key]:
                 self._nitems.append(len(SLRC(tp)))
Esempio n. 32
0
 def testConvertRangeToCode(self):
     testRange = {'start': 0, 'end': 5, 'num': 6}
     x = SLRC(testRange)
     x.kind = 'code'
     self.assertEqual(x(), 0)