Skip to content

Commit

Permalink
refactor(utils): print_ww signature & docs
Browse files Browse the repository at this point in the history
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
athewsey committed Jul 26, 2023
1 parent eae5d55 commit 736d2a9
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions utils/__init__.py
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)))




0 comments on commit 736d2a9

Please sign in to comment.