forked from aws-samples/amazon-bedrock-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(utils): print_ww signature & docs
Make `width` an explicit kwarg of `print_ww` to make the method signature more informative and avoid unnecessary dict modification step. Add docstrings & comments & lint.
- Loading branch information
Showing
1 changed file
with
10 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
import textwrap | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: MIT-0 | ||
"""General helper utilities the workshop notebooks""" | ||
# Python Built-Ins: | ||
from io import StringIO | ||
import sys | ||
import textwrap | ||
|
||
|
||
def print_ww(*args, **kwargs): | ||
def print_ww(*args, width: int = 100, **kwargs): | ||
"""Like print(), but wraps output to `width` characters (default 100)""" | ||
buffer = StringIO() | ||
try: | ||
try: | ||
_stdout = sys.stdout | ||
sys.stdout = buffer | ||
width = 100 | ||
if 'width' in kwargs: | ||
width = kwargs['width'] | ||
del kwargs['width'] | ||
print(*args, **kwargs) | ||
output = buffer.getvalue() | ||
output = buffer.getvalue() | ||
finally: | ||
sys.stdout = _stdout | ||
for line in output.splitlines(): | ||
print("\n".join(textwrap.wrap(line, width=width))) | ||
|
||
|
||
|
||
|