summaryrefslogtreecommitdiff
path: root/string_utils.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-07-28 22:13:43 -0700
committerScott Gasch <[email protected]>2021-07-28 22:13:43 -0700
commit4faa994d32223c8d560d9dad0ca90a3f7eb10d6a (patch)
tree1200e5615d94247a120b98a4ddceb2cd6674e0af /string_utils.py
parentc79ecbf708a63a54a9c3e8d189b65d4794930082 (diff)
Money, Rate, CentCount and a bunch of bugfixes.
Diffstat (limited to 'string_utils.py')
-rw-r--r--string_utils.py20
1 files changed, 19 insertions, 1 deletions
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"