当前位置: 首页>>代码示例>>Python>>正文


Python imp.load_compiled方法代码示例

本文整理汇总了Python中imp.load_compiled方法的典型用法代码示例。如果您正苦于以下问题:Python imp.load_compiled方法的具体用法?Python imp.load_compiled怎么用?Python imp.load_compiled使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在imp的用法示例。


在下文中一共展示了imp.load_compiled方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load_functions

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def load_functions(filepath=None):
    if not filepath:
        return None

    if not os.path.isfile(filepath):
        raise IOError("'{}' not found".format(filepath))

    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)

    else:
        raise ValueError("'{}' does not have the .py or .pyc extension".format(filepath))

    return py_mod 
开发者ID:jpmens,项目名称:mqttwarn,代码行数:21,代码来源:util.py

示例2: load_module

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
开发者ID:glmcdona,项目名称:meddle,代码行数:25,代码来源:ihooks.py

示例3: load_module_pyc

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def load_module_pyc(module_id, path):
        with open(path, 'rb') as fp:
            mod = imp.load_compiled(module_id, path, fp)
            # no source encoding here
            return mod 
开发者ID:jpush,项目名称:jbox,代码行数:7,代码来源:compat.py

示例4: __doUpdate

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def __doUpdate(fromVersion, toVersion):

        updateScriptsDir = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "dbUpdateScripts"))
        if not os.path.exists(updateScriptsDir):
            return False

        for version in range(fromVersion + 1, toVersion + 1):
            moduleName = "updateV%d" % (version)
            scriptName = moduleName + ".py"
            scriptPath = os.path.join(updateScriptsDir, scriptName)
            compiled = False

            if not os.path.exists(scriptPath):
                scriptName = moduleName + ".pyc"
                scriptPath = os.path.join(updateScriptsDir, scriptName)
                compiled = True

            log.info("... applying database upgrade: %s" % scriptPath)

            if not os.path.exists(scriptPath):
                return False

            success = False
            try:
                if compiled:
                    module = imp.load_compiled(moduleName, scriptPath)
                else:
                    module = imp.load_source(moduleName, scriptPath)

                try:
                    success = module.performUpdate()
                except Exception, e:
                    log.error(e)

                del sys.modules[moduleName]

            except Exception, e:
                log.error(e)
                return False 
开发者ID:sprinkler,项目名称:rainmachine-developer-resources,代码行数:41,代码来源:rmDatabaseUpdate.py

示例5: load_compiled

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
开发者ID:glmcdona,项目名称:meddle,代码行数:4,代码来源:ihooks.py

示例6: test_imp_load_compiled

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def test_imp_load_compiled(self):
        #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=17459
        self.assertEqual(imp.load_compiled("", ""), None)
        try:
            _x_mod = os.path.join(self.test_dir, "x.py")
            self.write_to_file(_x_mod, "")
            with open(_x_mod, "r") as f:
                self.assertEqual(imp.load_compiled("", "", f), None)
        finally:
            os.unlink(_x_mod) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:12,代码来源:test_imp.py

示例7: load_from_file

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def load_from_file(self, path, dbname, key):
        class_inst = None
        expected_class = 'Parser'

        try:
            ad = os.path.abspath(os.path.join(tools.ustr(config['root_path']), u'addons'))
            mod_path_list = map(lambda m: os.path.abspath(tools.ustr(m.strip())), config['addons_path'].split(','))
            mod_path_list.append(ad)
            mod_path_list = list(set(mod_path_list))

            for mod_path in mod_path_list:
                if os.path.lexists(mod_path + os.path.sep + path.split(os.path.sep)[0]):
                    filepath = mod_path + os.path.sep + path
                    filepath = os.path.normpath(filepath)
                    sys.path.append(os.path.dirname(filepath))
                    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
                    mod_name = '%s_%s_%s' % (dbname, mod_name, key)

                    if file_ext.lower() == '.py':
                        py_mod = imp.load_source(mod_name, filepath)

                    elif file_ext.lower() == '.pyc':
                        py_mod = imp.load_compiled(mod_name, filepath)

                    if expected_class in dir(py_mod):
                        class_inst = py_mod.Parser
                    return class_inst
                elif os.path.lexists(mod_path + os.path.sep + path.split(os.path.sep)[0] + '.zip'):
                    zimp = zipimport.zipimporter(mod_path + os.path.sep + path.split(os.path.sep)[0] + '.zip')
                    return zimp.load_module(path.split(os.path.sep)[0]).parser.Parser
        except SyntaxError, e:
            raise osv.except_osv(_('Syntax Error !'), e) 
开发者ID:iw3hxn,项目名称:LibrERP,代码行数:34,代码来源:report_xml.py

示例8: load_module

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def load_module(self, path):
        module_name, file_ext = os.path.splitext(os.path.split(path)[-1])

        if file_ext.lower() == '.py':
            module = imp.load_source(module_name, path)
        elif file_ext.lower() == '.pyc':
            module = imp.load_compiled(module_name, path)

        return module 
开发者ID:cloudbase,项目名称:cloudbase-init,代码行数:11,代码来源:classloader.py

示例9: load_module_pyc

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def load_module_pyc(module_id, path):  # noqa
        with open(path, "rb") as fp:
            mod = imp.load_compiled(module_id, path, fp)
            # no source encoding here
            del sys.modules[module_id]
            return mod 
开发者ID:sqlalchemy,项目名称:alembic,代码行数:8,代码来源:compat.py

示例10: function_importer

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def function_importer(mod_str): # pylint: disable=too-complex
    """Import Module from external source"""
    mod_split = mod_str.split(":")
    if len(mod_split) != 2:
        logger.error("Can not import function", mod=mod_str)
        return None

    mod_path = mod_split[0]
    funct_name = mod_split[1].split('.')

    path, filename = os.path.split(mod_path)
    mod_name, ext = os.path.splitext(filename) # pylint: disable=unused-variable
    mod = None

    # try to load precompiled in first if it exists
    if os.path.exists(os.path.join(path, mod_name)+'.pyc'):
        try:
            mod = imp.load_compiled(mod_name, mod_path)
        except: # pylint: disable=bare-except
            pass

    if os.path.exists(os.path.join(path, mod_name)+'.py'):
        try:
            mod = imp.load_source(mod_name, mod_path)
        except Exception as e:
            logger.error("No Class to import", mod=mod_str, error=e.message)

    # Pull function if embedded in classes
    for i, mod_part in enumerate(funct_name):
        if mod and hasattr(mod, mod_part):
            if i == len(funct_name) - 1:
                if len(funct_name) > 1:
                    return getattr(mod(), mod_part)
                return getattr(mod, mod_part)
            mod = getattr(mod, mod_part)

    logger.error("Function not valid/callable", mod=mod_str)
    return None 
开发者ID:Bridgewater,项目名称:appetite,代码行数:40,代码来源:helpers.py

示例11: test_imp_load_compiled

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def test_imp_load_compiled(self):
        #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=17459
        with self.assertRaises(FileNotFoundError):
            imp.load_compiled("", "") 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:6,代码来源:test_imp.py

示例12: load_from_file

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def load_from_file(filepath):
    mod_name,file_ext = os.path.splitext(os.path.split(filepath)[-1])
    if mod_name.startswith("__init__"): 
        return 
    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)
    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)        
    else:
        return None
    class_inst = getattr(py_mod, mod_name)()
    return class_inst 
开发者ID:opentelekomcloud,项目名称:python-otcclient,代码行数:14,代码来源:pluginmanager.py

示例13: test_load_compiled

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def test_load_compiled(self):
        compiled = os.__file__
        if compiled.endswith('.py'):
            compiled = compiled[:-3] + COMPILED_SUFFIX

        os.__doc__ = 'foo'
        self.assertEqual(os, imp.load_compiled("os", compiled))
        self.assertFalse(os.__doc__ == 'foo')
        with open(compiled, 'rb') as fp:
            os.__doc__ = 'foo'
            self.assertEqual(os, imp.load_compiled("os", compiled, fp))
            self.assertFalse(os.__doc__ == 'foo') 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:14,代码来源:test_import_jy.py

示例14: test_imp_load_compiled

# 需要导入模块: import imp [as 别名]
# 或者: from imp import load_compiled [as 别名]
def test_imp_load_compiled(self):
        __import__(self.mod_name)
        self.assertTrue(os.path.exists(self.bytecode))
        basename = os.path.basename(self.bytecode)
        mod = imp.load_compiled(self.mod_name, basename)
        self.assertEqual(mod.__file__, basename) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:8,代码来源:test_chdir.py


注:本文中的imp.load_compiled方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。