gpt4 book ai didi

python - 创建一个在运行时创建子模块的伪模块

转载 作者:行者123 更新时间:2023-12-01 02:24:59 24 4
gpt4 key购买 nike

为了支持我的 Python 项目中的扩展,我正在尝试创建一个伪模块,它将作为“扩展模块”的子模块提供服务。我在将子模块视为模块时遇到问题 - 似乎我需要在主伪模块上使用 from..import 访问它们,而不能只访问它们的完整路径。

这是一个最小的工作示例:

import sys
from types import ModuleType


class Foo(ModuleType):
@property
def bar(self):
# Here I would actually find the location of `bar.py` and load it
bar = ModuleType('foo.bar')
sys.modules['foo.bar'] = bar
return bar


sys.modules['foo'] = Foo('foo')

from foo import bar # without this line the next line fails
import foo.bar

这可行,但如果我注释掉 from foo import bar 行,它将失败:

ImportError: No module named bar

在 Python2 和 Python3 上,它会失败:

ModuleNotFoundError: No module named 'foo.bar'; 'foo' is not a package

如果我添加字段以使其成为一个包:

class Foo(ModuleType):
__all__ = ('bar',)
__package__ = 'foo'
__path__ = []
__file__ = __file__

它会失败:

ModuleNotFoundError: No module named 'foo.bar'

据我了解,问题是我还没有设置sys.modules['foo.bar']。但是...为了填充 sys.modules,我需要首先加载模块,除非我的项目的用户显式导入它,否则我不想这样做。

有什么方法可以让Python意识到,当它看到import foo.bar时,它需要先加载foo(或者我可以保证foo 此时已经被加载)并从中获取 bar

最佳答案

这篇文章不会回答“你就是这样做的。”如果您想知道如何自己执行此操作,请查看 PEP 302Idan Arye's solution这篇文章提供了一个可以轻松编写的食谱。该食谱位于本答案的末尾。

<小时/>

下面的代码块定义了两个可供使用的类:PseudoModulePseudoPackage。该行为仅与 import foo.x 是否应引发错误,指出 foo 不是包 或尝试加载 x 并确保它是一个模块。下面概述了几个示例用途。

伪模块

PseudoModule 可以用作函数的装饰器,它创建一个新的模块对象,当第一次访问属性时,它使用属性名称和命名空间调用装饰函数先前定义的元素。

例如,这将创建一个为每个访问的属性分配一个新整数的模块:

@PseudoModule
def access_tracker(attr, namespace):
namespace["_count"] = namespace.get("_count", -1) + 1
return namespace["_count"]

#PseudoModule will set `namespace[attr] = <return value>` for you
#this can be overriden by passing `remember_results=False` to the constructor

sys.modules["access_tracker"] = access_tracker

from access_tracker import zero, one, two, three

assert zero == 0 and one == 1 and two == 2 and three == 3

伪包

PseudoPackage 的使用方式与 PseudoModule 相同,但是如果修饰函数返回一个模块(或包),它将更正名称以使其符合子包的资格,并且 sys.modules 根据需要进行更新。 (顶层包仍然需要手动添加到sys.modules中)

以下是PseudoPackage的使用示例:

spam_submodules = {"bacon"}
spam_attributes = {"eggs", "ham"}

@PseudoPackage
def spam(name, namespace):
print("getting a component of spam:", name)
if name in spam_submodules:
@PseudoModule
def submodule(attr, nested_namespace):
print("getting a component of submodule {}: {}".format(name, attr))
return attr #use the string of the attribute
return submodule #PseudoPackage will rename the module to be spam.bacon for us
elif name in spam_attributes:
return "supported attribute"
else:
raise AttributeError("spam doesn't have any {!r}.".format(name))

sys.modules["spam"] = spam

import spam.bacon
#prints "getting a component of spam: bacon"

assert spam.bacon.something == "something"
#prints "getting a component of submodule bacon: something"

from spam import eggs
#prints "getting a component of spam: eggs"
assert eggs == "supported attribute"

import spam.ham #ham isn't a submodule, raises error!

PseudoPackage 的设置方式也使得任意深度包变得非常容易,尽管这个特定的示例并没有完成太多工作:

def make_abstract_package(qualname = ""):
"makes a PseudoPackage that has arbitrary nesting of subpackages"
def gen_func(attr, namespace):
print("getting {!r} from package {!r}".format(attr, qualname))
return make_abstract_package("{}.{}".format(qualname, attr))
#can pass the name of the module as second argument if needed
return PseudoPackage(gen_func, qualname)

sys.modules["foo"] = make_abstract_package("foo")

from foo.bar.baz import thing_I_want
##prints:
# getting 'bar' from package 'foo'
# getting 'baz' from package 'foo.bar'
# getting 'thing_I_want' from package 'foo.bar.baz'
print(thing_I_want)
#prints "<module 'foo.bar.baz.thing_I_want' from '<PseudoPackage>'>"
<小时/>

有关实现的一些说明

作为一般准则:

  • 计算模块属性的函数不应导入为其定义属性的模块
  • 如果您希望某个包或模块可供导入,您需要自行将其放入 sys.modules 中。
  • PseudoPackage 假设每个子模块都是唯一的,不要重用模块对象。

还值得注意的是,当 import 语句要求名称为模块时,sys.modules 仅使用 PseudoPackage 的子模块进行更新,例如,如果foosys.modules 中已有的包,但 foo.x 尚未被引用,那么所有这些断言都将通过:

assert "foo.x" not in sys.modules and not hasattr(foo,"x")
import foo; foo.x #foo.x is computed but not added to sys.modules
assert "foo.x" not in sys.modules and hasattr(foo,"x")
from foo import x #x is retrieved from namespace but sys.modules is still not affected
assert "foo.x" not in sys.modules

import foo.x #if x is a module then "foo.x" is added to sys.modules
assert "foo.x" in sys.modules

在上述情况下,如果 foo.x 不是模块,则语句 import foo.x 会引发 ModuleNotFoundError

最后,虽然我发现的有问题的边缘情况可以通过遵循上述指南来避免,但 _PseudoPackageLoader 的文档字符串描述了导致 future 可能修改的不良行为的实现细节。

<小时/>

食谱

import sys
from types import ModuleType
import importlib.abc #uses Loader and MetaPathFinder, more for inspection purposes then use

class RawPseudoModule(ModuleType):
"""
see PseudoModule for documentation, this class is not intended for direct use.

RawPseudoModule does not handle __path__ so the generating function of direct
instances are expected to make and return an appropriate value for __path__

*** if you do not know what an appropriate value for __path__ is
then use PseudoModule instead ***
"""
#using slots keeps these two variables out of the module dictionary
__slots__ = ["__generating_func", "__remember_results"]
def __init__(self, func, name=None, remember_results = True):
name = name or func.__name__
super(RawPseudoModule, self).__init__(name)
self.__file__ = "<{0.__class__.__name__}>".format(self)
self.__generating_func = func
self.__remember_results = remember_results

def __getattr__(self, attr):
value = self.__generating_func(attr, vars(self))
if self.__remember_results:
setattr(self, attr, value)
return value


class PseudoModule(RawPseudoModule):
"""
A module that has attributes generated from a specified function

The generating function passed to the constructor should have the signature:
f(attr:str, namespace:dict) -> object:
- attr is the name of the attribute accessed
- namespace is the currently defined values in the module

the function should return a value for the attribute or raise an AttributeError if it doesn't exist.

by default the result is then saved to the namespace so you don't
have to explicitly do "namespace[attr] = <value>" however this behaviour
can be overridden by specifying "remember_results = False" in the constructor.

If no name is specified in the constructor the function name will be
used for the module name instead, this allows the class to be used as a decorator

Note: the PseudoModule class is setup so that "import foo.bar"
when foo is a PseudoModule will fail stating "'foo' is not a package".
- to allow importing submodules use PseudoPackage.
- to handle the internal __path__ manually use RawPseudoPackage.

Note: the module is NOT added to sys.modules automatically.
"""
def __getattr__(self, attr):
#to not have submodules then __path__ must not exist
if attr == "__path__":
msg = "{0.__name__} is a PseudoModule, it is not a package so it doesn't have a __path__"
#this error message would only be seen by people who explicitly access __path__
raise AttributeError(msg.format(self))
return super(PseudoModule, self).__getattr__(attr)

class PseudoPackage(RawPseudoModule):
"""
A version of PseudoModule that sets itself up to allow importing subpackages

When a submodule is imported from a PseudoPackage:
- it is evaluated with the generating function.
- the name of the submodule is overriden to be correctly qualified
- and it is added to sys.modules to allow repeated imports.

Note: the top level package still needs to be added to sys.modules manually

Note: A RecursionError will be raised if the code that generates submodules
attempts to import another submodule from the PseudoPackage.
"""
#IMPLEMENTATION DETAIL: technically this doesn't deal with adding submodules to
# sys.modules, that is handled in _PseudoPackageLoader
# which explicitly checks for instances of PseudoPackage

__path__ = [] #packages must have a __path__ to be recognized as packages.

def __getattr__(self, attr):
value = super(PseudoPackage, self).__getattr__(attr)
if isinstance(value, ModuleType):
#I'm just going to say if it's a module then the name must be in this format.
value.__name__ = self.__name__ + "." + attr
return value


class _PseudoPackageLoader(importlib.abc.Loader, importlib.abc.MetaPathFinder):
"""
Singleton finder and loader for pseudo packages

When ever a subpackage of a PseudoPackage (that is already in sys.modules) is imported
this will handle loading it and adding the subpackage to sys.modules

Note that although PEP 302 states the finder should not depend on the parent
being loaded in sys.modules, this is implemented under the understanding that
the user of PseudoPackage will add their module to sys.modules manually themselves
so this will work only when the parent is present in sys.modules

Also PEP 302 indicates the module should be added to sys.modules first in case
it is imported during it's execution, however this is impossible due to the
nature of how the module actually gets loaded.
So for heaven's sake don't try to import a pseudo package or a module that uses
a pseudo package from within the code that generates it.

I have only tested this when the sub module is either PseudoModule or PseudoPackage
and it was created new from the generating function, ideally there would be a way
to allow the generating function to return an unexecuted module and this would
properly handle executing it but I don't know how to deal with that.
"""
def find_module(self, fullname, path):
#this will only support loading if the parent package is a PseudoPackage
base,_,_ = fullname.rpartition(".")
if isinstance(sys.modules.get(base), PseudoPackage):
return self
#I found that `if path is PseudoPackage.__path__` worked the same way for all the cases I tested
#however since load_module will fail if the base part isn't in sys.modules
# it seems safer to just check for that.


def load_module(self, fullname):
if fullname in sys.modules:
return sys.modules[fullname]
base,_,sub = fullname.rpartition(".")
parent = sys.modules[base]
try:
submodule = getattr(parent, sub)
except AttributeError:
#when we just access `foo.x` it raises an AttributeError
#but `import foo.x` should instead raise an ImportError
raise ImportError("cannot import name {!r}".format(sub))

if not isinstance(submodule, ModuleType):
#match the format of error raised when the submodule isn't a module
#example: `import sys.path` raises the same format of error.
raise ModuleNotFoundError("No module named {}".format(fullname))
#fill all the fields as described in PEP 302 except __name__
submodule.__loader__ = self
submodule.__package__ = base
submodule.__file__ = getattr(submodule, "__file__", "<submodule of PseudoPackage>")
#if there was a way to do this before the module was made that'd be nice
sys.modules[fullname] = submodule
#if we needed to execute the body of an unloaded module it'd be done here.
return submodule

#add the loader to sys.meta_path so it will handle our pseudo packages
sys.meta_path.append(_PseudoPackageLoader())

关于python - 创建一个在运行时创建子模块的伪模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47502063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com