Exemplo n.º 1
0
 def test_serialization(self):
     name = "obj"
     obj = {"test":1}
     serialization.serialize(obj, name)
     assert os.path.join(config.prefix, "obj.pck")
     
     obj2 = serialization.unserialize(name)
     assert obj == obj2
Exemplo n.º 2
0
Arquivo: jit.py Projeto: CSRedRat/hope
def jit(fkt):
    """
    Compiles a function to native code and return the optimized function. The new function has the performance of a compiled function written in C.

    :param fkt: function to compile to c
    :type fkt: function
    :returns: function -- optimized function

    This function can either be used as decorator

    .. code-block:: python

        @jit
        def sum(x, y):
            return x + y

    or as a normal function

    .. code-block:: python

        def sum(x, y):
            return x + y
        sum_opt = jit(sum)
    """

    if config.hopeless:
        return fkt

    hash = hashlib.sha224(inspect.getsource(fkt).encode('utf-8')).hexdigest()
    filename = "{0}_{1}".format(fkt.__name__, hash)

    if not os.path.exists(config.prefix):
        os.makedirs(config.prefix)

    if not config.prefix in sys.path:
        sys.path.append(os.path.abspath(config.prefix))

    wrapper = Wrapper(fkt, hash)

    try:
        state = serialization.unserialize(filename)
        if not state is None:
            _check_state(fkt, state)
        else:
            raise ImportError("No state found.")

        if sys.version_info[0] == 2:
            module = __import__(state["filename"], globals(), locals(), [], -1)
        else:
            import importlib
            module = importlib.import_module(state["filename"])

        if "bound" in state and state["bound"]:
            def _hope_callback(*args):
                return module.run(*args)
            setattr(cache, str(id(_hope_callback)), fkt)
            return _hope_callback
        else:
            module.set_create_signature(wrapper.create_signature)
            setattr(cache, str(id(module.run)), fkt)
            return module.run

    except LookupError as le:
        if config.verbose:
            warnings.warn("Recompiling... Reason: {0}".format(le))
        wrapper._recompile = True
        return wrapper.callback
    except ImportError as ie:
        return wrapper.callback
Exemplo n.º 3
0
Arquivo: jit.py Projeto: sthagen/hope
def jit(fkt):
    """
    Compiles a function to native code and return the optimized function. The new function has the performance of a compiled function written in C.

    :param fkt: function to compile to c
    :type fkt: function
    :returns: function -- optimized function

    This function can either be used as decorator

    .. code-block:: python

        @jit
        def sum(x, y):
            return x + y

    or as a normal function

    .. code-block:: python

        def sum(x, y):
            return x + y
        sum_opt = jit(sum)
    """

    if config.hopeless:
        return fkt

    argspec = inspect.getargspec(fkt)
    if argspec.varargs is not None or argspec.keywords is not None:
        raise ValueError("Jitted functions should not have *args or **kwargs")

    hash = hashlib.sha224(inspect.getsource(fkt).encode('utf-8')).hexdigest()
    filename = "{0}_{1}".format(fkt.__name__, hash)

    if not os.path.exists(config.prefix):
        os.makedirs(config.prefix)

    if not config.prefix in sys.path:
        sys.path.append(os.path.abspath(config.prefix))

    wrapper = Wrapper(fkt, hash)

    try:
        state = serialization.unserialize(filename)
        if not state is None:
            _check_state(fkt, state)
        else:
            raise ImportError("No state found.")

        if sys.version_info[0] == 2:
            module = __import__(state["filename"], globals(), locals(), [], -1)
        else:
            import importlib
            module = importlib.import_module(state["filename"])

        if "bound" in state and state["bound"]:

            def _hope_callback(*args):
                return module.run(*args)

            setattr(cache, str(id(_hope_callback)), fkt)
            return _hope_callback
        else:
            module.set_create_signature(wrapper.create_signature)
            setattr(cache, str(id(module.run)), fkt)
            return module.run

    except LookupError as le:
        if config.verbose:
            warnings.warn("Recompiling... Reason: {0}".format(le))
        wrapper._recompile = True
        return wrapper.callback
    except ImportError as ie:
        return wrapper.callback
Exemplo n.º 4
0
 def test_unserialize_inexistent(self):
     name = "obj1"
     obj = serialization.unserialize(name)
     assert obj == None