Skip to main content

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)

Path Manager

from cutility import PathManager, create_path_manager

pm = create_path_manager(project_root="./", data_root="./data", verbose=True)
print(pm.data_root)
print(pm.project_root)

# Load YAML configuration
config = pm.load_yaml_config("my_config")
print(config)

# Create directory structure
pm.create_directory_structure("./output", ["logs", "data", "results"])

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 Apache 2.0 License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cutility-0.1.7.dev9.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cutility-0.1.7.dev9-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file cutility-0.1.7.dev9.tar.gz.

File metadata

  • Download URL: cutility-0.1.7.dev9.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for cutility-0.1.7.dev9.tar.gz
Algorithm Hash digest
SHA256 a3bb5e8c0c47049faf2856e225b7928a1ba585bdb9a9296f3cdec00637f10763
MD5 a0579ce6057d7a607081a7a8c3b72055
BLAKE2b-256 e0b50b7c8d2261ea81622460384a1e060bff2f5b3a4cee72f0cdcd07316c2b76

See more details on using hashes here.

File details

Details for the file cutility-0.1.7.dev9-py3-none-any.whl.

File metadata

  • Download URL: cutility-0.1.7.dev9-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for cutility-0.1.7.dev9-py3-none-any.whl
Algorithm Hash digest
SHA256 6ccd8b5a33514ad3116a0ca36de7a564f1d88ff8e88596fb4a01033f55a6d258
MD5 8957c9fdd7512d48d8556dca4d285465
BLAKE2b-256 0f2d48388101ee1e6a55798e7f30ee37155cace0e4fbaadf15f75ef3c93a4317

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page