简体   繁体   English

在python中处理util函数

[英]Handling util functions in python

In our current c-project, we use python scripts for support and testing purposes such as unit testing, integration testing, benchmarking and communication. 在我们当前的c项目中,我们使用python脚本进行支持和测试,例如单元测试,集成测试,基准测试和通信。

Current folder structure (most files not shown): 当前文件夹结构(大多数文件未显示):

.
|-- workingcopy1
|-- |-- config
|-- |-- |-- __init__.py
|-- |-- |-- parameters_one.py
|-- |-- |-- parameters_two.py
|-- |-- |-- parameters_three.py
|-- |-- src
|-- |-- |-- main_application_c_code.c
|-- |-- tests
|-- |-- |-- tools
|-- |-- |-- |-- display_communication_activity.py
|-- |-- |-- run_all_unit_tests.py
|-- |-- tools
|-- |-- |-- script1.py
|-- |-- |-- script2.py
|-- |-- |-- script3.py
|-- |-- utils
|-- |-- |-- python_utils
|-- |-- |-- |-- __init__.py
|-- |-- |-- |-- communication_utils.py
|-- |-- |-- |-- conversion_utils.py
|-- |-- |-- |-- constants.py
|-- |-- |-- |-- time_utils.py
|-- workingcopy2
...
|-- workingcopy3
...

Some python files are intented to be executed as script files ( $ python script1.py ) and some are intended to be included as modules in other python files. 一些python文件有意作为脚本文件( $ python script1.py )执行,有些则打算作为模块包含在其他python文件中。

What we would like to achive is a structure that enables us to have parameter and utility functions that can be used by: 我们想要实现的是一种结构,它使我们能够使用参数和实用函数,可以通过以下方式使用:

  • Test code 测试代码
  • Other utility codes 其他实用程序代码
  • Smaller python application used for monitoring of our system. 用于监视我们系统的较小的python应用程序。 Ie custom benchmarking tools 即自定义基准测试工具

It should also be possible to have several workingcopies checked out 还应该可以检查几张工作复印件

Up until this date, all scripts have following lines at top: 截至此日期,所有脚本在顶部都有以下行:

import os, sys
current_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(current_path, '..', 'utils')) # Variates depending on script location in file tree
sys.path.append(os.path.join(current_path, '..', 'config')) # Variates depending on script location in file tree
import python_utils.constants as constants
import config.parameters_one as parameters

With about 20+ script files this has become hard to maintain. 有大约20多个脚本文件,这很难维护。 Is there any better way to achive this? 有没有更好的方法来实现这一目标?

You should convert your folders into Python modules by adding an empty __init__.py file into it. 您应该通过向其中添加一个空的__init__.py文件将您的文件夹转换为Python模块。

Also, you can add the Python shebang, so they are executable without explicitly calling the Python command from shell ( Should I put #! (shebang) in Python scripts, and what form should it take? ). 此外,您可以添加Python shebang,因此它们是可执行的,而无需从shell中显式调用Python命令( 我应该将#!(shebang)放在Python脚本中,应该采用什么形式? )。

Once your folders are modules, you have to add only the main source path and you will be able to import the children modules in an easier manner. 一旦您的文件夹是模块,您必须只添加主源路径,并且您将能够以更简单的方式导入子模块。 Furthermore, you should use a virtual environment (virtualenv) that can handle the paths for you ( http://docs.python-guide.org/en/latest/dev/virtualenvs/ ) (and maybe virtualenvwrapper that allows you extra functionality) 此外,您应该使用可以为您处理路径的虚拟环境(virtualenv)( http://docs.python-guide.org/en/latest/dev/virtualenvs/ )(也许virtualenvwrapper可以为您提供额外的功能)

I wanted to add a couple of additional strategies you could use here: One of the cool things about python is that everything is an object so you could import and pass your script modules as a variable to a function that run's them, initialising the appropriate path. 我想在这里添加一些你可以使用的其他策略:关于python的一个很酷的事情是,所有东西都是一个对象,所以你可以导入并将你的脚本模块作为变量传递给运行它们的函数,初始化相应的路径。 Also, the function could "discover" the scripts by looking into the folder and walking through it. 此外,该功能可以通过查看文件夹并遍历它来“发现”脚本。

Again, all this can be easily handled from pre-post activate virtualenvwrapper hooks . 同样,所有这些都可以通过pre-post activate virtualenvwrapper hook轻松处理。

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

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