简体   繁体   English

Python 项目中顶级模块的推荐名称?

[英]Recommended name for top level module in Python project?

I am developing a program that I plan to distribute to multiple users.我正在开发一个计划分发给多个用户的程序。 This consists of a folder containing the following files:这包括一个包含以下文件的文件夹:

  • convert_units.py转换单位.py
  • fit_parameters.py fit_parameters.py
  • plot_results.py plot_results.py
  • top_level_module.py顶级模块.py

The purpose of "top_level_module.py" is to import the other modules and call them as needed, as shown below: “top_level_module.py”的目的是导入其他模块,根据需要调用,如下图:

from convert_units import convert_units
from fit_parameters import fit_parameters
from plot_results import plot_results

input_data = "somefile.csv"
intermediate_result = convert_units(input_data)
final_result = fit_parameters(intermediate_result)
plot_results(final_result)

The goal of naming the above file "top_level_module" is to help users who wish to inspect the code.将上述文件命名为“top_level_module”的目的是帮助希望检查代码的用户。 Specifically, my hope is that when users see this filename they will immediately realise that this is the highest level module, and hence the correct file to read first.具体来说,我希望当用户看到这个文件名时,他们会立即意识到这是最高级别的模块,因此首先要读取正确的文件。 However, the name "top_level_module" seems verbose, and I am wondering if another name is already in common use for this purpose.但是,名称“top_level_module”似乎很冗长,我想知道是否已经为此目的普遍使用了另一个名称。

So my question is: Does anyone know if there is a convention for naming the top level module?所以我的问题是:有谁知道是否有命名顶级模块的约定? Or if any other name would be more widely intuitive?或者是否有任何其他名称会更直观?

One way that is used often, is to have a folder named after your module with the submoduls (convert_units.py etc.) as files in that folder and an __init__.py -file where you put your code from the main-module instead of in a separate top_level_module.py-file.经常使用的一种方法是使用以您的模块命名的文件夹,其中子模块(convert_units.py 等)作为该文件夹中的文件和一个__init__.py文件,您将代码从主模块而不是在单独的 top_level_module.py 文件中。

Your submoduls can even have their own submoduls and so on for bigger projects.对于更大的项目,您的子模块甚至可以拥有自己的子模块等。 For this you'd have eg instead of the convert_units.py-file a convert_units-folder again containing an __init__.py -file with the module-level code and other files or folders for the submoduls of this submodule convert_units.为此,您将拥有例如convert_units-folder 而不是convert_units.py 文件,该文件夹再次包含一个带有模块级代码的__init__.py文件以及该子模块convert_units 的子模块的其他文件或文件夹。

Your file-structure could in the end look something like this.您的文件结构最终可能看起来像这样。

my_fancy_library  # name folder after what you want to call your module
├── __init__.py  # module-level code goes here (<- top_level_module.py)
├── fit_parameters.py  # smaller submodul that fits inside one file
├── plot_results.py
├── convert_units  # bigger submodul with own submodules
│   ├── __init__.py  # module-level code of submodule
│   ├── validator.py
│   ├── custom_exceptions.py
...

Your __init__.py -file of your main-module then can look like this to implement your example:您的主模块的__init__.py文件然后看起来像这样来实现您的示例:

from .convert_units import convert_units
from .fit_parameters import fit_parameters
from .plot_results import plot_results


def fit_data_and_plot(input_data):
    intermediate_result = convert_units(input_data)
    final_result = fit_parameters(intermediate_result)
    plot_results(final_result)

Better to wrap your code inside a function, or otherwise it will be executed during the import.最好将代码包装在 function 中,否则它将在导入期间执行。 You then use this function in another module by importing it from my_fancy_library import fit_data_and_plot and calling it with the input-path fit_data_and_plot(input_data) .然后,您可以在另一个模块中使用此 function,方法是from my_fancy_library import fit_data_and_plot它并使用输入路径fit_data_and_plot(input_data)调用它。

You can also look at the source-code of other libraries too, to get an idea.您也可以查看其他库的源代码,以获得一个想法。 This can be really confusing for bigger libraries like numpy , so I would start with a smaller example like dateutil .这对于像numpy这样的大型库来说真的很令人困惑,所以我将从一个像dateutil这样的小例子开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM