carefree-toolkit
implemented some commonly used functions and modules
carefree-toolkit
requires Python 3.8 or higher.
pip install carefree-toolkit
or
git clone https://github.com/carefree0910/carefree-toolkit.git
cd carefree-toolkit
pip install -e .
class timeit(context_error_handler):
def __init__(self, msg)
Timing context manager.
msg
: str, name of the context which we want to timeit.
import time
from cftool.misc import timeit
# ~~~ [ info ] timing for sleep 1s : 1.0002
with timeit("sleep 1s"):
time.sleep(1)
def timestamp(simplify=False, ensure_different=False) -> str
Return current timestamp.
simplify
: bool. IfTrue
, format will be simplified to 'year-month-day'.ensure_different
: bool. IfTrue
, format will include millisecond.
from cftool.misc import timestamp
# 2019-09-30_21-49-56
print(timestamp())
# 2019-09-30
print(timestamp(simplify=True))
# 2019-09-30_21-49-56-279768
print(timestamp(ensure_different=True))
def prod(iterable) -> float
Return cumulative production of an iterable
.
iterable
: iterable.
from cftool.misc import prod
# 120.0
print(prod(range(1, 6)))
def hash_code(code) -> str
Return hash code for string code
.
code
: str.
from cftool.misc import hash_code
# True
hash_code("a") != hash_code("b")
def prefix_dict(d, prefix) -> dict
Prefix every key in dict d
with prefix
, connected with '_'
.
d
: dict.prefix
: str.
from cftool.misc import prefix_dict
# {"foo_a": 1, "foo_b": 2}
print(prefix_dict({"a": 1, "b": 2}, "foo"))
def shallow_copy_dict(d) -> dict
Shallow copy dict d
, nested dict is also supported.
d
: dict.
from cftool.misc import shallow_copy_dict
d = {"a": 1, "b": {"c": 2, "d": 3}}
sd = shallow_copy_dict(d)
d_copy = d.copy()
d["b"].pop("c")
# {'a': 1, 'b': {'d': 3}}
print(d)
# {'a': 1, 'b': {'c': 2, 'd': 3}}
print(sd)
# {'a': 1, 'b': {'d': 3}}
print(d_copy)
def update_dict(src_dict, tgt_dict) -> dict
Update tgt_dict
with src_dict
.
Changes will happen only on keys which
src_dict
holds, and the update procedure will be recursive.
Changed will happen inplace.
src_dict
: dict.tgt_dict
: str.
from cftool.misc import update_dict
src_dict = {"a": {"b": 1}, "c": 2}
tgt_dict = {"a": {"b": 0, "b1": 1}, "c": 0, "d": 1}
# {"a": {"b": 1, "b1": 1}, "c": 2, "d": 1}
print(update_dict(src_dict, tgt_dict))
def fix_float_to_length(num, length) -> str
Change a float number to string format with fixed length.
num
: float.length
: int.
import math
from cftool.misc import fix_float_to_length
# 1.000000
print(fix_float_to_length(1, 8))
# 1.000000
print(fix_float_to_length(1., 8))
# 1.000000
print(fix_float_to_length(1.0, 8))
# -1.00000
print(fix_float_to_length(-1, 8))
# -1.00000
print(fix_float_to_length(-1., 8))
# -1.00000
print(fix_float_to_length(-1.0, 8))
# 1234567.
print(fix_float_to_length(1234567, 8))
# 12345678
print(fix_float_to_length(12345678, 8))
# 123456789
print(fix_float_to_length(123456789, 8))
# + nan +
print("+" + fix_float_to_length(math.nan, 8) + "+")
def truncate_string_to_length(string, length) -> str
Truncate a string to make sure its length not exceeding a given length.
string
: str.length
: int.
from cftool.misc import truncate_string_to_length
# 123456
print(truncate_string_to_length("123456", 6))
# 12..67
print(truncate_string_to_length("1234567", 6))
# 12..78
print(truncate_string_to_length("12345678", 6))
# 12...78
print(truncate_string_to_length("12345678", 7))
def grouped(iterable, n, *, keep_tail) -> list
Group an iterable
every n
elements.
iterable
: iterable.n
: int.keep_tail
: bool, whether keep the 'tail' (see example below).
from cftool.misc import grouped
# [(0, 1), (2, 3), (4, 5)]
print(grouped(range(6), 2))
# [(0, 1, 2), (3, 4, 5)]
print(grouped(range(6), 3))
# [(0, 1, 2, 3)]
print(grouped(range(6), 4))
# [(0, 1, 2, 3), (4, 5)]
print(grouped(range(6), 4, keep_tail=True))
def is_numeric(s) -> bool
Check whether string s
is numeric.
s
: str.
from cftool.misc import is_numeric
# True
print(is_numeric(0x1))
# True
print(is_numeric(1e0))
# True
print(is_numeric("1"))
# True
print(is_numeric("1."))
# True
print(is_numeric("1.0"))
# True
print(is_numeric("1.00"))
# False
print(is_numeric("1.0.0"))
# True
print(is_numeric("nan"))
def get_one_hot(feature, dim) -> np.ndarray
Get one-hot representation.
feature
: array-like, source data of one-hot representation.dim
: int, dimension of the one-hot representation.
import numpy as np
from cftool.array import get_one_hot
feature = np.array([0, 1, 0])
# [[1 0], [0 1], [1 0]]
print(get_one_hot(feature, 2))
# [[1 0 0] [0 1 0] [1 0 0]]
print(get_one_hot(feature, 3))
# [[1 0 0] [0 1 0] [1 0 0]]
print(get_one_hot(feature.tolist(), 3))
def get_indices_from_another(base, segment) -> np.ndarray
Get segment
elements' indices in base
. This function will return positions where elements in segment
appear in base
.
All elements in segment should appear in base to ensure validity.
base
: np.ndarray, base array.segment
: np.ndarray, segment array.
import numpy as np
from cftool.array import get_indices_from_another
base, segment = np.array([1, 2, 3, 5, 7, 8, 9]), np.array([1, 3, 5, 7, 9])
# [0 2 3 4 6]
print(get_indices_from_another(base, segment))
# [0 1 2 3 4]
print(get_indices_from_another(segment, segment))
# [4 3 2 1 0]
print(get_indices_from_another(segment[::-1], segment))
def get_unique_indices(arr) -> UniqueIndices
Get indices for unique values of an array.
arr
: np.ndarray, target array which we wish to find indices of each unique value.return_raw
: bool, whether returning raw information.
import numpy as np
from cftool.array import get_unique_indices
arr = np.array([1, 2, 3, 2, 4, 1, 0, 1], np.int64)
unique_indices = get_unique_indices(arr)
# UniqueIndices(
# unique = array([0, 1, 2, 3, 4], dtype=int64),
# unique_cnt = array([1, 3, 2, 1, 1], dtype=int64),
# sorting_indices = array([6, 0, 5, 7, 1, 3, 2, 4], dtype=int64),
# split_arr = array([1, 4, 6, 7], dtype=int64))
# split_indices = [array([6], dtype=int64), array([0, 5, 7], dtype=int64), array([1, 3], dtype=int64),
# array([2], dtype=int64), array([4], dtype=int64)]
print(get_unique_indices(arr))
carefree-toolkit
is well documented, feel free to dive into the codes and explore something you may need!
carefree-toolkit
is MIT licensed, as found in the LICENSE
file.