python utils是一个模块,其中包含一些标准python安装中未包含的方便实用程序
python-utils的Python项目详细描述
python utils是小python函数和 使公共模式更短更容易的类。这决不是 完整的收藏,但它在过去对我很有用,我会 继续延伸。
使用python utils的库之一是django utils。
文档位于:https://python-utils.readthedocs.org/en/latest/
链接
- The source: https://github.com/WoLpH/python-utils
- Project page: https://pypi.python.org/pypi/python-utils
- Reporting bugs: https://github.com/WoLpH/python-utils/issues
- Documentation: https://python-utils.readthedocs.io/en/latest/
- My blog: https://wol.ph/
安装要求:
- six any recent version
安装:
软件包可以通过pip安装(这是推荐的方法):
pip install python-utils
或者,如果没有pip,那么也可以使用easy_install:
easy_install python-utils
或者从pypi(https://pypi.python.org/pypi/python-utils)或github下载最新版本。
请注意,pypi上的版本是用我的gpg密钥(https://pgp.mit.edu/pks/lookup?op=vindex&search=0xE81444E9CE1F695D)签名的,可以使用gpg进行检查:
gpg –verify python-utils-<version>.tar.gz.asc python-utils-<version>.tar.gz
快速启动
此模块使在python脚本中执行常见任务变得容易,例如 将文本转换为数字并确保字符串为Unicode或字节 格式。
示例
从几乎每个字符串中提取数字:
frompython_utilsimportconvertersnumber=converters.to_int('spam15eggs')assertnumber==15number=converters.to_int('spam')assertnumber==0number=converters.to_int('spam',default=1)assertnumber==1number=converters.to_float('spam1.234')
要以编程方式执行全局导入,可以使用import\u global 功能。这有效地模拟了来自…import的
frompython_utils.import_importimport_global# The following is the equivalent of `from some_module import *`import_global('some_module')
或者在您的类中添加一个名为logger的对应项,该日志可以轻松访问:
classMyClass(Logged):def__init__(self):Logged.__init__(self)my_class=MyClass()# Accessing the logging method:my_class.error('error')# With formatting:my_class.error('The logger supports %(formatting)s',formatting='named parameters')# Or to access the actual log function (overwriting the log formatting can# be done n the log method)importloggingmy_class.log(logging.ERROR,'log')