{"id":20992900,"url":"https://github.com/carefree0910/carefree-toolkit","last_synced_at":"2025-07-19T05:07:26.340Z","repository":{"id":49786316,"uuid":"268790852","full_name":"carefree0910/carefree-toolkit","owner":"carefree0910","description":"Some commonly used functions and modules","archived":false,"fork":false,"pushed_at":"2024-01-15T03:24:27.000Z","size":444,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"dev","last_synced_at":"2025-07-03T00:57:26.394Z","etag":null,"topics":["data-science","numpy","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/carefree0910.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-02T12:09:19.000Z","updated_at":"2023-10-23T07:37:04.000Z","dependencies_parsed_at":"2024-01-06T08:26:48.358Z","dependency_job_id":"96236a08-bdfc-4059-be76-1a372aa7437f","html_url":"https://github.com/carefree0910/carefree-toolkit","commit_stats":{"total_commits":433,"total_committers":2,"mean_commits":216.5,"dds":"0.18937644341801385","last_synced_commit":"93439c85f642c8bf2f7b2d3f5d3c6cab42f8a386"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/carefree0910/carefree-toolkit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carefree0910%2Fcarefree-toolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carefree0910%2Fcarefree-toolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carefree0910%2Fcarefree-toolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carefree0910%2Fcarefree-toolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/carefree0910","download_url":"https://codeload.github.com/carefree0910/carefree-toolkit/tar.gz/refs/heads/dev","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carefree0910%2Fcarefree-toolkit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265892110,"owners_count":23844968,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-science","numpy","python"],"created_at":"2024-11-19T07:13:06.798Z","updated_at":"2025-07-19T05:07:26.321Z","avatar_url":"https://github.com/carefree0910.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# carefree-toolkit\n\n`carefree-toolkit` implemented some commonly used functions and modules\n\n\n## Installation\n\n`carefree-toolkit` requires Python 3.8 or higher.\n\n```bash\npip install carefree-toolkit\n```\n\nor\n\n```bash\ngit clone https://github.com/carefree0910/carefree-toolkit.git\ncd carefree-toolkit\npip install -e .\n```\n\n\n## Usages\n\n### **`timeit`**\n\n```python\nclass timeit(context_error_handler):\n    def __init__(self, msg)\n```\n\nTiming context manager.\n\n#### Parameters\n\n+ **`msg`** : **str**, name of the context which we want to timeit.\n\n#### Example\n\n```python\nimport time\nfrom cftool.misc import timeit\n\n# ~~~  [ info ] timing for     sleep 1s     : 1.0002\nwith timeit(\"sleep 1s\"):\n    time.sleep(1)\n```\n\n\n### **`timestamp`**\n\n```python\ndef timestamp(simplify=False, ensure_different=False) -\u003e str\n```\n\nReturn current timestamp.\n\n#### Parameters\n\n+ **`simplify`** : **bool**. If `True`, format will be simplified to 'year-month-day'.\n+ **`ensure_different`** : **bool**. If `True`, format will include millisecond.\n\n#### Example\n\n```python\nfrom cftool.misc import timestamp\n\n# 2019-09-30_21-49-56\nprint(timestamp())\n# 2019-09-30\nprint(timestamp(simplify=True))\n# 2019-09-30_21-49-56-279768\nprint(timestamp(ensure_different=True))\n```\n\n\n### **`prod`**\n\n```python\ndef prod(iterable) -\u003e float\n```\n\nReturn cumulative production of an **`iterable`**.\n\n#### Parameters\n\n+ **`iterable`** : **iterable**.\n\n#### Example\n\n```python\nfrom cftool.misc import prod\n\n# 120.0\nprint(prod(range(1, 6)))\n```\n\n\n### **`hash_code`**\n\n```python\ndef hash_code(code) -\u003e str\n```\n\nReturn hash code for string **`code`**.\n\n#### Parameters\n\n+ **`code`** : **str**.\n\n#### Example\n\n```python\nfrom cftool.misc import hash_code\n\n# True\nhash_code(\"a\") != hash_code(\"b\")\n```\n\n\n### **`prefix_dict`**\n\n```python\ndef prefix_dict(d, prefix) -\u003e dict\n```\n\nPrefix every key in dict **`d`** with **`prefix`**, connected with `'_'`.\n\n#### Parameters\n\n+ **`d`** : **dict**.\n+ **`prefix`** : **str**.\n\n#### Example\n\n```python\nfrom cftool.misc import prefix_dict\n\n# {\"foo_a\": 1, \"foo_b\": 2}\nprint(prefix_dict({\"a\": 1, \"b\": 2}, \"foo\"))\n```\n\n\n### **`shallow_copy_dict`**\n\n```python\ndef shallow_copy_dict(d) -\u003e dict\n```\n\nShallow copy dict **`d`**, nested dict is also supported.\n\n#### Parameters\n\n+ **`d`** : **dict**.\n\n#### Example\n\n```python\nfrom cftool.misc import shallow_copy_dict\n\nd = {\"a\": 1, \"b\": {\"c\": 2, \"d\": 3}}\nsd = shallow_copy_dict(d)\nd_copy = d.copy()\nd[\"b\"].pop(\"c\")\n# {'a': 1, 'b': {'d': 3}}\nprint(d)\n# {'a': 1, 'b': {'c': 2, 'd': 3}}\nprint(sd)\n# {'a': 1, 'b': {'d': 3}}\nprint(d_copy)\n```\n\n\n### **`update_dict`**\n\n```python\ndef update_dict(src_dict, tgt_dict) -\u003e dict\n```\n\nUpdate **`tgt_dict`** with **`src_dict`**.\n\n\u003e Changes will happen only on keys which **`src_dict`** holds, and the update procedure will be recursive.\n\n\u003e Changed will happen inplace.\n\n#### Parameters\n\n+ **`src_dict`** : **dict**.\n+ **`tgt_dict`** : **str**.\n\n#### Example\n\n```python\nfrom cftool.misc import update_dict\n\nsrc_dict = {\"a\": {\"b\": 1}, \"c\": 2}\ntgt_dict = {\"a\": {\"b\": 0, \"b1\": 1}, \"c\": 0, \"d\": 1}\n# {\"a\": {\"b\": 1, \"b1\": 1}, \"c\": 2, \"d\": 1}\nprint(update_dict(src_dict, tgt_dict))\n```\n\n\n### **`fix_float_to_length`**\n\n```python\ndef fix_float_to_length(num, length) -\u003e str\n```\n\nChange a float number to string format with fixed length.\n\n#### Parameters\n\n+ **`num`** : **float**.\n+ **`length`** : **int**.\n\n#### Example\n\n```python\nimport math\nfrom cftool.misc import fix_float_to_length\n\n# 1.000000\nprint(fix_float_to_length(1, 8))\n# 1.000000\nprint(fix_float_to_length(1., 8))\n# 1.000000\nprint(fix_float_to_length(1.0, 8))\n# -1.00000\nprint(fix_float_to_length(-1, 8))\n# -1.00000\nprint(fix_float_to_length(-1., 8))\n# -1.00000\nprint(fix_float_to_length(-1.0, 8))\n# 1234567.\nprint(fix_float_to_length(1234567, 8))\n# 12345678\nprint(fix_float_to_length(12345678, 8))\n# 123456789\nprint(fix_float_to_length(123456789, 8))\n# +  nan   +\nprint(\"+\" + fix_float_to_length(math.nan, 8) + \"+\")\n```\n\n\n### **`truncate_string_to_length`**\n\n```python\ndef truncate_string_to_length(string, length) -\u003e str\n```\n\nTruncate a string to make sure its length not exceeding a given length.\n\n#### Parameters\n\n+ **`string`** : **str**.\n+ **`length`** : **int**.\n\n#### Example\n\n```python\nfrom cftool.misc import truncate_string_to_length\n\n# 123456\nprint(truncate_string_to_length(\"123456\", 6))\n# 12..67\nprint(truncate_string_to_length(\"1234567\", 6))\n# 12..78\nprint(truncate_string_to_length(\"12345678\", 6))\n# 12...78\nprint(truncate_string_to_length(\"12345678\", 7))\n```\n\n\n### **`grouped`**\n\n```python\ndef grouped(iterable, n, *, keep_tail) -\u003e list\n```\n\nGroup an **`iterable`** every **`n`** elements.\n\n#### Parameters\n\n+ **`iterable`** : **iterable**.\n+ **`n`** : **int**.\n+ **`keep_tail`** : **bool**, whether keep the 'tail' (see example below).\n\n#### Example\n\n```python\nfrom cftool.misc import grouped\n\n# [(0, 1), (2, 3), (4, 5)]\nprint(grouped(range(6), 2))\n# [(0, 1, 2), (3, 4, 5)]\nprint(grouped(range(6), 3))\n# [(0, 1, 2, 3)]\nprint(grouped(range(6), 4))\n# [(0, 1, 2, 3), (4, 5)]\nprint(grouped(range(6), 4, keep_tail=True))\n```\n\n\n### **`is_number`**\n\n```python\ndef is_numeric(s) -\u003e bool\n```\n\nCheck whether string **`s`** is numeric.\n\n#### Parameters\n\n+ **`s`** : **str**.\n\n#### Example\n\n```python\nfrom cftool.misc import is_numeric\n\n# True\nprint(is_numeric(0x1))\n# True\nprint(is_numeric(1e0))\n# True\nprint(is_numeric(\"1\"))\n# True\nprint(is_numeric(\"1.\"))\n# True\nprint(is_numeric(\"1.0\"))\n# True\nprint(is_numeric(\"1.00\"))\n# False\nprint(is_numeric(\"1.0.0\"))\n# True\nprint(is_numeric(\"nan\"))\n```\n\n\n### **`get_one_hot`**\n\n```python\ndef get_one_hot(feature, dim) -\u003e np.ndarray\n```\n\nGet one-hot representation.\n\n#### Parameters\n\n+ **`feature`** : **array-like**, source data of one-hot representation.\n+ **`dim`** : **int**, dimension of the one-hot representation. \n\n#### Example\n\n```python\nimport numpy as np\nfrom cftool.array import get_one_hot\n\nfeature = np.array([0, 1, 0])\n# [[1 0], [0 1], [1 0]]\nprint(get_one_hot(feature, 2))\n# [[1 0 0] [0 1 0] [1 0 0]]\nprint(get_one_hot(feature, 3))\n# [[1 0 0] [0 1 0] [1 0 0]]\nprint(get_one_hot(feature.tolist(), 3))\n```\n\n\n### **`get_indices_from_another`**\n\n```python\ndef get_indices_from_another(base, segment) -\u003e np.ndarray\n```\n\nGet **`segment`** elements' indices in **`base`**. This function will return positions where elements in **`segment`** appear in **`base`**.\n\n\u003e All elements in segment should appear in base to ensure validity.\n\n#### Parameters\n\n+ **`base`** : **np.ndarray**, base array.\n+ **`segment`** : **np.ndarray**, segment array. \n\n#### Example\n\n```python\nimport numpy as np\nfrom cftool.array import get_indices_from_another\n\nbase, segment = np.array([1, 2, 3, 5, 7, 8, 9]), np.array([1, 3, 5, 7, 9])\n# [0 2 3 4 6]\nprint(get_indices_from_another(base, segment))\n# [0 1 2 3 4]\nprint(get_indices_from_another(segment, segment))\n# [4 3 2 1 0]\nprint(get_indices_from_another(segment[::-1], segment))\n```\n\n\n### **`get_unique_indices`**\n\n```python\ndef get_unique_indices(arr) -\u003e UniqueIndices\n```\n\n Get indices for unique values of an array.\n\n#### Parameters\n\n+ **`arr`** : **np.ndarray**, target array which we wish to find indices of each unique value.\n+ **`return_raw`** : **bool**, whether returning raw information.\n\n#### Example\n\n```python\nimport numpy as np\nfrom cftool.array import get_unique_indices\n\narr = np.array([1, 2, 3, 2, 4, 1, 0, 1], np.int64)\nunique_indices = get_unique_indices(arr)\n# UniqueIndices(\n#   unique          = array([0, 1, 2, 3, 4], dtype=int64),\n#   unique_cnt      = array([1, 3, 2, 1, 1], dtype=int64),\n#   sorting_indices = array([6, 0, 5, 7, 1, 3, 2, 4], dtype=int64),\n#   split_arr       = array([1, 4, 6, 7], dtype=int64))\n#   split_indices   = [array([6], dtype=int64), array([0, 5, 7], dtype=int64), array([1, 3], dtype=int64),\n#                      array([2], dtype=int64), array([4], dtype=int64)]\nprint(get_unique_indices(arr))\n```\n\n\n### And more...\n\n`carefree-toolkit` is well documented, feel free to dive into the codes and explore something you may need!\n\n\n## License\n\n`carefree-toolkit` is MIT licensed, as found in the [`LICENSE`](https://github.com/carefree0910/carefree-toolkit/blob/master/LICENSE) file.\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarefree0910%2Fcarefree-toolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcarefree0910%2Fcarefree-toolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarefree0910%2Fcarefree-toolkit/lists"}