HamPy – The All-in-One Python Utility Module

Safe, Efficient, and Reliable Tools for Data Handling, Validation, Networking, and More.

pip install HamPy

Safe Casting & Parsing

Convert and validate your data effortlessly with built-in fallback mechanisms and error handling.

from HamPy import safe_cast, is_numeric, parse_json

num = safe_cast("123", int, fallback=0)   # 123
print(is_numeric("45.6"))                # True
data = parse_json('{"key": "value"}')    # {'key': 'value'}

Input Handling

Ask users safely and handle their inputs with comprehensive validation and type conversion.

from HamPy import safe_input, ask_yes_no, choose_from_list

age = safe_input("Enter your age: ", int, 18)
confirmed = ask_yes_no("Do you agree?")
color = choose_from_list("Pick a color:", ["Red", "Green", "Blue"])

Validation Utilities

Quickly validate numbers, emails, phones, and passwords with industry-standard patterns.

from HamPy import validate_email, validate_phone, validate_password

validate_email("test@example.com")            # True
validate_phone("+911234567890")               # True
validate_password("StrongP@ssword123!")       # True

Error Handling & Logging

Execute safely, log errors, and retry operations with configurable parameters and fallbacks.

from HamPy import safe_exec, log_error, retry

result = safe_exec(lambda: 1 / 0, fallback=0)  # 0
log_error(Exception("Oops!"), "Division failed")
value = retry(lambda: int("abc"), attempts=3, delay=1)  # Raises Exception

Data Helpers

Flatten, chunk, and deduplicate lists easily with optimized algorithms for better performance.

from HamPy import flatten_list, chunk_list, unique_list

nested = [1, [2, 3], [4, [5, 6]]]
print(flatten_list(nested))      # [1, 2, 3, 4, 5, 6]

print(chunk_list([1,2,3,4,5], 2))  # [[1,2],[3,4],[5]]
print(unique_list([1,2,2,3]))      # [1,2,3]

String Helpers

Clean, normalize, and manipulate strings easily with Unicode support and edge case handling.

from HamPy import safe_strip, slugify, truncate, normalize_whitespace

print(safe_strip("  hello "))           # "hello"
print(slugify("Hello World!"))          # "hello-world"
print(truncate("This is long text", 10))  # "This is lo..."
print(normalize_whitespace("a   b  c")) # "a b c"

File & OS Utilities

Safely read, write, delete files and check paths with proper error handling and permissions.

from HamPy import safe_read, safe_write, file_exists, safe_delete

safe_write("file.txt", "Hello HamPy!")
print(safe_read("file.txt"))   # "Hello HamPy!"
print(file_exists("file.txt")) # True
safe_delete("file.txt")

Networking Utilities

Reliable network requests and connectivity checks with timeout handling and retry mechanisms.

from HamPy import is_internet_available, safe_get

print(is_internet_available())               # True
content = safe_get("https://example.com")    # "..."