清洁Python导入语句

发布于 2025-01-28 15:55:49 字数 1504 浏览 1 评论 0原文

我为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 技术交流群。

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

发布评论

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

评论(1

寒尘 2025-02-04 15:55:49

我认为这是正确的行为。为了将IntelliSense建议仅减少到类名称,您需要将模块移至另一个软件包。

例如

my_project/
 └-- my_package/
      └-- __init__.py
      └-- client.py
      └-- api
      |   └-- endpoints.py
      └-- endpoints/
          └-- __init__.py
          └-- contracts.py
          └-- credits.py
          └-- payments.py
          └-- users.py
# my_package/endpoints/__init__.py
# my_package/api/endpoints.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
from my_package.api.endpoints import Contracts
                                     Credits
                                     Payments
                                     Users

,您当然可以以不同的方式命名包裹,也可以选择适合您需求的另一个结构。例如,您可以将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.

my_project/
 └-- my_package/
      └-- __init__.py
      └-- client.py
      └-- api
      |   └-- endpoints.py
      └-- endpoints/
          └-- __init__.py
          └-- contracts.py
          └-- credits.py
          └-- payments.py
          └-- users.py
# my_package/endpoints/__init__.py
# my_package/api/endpoints.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
from my_package.api.endpoints import Contracts
                                     Credits
                                     Payments
                                     Users

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 an impl subpackage and keep the endpoints/__init__.py file just with slightly modified imports in it. Or you can move contracts.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.)

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