AngelTools
自用的 python 小工具合集
就是开发过程中常用的一些小方法、小轮子的合集,免去每次用到都得复制
每个人开发思维都不一样,不一定适合其他人
安装
$ pip install angeltools
或者直接下载 duplremover 文件夹到你的项目目录下也行
现有方法:
- 比例控制器:Scaler
"""
控制数值区间的方法
"""
from angeltools.MathTool import Scaler
scaler = Scaler()
num = 998
scaler.arctan(x=num, lower_point_x=80, lower_limit=80, upper_limit=100)
# arctan 反正切函数, 用于控制连续变量最大值区间。
# 人话就是用反正切计算比例,确保一个数不超出你锁定的范围
# 要确定一个反正切函数,最少需要确定函数过某一个点,此处称为 锚点
# 变量x
# 锚点的x值
# 锚点的y值
# y值上限
- 顺序多线程工具 Slaves
from angeltools.Slavers import Slaves
import time
import random
def do_add(args):
x, y = args
time.sleep(random.randint(1, 2))
return x + y
data = [[x0, y0] for x0, y0 in zip(range(10, 20), range(1, 10))]
ts = time.time()
results = Slaves(workers=10).work(do_add, params_list=data)
te = time.time()
print(results, te - ts)
# Slaves start working: do_add
# [11, 13, 15, 17, 19, 21, 23, 25, 27] 2.007810354232788
- 顺序多进程工具 BigSlaves
from angeltools.Slavers import BigSlaves
import time
import random
def do_add(args):
x, y = args
time.sleep(random.randint(1, 2))
return x + y
data = [[x0, y0] for x0, y0 in zip(range(10, 20), range(1, 10))]
ts = time.time()
results = BigSlaves().work(do_add, params_list=data) # 默认进程数是当前系统可用进程-1
te = time.time()
print(results, te - ts)
# BigSlaves start working: do_add
# [11, 13, 15, 17, 19, 21, 23, 25, 27] 3.0164289474487305
- 其他工具
- 基于文件的异步锁
# 由于文件io的延迟性,文件锁不适用于高并发的场景,也不能实现分布式,只适合单机器低速异步进程的隔离
# 优点是不需要数据库或第三方依赖,即开即用
# 高并发可以使用 python-redis-lock
from angeltools.StrTool import FileLock
def do_jobs():
pass
with FileLock(lock_id='test-lock'):
do_jobs()
- url 组合拆解
from angeltools.StrTool import UrlFormat
uf = UrlFormat(url='http://www.baidu.com?page=1&user=me&name=%E5%BC%A0%E4%B8%89')
uf.url_format(params_only=True, unquote_params=True) # {'page': '1', 'user': 'me', 'name': '张三'}
uf.split_url() # {'queries': {'page': '1', 'user': 'me', 'name': '张三'}, 'host': 'www.baidu.com', 'protocol': 'http', 'path': '', 'require_params': '', 'fragment': ''}
uf.make_url(
'http://www.baidu.com',
params_add_dic={'page': '1', 'user': 'me','name': '张三'}
) # 'http://www.baidu.com?page=1&user=me&name=%E5%BC%A0%E4%B8%89'
- scrapy 爬虫xpath包装
from angeltools.StrTool import ScrapyXpath
# 包装 scrapy response 的xpath方法,不用每次都 extract 再判断结果,使爬虫更整洁
# 也可以传入由 requests 获取的 response text 和 url,变成 scrapy selector 对象方便提取
import scrapy
div = scrapy.http.HtmlResponse().xpath('//div[@class="xxx"]')
sx = ScrapyXpath(scrapy_selector=div)
sx.xe('./@href') # https://www.xxx.com
sx.xe('.//text()') # ["abc", "efg", ...] 结果有多个时返回列表
# 或者
import requests
url = 'http://www.baidu.com'
html_content = requests.get(url)
sx = ScrapyXpath(url='http://www.baidu.com', html_content=html_content)
sx.xe('./@href') # https://www.xxx.com
sx.xe('.//text()') # ["abc", "efg", ...] 结果有多个时返回列表
- 图片转字符块工具
from angeltools.ImageTool import image2chars
image2chars(
'/home/Angel.png',
width=30,
k=1.0,
# outfile='/home/测试123.txt',
reverse=True
)
"""
- . -
+ @ # . @ -
# # @ . . + - + - - + . - . + - . @ -
. @ - # = + @ # # @ - + @ # # @ . - @ * # * . @ -
= @ = @ + @ . @ = # * - @ . # # - = @ . . @ -
# @ @ @ @ - + @ @ = @ = . @ . # # = = = . @ -
- @ + - - @ * + @ @ = * @ - * @ . * # . + * . @ -
= * + * - * * + . * @ * @ . * # # - . * .
+ = . + @ .
- # # # +
"""
- 文字转字符块工具
from angeltools.ImageTool import text2chars
text2chars(
text="ANGEL",
# font_path='/etc/fonts/msyh.ttf',
width=50,
k=0.6,
# outfile='/home/测试123.txt',
chart_list=[' ', '-', '/', '%'],
)
"""
- - - - - - - / / / - - - / / / / / - - -
/ % % % % % % % % - - % % / / / % % - % % / / / / / - - % /
% / / % - % % % % % % - % % - - - % % - - % /
% % % % % % / % - / % - % % / % / / % % % % % % % - % /
- % % % % % % / % % / % % % - % % - - % % % % - - % /
% % - % % - % % / % % - - % % / / / % % % % % % / / / / / - % % / / / / -
- - - - - - - - / / / - - - - - - - - - - - - - - - - -
"""
- 使用redis的文件缓存器(模仿fdfs)
from angeltools.Db.redis_connector import RedisFdfs
conn = {
"host": '127.0.0.1',
"port": 6379,
"password": None,
}
rdfs = RedisFdfs(
db=1,
connect_params=conn, # redis 连接配置
expire=30 # 缓存过期时间(默认1星期)
)
test_id = rdfs.upload('test data 001')
print(test_id)
# RedisFdfsFile_cceceb50-cc40-11ec-bb8b-adb0630c08c7
test_data = rdfs.get('RedisFdfsFile_cceceb50-cc40-11ec-bb8b-adb0630c08c7')
print(test_data)
# test data 001
# 没有结果或出错的时候返回空字符 ''
test_del = rdfs.delete('RedisFdfsFile_cceceb50-cc40-11ec-bb8b-adb0630c08c7')
print(test_del)
# True
- 终端工具
- 图片转字符块工具
ima2char [image] [-h 查看帮助]
示例:
img2char /home/ABC.png -w 20
输出:
. . .
* = + = + + + - + .
- = - . + = . = .
+ . = . + + . +
+ + . . = - = - - -
- - - + . = . - = . - -
= + + + * . + . + . +
. + + . + - - = .
+ . . + = - + + = + + + .
. . . . .
- 文字转字符块工具
txt2char [text] [-h 查看帮助]
例如:
txt2char ABC -w 30
输出
. . . . . . . . .
- @ @ + # @ @ @ @ @ * + # @ @ @ * .
* @ @ # # @ * = = # @ * + @ @ = + # @ #
@ # # @ . # @ . @ @ @ @ - # @ +
+ @ = + @ = # @ = + + * @ = - @ # .
# @ . @ @ # @ @ @ @ @ @ . + @ *
. @ @ - . # @ - # @ + - - + @ # - @ *
= @ @ @ @ @ @ * # @ . # @ . . @ @ = @ +
# @ = + + + @ @ # @ + - - + @ @ . * @ * . - @ @ .
- @ # * @ + # @ @ @ @ @ @ + # @ @ @ @ @ +
. + . . + - - + + + + - . + = * = .
Metadata
Release files for angeltools 0.3.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| angeltools-0.3.7.tar.gz | 43.5 kB | Without provenance | Details for angeltools-0.3.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| angeltools-0.3.7-py3-none-any.whl | Python 3 | none | any | Details for angeltools-0.3.7-py3-none-any.whl |
Total release size: 91.0 kB
Release files / angeltools-0.3.7.tar.gz
| Download URL | angeltools-0.3.7.tar.gz |
|---|---|
| Size | 43.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b0d046e18f230e9e1e9b15a73ca6d9aa2588ba034b882af3d571369852446262
|
|
BLAKE2b-256 checksum How to use checksums |
e9aa9dffda1af9c00a4a0ec6f2bb9a4e838bc10baca7620b1d37a2630bcd536e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.18
|
Release files / angeltools-0.3.7-py3-none-any.whl
| Download URL | angeltools-0.3.7-py3-none-any.whl |
|---|---|
| Size | 47.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1eafbe2d5f8791210f3100c25191da5df722e0fec155a80ed617bbcb5db25170
|
|
BLAKE2b-256 checksum How to use checksums |
22feffafc2f10db3fe2fae068c26eacfae3dc5a34ae9883e6cd8853b71073bd5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.18
|
Security for angeltools 0.3.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.