Skip to main content

允许使用参数测试可调用对象的二分法

项目描述

功能

有用的python函数集合

执照 未解决问题的百分比 可维护性 编解码器 linux-测试 窗户测试 GitHub 发布

ofunctions 是一组不同的循环函数

  • bisection:具有任意数量参数的任何函数的二等分算法,适用于 LtoR 和 RtoL
  • 校验和:用于检查和创建校验和文件的各种 SHA256 工具
  • csv:CSV 文件阅读器,比通用阅读器具有各种增强功能
  • delay_keyboardinterrupt:只是一个漂亮的工具来捕捉 CTRL+C 信号
  • file_utils:其中的文件处理函数
    • get_paths_recursive:遍历目录/文件的路径,可以处理权限错误,具有包含/排除列表和通配符支持...
    • check_path_access:检查路径是否可写,回退用于读测试,并分割路径直到找到拒绝权限的部分
    • check_file_timestamp_delta:根据文件 ctime、mtime 或 atime 检查时间增量(秒、分钟、小时...)
    • hide_file:隐藏/取消隐藏 windows & linux 下的文件
    • get_writable_temp_dir:返回允许我们写入的临时目录
    • get_writable_random_file:返回我们可以写入的尚未存在的文件的文件名
  • json_sanitize:确保 json 不包含不受支持的字符,是的,我看着你 Windows 事件日志
  • logger_utils:基本无脑控制台+文件日志创建
  • mailer:处理电子邮件发送的类,无论 ssl/tls 协议如何,批量或作为单个邮件,带有附件
  • 网络:各种工具,如 ping、互联网检查、MTU 探测和公共 IP 发现
  • 平台:这里没什么特别的,只需检查我们正在运行的拱门
  • 进程:简单的 kill-them-all 函数来终止子进程
  • random:基本随机字符串和密码生成器
  • service_control:控制Windows/Linux服务启动/停止/状态
  • string_handling:从字符串中删除重音符号/特殊字符
  • threading:函数的线程装饰器

ofunctions 与 Python 2.7 和 3.5+ 兼容,并在 Linux 和 Windows 上进行了测试。还有两个仅适用于 Python 3.5+ 的子包

  • delay_keyboardinterrupt(信号处理在 Python 2.7 中有所不同)
  • 线程(我们在 python 2.7 中没有 concurrent_futures)

设置

pip install ofunctions.<subpackage>

二等分用法

ofunctions.bisection 是一种二分法算法,可用于所有类型的二分法、数学运算、核二分法……假设我们有一个函数 foo,它接受参数 x。x 可能介于 0 和 999 之间,对于高于 712 的给定 x 值,foo(x) 返回“gotcha”。为了找出在哪个 x 值 foo(x) 成为“陷阱”,我们可以对每个可能的 x 值运行 foo(x),直到结果成为我们所期望的。上述解决方案有效,但需要时间(最多 1000 次 foo(x) 运行)。我们可以通过检查 foo(x) 最多 10 步获得相同的结果,其中 x 将是所有可能值的中间。查看该中间值的结果,我们将知道预期结果应该是较低还是较高的 x 值。我们可以重复这个动作,直到我们得到精确的结果。

现在让我们以不那么抽象的方式对上面的示例进行编码:

def foo(x):
	# We'll need to find value 712 te quickest way possible
	if x >= 712:
		return "gotcha"
	return False

from ofunctions.bisection import bisect

value = bisect(foo, range(0, 1000), expected_result="gotcha")
print('Value is %s' % value)

可以调整上述概念以计算以太网 MTU 值或任何其他需要计算的值。有关 MTU 探测示例,请参阅 ofunctions.network 代码。

校验和用法

csv 用法

delay_keyboardinterrupt 用法

DelayedKeyboardInterrupt 类允许拦截 CTRL+C 调用,以便在不中断的情况下完成原子操作。易于使用,我们使用pythonic语法如下:

设置:

pip install ofunctions.mailer

用法:

with DelayedKeyboardInterrupt():
	<your code that should not be interrupted>

file_utils 用法

ofuntions.file_utils 是一组工具来处理:

  • 路径列表

设置

pip install ofunctions.file_utils

json_sanitize 用法

json_sanitize 将从 json 内容(0x00-0x1F 和 0x7F-0x9F)中删除任何控制字符,其中一些通常不可打印且不可见。这在处理需要作为 json 传递的各种日志文件(例如:Windows 事件日志)时特别有用。它还将从值名称中删除点,因为这些在 json 标准中是被禁止的。

设置:

pip install ofunctions.json_sanitize

用法:

my_json = {'some.name': 'some\tvalue'}
my_santized_json = json_sanitize(my_json)

my_santized_json 将包含{'somename': 'somevalue'}

logger_utils 用法

邮件使用

ofunctions.mailer 是一个简单的邮件类和一个 rfc822 电子邮件验证函数。

设置:

pip install ofunctions.mailer

快速使用:

from ofunctions.mailer import Mailer

mailer = Mailer()  # Uses localhost:25
mailer.send_email(subject='test', sender_mail='me@example.com', recipient_mails='them@example.com', body='some body just told me')

智能继电器使用:

from ofunctions.mailer import Mailer

mailer = Mailer(smtp_server='mail.example.com', smtp_port=587, security='tls', smtp_user='me', smtp_password='secure_p@$$w0rd_lol')
mailer.send_email(subject='test', sender_mail='me@example.com', recipient_mails='them@example.com ; another_recipient@example.com', body='some body just told me')

批量邮件使用:

from ofunctions.mailer import Mailer

recipients = ['me@example.com', 'them@example.com', 'anyone@example.com', 'malformed_address_at_example.com']

mailer = Mailer(smtp_server='mail.example.com', smtp_port=465, security='ssl', debug=True, verify_certificates=False)

# split_mails=True will send one email per recipient
# split_mails=False will send one email for all recipients, which will be limited to the number of recipients the destination SMTP server allows
mailer.send_email(subject='test', sender_mail='me@example.com', recipient_mails=recipients, body='some body just told me', split_mails=True)

附件用法:

from ofunctions.mailer import Mailer

mailer = Mailer()  # Uses localhost:25

# attachment can be a binary blob or a file path
# filename is optional, and will rename a binary blob to something more meaningful
mailer.send_email(subject='test', sender_mail='me@example.com', recipient_mails='them@example.com', body='some body just told me', attachment=attachment, filename='My Attachment File.txt')

网络使用

平台使用

进程用法

随机使用

service_control 用法

string_handling 用法

线程使用

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

ofunctions.bisection-1.0.0.tar.gz (10.2 kB 查看哈希

已上传 source

内置分布

ofunctions.bisection-1.0.0-py3-none-any.whl (8.0 kB 查看哈希

已上传 py3