+class PostvarsParser:
+ """Postvars wrapper for validating and retrieving posted form data."""
+
+ def __init__(self, postvars: dict[str, list[str]]) -> None:
+ self.postvars = postvars
+
+ def get_str(self, key: str) -> str:
+ """Retrieve string value of key from self.postvars."""
+ if key not in self.postvars:
+ raise BadFormatException(f'missing value for form field: {key}')
+ return self.postvars[key][0]
+
+ def get_float(self, key: str) -> float:
+ """Retrieve float value of key from self.postvars."""
+ val = self.get_str(key)
+ try:
+ return float(val)
+ except ValueError as e:
+ msg = f'cannot float form field value: {val}'
+ raise BadFormatException(msg) from e
+
+