PEP 302 示例:新导入挂钩

发布于 2024-07-23 12:23:24 字数 196 浏览 2 评论 0 原文

在哪里可以找到 PEP 302?

我想以尽可能向前兼容的方式实现自定义查找器和加载器。 换句话说,该实现应该可以在 python 2.x 和 3.x 中工作。

Where can I find an example implementation of the "New Import Hooks" described in PEP 302?

I would like to implement a custom finder and loader in the most forward compatible way possible. In other words, the implementation should work in python 2.x and 3.x.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2024-07-30 12:23:24

您可以找到数千个开源示例,例如使用 谷歌代码搜索,这里是:

http://www.google.com/codesearch?hl=en&lr=&q="imp.find_module"+"imp.load_module"&sbtn=Search

编辑:正如提问者澄清的那样,他正在寻找实现的示例,而不是使用,更好的 网址搜索的 是:

http://www.google.com/codesearch?hl=en&sa=N&q="path_hooks"++lang:python&ct=rr&cs_r=lang:python

一个可读的示例(尽管不适合生产使用,因为 reddit讨论指出!)是urlimport

至于同时支持 Python 2 和 Python 3,这听起来雄心勃勃——我不知道有任何现有的导入钩子声称如此。 站在你的立场上,我会首先提供对 Python 2.6 的全面支持,然后一旦它起作用(并且进行了一系列良好的测试并且不使用 -3 开关),我就会2to3 源代码,看看是否有任何问题(如果有,找出原因,修复 2.6 源代码,迭代)。

You can find thousands of open-source examples e.g. with a google code search, here it is:

http://www.google.com/codesearch?hl=en&lr=&q="imp.find_module"+"imp.load_module"&sbtn=Search

Edit: as the questioner clarified he's looking for example of implementation, not use, a better URL for the search is:

http://www.google.com/codesearch?hl=en&sa=N&q="path_hooks"++lang:python&ct=rr&cs_r=lang:python

One readable example (though NOT suitable for production use, as the reddit discussion points out!) is urlimport.

As for supporting Python 2 and Python 3 at the same time, that sounds ambitious -- I don't know of any existing import hook which claims to. In your shoes, I'd start with offering full support for Python 2.6, then once that's working (and has a good battery of tests and makes nary a peep with the -3 switch), I'd 2to3 the sources and see if anything breaks (if so, find out why, fix the 2.6 sources, iterate).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文