PyDev:模块的 @DynamicAttrs

发布于 2024-12-06 05:01:09 字数 754 浏览 1 评论 0原文

在我正在编写的程序中,我创建了一个名为 settings 的模块,它声明了一些常量,但从配置文件加载其他常量,将它们放置在模块命名空间中(例如:π 的值可能是模块的代码,但配置文件中用户的权重)。

这样,在其他模块中,我可以执行以下操作:

from settings import *

一切对我来说都工作正常,但是 - 使用 Aptana Studio / PyDev,代码分析工具会抛出很多未定义的变量错误,如下所示:

在此处输入图像描述

我发现 这里有一个标志可用于防止类文档字符串中的这种行为,但如果我尝试在模块级别使用它,它不会产生任何效果。所以我想知道两件事:

  • 有没有办法有选择地消除这些错误(这意味着我不想完全关闭“将未定义变量标记为错误”选项:在其他模块中它实际上可能是一个错误)?

  • 如果没有,是否有一种替代模式可以在野生导入方面实现我想要的目标,但又不会混淆代码分析工具?

先发制人的说明:我完全清楚不鼓励野生进口的事实。

In the program I am writing, I created a module called settings that declares a few constants but loads other from a configuration file, placing them in the module namespace (for example: the value of π might be in the code of the module, but the weight of the user in a configuration file).

This is such that in other modules, I can do:

from settings import *

Everything works fine for me but - using Aptana Studio / PyDev, the code analysis tool throws a lot of undefined variable errors like this:

enter image description here

I found here that there is a flag usable to prevent this behaviour in class docstrings, but it has no effect if I try to use it at module level. So I wonder two things:

  • Is there a way to selectively get rid of these errors (meaning that I wouldn't want to completely turn off the option "mark as errors the undefined variables": in other modules it could in fact be an error)?

  • If not, is there an alternative pattern to achieve what I want in terms of wild imports, but without confusing the code analysis tool?

Pre-emptive note: I am perfectly aware of the fact wild imports are discouraged.

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

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

发布评论

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

评论(2

溺渁∝ 2024-12-13 05:01:09

实际上,即使不是疯狂导入,您也可能会遇到相同的错误(即:导入设置/settings.MY_VARIABLE 仍会显示错误,因为代码分析找不到它)。

除了引用它的每个地方的 @UndefinedVariable 之外(CTRL+1 将显示该选项),我认为对于您来说这是一个更好的模式模块将是:

MY_VARIABLE = 'default value'
...

update_default_values() # Go on and override the defaults.

这样,代码分析(以及阅读您的模块的任何人)就会知道需要哪些变量。

否则,如果您以前不知道它们,我认为更好的方法是拥有一个方法(即:get_settings('MY_VARIABLE'))。

与实际问题无关。我真的建议不要在这里使用野生导入(甚至不导入常量...即:from settings import MY_VARIABLE)。

设置模块的更好方法始终是使用:(

import settings
settings.MY_VARIABLE 

因为否则,如果任何地方决定要更改 MY_VARIABLE,则任何将引用放入其自己的命名空间的地方可能永远不会获得更改的变量)。

更安全的方法是使用方法 get_setting('var'),因为它可以让您更好地延迟加载您的首选项(即:不在导入时加载,但在导入时加载)第一次打电话)。

Actually you'd probably have the same error even if it wasn't a wild import (i.e.: import settings / settings.MY_VARIABLE would still show an error because the code-analysis can't find it).

Aside from the @UndefinedVariable in each place that references it (CTRL+1 will show that option), I think that a better pattern for your module would be:

MY_VARIABLE = 'default value'
...

update_default_values() # Go on and override the defaults.

That way, the code-analysis (and anyone reading your module), would know which variables are expected.

Otherwise, if you don't know them before, I think a better approach would be having a method (i.e.: get_settings('MY_VARIABLE')).

Unrelated to the actual problem. I'd really advise against using a wild import here (nor even importing the constant... i.e.: from settings import MY_VARIABLE).

A better approach for a settings module is always using:

import settings
settings.MY_VARIABLE 

(because otherwise, if any place decides it wants to change the MY_VARIABLE, any place that has put the reference in its own namespace will probably never get the changed variable).

An even safer approach would be having a method get_setting('var'), as it would allow you to a better lazy-loading of your preferences (i.e.: don't load on import, but when it's called the 1st time).

初雪 2024-12-13 05:01:09

您可以在错误上使用 Ctrl-1 并选择 @UndefineVariable 或在包含您想要忽略的错误的行上键入 #@UndefineVariable

您可以尝试通过转到 Window > 来添加要由 PyDev 解释器扫描的模块。首选项,然后 PyDev >解释器——Python。在“库”选项卡下,单击新建文件夹并浏览到包含设置的目录,然后单击应用。希望 Pydev 能够找到您的包并识别大量导入的变量。

You can use Ctrl-1 on an error and choose @UndefinedVariable or type #@UndefinedVariable on a line that has an error you want to ignore.

You can try to add your module to be scanned by the PyDev interpreter by going to Window > Preferences, then PyDev > Interpreter - Python. Under the Libraries tab, click New Folder and browse to the directory that contains settings, then click Apply. Hopefully, Pydev will find your package and recognize the wildly-imported variables.

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