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.5.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.5-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_utils_rondomondo-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 2191eff3bc23f97036a33a0f8fd654c3171c21efb50c8e3161a5eb85a0eb03c3
MD5 e74104c1f225dd892241e27b3f72cd8d
BLAKE2b-256 7dd5043cb12b2d84e7512cd9dc0321096911d5730c3e2065b5c5c0ddb83d08be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_utils_rondomondo-0.1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c8cf830058562f4e4e71c385e312f4d4141b7b2c40bc519dc27d19e3a0b2cff8
MD5 456287d09ea603e32599e266d7f224b5
BLAKE2b-256 cfa5bb17a91e2c5d2e573db75ad51fbc3ff7a294b6363aa6ef626520a58c1744

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