Common Utility functions for development
Project description
cutility
Common utilities for faster development
Table of Contents
Installation
You can install cutility using pip:
pip install --upgrade cutility
Usage
Utils
Measure Execution Time
from cutility.utils import get_exec_time
@get_exec_time
def foo():
import time
time.sleep(1)
foo()
# Output: Time taken to execute 'foo': 0:00:01.005044
Check Path Existence
from cutility.utils import check_path_exist
exists = check_path_exist("./data/temp.txt")
print(exists) # Output: False
Load Environment Variables
from cutility.utils import load_env
ENV = load_env("./.env")
print(ENV)
print(os.getenv("DATA_ROOT"))
print(os.getenv("PROJECT_ROOT"))
print(os.getenv("CONFIG_PATH"))
Loggers
Simple Logger
from cutility.loggers import get_simple_logger
log = get_simple_logger()
log.i("Hello world of loggers")
# Output: [2023-12-17 02:21:03,847] - [INFO] : Hello world of loggers
IO
Read and Write Files
from cutility.io import readers, writers
# Read and write text files
text_content = readers.read_text("./data/example_r.txt")
writers.write_text(text_content, "./data/example_w.txt")
# Read and write JSON files
json_data = readers.read_json("./data/example_r.json")
writers.write_json(json_data, "./data/example_w.json")
# Read and write YAML files
yaml_data = readers.read_yaml("./data/example_r.yaml")
writers.write_yaml(yaml_data, "./data/example_w.yaml")
Dir Handler
from cutility import get_dir_handler
dirh = get_dir_handler(project_root="./", data_root="./data", verbose=True)
print(dirh.get_data_root())
print(dirh.get_project_root())
Cleaners
Generic Cleaner
from cutility.cleaners import GenericSimpleTextCleaner
gtc = GenericSimpleTextCleaner()
sample_text = """Check out this link: https://example.com. ๐ #Python @user1, sample@gmail.com 123-456-7908"""
cleaning_steps = [
(gtc.replace_contacts, {"repl": " {{PHONE}} "}),
(gtc.replace_emails, {"repl": " {{EMAIL}} "}),
(gtc.clean_emojis, {}),
(gtc.clean_hashtags, {}),
(gtc.clean_web_links, {}),
]
cleaned_text = gtc.apply_text_cleaning_functions(sample_text, cleaning_steps)
print(cleaned_text)
Simple Text Cleaner
from cutility.cleaners import SimpleTextCleaner
cleaned_text = SimpleTextCleaner.clean_emojis("๐ Sed euismod justo t semper justo. ๐")
print(cleaned_text)
PII Text Cleaner
from cutility.cleaners import PiiTextCleaner
text = "My contact number is +1(123) 456 7890 and my email is email@company.com"
cleaned_text = PiiTextCleaner.replace_emails(PiiTextCleaner.replace_contacts(text))
print(cleaned_text)
Project Structure
./src
โโโ cutility
โโโ __init__.py
โโโ _version.py
โโโ cleaners
โ โโโ __init__.py
โ โโโ clean.py
โ โโโ pii_cleaner.py
โ โโโ text_cleaner.py
โโโ dir_handler.py
โโโ io
โ โโโ __init__.py
โ โโโ readers
โ โโโ writers
โโโ loggers
โ โโโ __init__.py
โ โโโ _simple_logger.py
โโโ utils
โโโ __init__.py
โโโ env_loader.py
โโโ exec_time.py
โโโ path.py
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cutility-1.0.0.tar.gz
(18.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
cutility-1.0.0-py3-none-any.whl
(18.4 kB
view details)
File details
Details for the file cutility-1.0.0.tar.gz.
File metadata
- Download URL: cutility-1.0.0.tar.gz
- Upload date:
- Size: 18.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a29af4c077948bbcd9ab7bc39d4c0fbb6126e42d7c49775fc99c938ca5d6a76
|
|
| MD5 |
8f5180fc342d85ce97ec28bb1f94d760
|
|
| BLAKE2b-256 |
f3da9ccad78d9981f5731de0de957485fdf04e45473721a5072d3f3633af0862
|
File details
Details for the file cutility-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cutility-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20e28a5d4b36e434b793f9c690abedcd3208cb60e5975a7e4b0c43dd4670be2e
|
|
| MD5 |
80346bb6029f3c407c7d4b5f7b1a6ab9
|
|
| BLAKE2b-256 |
298689e7606c0ad6d1b54b5eeec6b8ea4e7863051e8f5eb27420fb8d204247d7
|