# carefree-toolkit
`carefree-toolkit` implemented some commonly used functions and modules
## Installation
`carefree-toolkit` requires Python 3.7 or higher.
```bash
pip install carefree-toolkit
```
or
```bash
git clone https://github.com/carefree0910/carefree-toolkit.git
cd carefree-toolkit
pip install -e .
```
## Usages
### **`timeit`**
```python
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
```python
import time
from cftool.misc import timeit
# ~~~ [ info ] timing for sleep 1s : 1.0002
with timeit("sleep 1s"):
time.sleep(1)
```
### **`timestamp`**
```python
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
```python
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))
```
### **`prod`**
```python
def prod(iterable) -> float
```
Return cumulative production of an **`iterable`**.
#### Parameters
+ **`iterable`** : **iterable**.
#### Example
```python
from cftool.misc import prod
# 120.0
print(prod(range(1, 6)))
```
### **`hash_code`**
```python
def hash_code(code) -> str
```
Return hash code for string **`code`**.
#### Parameters
+ **`code`** : **str**.
#### Example
```python
from cftool.misc import hash_code
# True
hash_code("a") != hash_code("b")
```
### **`prefix_dict`**
```python
def prefix_dict(d, prefix) -> dict
```
Prefix every key in dict **`d`** with **`prefix`**, connected with `'_'`.
#### Parameters
+ **`d`** : **dict**.
+ **`prefix`** : **str**.
#### Example
```python
from cftool.misc import prefix_dict
# {"foo_a": 1, "foo_b": 2}
print(prefix_dict({"a": 1, "b": 2}, "foo"))
```
### **`shallow_copy_dict`**
```python
def shallow_copy_dict(d) -> dict
```
Shallow copy dict **`d`**, nested dict is also supported.
#### Parameters
+ **`d`** : **dict**.
#### Example
```python
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)
```
### **`update_dict`**
```python
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
```python
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))
```
### **`fix_float_to_length`**
```python
def fix_float_to_length(num, length) -> str
```
Change a float number to string format with fixed length.
#### Parameters
+ **`num`** : **float**.
+ **`length`** : **int**.
#### Example
```python
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) + "+")
```
### **`truncate_string_to_length`**
```python
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
```python
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))
```
### **`grouped`**
```python
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
```python
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))
```
### **`is_number`**
```python
def is_numeric(s) -> bool
```
Check whether string **`s`** is numeric.
#### Parameters
+ **`s`** : **str**.
#### Example
```python
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"))
```
### **`get_one_hot`**
```python
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
```python
import numpy as np
from cftool.misc 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))
```
### **`get_indices_from_another`**
```python
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
```python
import numpy as np
from cftool.misc 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))
```
### **`get_unique_indices`**
```python
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
```python
import numpy as np
from cftool.misc 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),
没有合适的资源?快使用搜索试试~ 我知道了~
PyPI 官网下载 | carefree-toolkit-0.2.7.tar.gz
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 43 浏览量
2022-01-31
11:28:12
上传
评论
收藏 123KB GZ 举报
温馨提示
共54个文件
py:44个
txt:4个
pkg-info:2个
资源来自pypi官网。 资源全名:carefree-toolkit-0.2.7.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
carefree-toolkit-0.2.7.tar.gz (54个子文件)
carefree-toolkit-0.2.7
PKG-INFO 12KB
carefree_toolkit.egg-info
PKG-INFO 12KB
requires.txt 134B
SOURCES.txt 1KB
top_level.txt 7B
dependency_links.txt 1B
setup.cfg 42B
setup.py 2KB
cftool
optim
gd
bases.py 6KB
optimizers
adam.py 1KB
__init__.py 113B
sgd.py 934B
rmsprop.py 785B
__init__.py 120B
bo
__init__.py 81B
acquisition.py 3KB
model.py 4KB
target_space.py 3KB
__init__.py 157B
ml
param_utils
core.py 7KB
normalizers
bases.py 4KB
__init__.py 73B
cube
core.py 1KB
__init__.py 68B
types.py 306B
distributions.py 4KB
__init__.py 438B
data_types.py 6KB
utils.py 56KB
hpo
naive
core.py 189B
__init__.py 56B
bo
core.py 2KB
__init__.py 62B
__init__.py 101B
base.py 10KB
__init__.py 230B
stat.py 6KB
misc.py 75KB
dist
core.py 21KB
utils.py 3KB
__init__.py 103B
__init__.py 0B
c
cython_wrappers.py 1014B
__init__.py 777B
cython_utils.c 427KB
cython_substitute.py 2KB
cython_utils.pyx 5KB
manage.py 25KB
cv
processor
core.py 11KB
__init__.py 58B
utils.py 1KB
__init__.py 186B
shape.py 24KB
README.md 8KB
共 54 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功