makepy:免提python模块编程
makep的Python项目详细描述
**<span style="color:red">
This is the backport version 0.0.26 of 'makepy' for Python 2.
Please upgrade to Python 3+ and use the current 'makepy' version 0.0.26.
</span>**
makepy:免提python模块编程
本项目提供:
makepy
:一个简化python项目设置的命令行工具,
安装和测试。makepy.mainlog
:一个用于生成日志的模块
以及structlog
设置更简单,更容易出错。makepy.argparse
:提供切入点的模块ArgumentParser
为了编写可读性更好的代码
通过pip3 install--user makepy安装
主日志模块
顾名思义,只在主模块中使用makepy.mainlog
。
不要在主模块之外设置日志记录!
模块的主要功能是mainlog.设置日志
:
importloggingfrommakepyimportmainloglog=logging.getLogger('app')defmain(argv=None):level=logging.INFOmainlog.setup_logging(level=level,mode='json')log.info('Hello %s!','makepy',extra={'v':1})main()# {"message": "Hello makepy!", "v": 1}
当前支持的日志记录模式是json
和console
(默认)。
使用"mode="console"或"no mode"将生成常规stdlib日志,如:
INFO:app:Hello makepy!
使用mainlog.setup_logging(level=level,use_structlog=true)
设置structlog
日志。
如果未安装struclog,则使用stdliblogging
作为回退。
预定义的structlog设置将格式化stdlib日志,如下所示。
[info ] info msg 1 [stdlib]
[debug ] debug msg 2 [stdlib]
[error ] error msg 3 [stdlib]
如果在模块中使用structlog记录器,还可以获得额外的键值对。
[info ] info msg [structlog] a=[1, 2, 3] v=1
[debug ] debug msg [structlog] b=('a', 'b', 'c') v=2
[error ] error msg [structlog] c={'x': 1} v=3
如果安装了colorama
,日志将被很好地着色(structlog特性)。
argparse模块
为了编写更好的命令行应用程序,提供了一个兼容的
ArgumentParser
使用4个字母的opti
和flag
方法,替换原来的
添加参数
方法。
frommakepyimportargparsedesc='My CLI Tool'p=argparse.ArgumentParser(description=desc)p.flag('--json',help='use json output format')p.flag('--dry_run',help='perform dry run')p.opti('--num','-n',help='number of iterations',metavar='N',type=int,default=1)p.opti('--file','-f',help='input file',required=True)p.opti('command',help='command to run',choices=['upper','lower'])
使用较短的名称和良好的对齐方式可以使argparse
代码更具可读性。
是的,我知道,为了允许这种基于多列的编码,您需要禁用一些linter规则。
但值得一提的是,这不仅适用于argparse
代码,还适用于更可读的python代码。
makepy的argumentparser还提供了一些设置其他常用模块的快捷方式
直接通过以下标志:
使用"调试":添加"调试"标志
with_logging
:解析参数后,使用makepy.mainlog
自动设置日志记录with_input
:添加--input
选项,默认为-
(又名。stdin
)使用受保护的空格
:修改argparse
格式化程序,以保护定义的空白 在您的help
语句中。否则argparse
将删除任何换行符和重复空格, 等等,创建浓缩帮助段落。使用此选项,现在可以安全地对齐帮助文本 直接输入你的代码。请参见makep/cli.py
作为示例。
下面是设置常见调试和日志记录功能的示例:
p=argparse.ArgumentParser(description=desc).with_logging(use_structlog=True).with_debug()
如果您不喜欢单行本,您也可以换行。
p=argparse.ArgumentParser(description=desc)p.with_logging(use_structlog=True)p.with_debug()p.with_input()
将 这个项目还提供了一个 以下是makepy支持的一些命令: 您还可以链接命令: 运行 一些makepy功能仍然只能通过使用
包括文件等。你可以
在你的项目中使用这些。只需将它们复制到项目中的 一般来说,该项目旨在提供一些灵活的工具和模块,以帮助
开发python模块、库和命令行工具的日常python编程任务。
它旨在捕获最佳实践并使其可重用,从而允许您编写越来越少的
可读代码,不会破坏增强模块和工具的灵活性或兼容性。 大多数python程序员都知道 我不想重复我自己,我想从我的项目中提取最常见的实践
并提供给我的下一个项目和其他人使用。 要设置 我会不断更新makepy,并在以后的学习中不断学习,我很高兴欢迎pull请求。与_logging一起使用和可选地将
与_debug一起使用可以让您快速
安装程序
日志记录
或
结构日志
日志记录程序,具有可读控制台输出。
因此,
使用_logging
支持相同的模式
和使用_structlog
键值参数
如上文所述,由mainlog.setup\u logging使用。
makepy命令
makepy
命令,用于自动创建项目,增量
通过tox
构建、测试并上传到pypi。makepy init --trg ~/workspace/newproject # setup new project/package named "newproject"
cd ~/workspace/newproject # enter new project
makepy backport # backport project to python2
tox -e py3 # use `tox` to install and test the package in a Python 3 environment
tox # install and test in all testenvs defined in `tox.ini`
makepy # install and test in the default testenv
makepy clean # cleanup test environments and build files
makepy dist # build python wheel for current project
makepy dist -P 2 # build python wheel for python2
makepy dists # build both wheels for python2 and python3
makepy version # read version string from main __init__.py
makepy bumpversion # increase patch level in main __init__.py
makepy install # pip install the wheel in the system (may require sudo)
makepy dev-install # pip install the current editable source code in the system
makepy uninstall # uninstall current project/package from all pips
makepy clean bumpversion dists
,并且makepy
将重新排序
它们和添加所有必需的依赖命令,例如makepy install-p 2
是等效的
要makepy backport dist install-p 2
makepy
命令依赖并可以初始化python配置文件中的值
tox.ini
和setup.cfg
。它还可以创建一个通用的py2-py3+
与此项目中找到的setup.py
兼容。makepy init--trg path_to_new_project
设置所有必需的文件。使用-f
允许
正在覆盖现有文件。有关更多选项,请参见makepy--help
。makepy+make
make
dir,然后include
在您的makefile
中,由此项目完成。有关详细信息和帮助,请参阅每个mk文件。目标
动机
argparse
,logging
或
structlog
,tox
和pip
,许多还使用tween
,
设置工具
和其他。但是,当使用这些工具时,您将编写
一次又一次相同或非常相似的样板代码。历史记录
logging
和argparse
的实用程序模块分散在几个私有的
项目(并在公司项目中重新实施)。大多数makepy
命令都位于
在我最终
开始将功能移植到makepy
。仍保留一些make
功能,可以在
此项目的mk
文件,例如make标记
和make publish
未决问题/任务
- 为名称空间添加Python2支持。
- 端口文档字符串+创建已读的文档。
- 端口版本管理,使用外部
bumpversion
命令。 - 制造商提供的端口集成测试。
- 来自Make的Port Docker测试。
- 从Make发布左轮。
- make+删除make相关代码的端口剩余部分。