Пример #1
0
def get_xgraph_str(xg: XGraph):
    # graph_str, data_str = XGraphIO.to_string(xg)
    # return " " + str(len(graph_str)) + " "  + graph_str + " " + str(len(data_str) + 1) + " " + data_str
    of = OpaqueFuncRegistry.Get("pyxir.io.get_serialized_xgraph")
    s = BytesContainer(b"")
    of(xg, s)
    # import pdb; pdb.set_trace()
    return s.get_bytes()
Пример #2
0
def read_xgraph_str(xg_str: bytes):
    of = OpaqueFuncRegistry.Get("pyxir.io.deserialize_xgraph")
    xg = XGraph()
    s = BytesContainer(xg_str)
    # import pdb; pdb.set_trace()
    of(xg, s)
    return xg
Пример #3
0
    def test_empty_directory_serialization(self):
        dir_path = "/tmp/test_dir"
        if os.path.exists(dir_path):
            shutil.rmtree(dir_path)

        os.makedirs(dir_path)

        bytes_c = BytesContainer(b"")
        of = OpaqueFuncRegistry.Get("pyxir.io.serialize_dir")
        of(dir_path, bytes_c)

        shutil.rmtree("/tmp/test_dir")
        assert not os.path.exists("/tmp/test_dir")

        of_de = OpaqueFuncRegistry.Get("pyxir.io.deserialize_dir")
        of_de(dir_path, bytes_c.get_bytes())

        assert os.path.exists("/tmp/test_dir")
Пример #4
0
    def test_directory_serialization(self):
        dir_path = "/tmp/test_dir"
        if os.path.exists(dir_path):
            shutil.rmtree(dir_path)

        os.makedirs(dir_path)
        with open("/tmp/test_dir/test.txt", 'w') as f:
            f.write("testtest")

        bytes_c = BytesContainer(b"")
        of = OpaqueFuncRegistry.Get("pyxir.io.serialize_dir")
        of(dir_path, bytes_c)

        shutil.rmtree("/tmp/test_dir")
        assert not os.path.exists("/tmp/test_dir")

        of_de = OpaqueFuncRegistry.Get("pyxir.io.deserialize_dir")
        of_de(dir_path, bytes_c.get_bytes())

        assert os.path.exists("/tmp/test_dir")
        assert os.path.exists("/tmp/test_dir/test.txt")
        with open("/tmp/test_dir/test.txt", 'r') as f:
            assert f.read() == "testtest"
Пример #5
0
    def test_constructor(self):
        b = b"test"
        bc = BytesContainer(b)
        assert isinstance(bc.get_bytes(), bytes)
        assert bc.get_bytes() == b"test"
        assert bc.get_bytes() != "test"

        b2 = "test".encode('latin1')
        bc2 = BytesContainer(b2)
        assert bc.get_bytes() == "test".encode('latin1')
Пример #6
0
 def test_set_bytes_latin1(self):
     b = b"test"
     bc = BytesContainer(b)
     bc.set_bytes("2".encode('latin1'))
     assert bc == "2".encode('latin1')
     assert bc.get_bytes() == "2".encode('latin1')
Пример #7
0
 def test_set_bytes(self):
     b = b"test"
     bc = BytesContainer(b)
     bc.set_bytes(b"2")
     assert bc == b"2"
     assert bc.get_bytes() == b"2"
Пример #8
0
 def test_eq(self):
     b = b"test"
     bc = BytesContainer(b)
     assert bc == b"test"
Пример #9
0
class OpaqueFunc(object):

    # TypeCode conversion functions
    # First: C++ -> Python
    # Second: Python -> C++
    type_codes_ = {
        TypeCode.vInt: (
            lambda arg_: IntVector(arg_.ints),
            lambda arg_: lpx.OpaqueValue(lpx.IntVector(arg_))),
        TypeCode.Str: (
            lambda arg_: arg_.s,
            lambda arg_: lpx.OpaqueValue(arg_)),
        TypeCode.Byte: (
            lambda arg_: arg_.bytes,
            lambda arg_: lpx.OpaqueValue(arg_)),
        TypeCode.vStr: (
            lambda arg_: StrVector(arg_.strings),
            lambda arg_: lpx.OpaqueValue(lpx.StrVector(arg_))),
        TypeCode.StrContainer: (
            lambda arg_: StrContainer.from_lib(arg_.str_c),
            lambda arg_: lpx.OpaqueValue(arg_._str_c)),
        TypeCode.BytesContainer: (
            lambda arg_: BytesContainer.from_lib(arg_.bytes_c),
            lambda arg_: lpx.OpaqueValue(arg_._bytes_c)),
        TypeCode.XGraph: (
            lambda arg_: XGraph._from_xgraph(arg_.xg),
            lambda arg_: lpx.OpaqueValue(arg_._xgraph)),
        TypeCode.XBuffer: (
            lambda arg_: XBuffer.from_lib(arg_.xb),
            lambda arg_: lpx.OpaqueValue(arg_._xb)),
        TypeCode.vXBuffer: (
            lambda arg_: [XBuffer.from_lib(e) for e in arg_.xbuffers],
            lambda arg_: lpx.OpaqueValue(
                lpx.XBufferHolderVector([xb._xb for xb in arg_]))),
        TypeCode.OpaqueFunc: (
            lambda arg_: OpaqueFunc.from_lib(arg_.of),
            lambda arg_: lpx.OpaqueValue(arg_._of))
    }

    def __init__(self,
                 func: Callable = None,
                 type_codes: List[TypeCode] = None) -> None:

        self._of = lpx.OpaqueFunc()
        if type_codes is None:
            type_codes = []

        if func is not None:
            self.set_func(func, type_codes)

    @classmethod
    def from_lib(cls, _of: lpx.OpaqueFunc) -> 'OpaqueFunc':
        of = OpaqueFunc.__new__(cls)
        of._of = _of
        return of

    def set_func(self, func: Callable, type_codes: List[TypeCode]):

        # if type_codes is not None:
        for tc in type_codes:
            if tc not in OpaqueFunc.type_codes_:
                raise NotImplementedError("Function with argument of"
                                          " unsupported type: {} provided"
                                          .format(tc.name))

        def opaque_func_wrapper(args):
            new_args = []

            if type_codes is not None:
                args_type_codes = type_codes
            else:
                args_type_codes = [TypeCode(args[i].get_type_code_int())
                                   for i in range(len(args))]

            for tc, arg_ in zip(args_type_codes, args):
                if tc not in OpaqueFunc.type_codes_:
                    raise ValueError(f"Unsupported type code: {tc}")
                arg_ = OpaqueFunc.type_codes_[tc][0](arg_)
                new_args.append(arg_)

            func(*new_args)

        arg_type_codes_ = lpx.IntVector([tc.value for tc in type_codes])
        self._of.set_func(opaque_func_wrapper, arg_type_codes_)

    def __call__(self, *args: Any) -> None:
        """ Call internal lib OpaqueFunc with provided args """

        args_type_codes = self.get_arg_type_codes()

        if len(args) != len(args_type_codes):
            raise ValueError("Invalid number of arguments detected."
                             " OpaqueFunc is expecting {} arguments"
                             " but got: {}"
                             .format(len(args_type_codes), len(args)))

        oa_v = []
        for tc, arg_ in zip(args_type_codes, args):
            if tc not in OpaqueFunc.type_codes_:
                raise ValueError(f"Unsupported type code: {tc}")
            oa_v.append(OpaqueFunc.type_codes_[tc][1](arg_))

        oa = lpx.OpaqueArgs(oa_v)

        self._of(oa)

    def get_arg_type_codes(self):
        return [TypeCode(i) for i in self._of.get_arg_type_codes()]

    def get_nb_type_codes(self):
        return len(self.get_arg_type_codes())

    def __del__(self):
        pass