← Practical Python
05 / 10 · 23 MIN

Precision, external data, and time

Handle decimals and timestamps without hidden assumptions.

Understand the concept

CSV and API data need schema, value, and rule validation. Converting text does not prove functional validity. For exact decimal arithmetic, Decimal constructed from text avoids importing a float approximation. Rounding and scale still need explicit definition.

Apply and decide

A timezone-naive datetime does not identify a universal instant by itself. Use timezone-aware values for events and preserve business date separately where needed. Clock changes can create repeated or nonexistent local times. Sorting strings with different offsets is not a generally safe way to order instants.

Guided workplace application

In a fictional collection file, the rule requires finite, nonnegative amounts with two decimal places. Construct Decimal from original text without passing through float. Check is_finite before comparing bounds: NaN can be representable without being a valid amount. If the contract permits rounding, specify mode and scale; quantize with ROUND_HALF_UP is not the same rule as HALF_EVEN. If the contract rejects extra places, do not silently round them. For timestamps retain offset or zone and information disambiguating a repeated hour. fold distinguishes known occurrences but does not discover which happened. Converting an instant to UTC differs from appending a UTC label to a local time without context.

Finiteness before amount rules

Constructing Decimal from text does not guarantee a finite amount. NaN and Infinity are representable and need rejection when the domain forbids them. Use is_finite before scale, sign, and range rules; also decide rounding policy. A JSON token and a string containing NaN follow different parsing paths. Do not assume decoder configuration replaces field validation.

Duration and instant use different references

Use time.monotonic differences for local budgets that must not move backward when wall time is adjusted. Do not store its absolute value as a UTC timestamp. To relate instants across systems, retain offset-aware dates and convert correctly. On the same day, 09:00 at offset +01:00 corresponds to 08:00 UTC, one hour before 09:00 UTC. astimezone converts representation while preserving the instant; merely replacing tzinfo may change interpretation of the value.

from decimal import Decimal
from datetime import datetime, timezone
amount = Decimal("0.1") + Decimal("0.2")
observed_at = datetime.now(timezone.utc)
IN PRACTICE

Decimal("0.1") + Decimal("0.2") represents exactly 0.3. An event can be stored in UTC and displayed in Europe/Lisbon.

Common pitfalls

Building Decimal from float; accepting NaN; choosing fold without evidence.

Related topics: Observable and repeatable automation · Data, types, and decisions

Take this idea with you

Numeric precision, data validity, and time interpretation are explicit decisions.

Create account

Reference: Python 3.14: Decimal arithmetic · Python 3.14; DR Python 2026.3