From 4faa994d32223c8d560d9dad0ca90a3f7eb10d6a Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Wed, 28 Jul 2021 22:13:43 -0700 Subject: Money, Rate, CentCount and a bunch of bugfixes. --- string_utils.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'string_utils.py') diff --git a/string_utils.py b/string_utils.py index 911008d..6fc257d 100644 --- a/string_utils.py +++ b/string_utils.py @@ -1,13 +1,15 @@ #!/usr/bin/env python3 +import contextlib import datetime +import io from itertools import zip_longest import json import logging import random import re import string -from typing import Any, List, Optional +from typing import Any, Callable, List, Optional import unicodedata from uuid import uuid4 @@ -921,6 +923,22 @@ def sprintf(*args, **kwargs) -> str: return ret +class SprintfStdout(object): + def __init__(self) -> None: + self.destination = io.StringIO() + self.recorder = None + + def __enter__(self) -> Callable[[], str]: + self.recorder = contextlib.redirect_stdout(self.destination) + self.recorder.__enter__() + return lambda: self.destination.getvalue() + + def __exit__(self, *args) -> None: + self.recorder.__exit__(*args) + self.destination.seek(0) + return None # don't suppress exceptions + + def is_are(n: int) -> str: if n == 1: return "is" -- cgit v1.3