Skip to main content

Essential Python utilities for console, math, system, and more

Project description

Swiss-Utils – Essential Python Utilities

Python Version License

A collection of battle‑tested utility modules for everyday Python tasks: console interaction, file dialogs, precise arithmetic, data handling, string formatting, system operations, and more.

Designed to be lightweight, self‑contained, and easy to integrate into any project.


📦 Installation

pip install swiss-utils 

Or using UV:

uv add swiss-utils

Or clone & install manually:

git clone https://github.com/codigomex/Swiss-Utils.git
cd swiss_utils
pip install -e .

🚀 Quick Start

from swiss_utils.console import pparr, ask_type, clear
from swiss_utils.math import div_prec, precise_round
from swiss_utils.system import show_tmp, init_tmp

# Or you can import everything using (eg):
# from swiss_utils import div_prec, init_tmp, pparr, etc.

# Clear terminal and print formatted message
clear()
pparr("Hello, world!", indent=">>> ")

# Safe division with decimal precision
result = div_prec(10, 3, precision=4)
print(result)  # 3.3333

# Round with specific rounding mode
rounded = precise_round(2.675, decimals=2)   # 2.68 (ROUND_HALF_UP)

# Show any text in a temporary file (opens in system editor)
init_tmp()
show_tmp("Important data\nLine 2", wait=True)

📚 Module Overview

Module Description
config Global constants: OS detection, temp dir, ANSI escape, terminal width.
console Interactive I/O: progress bar, waiting dots, user input validation, dates.
data Dictionary comparison, UUID‑like token generator, pretty data display.
format Random strings, ANSI stripping, time formatting, terminal colors (cl).
forms Tkinter file & directory dialogs (returns Path or None).
math High‑precision rounding, division (with configurable precision), time‑to‑seconds.
py @immutable decorator – make class instances read‑only after __init__.
system File execution, temp file handling, error traceback, user input via editor.
units Convert between pixels, mm, points, inches – exact Decimal results.

🧩 Detailed Examples

1. Console & User Input

# swiss_utils.console
from swiss_utils import ask_verif, ask_date, op_yn, draw_progress_bar

# Yes/No prompt
if op_yn():
    print("User agreed")

# Validate from a set of answers
color = ask_verif("Choose color:", "red", "green", "blue")

# Date input (no future dates allowed)
date = ask_date("Enter birth date (YYYY-MM-DD):", fmt="%Y-%m-%d", future=False)

# Progress bar
for p in range(0, 101, 10):
    draw_progress_bar(p)

2. Immutable Classes

# swiss_utils.py
from swiss_utils import immutable

@immutable
class Config:
    def __init__(self, host, port):
        self.host = host
        self.port = port

cfg = Config("localhost", 8080)
cfg.host = "other"   # ❌ raises AttributeError

3. Precise Arithmetic

# swiss_utils.math
from swiss_utils import div_prec, precise_round
from decimal import ROUND_DOWN

# Division with 6 decimal places
result = div_prec(22, 7, precision=6)   # 3.142857

# Round with custom mode
rounded = precise_round(3.14159, decimals=4, rounding_mode=ROUND_DOWN)  # 3.1415

4. Temporary Files / System

# swiss_utils.system
from swiss_utils import init_tmp, show_tmp, get_user_input

init_tmp(delete=True)          # clean previous temp dir

# Open a text file for long user input
lines = get_user_input("Describe the problem in detail:")
print(f"You wrote {len(lines)} lines")

# Show dictionary/object dump in a temp file
show_tmp("Debug info:\n" + str(some_dict), wait=False)

5. File Dialogs (no extra GUI loops)

# swiss_utils.forms
from swiss_utils import pic_dir, pic_file

folder = pic_dir(Path.home())      # returns Path or None
file   = pic_file(Path.cwd(), ext="json")

6. Colors & Formatting

# swiss_utils.format
from swiss_utils import cl

print(f"{cl.bgreen}Success!{cl.endc}")
print(f"{cl.red}Error:{cl.endc} Something went wrong")

🛠 Development & Testing

# Run all tests (if you add a tests/ folder)
pytest tests/

# Static type check
mypy swiss_utils/

🤝 Contributing

Pull requests are welcome @codigomex! For major changes, please open an issue first.

📄 License

GPL‑3.0 © CodigoMex

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

swissutils-1.2.0.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

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

swissutils-1.2.0-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

File details

Details for the file swissutils-1.2.0.tar.gz.

File metadata

  • Download URL: swissutils-1.2.0.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for swissutils-1.2.0.tar.gz
Algorithm Hash digest
SHA256 2128ebf27fc15796ddcdf483265980c37e6a035c35c199cfc3c416503adeee64
MD5 b3130d8b3cd2d4d3de14109ec2c1fafe
BLAKE2b-256 fc2626f1ad01b6f87c5c7975411e4aa9398ff2972e68d522e4242af58981347b

See more details on using hashes here.

File details

Details for the file swissutils-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: swissutils-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for swissutils-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a18375c92cd80dfae0941944aa9b4bc764dae47251f13aa37b2b83a3f67e8289
MD5 71c3a4034c0ed7b355b1003b6fd7cabd
BLAKE2b-256 a16f58399365da2b9daaf48d41891d44b842e19fbf47178760d7434823638562

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