如何为包实现标准记录器和argparser
我正在尝试为我们的内部软件开发流程实现一个 python 构建包和功能测试包。我的主要目标是实现一个全局/标准记录器(类似于此处的参考) argparser,所有模块都将从其透视包中使用(即构建记录器和解析器将不同于测试套件记录器和解析器)。
这应该在包的 __init__.py
中完成吗?还有其他方法可以实现这一目标吗?我还没有看到 __init__.py
的真正需要是什么?也许,这回答了我自己的问题?
我编写过许多 python 模块和其他应用程序,但从未编写过包/库。这个概念似乎为我带来了新的可能性,例如标准化这些流程。
基本上,总而言之,我对这些包的目标是:
1.) 消除对长参数列表和/或访问系统环境的冗余传递/解析的需要。
2.) 消除我们过去从不同的测试和构建脚本中看到的无数输出。
3.) 提供一个易于使用的自文档包。关于包文档的任何建议也将非常有用。 :)
这将使这些软件包的用户能够:
1.) 轻松访问一组标准参数。例如,包中的每个模块将(按优先顺序)使用标准标志解析命令行参数,加载配置文件或使用默认值。
2.) 以标准方式轻松记录错误、警告和调试语句。
3.) 随着这些测试/构建模块的传递,这最终将使“用户”(即没有编写该模块的人)能够以通用方式运行它。
我想我正在寻找一些真正的“Pythonic”实施建议,因为那里的各种选项和可能性似乎令人难以抗拒。提前致谢。
I am trying to implement a python build package and functional testing package for our internal software development processes. My primary goal is to implement a global/standard logger (similar to the reference here) and argparser that all modules will use from within their perspective packages (i.e. the build logger and parser will differ from the test suite logger and parser).
Should this be done in __init__.py
of the package? Are there any other methods to achieve this? I have yet to see what the real need for __init__.py
is? Perhaps, this answered my own question?
I have written numerous python modules and other apps, but never packages/libraries. This concept seems to introduce new possibilities for me, such as standardizing these processes.
Basically, to summarize, my goals for these packages are:
1.) Eliminate the need for redundant passing/parsing of long lists of arguments and/or accessing the system environment.
2.) Eliminate the myriad of outputs that we've seen in the past from varying tests and build scripts.
3.) Provide a self-documenting package that will be easy to use. Any advice on package documentation would be extremely useful, as well. :)
This will enable users of these packages to:
1.) Easily access a standard set of arguments. For example, every module in the package will (in order of precedence) parse command-line arguments with standard flags, load a config file, or use default values.
2.) Easily log errors, warnings, and debug statements in a standard way.
3.) As these test/build modules are passed around, this will ultimately enable "users" (i.e. someone who did not write the module) to run it in a universal way.
I suppose I am looking for some real "Pythonic" implementation advice because the variety of options and possibilities out there seem to be overwhelming. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于日志记录,只需查看 Python 日志记录文档。这是一个非常容易实现且非常灵活的日志系统。
至于您关于 __init__.py 的问题,如果您来自其他编程语言,请将其视为构造函数。如果您希望在加载时分配一些默认值,那么它们应该进入 __init__.py 。
我对 argparse 不太熟悉,所以其他人可能能够对此有所了解。
For logging look no further than the python logging documentation. It is a very easy to implement logging system that is very flexible.
As to your question about
__init__.py
, if you come from other programming languages, think of it as a constructor. If you want some default values assigned on load, then they should go into__init__.py
.I am not very familiar with
argparse
, so someone else may be able to shed some light on that.