🐍 python Price tolerance

python

the living tribunal

Moderator
Staff member
moderator
Python:
import re

# Matches: "that will be 15.56$" (case-insensitive), captures the price
_PATTERN = re.compile(r'^that will be\s+(\d+(?:\.\d{1,2})?)\$\s*$', re.IGNORECASE)

def price_within_upward_tolerance(text: str, base: float = 15.56, upward_tol: float = 5.0) -> bool:
    """
    Returns True if `text` looks like 'that will be <amount>$' and amount ∈ [base, base + upward_tol].
    Defaults: base=15.56, upward tolerance = 5.00 (i.e., up to 20.56).
    """
    m = _PATTERN.match(text)
    if not m:
        return False
    amount = float(m.group(1))
    return base <= amount <= base + upward_tol
@fukurou
 
Top