问题:我应该如何组织Python源代码?[关闭]

我正在开始使用Python(现在是时候尝试了),并且我正在寻找一些最佳实践。

我的第一个项目是一个在多个线程中运行命令行实验的队列。我开始得到一个很长的main.py文件,我想将其分解。总的来说,我在寻找:python程序员如何组织多个源文件?有没有适合您的特定结构?

我的具体问题包括:

  1. 每个类都应该放在单独的文件中吗?
  2. 我应该如何组织相对于源代码的单元测试?
  3. 我应该在哪里放置doc注释,尤其是命令行操作的注释?
  4. 如果使用多个目录,如何在它们之间导入类?

我可能会通过反复试验得出一些自己的结论,但是我宁愿从好的东西开始。

I’m getting started with Python (it’s high time I give it a shot), and I’m looking for some best practices.

My first project is a queue which runs command-line experiments in multiple threads. I’m starting to get a very long main.py file, and I’d like to break it up. In general, I’m looking for: How do python programmers organize multiple source files? Is there a particular structure that works for you?

My specific questions include:

  1. Should each class be in a separate file?
  2. How should I organize unit tests relative to source code?
  3. Where should I put doc comments, specifically those for command-line operation?
  4. If I use multiple directories, how do I import classes between them?

I can probably draw some of my own conclusions here by trial and error, but I’d rather start from something good.


回答 0

Eric指向文章很棒,因为它涵盖了组织大型Python代码库的详细信息。

如果您是从Google登陆到这里的,并试图找出如何将一个大型源文件拆分为多个,更易于管理的文件,那么我将简要概述该过程。

假设当前所有内容都在一个名为的文件中main.py

  • 在同一文件夹中创建另一个源文件(utils.py在此示例中,我们称为我们的文件)
  • 将所需的任何类,函数,语句等main.py移入utils.py
  • main.py顶部添加一行:import utils

从概念上讲,此操作是utils在另一个源文件中创建一个新模块。然后,您可以在任何需要的地方导入它。

The article Eric pointed to is awesome because it covers details of organising large Python code bases.

If you’ve landed here from Google and are trying to find out how to split one large source file into multiple, more manageable, files I’ll summarise the process briefly.

Assume you currently have everything in a file called main.py:

  • Create another source file in the same folder (let’s call ours utils.py for this example)
  • Move whatever classes, functions, statements, etc you need from main.py into utils.py
  • In main.py add a single line at the top: import utils

Conceptually what this does is to create a new module called utils in another source file. You can then import it wherever it’s needed.


回答 1

组织代码和测试的方式与使用任何OO语言完全相同。

我做这件事的答案。可能不对,但对我有用

  1. 取决于如何拆分功能。对于我的主要python应用程序,我有1个文件,其中包含用于入口点的类,然后包含功能不同的包
  2. 我使用PyDev进行蚀,并像使用Java一样组织它。
>  Workspace
>     |
>     |-Src
>     |   |-Package1
>     |   |-Package2
>     |   |-main.py
>     |-Test
>         |-TestPackage1
>         |-TestPackage2
  1. 随处使用DocString跟踪所有内容
  2. 确保相关__init__.py文件位于文件夹中之后。这只是一个简单的例子from module import class

The way you should organise your code and tests is exactly the same you would for any OO language.

Answers from the way I do it. It may not be right but works for me

  1. Depends on how your functionality is split. For my main python app I have 1 file with classes for the entry points and then packages of different bits of functionality
  2. I use PyDev for eclipse and organise it like I would for Java.
>  Workspace
>     |
>     |-Src
>     |   |-Package1
>     |   |-Package2
>     |   |-main.py
>     |-Test
>         |-TestPackage1
>         |-TestPackage2
  1. Use DocString everywhere to keep track of everything
  2. After making sure that the relevant __init__.py files are in the folders. its just a simple case of from module import class

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。