Logrus 是随机效用函数的集合
项目描述
罗格鲁斯
目录
logrus 是随机效用函数的集合。这里没有什么特别的,它们只是我在每项工作中重写的函数的另一种实现,用于各种实用程序脚本。通过现在将它们开源,我希望不必再次编写它们。
是的,这个名字来自 Zelazny 的 Amber 系列。
安装
pip install thelogrus
执照
阿帕奇 2.0 许可证。
包含的命令
人类时间
从标准输入或 arg 1 获取以秒为单位的值,并使用该humanTime函数将其转换为更适合肉类的格式。
human-time 1234将打印“20 分 34 秒”
包含的功能
thelogrus.cli
exec_subcommand(未找到)
创建git-style 驱动程序命令。如果您的脚本名为 named foo,并且以 as 运行并且在您的namedfoo bar baz中有一个可执行文件,它将作为命令行参数调用with 。$PATHfoo-barfoo-barbaz
unfound是一个可选参数,应该是一个函数指针,如果exec_subcommand找不到合适的子命令将被调用。主要对您有自定义使用消息有用。
示例用法:
#!/usr/bin/env python3
#
# Test script for thelogrus
#
# Confirms that thelogrus.cli.exec_subcommand works as expected
#
# Copyright 2019, Joe Block <jpb@unixorn.net>
import sys
from thelogrus.cli import exec_subcommand
def _usage(message):
'''
Custom usage printer
'''
print("%s" % sys.argv[0])
print("Called as %s" % (' '.join(sys.argv)))
print("Oh look, a custom usage message.")
print("Attempted to find an executable using all the permutations of %s with no luck." % '-'.join(sys.argv))
print("%s" % message)
if __name__ == '__main__':
exec_subcommand(unfound=_usage)
查找子命令(参数)
给定一个列表 ['foo','bar', 'baz'],尝试创建一个格式为foo-bar-baz. 如果该命令存在,我们运行它。如果不存在,我们检查是否foo-bar存在,在这种情况下我们运行foo-bar baz. 我们不断从命令名称的末尾切分块并将它们添加到参数列表中,直到找到可以运行的有效命令名称。
这使我们可以轻松地制作 git 风格的命令驱动程序,例如,我们有一个驱动程序脚本、foo和子命令脚本foo-bar和foo-baz,当用户键入时,foo bar foobar我们找到 foo-bar 脚本并运行它foo-bar foobar
示例用法:
#!/usr/bin/env python3
import os
import subprocess
import sys
from thelogrus.cli import find_subcommand
def subcommander_driver():
'''
Process the command line arguments and run the appropriate subcommand.
We want to be able to do git-style handoffs to subcommands where if we
do `foo blah foo bar` and the executable foo-blah-foo exists, we'll call
it with the argument bar.
We deliberately don't do anything with the arguments other than hand
them off to the foo subcommand. Subcommands are responsible for their
own argument parsing.
'''
try:
(command, args) = find_subcommand(sys.argv)
# If we can't construct a subcommand from sys.argv, it'll still be able
# to find this driver script, and re-running ourself isn't useful.
if os.path.basename(command) == sys.argv[0]:
print("Could not find a subcommand for %s" % ' '.join(sys.argv))
sys.exit(1)
except Exception as e:
print(str(e))
sys.exit(1)
subprocess.check_call([command] + args)
if __name__ == '__main__':
subcommander_driver()
is_program(名称)
在 中搜索给定程序,如果它存在并且是可执行的,则$PATH返回。True
运行(命令)
运行命令(str 或列表)并返回其stdout.
thelogrus.logging
getCustomLogger(名称,logLevel)
返回具有良好格式输出的自定义记录器。
thelogrus.time
人类时间(秒)
以秒为单位获取一个值,以适合肉类的格式返回。humanFriendlyTime(8675309)将返回“100 天 9 小时 48 分 29 秒”。
thelogrus.utils
mkdir_p(路径)
os 模块没有mkdir -p等效的,所以添加了一个。
obfuscateString(snippet, showLength=5, smear='*')
获取一个字符串,从开头和结尾切掉 showLength 字符,并用该smear字符替换其他所有内容。仅对在日志中显示足够多的凭据以显示它正在使用正确的凭据有用,但不显示实际凭据。
squashDicts(*dict_args)
返回一个将所有 dict_args 压缩在一起的 dict。
thelogrus.yaml
readYamlFile(路径:str)
返回 YAML 文件中包含的数据结构
Args: path (str): 读取路径
返回:从 YAML 文件内容解码的数据
writeYamlFile(路径:str,数据)
将数据结构或对象写入 YAML 文件
args: path (str): 数据文件的路径 data (any): 要转换和写入的数据
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。