예제 #1
0
    def instantiate(self, module_name=None):
        """Instantiate a codebase on disk and load it."""
        with _codebase_lock:
            if self.hash not in _codebase_cache:
                try:
                    if not os.path.exists(_codebase_instantiation_dir):
                        os.makedirs(_codebase_instantiation_dir)
                except Exception:
                    logging.getLogger(__name__).warn(
                        "Exception trying to make directory %s",
                        _codebase_instantiation_dir)

                disk_path = os.path.join(_codebase_instantiation_dir,
                                         self.hash)

                #preload the files, since they're lazy.
                object_database.current_transaction().db().requestLazyObjects(
                    set(self.files.values()))

                fileContents = {
                    fpath: file.contents
                    for fpath, file in self.files.items()
                }

                _codebase_cache[self.hash] = TypedPythonCodebase.Instantiate(
                    fileContents, disk_path)

            if module_name is None:
                return _codebase_cache[self.hash]

            return _codebase_cache[self.hash].getModuleByName(module_name)
예제 #2
0
    def test_instantiated_codebase(self):
        codebase = Codebase.Instantiate({
            'test_module/__init__.py': '',
            'test_module/inner.py': 'f = lambda: 10',
        })
        self.assertEqual(codebase.getClassByName('test_module.inner.f')(), 10)

        codebase2 = Codebase._FromModule(codebase.getModuleByName("test_module"))
        self.assertEqual(codebase2.getClassByName('test_module.inner.f')(), 10)

        self.assertEqual(codebase.filesToContents, codebase2.filesToContents)
예제 #3
0
    def test_basic(self):
        codebase = Codebase.Instantiate({
            "a.py":
            textwrap.dedent("""
            from typed_python import Function, ListOf, OneOf

            @Function
            def g(x):
                return x+x

            @Function
            def f(x: float):
                y = 0
                while x > 0:
                    x -= 1
                    y += g(x)
                return y
            """)
        })

        f = codebase.getClassByName("a.f")

        t0 = time.time()
        compiledCodebase = CodebaseCompiler.compile(codebase)
        compilation_time = time.time() - t0

        t0 = time.time()
        f(100000)
        f_time_first = time.time() - t0

        t0 = time.time()
        compiledCodebase.install()
        install_time = time.time() - t0

        t0 = time.time()
        f(100000)
        f_time_second = time.time() - t0

        self.assertTrue(f_time_second < f_time_first * .1)
        self.assertTrue(install_time < compilation_time * 0.1)