本文整理匯總了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
示例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
示例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
示例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
示例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)
示例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)
示例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)
示例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
示例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
示例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
示例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("", "")
示例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
示例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')
示例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)