Functional Weave
Code in Rust

finance.uk-bank-rate@1.0.0

impl/python.py

1,210 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from .dates_add_days import add_days  ← from dates.add-days ^1.0.0 · built alongside by fune
from .finance_uk_bank_rate_data import BANK_RATE, BANK_RATE_HISTORY  ← this capability’s own data, compiled from data/bank-rate.json into the same file by fune build
from .finance_uk_bank_rate_types import BankRate


def uk_bank_rate(on_date: str) -> BankRate:
    """Bank Rate in force on a date: the latest change on or before it."""
    add_days(on_date, 0)  # validates the date; ISO dates then compare as strings
    for row in BANK_RATE:
        if on_date >= row.valid_from and (row.valid_to is None or on_date <= row.valid_to):
            return BankRate(basis_points=row.basis_points, valid_from=row.valid_from, valid_to=row.valid_to)
    # A history=current build keeps only the rate in force; answering an older
    # date with it would be exactly the wrong answer this data exists to prevent.
    if BANK_RATE_HISTORY != "full" and len(BANK_RATE) > 0:
        earliest = min(r.valid_from for r in BANK_RATE)
        if on_date < earliest:
            raise ValueError(
                "no Bank Rate on %s: this build was installed with history=%s, so it only carries rates from %s. "
                "Reinstall with history=full for older dates." % (on_date, BANK_RATE_HISTORY, earliest)
            )
    raise ValueError("no Bank Rate on %s" % (on_date,))