清洁Python导入语句
我为REST API创建了一个Python包装器。 API有很多端点,因此我为每个端点创建了一个文件。
总体上的想法是,用户可以这样做,
from my_package import Client
from my_package.endpoints import Users
prod = Client("myhostname.com", "Username", "Password")
all_users = Users(prod).list()
我的软件包的文件当前是这样的,
my_project/
└-- my_package/
| └-- __init__.py
| └-- client.py
| └-- const.py
| └-- endpoints/
| └-- __init__.py
| └-- contracts.py
| └-- credits.py
| └-- payments.py
| └-- users.py
└-- setup.cfg
└-- readme.md
└-- pyproject.toml
# my_package/__init__.py
from my_package.client import Client
# my_package/endpoints/__init__.py
from my_package.endpoints.contracts import Contracts
from my_package.endpoints.creditsimport Credits
from my_package.endpoints.paymentsimport Payments
from my_package.endpoints.usersimport Users
现在问题是,当您导入时,通过Intellisense为您提供了许多重复。
from my_package.endpoints import contracts
Contracts
credits
Credits
payments
Payments
users
Users
我该如何清理?
I've created a python wrapper for a REST API. The API has a lot of endpoints, so I created a single file for every endpoint.
The general idea is that the user can do something like this
from my_package import Client
from my_package.endpoints import Users
prod = Client("myhostname.com", "Username", "Password")
all_users = Users(prod).list()
The files of my package are currently structured like this
my_project/
└-- my_package/
| └-- __init__.py
| └-- client.py
| └-- const.py
| └-- endpoints/
| └-- __init__.py
| └-- contracts.py
| └-- credits.py
| └-- payments.py
| └-- users.py
└-- setup.cfg
└-- readme.md
└-- pyproject.toml
# my_package/__init__.py
from my_package.client import Client
# my_package/endpoints/__init__.py
from my_package.endpoints.contracts import Contracts
from my_package.endpoints.creditsimport Credits
from my_package.endpoints.paymentsimport Payments
from my_package.endpoints.usersimport Users
The problem now, is that when you import, you are provided with a lot of duplicates, through intellisense.
from my_package.endpoints import contracts
Contracts
credits
Credits
payments
Payments
users
Users
How can I clean this up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是正确的行为。为了将IntelliSense建议仅减少到类名称,您需要将模块移至另一个软件包。
例如
,您当然可以以不同的方式命名包裹,也可以选择适合您需求的另一个结构。例如,您可以将
contracts.py
和朋友移动到impl
subpackage,并保留endpoints/__ init __. py
文件,只需稍有修改在其中。或者,您可以将Contracts.py
和朋友移至另一个软件包。选项数量是无限的。关键是Intellisense始终建议您在给定模块中的所有名称。它们是子模块名称,班级名称还是任何其他名称。这就是用户通常想要的及其所用的东西。我们通常使用文档作为通过库中的用户导航而不是IntelliSense的方式。但是,如果建议对您来说确实很重要,则必须将名称移到模块之外。 (或者使用导入系统和Dunder属性进行一些丑陋的技巧。)
I think that this is the correct behavior. To reduce the intellisense suggestions to the class names only, you would need to move the modules to another package.
E.g.
You can, of course, name the packages differently or choose another structure that fits your needs. For example, you can move
contracts.py
and friends to animpl
subpackage and keep theendpoints/__init__.py
file just with slightly modified imports in it. Or you can movecontracts.py
and friends to another package. The number of options is unlimited.The point is that intellisense always suggests you all the names in the given module. Whether they are submodule names, class names, or any other. And that's what users usually want and what they are used to. We usually use documentation as the way to navigate users through our libraries, not intellisense. But if the suggestions really matter to you, you have to move the names outside of the module. (Or do some ugly tricks with import system and dunder attributes.)