carefree-toolkit
carefree-toolkit
implemented some commonly used functions and modules
Installation
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 .
Usages
timeit
class timeit(context_error_handler):
def __init__(self, msg)
Timing context manager.
Parameters
msg
: str, name of the context which we want to timeit.
Example
import time
from cftool.misc import timeit
with timeit("sleep 1s"):
time.sleep(1)
timestamp
def timestamp(simplify=False, ensure_different=False) -> str
Return current timestamp.
Parameters
simplify
: bool. If True
, format will be simplified to 'year-month-day'.ensure_different
: bool. If True
, format will include millisecond.
Example
from cftool.misc import timestamp
print(timestamp())
print(timestamp(simplify=True))
print(timestamp(ensure_different=True))
prod
def prod(iterable) -> float
Return cumulative production of an iterable
.
Parameters
Example
from cftool.misc import prod
print(prod(range(1, 6)))
hash_code
def hash_code(code) -> str
Return hash code for string code
.
Parameters
Example
from cftool.misc import hash_code
hash_code("a") != hash_code("b")
prefix_dict
def prefix_dict(d, prefix) -> dict
Prefix every key in dict d
with prefix
, connected with '_'
.
Parameters
Example
from cftool.misc import prefix_dict
print(prefix_dict({"a": 1, "b": 2}, "foo"))
shallow_copy_dict
def shallow_copy_dict(d) -> dict
Shallow copy dict d
, nested dict is also supported.
Parameters
Example
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")
print(d)
print(sd)
print(d_copy)
update_dict
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.
Parameters
src_dict
: dict.tgt_dict
: str.
Example
from cftool.misc import update_dict
src_dict = {"a": {"b": 1}, "c": 2}
tgt_dict = {"a": {"b": 0, "b1": 1}, "c": 0, "d": 1}
print(update_dict(src_dict, tgt_dict))
fix_float_to_length
def fix_float_to_length(num, length) -> str
Change a float number to string format with fixed length.
Parameters
num
: float.length
: int.
Example
import math
from cftool.misc import fix_float_to_length
print(fix_float_to_length(1, 8))
print(fix_float_to_length(1., 8))
print(fix_float_to_length(1.0, 8))
print(fix_float_to_length(-1, 8))
print(fix_float_to_length(-1., 8))
print(fix_float_to_length(-1.0, 8))
print(fix_float_to_length(1234567, 8))
print(fix_float_to_length(12345678, 8))
print(fix_float_to_length(123456789, 8))
print("+" + fix_float_to_length(math.nan, 8) + "+")
truncate_string_to_length
def truncate_string_to_length(string, length) -> str
Truncate a string to make sure its length not exceeding a given length.
Parameters
string
: str.length
: int.
Example
from cftool.misc import truncate_string_to_length
print(truncate_string_to_length("123456", 6))
print(truncate_string_to_length("1234567", 6))
print(truncate_string_to_length("12345678", 6))
print(truncate_string_to_length("12345678", 7))
grouped
def grouped(iterable, n, *, keep_tail) -> list
Group an iterable
every n
elements.
Parameters
iterable
: iterable.n
: int.keep_tail
: bool, whether keep the 'tail' (see example below).
Example
from cftool.misc import grouped
print(grouped(range(6), 2))
print(grouped(range(6), 3))
print(grouped(range(6), 4))
print(grouped(range(6), 4, keep_tail=True))
is_number
def is_numeric(s) -> bool
Check whether string s
is numeric.
Parameters
Example
from cftool.misc import is_numeric
print(is_numeric(0x1))
print(is_numeric(1e0))
print(is_numeric("1"))
print(is_numeric("1."))
print(is_numeric("1.0"))
print(is_numeric("1.00"))
print(is_numeric("1.0.0"))
print(is_numeric("nan"))
get_one_hot
def get_one_hot(feature, dim) -> np.ndarray
Get one-hot representation.
Parameters
feature
: array-like, source data of one-hot representation.dim
: int, dimension of the one-hot representation.
Example
import numpy as np
from cftool.array import get_one_hot
feature = np.array([0, 1, 0])
print(get_one_hot(feature, 2))
print(get_one_hot(feature, 3))
print(get_one_hot(feature.tolist(), 3))
get_indices_from_another
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.
Parameters
base
: np.ndarray, base array.segment
: np.ndarray, segment array.
Example
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])
print(get_indices_from_another(base, segment))
print(get_indices_from_another(segment, segment))
print(get_indices_from_another(segment[::-1], segment))
get_unique_indices
def get_unique_indices(arr) -> UniqueIndices
Get indices for unique values of an array.
Parameters
arr
: np.ndarray, target array which we wish to find indices of each unique value.return_raw
: bool, whether returning raw information.
Example
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)
print(get_unique_indices(arr))
And more...
carefree-toolkit
is well documented, feel free to dive into the codes and explore something you may need!
License
carefree-toolkit
is MIT licensed, as found in the LICENSE
file.