Skip to content

lpgcast.fidelity

Is a coerced value what its quote states?

Checked for numbers written as digits and for dates; every other value passes. A quote with no digit (three pumps, yesterday) is not checked, since a spelled-out or relative value cannot be read back without inference.

Numbers: the quote's digit runs are read with every plausible separator reading (1,250 and 1.250 as grouping, 3,75 as a decimal comma), a trailing k/M/bn or its word as a multiplier and % as a hundredth; the value matches when its magnitude equals one reading. Units are not converted: 3.75 kW does not support 3750.

Dates: each part the value gives must be in the quote — the day as a number, the month as a number or an English month name, the year as four or two digits. A quote with no four-digit number leaves the year to context. A month written in another language is not checked.

Functions:

value_mismatch(value, field, quote)

Why value, coerced for field, is not what quote states; None when it is, or when it cannot be checked.

Source code in lpgcast/fidelity.py
def value_mismatch(value: Any, field: Field, quote: str) -> str | None:
    """Why *value*, coerced for *field*, is not what *quote* states; None when
    it is, or when it cannot be checked."""
    if field.type is None or not re.search(r"\d", quote):
        return None
    ftype = FieldType(field.type)
    if ftype == FieldType.LIST:
        ftype = FieldType(field.item_type or FieldType.STRING)
        items = value if isinstance(value, list) else [value]
    else:
        items = [value]
    if ftype in _NUMERIC:
        bad = [item for item in items if _number_mismatch(item, quote)]
        if bad:
            return (
                f"{field.name}: {bad[0]!r} is not a number the quote {quote!r} states."
            )
    elif ftype == FieldType.DATETIME:
        bad = [item for item in items if _date_mismatch(item, quote)]
        if bad:
            return f"{field.name}: {bad[0]!r} has a date part the quote {quote!r} does not state."
    return None