我在第三方应用程序中使用jython。第三方应用程序有一些内置库foo
。为了做一些(单元)测试,我们希望在应用程序之外运行一些代码。由于foo
被绑定到应用程序,我们决定编写我们自己的模拟实现。如何更改导入库的名称?
但是有一个问题,我们在python中实现了我们的模拟类,而他们的类是在java中。因此,为了使用他们的代码,人们会做import foo
,并且之后foo是模拟类。但是,如果我们像这样导入python模块,我们将模块附加到名称上,因此必须编写foo.foo
才能进入该类。
为了方便的原因,我们很乐意能写from ourlib.thirdparty import foo
绑定foo
到foo
- 类。但是我们希望避免直接导入ourlib.thirdparty
中的所有类,因为每个文件的加载时间需要相当长的一段时间。
有什么办法可以在python中做到这一点? (我没有导入挂钩跑远我想简单地从load_module
返回类或覆盖了我写sys.modules
(我认为这两种方法都是丑陋的,尤其是后来))
编辑:
OK :这里是ourlib.thirdparty文件是什么样子的简化(没有魔法):
foo.py:
try:
import foo
except ImportError:
class foo
....
ACTU盟友他们看起来像这样:
foo.py:
class foo
....
__init__.py in ourlib.thirdparty
import sys
import os.path
import imp
#TODO: 3.0 importlib.util abstract base classes could greatly simplify this code or make it prettier.
class Importer(object):
def __init__(self, path_entry):
if not path_entry.startswith(os.path.join(os.path.dirname(__file__), 'thirdparty')):
raise ImportError('Custom importer only for thirdparty objects')
self._importTuples = {}
def find_module(self, fullname):
module = fullname.rpartition('.')[2]
try:
if fullname not in self._importTuples:
fileObj, self._importTuples[fullname] = imp.find_module(module)
if isinstance(fileObj, file):
fileObj.close()
except:
print 'backup'
path = os.path.join(os.path.join(os.path.dirname(__file__), 'thirdparty'), module+'.py')
if not os.path.isfile(path):
return None
raise ImportError("Could not find dummy class for %s (%s)\n(searched:%s)" % (module, fullname, path))
self._importTuples[fullname] = path, ('.py', 'r', imp.PY_SOURCE)
return self
def load_module(self, fullname):
fp = None
python = False
print fullname
if self._importTuples[fullname][1][2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.PY_FROZEN):
fp = open(self._importTuples[fullname][0], self._importTuples[fullname][1][1])
python = True
try:
imp.load_module(fullname, fp, *self._importTuples[fullname])
finally:
if python:
module = fullname.rpartition('.')[2]
#setattr(sys.modules[fullname], module, getattr(sys.modules[fullname], module))
#sys.modules[fullname] = getattr(sys.modules[fullname], module)
if isinstance(fp, file):
fp.close()
return getattr(sys.modules[fullname], module)
sys.path_hooks.append(Importer)
':
所以,下面的代码将也完全为你工作? –
什么是'ourlib.thirdparty'?那是你写的模拟python代码,还是java代码? – mgilson
你需要'ourlib.thirdparty'中的其他类吗?你的第三段有点含糊。 – deadly