Skip to main content

Reusable Python utilities for dates, money, and more

Project description

python-utils-rondomondo

Reusable Python utilities for dates and money.

Install

pip install python-utils-rondomondo

Or with uv:

uv add python-utils-rondomondo

Usage

from python_utils.dates import now_utc, parse_duration, date_windows
from python_utils.money import to_money, fmt_accounting, format_currency, allocate

Dates

Function Description
now_utc(microsecond=False) Current UTC datetime, always timezone-aware
to_iso(dt, timespec="seconds") Format datetime as ISO 8601 string (space separator)
from_iso(s) Parse ISO 8601 string to datetime
parse_duration(s) Parse human duration like "7days", "3hrs", "90secs" to timedelta
midnight_before(dt) Midnight at the start of the day before dt
date_windows(start, end, window) Split a range into non-overlapping timedelta windows
is_leap(year) True if the given year is a leap year
same_day_next_year(dt) Advance by one calendar year, clamping Feb 29 to Feb 28
from python_utils.dates import now_utc, parse_duration, date_windows, to_iso
from datetime import timedelta

now = now_utc()
end = now + parse_duration("7days")
windows = date_windows(now, end, timedelta(days=2))
print(to_iso(now))  # '2026-05-19 14:30:00+00:00'

Money

Function Description
to_money(value) Convert str, int, or Decimal to a cent-quantised Decimal (half-up)
to_decimal(value) Convert int, float, str, or Decimal to Decimal without rounding
round_half_up(amount, places=2) Round using half-up convention
round_bankers(amount, places=2) Round using banker's rounding (half to even)
truncate(amount, places=2) Truncate toward zero
format_currency(amount, symbol="$", thousands=True) Format Decimal as a currency string
parse_currency(s) Parse a currency string like "$1,234.56" or "-£99.00" to Decimal
fmt_accounting(value) Accounting convention: negatives as (120.00), positives as 500.00
allocate(amount, ratios) Distribute amount across ratios with no penny lost or gained
from decimal import Decimal
from python_utils.money import to_money, format_currency, fmt_accounting, allocate

price = to_money("19.99")
tax   = to_money(price * Decimal("0.1"))  # 2.00
total = price + tax                       # 21.99

print(format_currency(total))             # '$21.99'
print(fmt_accounting(Decimal("-120.00"))) # '(120.00)'
print(fmt_accounting(Decimal("500.00"))) # '500.00 '

shares = allocate(Decimal("100.00"), [1, 2, 3])  # [33.33, 33.33, 33.34] (sums exactly)

Examples

F-string formatting reference

src/python_utils/examples/fstrings.py is a runnable reference script covering the full range of Python f-string format specs, using an invoice as the running example:

  • Number formatting: decimal places, thousands separators, alignment, currency symbols
  • Sign formatting: explicit +, space padding, accounting parentheses
  • Integer bases: hex, octal, binary, scientific notation, percent
  • String alignment, padding, fill characters, truncation
  • Expressions, conditionals, and method calls inside f-strings
  • Conversion flags: !s, !r, !a
  • A complete invoice table tying all patterns together
python src/python_utils/examples/fstrings.py

Requirements

Python 3.12+

License

MIT

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

python_utils_rondomondo-0.1.4.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

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

python_utils_rondomondo-0.1.4-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file python_utils_rondomondo-0.1.4.tar.gz.

File metadata

  • Download URL: python_utils_rondomondo-0.1.4.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for python_utils_rondomondo-0.1.4.tar.gz
Algorithm Hash digest
SHA256 c6cda983cfc941a1d91f45c1b7fedf70ae75f29724e4d2e9888fe6d7304ad942
MD5 38b825b8581158126c16a7a7eb64fa87
BLAKE2b-256 fc43b54fbf6055c31e5ca9c07902f4d6af232022ccbe625f5bb584c5f75b4a41

See more details on using hashes here.

File details

Details for the file python_utils_rondomondo-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: python_utils_rondomondo-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for python_utils_rondomondo-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 370b4cf42231b36de5a6c49b0bfbc62f73fb28613f8b6518004844199b0d92d8
MD5 864e09089453cb4d64517743ee203b36
BLAKE2b-256 4576d0cc5724ede133f91030ca00ec6beb2d7357558d8f1b45eea6906725202c

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