示例#1
0
 def test_constructor_4(self):
     print("Run: %s.test_constructor() " % self.__class__.__name__)
     attrs = {}
     attrs["mode"] = "STEP"
     attrs["trigger"] = "def_trigger"
     attrs["grows"] = "3"
     attrs["compression"] = "32008"
     attrs["compression_opts"] = "2,0"
     attrs["shuffle"] = "true"
     el = EField(self._fattrs, None)
     st = EStrategy(attrs, el)
     self.assertTrue(isinstance(st, Element))
     self.assertTrue(isinstance(st, EStrategy))
     self.assertEqual(st.tagName, "strategy")
     self.assertEqual(st.content, [])
     self.assertEqual(st.doc, "")
     self.assertEqual(st.last.strategy, attrs["mode"])
     self.assertEqual(el.strategy, attrs["mode"])
     self.assertEqual(st.last.trigger, attrs["trigger"])
     self.assertEqual(el.trigger, attrs["trigger"])
     self.assertEqual(st.last.grows, int(attrs["grows"]))
     self.assertEqual(el.grows, int(attrs["grows"]))
     self.assertEqual(st.last.compression, int(attrs["compression"]))
     self.assertEqual(el.compression, int(attrs["compression"]))
     self.assertEqual(
         st.last.compression_opts,
         [int(elm) for elm in attrs["compression_opts"].split(",")])
     self.assertEqual(
         el.compression_opts,
         [int(elm) for elm in attrs["compression_opts"].split(",")])
     self.assertEqual(st.last.shuffle, Converters.toBool(attrs["shuffle"]))
     self.assertEqual(el.shuffle, Converters.toBool(attrs["shuffle"]))
示例#2
0
 def test_constructor_3(self):
     print("Run: %s.test_constructor() " % self.__class__.__name__)
     attrs = {}
     attrs["mode"] = "STEP"
     attrs["trigger"] = "def_trigger"
     attrs["grows"] = "3"
     attrs["compression"] = "true"
     attrs["rate"] = "10"
     attrs["shuffle"] = "true"
     el = EField(self._fattrs, None)
     st = EStrategy(attrs, el)
     self.assertTrue(isinstance(st, Element))
     self.assertTrue(isinstance(st, EStrategy))
     self.assertEqual(st.tagName, "strategy")
     self.assertEqual(st.content, [])
     self.assertEqual(st.doc, "")
     self.assertEqual(st.last.strategy, attrs["mode"])
     self.assertEqual(el.strategy, attrs["mode"])
     self.assertEqual(st.last.trigger, attrs["trigger"])
     self.assertEqual(el.trigger, attrs["trigger"])
     self.assertEqual(st.last.grows, int(attrs["grows"]))
     self.assertEqual(el.grows, int(attrs["grows"]))
     self.assertEqual(st.last.compression,
                      Converters.toBool(attrs["compression"]))
     self.assertEqual(el.compression,
                      Converters.toBool(attrs["compression"]))
     self.assertEqual(st.last.rate,
                      int(attrs["rate"]) if int(attrs["rate"]) < 10 else 9)
     self.assertEqual(el.rate,
                      int(attrs["rate"]) if int(attrs["rate"]) < 10 else 9)
     self.assertEqual(st.last.shuffle, Converters.toBool(attrs["shuffle"]))
     self.assertEqual(el.shuffle, Converters.toBool(attrs["shuffle"]))
示例#3
0
    def test_store_1d_single(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__,
                                      fun)
        FileWriter.writer = H5CppWriter
        self._nxFile = FileWriter.create_file(self._fname,
                                              overwrite=True).root()
        eFile = EFile({}, None, self._nxFile)
        el = EGroup(self._gattrs, eFile)
        self.assertEqual(el.tagAttributes, {})

        attrs = {
            #    "string":["My string","NX_CHAR", "string" , (1,)],
            #    "datetime":["12:34:34","NX_DATE_TIME", "string", (1,) ],
            #    "iso8601":["12:34:34","ISO8601", "string", (1,)],
            "int": [-123, "NX_INT", "int64", (1, )],
            "int8": [12, "NX_INT8", "int8", (1, )],
            "int16": [-123, "NX_INT16", "int16", (1, )],
            "int32": [12345, "NX_INT32", "int32", (1, )],
            "int64": [-12345, "NX_INT64", "int64", (1, )],
            "uint": [123, "NX_UINT", "uint64", (1, )],
            "uint8": [12, "NX_UINT8", "uint8", (1, )],
            "uint16": [123, "NX_UINT16", "uint16", (1, )],
            "uint32": [12345, "NX_UINT32", "uint32", (1, )],
            "uint64": [12345, "NX_UINT64", "uint64", (1, )],
            "float": [-12.345, "NX_FLOAT", "float64", (1, ), 1.e-14],
            "number": [-12.345e+2, "NX_NUMBER", "float64", (1, ), 1.e-14],
            "float32": [-12.345e-1, "NX_FLOAT32", "float32", (1, ), 1.e-5],
            "float64": [-12.345, "NX_FLOAT64", "float64", (1, ), 1.e-14],
            "bool": [True, "NX_BOOLEAN", "bool", (1, )],
            "bool2": ["FaLse", "NX_BOOLEAN", "bool", (1, )],
            "bool3": ["false", "NX_BOOLEAN", "bool", (1, )],
            "bool4": ["true", "NX_BOOLEAN", "bool", (1, )]
        }

        for nm in attrs.keys():
            if attrs[nm][2] == 'string':
                "writing multi-dimensional string is not supported by pninx"
                continue
            el.tagAttributes[nm] = (attrs[nm][1], str(attrs[nm][0]),
                                    attrs[nm][3])
            el.store()
            at = el.h5Object.attributes[nm]
            self.assertEqual(at.dtype, attrs[nm][2])
            if attrs[nm][2] == "bool":
                self.assertEqual(Converters.toBool(str(attrs[nm][0])), at[...])

            elif len(attrs[nm]) > 4:
                self.assertTrue(abs(at[...] - attrs[nm][0]) <= attrs[nm][4])
            else:

                if isinstance(at[...], numpy.ndarray):
                    self.assertEqual(
                        at[...], numpy.array(attrs[nm][0], dtype=attrs[nm][2]))
                else:
                    self.assertEqual(at[...], attrs[nm][0])

        self._nxFile.close()
        os.remove(self._fname)
示例#4
0
    def test_getData_local_scalar(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        arr = {
            "none": ["null", None, "NoneType", []],
            "int": [-23243, "SCALAR", "DevLong64", []],
            "long": [10000023540000000000000003, "SCALAR", "DevLong64", []],
            "float": [121.223e-01, "SCALAR", "DevDouble", []],
            "str": ['My String 2', "SCALAR", "DevString", []],
            "unicode": [u'12\xf8\xff\xf4', "SCALAR", "DevString", []],
            "bool": ['true', "SCALAR", "DevBoolean", []],
        }

        for a in arr:
            ds = ClientSource()
            ds.name = a
            gjson = '{"data":{"myrecord":"1"}}'
            if arr[a][2] == "DevString":
                ljson = '{"data":{"%s":"%s"}}' % (a, arr[a][0])
            else:
                ljson = '{"data":{"%s":%s}}' % (a, arr[a][0])
            self.assertEqual(ds.setJSON(json.loads(gjson), json.loads(ljson)),
                             None)
            dt = ds.getData()
            if arr[a][2] == "NoneType":
                self.assertTrue(dt is None)
            elif arr[a][2] == "DevBoolean":
                self.checkData(dt, arr[a][1], Converters.toBool(arr[a][0]),
                               arr[a][2], arr[a][3])
            else:
                self.checkData(dt, arr[a][1], arr[a][0], arr[a][2], arr[a][3])
示例#5
0
    def test_getData_global_image(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        arr = {
            "int": [1243, "IMAGE", "DevLong64", []],
            "long": [-10000000000000000000000003, "IMAGE", "DevLong64", []],
            "float": [-1.223e-01, "IMAGE", "DevDouble", []],
            "str": ['My String', "IMAGE", "DevString", []],
            "unicode": ["Hello", "IMAGE", "DevString", []],
            #            "unicode":[u'\x12\xf8\xff\xf4',"IMAGE","DevString",[]],
            "bool": ['true', "IMAGE", "DevBoolean", []],
        }

        for k in arr:

            if arr[k][2] != "DevBoolean":
                mlen = [
                    self.__rnd.randint(1, 10),
                    self.__rnd.randint(1, 10),
                    self.__rnd.randint(0, 3)
                ]
                arr[k][0] = [[
                    arr[k][0] * self.__rnd.randint(0, 3)
                    for r in range(mlen[1])
                ] for c in range(mlen[0])]
            else:
                mlen = [self.__rnd.randint(1, 10), self.__rnd.randint(1, 10)]
                if arr[k][2] == 'DevBoolean':
                    arr[k][0] = [[
                        ("true" if self.__rnd.randint(0, 1) else "false")
                        for c in range(mlen[1])
                    ] for r in range(mlen[0])]

            arr[k][3] = [mlen[0], mlen[1]]

            ds = ClientSource()
            ds.name = k
            if arr[k][2] == "DevString":
                gjson = '{"data":{"%s":%s}}' % (k, str(arr[k][0]).replace(
                    "'", "\""))
            elif arr[k][2] == "DevBoolean":
                gjson = '{"data":{"%s":%s}}' % (k, '[' + "".join([
                    '[' + ''.join([a + ',' for a in row])[:-1] + "],"
                    for row in arr[k][0]
                ])[:-1] + ']')
            else:
                gjson = '{"data":{"%s":%s}}' % (k, '[' + "".join([
                    '[' + ''.join([str(a) + ',' for a in row])[:-1] + "],"
                    for row in arr[k][0]
                ])[:-1] + ']')
            self.assertEqual(ds.setJSON(json.loads(gjson)), None)
            dt = ds.getData()
            if arr[k][2] == "DevBoolean":
                self.checkData(dt, arr[k][1],
                               [[Converters.toBool(a) for a in row]
                                for row in arr[k][0]], arr[k][2], arr[k][3])
            else:
                self.checkData(dt, arr[k][1], arr[k][0], arr[k][2], arr[k][3])
示例#6
0
    def test_createAttributes_1d_single(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__,
                                      fun)
        self.createTree()
        el = FElement(self._tfname, self._fattrs, None)
        el2 = FElementWithAttr(self._tfname, self._fattrs, el, self._group)
        self.assertEqual(el2.tagAttributes, {})

        attrs = {
            #        "string":["My string","NX_CHAR", "string" , (1,)],
            #        "datetime":["12:34:34","NX_DATE_TIME", "string", (1,) ],
            #        "iso8601":["12:34:34","ISO8601", "string", (1,)],
            "int": [-123, "NX_INT", "int64", (1, )],
            "int8": [12, "NX_INT8", "int8", (1, )],
            "int16": [-123, "NX_INT16", "int16", (1, )],
            "int32": [12345, "NX_INT32", "int32", (1, )],
            "int64": [-12345, "NX_INT64", "int64", (1, )],
            "uint": [123, "NX_UINT", "uint64", (1, )],
            "uint8": [12, "NX_UINT8", "uint8", (1, )],
            "uint16": [123, "NX_UINT16", "uint16", (1, )],
            "uint32": [12345, "NX_UINT32", "uint32", (1, )],
            "uint64": [12345, "NX_UINT64", "uint64", (1, )],
            "float": [-12.345, "NX_FLOAT", "float64", (1, ), 1.e-14],
            "number": [-12.345e+2, "NX_NUMBER", "float64", (1, ), 1.e-14],
            "float32": [-12.345e-1, "NX_FLOAT32", "float32", (1, ), 1.e-5],
            "float64": [-12.345, "NX_FLOAT64", "float64", (1, ), 1.e-14],
            "bool": [True, "NX_BOOLEAN", "bool", (1, )],
            "bool2": ["FaLse", "NX_BOOLEAN", "bool", (1, )],
            "bool3": ["false", "NX_BOOLEAN", "bool", (1, )],
            "bool4": ["true", "NX_BOOLEAN", "bool", (1, )]
        }

        for nm in attrs.keys():
            if attrs[nm][2] == 'string':
                "writing multi-dimensional string is not supported by pninx"
                continue
            el2.tagAttributes[nm] = (attrs[nm][1], str(attrs[nm][0]),
                                     attrs[nm][3])
            el2._createAttributes()
            at = el2.h5Attribute(nm)
            self.assertEqual(at.dtype, attrs[nm][2])
            if attrs[nm][2] == "bool":
                self.assertEqual(Converters.toBool(str(attrs[nm][0])), at[...])

            elif len(attrs[nm]) > 4:
                self.assertTrue(abs(at[...] - attrs[nm][0]) <= attrs[nm][4])
            else:

                if isinstance(at[...], numpy.ndarray):
                    self.assertEqual(
                        at[...], numpy.array(attrs[nm][0], dtype=attrs[nm][2]))
                else:
                    self.assertEqual(at[...], attrs[nm][0])
示例#7
0
    def test_toBool_instance(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        bools = {
            True: True,
            False: False,
            "True": True,
            "False": False,
            "true": True,
            "false": False,
            "TRUE": True,
            "FALSE": False,
            "tRUE": True,
            "fALSE": False,
            "TrUE": True,
            "FaLSE": False,
            "TrUE": True,
            "FAlSE": False,
            "TRuE": True,
            "FALsE": False,
            "TRUe": True,
            "FALSe": False,
            "trUE": True,
            "faLSE": False,
            "tRuE": True,
            "fAlSE": False,
            "tRUe": True,
            "fALsE": False,
            "tRUe": True,
            "fALSe": False,
            "TruE": True,
            "FalSE": False,
            "TrUe": True,
            "FaLsE": False,
            "TrUe": True,
            "FaLSe": False,
            "TRue": True,
            "FAlsE": False,
            "TRue": True,
            "FAlSe": False,
            "TRue": True,
            "FAlse": False,
            "truE": True,
            "falSE": False,
            "trUe": True,
            "faLsE": False,
            "tRue": True,
            "fAlsE": False,
            "True": True,
            "FalsE": False,
            "BleBle": True,
            "FaLSe": False,
            "bleble": True,
            "FAlSe": False,
            "xxxxxx": True,
            "FalSe": False,
            "bldsff": True,
            "fALse": False,
            "blerew": True,
            "FaLse": False,
            "bwerle": True,
            "FAlse": False,
            "alebwe": True,
            "fAlse": False,
            "glewer": True,
            "faLse": False,
            "fgeble": True,
            "falSe": False,
            "fall": True,
            "falsE": False,
        }

        el = Converters()
        self.assertTrue(isinstance(el, object))
        self.assertTrue(hasattr(el, "toBool"))

        for b in bools:
            self.assertEqual(el.toBool(b), bools[b])
示例#8
0
    def test_createAttributes_2d_X1(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__,
                                      fun)
        self.createTree()
        el = FElement(self._tfname, self._fattrs, None)
        el2 = FElementWithAttr(self._tfname, self._fattrs, el, self._group)
        self.assertEqual(el2.tagAttributes, {})

        attrs = {
            #        "string":["My string","NX_CHAR", "string" , (1,)],
            #        "datetime":["12:34:34","NX_DATE_TIME", "string", (1,) ],
            #        "iso8601":["12:34:34","ISO8601", "string", (1,)],
            "int": [-123, "NX_INT", "int64", (1, )],
            "int8": [12, "NX_INT8", "int8", (1, )],
            "int16": [-123, "NX_INT16", "int16", (1, )],
            "int32": [12345, "NX_INT32", "int32", (1, )],
            "int64": [-12345, "NX_INT64", "int64", (1, )],
            "uint": [123, "NX_UINT", "uint64", (1, )],
            "uint8": [12, "NX_UINT8", "uint8", (1, )],
            "uint16": [123, "NX_UINT16", "uint16", (1, )],
            "uint32": [12345, "NX_UINT32", "uint32", (1, )],
            "uint64": [12345, "NX_UINT64", "uint64", (1, )],
            "float": [-12.345, "NX_FLOAT", "float64", (1, ), 1.e-14],
            "number": [-12.345e+2, "NX_NUMBER", "float64", (1, ), 1.e-14],
            "float32": [-12.345e-1, "NX_FLOAT32", "float32", (1, ), 1.e-5],
            "float64": [-12.345, "NX_FLOAT64", "float64", (1, ), 1.e-14],
            "bool": [True, "NX_BOOLEAN", "bool", (1, )],
            "bool2": ["FaLse", "NX_BOOLEAN", "bool", (1, )],
            "bool3": ["false", "NX_BOOLEAN", "bool", (1, )],
            "bool4": ["true", "NX_BOOLEAN", "bool", (1, )]
        }

        for nm in attrs.keys():
            if attrs[nm][2] != "bool":
                mlen = [self.__rnd.randint(2, 10), 1]
                attrs[nm][0] = [[
                    attrs[nm][0] * self.__rnd.randint(0, 3)
                    for r in range(mlen[1])
                ] for c in range(mlen[0])]
            else:
                mlen = [self.__rnd.randint(2, 10), 1]
                if nm == 'bool':
                    attrs[nm][0] = [[
                        bool(self.__rnd.randint(0, 1)) for c in range(mlen[1])
                    ] for r in range(mlen[0])]
                else:
                    attrs[nm][0] = [[
                        ("True" if self.__rnd.randint(0, 1) else "False")
                        for c in range(mlen[1])
                    ] for r in range(mlen[0])]

            attrs[nm][3] = (mlen[0], mlen[1])

        for nm in attrs.keys():
            el2.tagAttributes[nm] = (attrs[nm][1], "".join([
                "".join([str(it) + " " for it in sub]) + "\n"
                for sub in attrs[nm][0]
            ]), attrs[nm][3])
            el2._createAttributes()
            at = el2.h5Attribute(nm)
            self.assertEqual(at.dtype, attrs[nm][2])
            if attrs[nm][2] == "bool":
                for i in range(len(attrs[nm][0])):
                    for j in range(len(attrs[nm][0][i])):
                        self.assertEqual(
                            Converters.toBool(str(attrs[nm][0][i][j])), at[i,
                                                                           j])
                pass
            elif len(attrs[nm]) > 4:
                for i in range(len(attrs[nm][0])):
                    for j in range(len(attrs[nm][0][i])):
                        self.assertTrue(
                            abs(at[i, j] - attrs[nm][0][i][j]) <= attrs[nm][4])
            else:
                for i in range(len(attrs[nm][0])):
                    for j in range(len(attrs[nm][0][i])):
                        self.assertEqual(at[i, j], attrs[nm][0][i][j])
示例#9
0
    def test_store_2d_single(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))
        self._fname = '%s/%s%s.h5' % (os.getcwd(), self.__class__.__name__,
                                      fun)
        FileWriter.writer = H5CppWriter
        self._nxFile = FileWriter.create_file(self._fname,
                                              overwrite=True).root()
        eFile = EFile({}, None, self._nxFile)
        el = EGroup(self._gattrs, eFile)
        self.assertEqual(el.tagAttributes, {})

        attrs = {
            #   "string":["My string","NX_CHAR", "string" , (1,)],
            #   "datetime":["12:34:34","NX_DATE_TIME", "string", (1,) ],
            #   "iso8601":["12:34:34","ISO8601", "string", (1,)],
            "int": [-123, "NX_INT", "int64", (1, )],
            "int8": [12, "NX_INT8", "int8", (1, )],
            "int16": [-123, "NX_INT16", "int16", (1, )],
            "int32": [12345, "NX_INT32", "int32", (1, )],
            "int64": [-12345, "NX_INT64", "int64", (1, )],
            "uint": [123, "NX_UINT", "uint64", (1, )],
            "uint8": [12, "NX_UINT8", "uint8", (1, )],
            "uint16": [123, "NX_UINT16", "uint16", (1, )],
            "uint32": [12345, "NX_UINT32", "uint32", (1, )],
            "uint64": [12345, "NX_UINT64", "uint64", (1, )],
            "float": [-12.345, "NX_FLOAT", "float64", (1, ), 1.e-14],
            "number": [-12.345e+2, "NX_NUMBER", "float64", (1, ), 1.e-14],
            "float32": [-12.345e-1, "NX_FLOAT32", "float32", (1, ), 1.e-5],
            "float64": [-12.345, "NX_FLOAT64", "float64", (1, ), 1.e-14],
            "bool": [True, "NX_BOOLEAN", "bool", (1, )],
            "bool2": ["FaLse", "NX_BOOLEAN", "bool", (1, )],
            "bool3": ["false", "NX_BOOLEAN", "bool", (1, )],
            "bool4": ["true", "NX_BOOLEAN", "bool", (1, )]
        }

        for nm in attrs.keys():
            if attrs[nm][2] != "bool":
                mlen = [
                    self.__rnd.randint(1, 1),
                    self.__rnd.randint(1, 1),
                    (2 << numpy.dtype(attrs[nm][2]).itemsize)
                ]
                #                print "SH",nm,mlen[2]
                attrs[nm][0] = [[
                    attrs[nm][0] * self.__rnd.randint(0, 3)
                    for r in range(mlen[1])
                ] for c in range(mlen[0])]
            else:
                mlen = [self.__rnd.randint(1, 1), self.__rnd.randint(1, 1)]
                if nm == 'bool':
                    attrs[nm][0] = [[
                        bool(self.__rnd.randint(0, 1)) for c in range(mlen[1])
                    ] for r in range(mlen[0])]
                else:
                    attrs[nm][0] = [[
                        ("True" if self.__rnd.randint(0, 1) else "False")
                        for c in range(mlen[1])
                    ] for r in range(mlen[0])]

            attrs[nm][3] = (mlen[0], mlen[1])

        for nm in attrs.keys():
            el.tagAttributes[nm] = (attrs[nm][1], "".join([
                "".join([str(it) + " " for it in sub]) + "\n"
                for sub in attrs[nm][0]
            ]), attrs[nm][3])
            el.store()
            at = el.h5Object.attributes[nm]
            self.assertEqual(at.dtype, attrs[nm][2])
            if attrs[nm][2] == "bool":
                for i in range(len(attrs[nm][0])):
                    for j in range(len(attrs[nm][0][i])):
                        self.assertEqual(
                            Converters.toBool(str(attrs[nm][0][i][j])),
                            at[...])
                pass
            elif len(attrs[nm]) > 4:
                for i in range(len(attrs[nm][0])):
                    for j in range(len(attrs[nm][0][i])):
                        self.assertTrue(
                            abs(at[...] - attrs[nm][0][i][j]) <= attrs[nm][4])
            else:
                for i in range(len(attrs[nm][0])):
                    for j in range(len(attrs[nm][0][i])):
                        self.assertEqual(at[...], attrs[nm][0][i][j])

        self._nxFile.close()
        os.remove(self._fname)
示例#10
0
    def test_getData_global_image(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        script = """
if type(ds.inp[0][0]) == type(ds.inp2[0][0]):
    ds.res = ds.inp + ds.inp2
else:
    ds.res = [[str(j) for j in i] for i in ds.inp] + """ \
        """[[str(j2) for j2 in i2] for i2 in ds.inp2]
"""

        dp = DataSourcePool()

        arr = {
            "int": [1243, "IMAGE", "DevLong64", []],
            "long": [-10000000000000000000000003, "IMAGE", "DevLong64", []],
            "float": [-1.223e-01, "IMAGE", "DevDouble", []],
            "str": ['My String', "IMAGE", "DevString", []],
            "unicode": ["Hello", "IMAGE", "DevString", []],
            #            "unicode":[u'\x12\xf8\xff\xf4',"IMAGE","DevString",[]],
            "bool": ['true', "IMAGE", "DevBoolean", []],
        }

        arr2 = {
            "int": [1123, "SPECTRUM", "DevLong64", []],
            "long": [-10123000000000000000000003, "SPECTRUM", "DevLong64", []],
            "float": [-1.112e-01, "SPECTRUM", "DevDouble", []],
            "str": ['My String2', "SPECTRUM", "DevString", []],
            "unicode": ["Hello!", "SPECTRUM", "DevString", []],
            #            "unicode":[u'\x14\xf8\xff\xf4',"SPECTRUM","DevString",[]],
            "bool": ['false', "SPECTRUM", "DevBoolean", []],
        }

        for k in arr:
            nl2 = self.__rnd.randint(1, 10)
            if arr[k][2] != "DevBoolean":
                mlen = [
                    self.__rnd.randint(1, 10), nl2, self.__rnd.randint(0, 3)]
                arr[k][0] = [[arr[k][0] * self.__rnd.randint(1, 3)
                              for r in range(mlen[1])] for c in range(mlen[0])]
            else:
                mlen = [self.__rnd.randint(1, 10), nl2]
                if arr[k][2] == 'DevBoolean':
                    arr[k][0] = [[("true" if self.__rnd.randint(0, 1)
                                   else "false")
                                  for c in range(mlen[1])]
                                 for r in range(mlen[0])]

            arr[k][3] = [mlen[0], mlen[1]]

            if arr2[k][2] != "DevBoolean":
                mlen = [self.__rnd.randint(1, 10), self.__rnd.randint(
                    1, 10), self.__rnd.randint(0, 3)]
                arr2[k][0] = [[arr2[k][0] * self.__rnd.randint(1, 3)
                               for r in range(mlen[1])]
                              for c in range(mlen[0])]
            else:
                mlen = [self.__rnd.randint(1, 10), self.__rnd.randint(1, 10)]
                if arr2[k][2] == 'DevBoolean':
                    arr2[k][0] = [[("true" if self.__rnd.randint(0, 1)
                                    else "false")
                                   for c in range(mlen[1])]
                                  for r in range(mlen[0])]

            arr2[k][3] = [mlen[0], mlen[1]]

        for k in arr:
            for k2 in arr2:

                ds = PyEvalSource()
                self.assertTrue(isinstance(ds, DataSource))
                self.myAssertRaise(DataSourceSetupError, ds.getData)
                self.assertEqual(ds.setup("""
<datasource>
  <datasource type='CLIENT' name='inp'>
    <record name='rinp' />
  </datasource>
  <datasource type='CLIENT' name='inp2'>
    <record name='rinp2' />
  </datasource>
  <result name='res'>%s</result>
</datasource>
""" % script), None)

                if arr[k][2] == "DevString":
                    gjson = '{"data":{"rinp":%s}}' % (
                        str(arr[k][0]).replace("'", "\""))
                elif arr[k][2] == "DevBoolean":
                    gjson = '{"data":{"rinp":%s}}' % (
                        '[' + "".join(['[' + ''.join(
                            [a + ',' for a in row])[:-1] + "],"
                            for row in arr[k][0]])[:-1] + ']')
                else:
                    gjson = '{"data":{"rinp":%s}}' % (
                        '[' + "".join(['[' + ''.join(
                            [str(a) + ',' for a in row])[:-1] + "],"
                            for row in arr[k][0]])[:-1] + ']')

                if arr2[k2][2] == "DevString":
                    gjson2 = '{"data":{"rinp2":%s}}' % (
                        str(arr2[k2][0]).replace("'", "\""))
                elif arr2[k2][2] == "DevBoolean":
                    gjson2 = '{"data":{"rinp2":%s}}' % (
                        '[' + "".join(['[' + ''.join(
                            [a + ',' for a in row])[:-1] + "],"
                            for row in arr2[k2][0]])[:-1] + ']')
                else:
                    gjson2 = '{"data":{"rinp2":%s}}' % (
                        '[' + "".join(['[' + ''.join(
                            [str(a) + ',' for a in row])[:-1] + "],"
                            for row in arr2[k2][0]])[:-1] + ']')

                self.assertEqual(ds.setDataSources(dp), None)
                self.assertEqual(
                    ds.setJSON(json.loads(gjson), json.loads(gjson2)), None)
                dt = ds.getData()
                v1 = [[Converters.toBool(a) for a in row]
                      for row in arr[k][0]] \
                    if arr[k][2] == "DevBoolean" else arr[k][0]
                v2 = [[Converters.toBool(a) for a in row]
                      for row in arr2[k2][0]] \
                    if arr2[k2][2] == "DevBoolean" else arr2[k2][0]
                vv = v1 + v2 if str(type(v1[0][0])) == str(type(v2[0][0])) \
                    else ([[unicode(j) for j in i] for i in v1] +
                          [[unicode(j2) for j2 in i2] for i2 in v2])
                self.checkData(dt, arr[k][1], vv,
                               NTP.pTt[type(vv[0][0]).__name__], [
                               arr[k][3][0] + arr2[k2][3][0], arr[k][3][1]])
示例#11
0
    def test_getData_global_scalar(self):
        fun = sys._getframe().f_code.co_name
        print("Run: %s.%s() " % (self.__class__.__name__, fun))

        script = """
if type(ds.inp) == type(ds.inp2):
    ds.res = ds.inp + ds.inp2
else:
    ds.res = unicode(ds.inp) + unicode(ds.inp2)
"""
        if sys.version_info > (3,):
            script = script.replace("unicode", "str")
        dp = DataSourcePool()

        arr = {
            "int": [1243, "SCALAR", "DevLong64", []],
            "long": [-10000000000000000000000003, "SCALAR", "DevLong64", []],
            "float": [-1.223e-01, "SCALAR", "DevDouble", []],
            "str": ['My String', "SCALAR", "DevString", []],
            "unicode": [u'12\xf8\xff\xf4', "SCALAR", "DevString", []],
            "bool": ['true', "SCALAR", "DevBoolean", []],
        }

        arr2 = {
            "int": [1123, "SCALAR", "DevLong64", []],
            "long": [-11231000000000000000000003, "SCALAR", "DevLong64", []],
            "float": [-1.113e-01, "SCALAR", "DevDouble", []],
            "str": ['My funy', "SCALAR", "DevString", []],
            "unicode": [u'\xf812\xff\xf4', "SCALAR", "DevString", []],
            "bool": ['false', "SCALAR", "DevBoolean", []],
        }

        for a in arr:
            for a2 in arr2:

                ds = PyEvalSource()
                self.assertTrue(isinstance(ds, DataSource))
                self.myAssertRaise(DataSourceSetupError, ds.getData)
                self.assertEqual(ds.setup("""
<datasource>
  <datasource type='CLIENT' name='inp'>
    <record name='rinp' />
  </datasource>
  <datasource type='CLIENT' name='inp2'>
    <record name='rinp2' />
  </datasource>
  <result name='res'>%s</result>
</datasource>
""" % script), None)
                if arr[a][2] == "DevString":
                    gjson = '{"data":{"rinp":"%s"}}' % (arr[a][0])
                else:
                    gjson = '{"data":{"rinp":%s}}' % (arr[a][0])
                if arr2[a2][2] == "DevString":
                    gjson2 = '{"data":{"rinp2":"%s"}}' % (arr2[a2][0])
                else:
                    gjson2 = '{"data":{"rinp2":%s}}' % (arr2[a2][0])

                self.assertEqual(ds.setDataSources(dp), None)
                self.assertEqual(
                    ds.setJSON(json.loads(gjson), json.loads(gjson2)), None)
                dt = ds.getData()
                v1 = Converters.toBool(arr[a][0]) if arr[
                    a][2] == "DevBoolean" else arr[a][0]
                v2 = Converters.toBool(arr2[a2][0]) if arr2[
                    a2][2] == "DevBoolean" else arr2[a2][0]
                vv = v1 + v2 if type(v1) == type(
                    v2) else unicode(v1) + unicode(v2)
                self.checkData(
                    dt, arr[a][1], vv, NTP.pTt[type(vv).__name__], arr[a][3])