some python utils
Project description
My utils for python,让常用功能可以一行搞定
Bisect
MappingSpace (arr: Sequence, key: Callable)
Bisect in python < 3.9 have no key parameter. Use MappingSpace for map f: arr->f(arr) for bisect model.
python < 3.9 的 bisect 不带key参数,可以用这个类作为映射:
import bisect
from kzhutil.bisect import MappingSpace
bisect.bisect(MappingSpace(range(10**8), lambda x: x**2), 2e14)
# 14142136
File
一些将读写json、csv和普通文件浓缩成一行的方法
read write or append a file in one line:
read_from_file(fn, binary=False, encoding=None)
write_over_file(fn, s: AnyStr, binary=False, encoding=None)
append_to_file(fn, s: AnyStr, binary=False, encoding=None)
jsonl file: list of jsons separated into lines
write_jsonl(fn, data: List[object], encoding=None)
read_jsonl(fn, encoding=None)
csv file
read_csv(fn, encoding=None)
write_csv(fn, data: List[object], encoding=None)
read_csv_dict(fn, encoding=None)
write_csv_dict(fn, fieldnames, data: List[Dict], encoding=None)
Functools
try_for(n)
A decorator to try a function up to n times till success. Useful for function with failure probability. e.g. web crawler.
一个函数装饰器,可以让后面的函数最多尝试n次,直到某次运行成功再返回。适合用于爬虫这类有成果概率的场景。
import random
from kzhutil.functools import try_for
random.seed(42)
@try_for(10)
def func():
n = random.randint(0, 10) # 10, 1, 0, 4, 3, 3, 2, 1, 10, 8
print(n, end=" ")
assert n == 3
return 'success'
print(func()) # 10 1 0 4 3 success
try_for_(n, f: Callable, *args, **kwargs)
The kernel of try_for(n)
try_until(f: Callable, args: Iterable)
try f(*args[0]), f(*args[1]), f(*args[2]), ... until success then return
repeat_for(n)
repeat a function for n times
repeat_for_(n, f: Callable, *args, **kwargs)
Hash
Hash string/bytes in one line and return hex string. All hash functions:
比起hashlib,这里提供的hash函数既能接受string又能接受bytes,并且直接返回十六进制string,所有hash函数:
md5, sha1, sha224, sha256, sha384, sha512
io_util
get_clipboard()
Windows Only. 获得剪切板内容,只支持windows平台。
set_clipboard(s: AnyStr)
Windows Only. 设置剪切板内容,只支持windows平台。
Math
normalize(array, **kwargs)
Normalize the array to standard normal distribution. 正态分布归一化
array: numpy.ndarray or torch.Tensor.
**kwargs: (axis, keepdims) for numpy or (dim, keepdim) for pytorch.
uniform_normalize(array, **kwargs)
Normalize the array to uniform distribution [0, 1]. 归一化
array: numpy.ndarray or torch.Tensor.
**kwargs: (axis, keepdims) for numpy or (dim, keepdim) for pytorch.
primes(n)
Get all primes not large than n.
from kzhutil.math import primes
primes(19) # [2, 3, 5, 7, 11, 13, 17, 19]
exgcd(a, b)
Get solution x, y for ax+by=gcd(a,b).
return: x, y, gcd(a, b)
from kzhutil.math import exgcd
exgcd(18, 44) # 5, -2, 2
inverse(x, n)
Get the inverse element of in where
continued_fraction(d, eps=1e-12, max_denominator=100000000)
Calculate continued fraction coefficients [a0, a1, a2, ...] of
e.g. π -> [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3]
convergent_fraction(a)
Get the convergent fraction of a cotinued fraction. e.g. [3, 7, 15, 1] -> 355/113
miller_rabin(n, test_time=16)
Miller-Rabin prime test with time complexity in . The error rate is approximate to
next_prime(n)
Get the next prime of n (n>2)
random_prime(n)
Get a random prime smaller than
euler_phi(n)
Euler's totient function.
torch
utils for pyroch
set_seed(seed)
set seed for torch, numpy, random
transformers
load_pretrained(model_class, model_name, model_path, **kwargs)
Load pretrained transformers. If a saved model provided in model_path
, then load directly. Else load from hub and save to model_path
.
from kzhutil.transformers import load_pretrained
from transformers import AutoModelForMaskedLM
load_pretrained(AutoModelForMaskedLM, "bert-base-uncased", "/home/bert")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for kzhutil-0.0.3-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d960e59fdcaa888d5a26213558341f3ca7f275398134be5265a197c78430c7a |
|
MD5 | 19d45b6a541504b6f16d99c86168eac8 |
|
BLAKE2b-256 | 584c157a527b2a0ba4f263d86862a50e3e23a0a9a6dcbbf1b280b97ce19d7e4e |
Hashes for kzhutil-0.0.3-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 169ccfe64905b8cad76d6969255836f375416c3ec09853604e6f288f707c6386 |
|
MD5 | ba49cf287908391a688bffd38b764543 |
|
BLAKE2b-256 | 8050bb2cdf5648982d71183f994c4a39b645743ca4d23c6e676ebb3fb7672a08 |
Hashes for kzhutil-0.0.3-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ddf0e346913e2e4122bc56925c9999737550589dc620e0abb133ffdb8ccea01 |
|
MD5 | 5035adb2dd5cdf77fd7a3fdcb46627f2 |
|
BLAKE2b-256 | 2f5b758a53204e45be706037f4d27a5e52bbe81ecd60ec328625f1599aa347c0 |
Hashes for kzhutil-0.0.3-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 558be82be8155da7bee0e02256199489aab8f467dea60906cc642379022f72f0 |
|
MD5 | ecc66606c989eb295412331dfd0162ca |
|
BLAKE2b-256 | 41e91b7130254c593cabc5867ddd8f982e89965d38b7e929996d380bf0939e34 |
Hashes for kzhutil-0.0.3-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7837923ea01196cd33d94d6315c9389bd9e45127abf080fb00092abc412b2ddc |
|
MD5 | 3a96ced51382c8e5d7dd06da83d09734 |
|
BLAKE2b-256 | a0f012626d865c6be4d0f9014b31de39b1da8d063617a3b60b8783a773556c5a |
Hashes for kzhutil-0.0.3-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fefb62bd991c417b9238fd670411018a73ce992871f99741b2056941711e4314 |
|
MD5 | 7e4adf662945bc21168a1732e7863ce0 |
|
BLAKE2b-256 | 1dbcdc48569655c0f76e502b29c2d477f2bd8fc16fea59544ed714a731b555ed |