设置全局环境并在其中运行另一个脚本,例如pdb、profile等。
项目描述
使用main-wrapper作为命令行脚本或库,以便编写Python脚本,这些脚本设置或更改某些全局Python环境,然后在其中运行另一个脚本。它试图将其他Python软件的所有华丽和光鲜之处结合到一个库中,例如使用标准库中的python -m与pdb和profile,或者使用coverage包的命令行run命令。
安装
使用安装标准Python 3分发的任何工具进行安装,例如pip
$ sudo pip3 install main-wrapper
用法
您可以将此包用作代码中需要包装另一个脚本的库,或作为命令行脚本。
要将它用作库,请使用提供的装饰器来包装您需要脚本在其中运行的全球环境设置函数
import logging import argparse import mainwrapper parser = argparse.ArgumentParser() parser.add_argument( "--level", default="INFO", help="The level of messages to log at or above", ) @mainwrapper.wrap_main(parser) def main(level=parser.get_default("level")): """ As an example, this function will set up logging at level INFO. """ logging.basicConfig(level=getattr(logging, level))
支持运行包装函数和最终脚本的Python全局执行环境的更改也将在完成后清理,因此可以使用此库在同一个进程中执行多个脚本,就像它们独立运行一样。
有关使用此包作为命令行脚本的选项和参数的详细信息,请参阅命令行帮助。
$ usage: python-main-wrapper [-h] wrapper script Set up global environment and run another script within, ala pdb, profile, etc.. Both script arguments may either be a path to a Python script, a Python module or package to be run in the same manner as Python's `-m` option, or a setuptools `path.to.import:callable` entry-point. positional arguments: wrapper A Python script that sets up the environment script The Python script to run within the wrapper's environment optional arguments: -h, --help show this help message and exit
请注意,此包在内部使用argparse.ArgumentParser.parse_known_args,因此请确保使用其支持--约定来分隔传递给包装脚本的参数和选项。
$ python-main-wrapper site site:_script --help ... site.py [--user-base] [--user-site] Without arguments print some useful information With arguments print the value of USER_BASE and/or USER_SITE separated by ':'. Exit codes with --user-base or --user-site: 0 - user site directory is enabled 1 - user site directory is disabled by user 2 - user site directory is disabled by super user or for security reasons >2 - unknown error
动机
多年来,我反复编写此类包装脚本,并且一直在努力记住上次是如何做的。我制作了这个包来收集所有这些知识,并且当我发现改进时,有一个地方可以放置它们。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
main-wrapper-0.1.1.tar.gz (8.7 kB 查看哈希值)
构建分布
main_wrapper-0.1.1-py3-none-any.whl (7.8 kB 查看哈希值)
关闭
main-wrapper-0.1.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 73e9b9311baf2c80a54a62fcf40dfb8a960290afe40da7927066adb97d53034b |
|
MD5 | 584afaabeee677f31b31949042954a96 |
|
BLAKE2b-256 | 58b99747f029c3afe704be9d47a80f22c4e2bf601ffafac72b9913b6654130dd |
关闭
main_wrapper-0.1.1-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 46f338144ccaf6e7035c93d8cd2e950e11b4d2118b551f8148611e6d73ba1d9c |
|
MD5 | 9dd6547e0569103b27512eb06a3eac94 |
|
BLAKE2b-256 | b32f7a7d13d2f9f84a82b46fb11f69292968696781c7167e6824c79fcfe7bf77 |