Skip to content

Commit 65b979d

Browse files
committed
Added contextmanager for printing variable assignments in terminal, copied from chempy
1 parent cdf5a93 commit 65b979d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

domdf_python_tools/terminal.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,42 @@
3434
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
3535
# MA 02110-1301, USA.
3636
#
37+
# Based on ChemPy (https://github.com/bjodah/chempy)
38+
# | Copyright (c) 2015-2018, Björn Dahlgren
39+
# | All rights reserved.
40+
# |
41+
# | Redistribution and use in source and binary forms, with or without modification,
42+
# | are permitted provided that the following conditions are met:
43+
# |
44+
# | Redistributions of source code must retain the above copyright notice, this
45+
# | list of conditions and the following disclaimer.
46+
# |
47+
# | Redistributions in binary form must reproduce the above copyright notice, this
48+
# | list of conditions and the following disclaimer in the documentation and/or
49+
# | other materials provided with the distribution.
50+
# |
51+
# | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
52+
# | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53+
# | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54+
# | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
55+
# | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56+
# | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57+
# | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
58+
# | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59+
# | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60+
# | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61+
#
3762

3863
# stdlib
64+
import inspect
3965
import os
4066
import platform
67+
import pprint
4168
import shlex
4269
import struct
4370
import subprocess
4471
import sys
72+
import textwrap
4573

4674

4775
def clear():
@@ -188,6 +216,25 @@ def ioctl_GWINSZ(fd):
188216
return int(cr[1]), int(cr[0])
189217

190218

219+
class Echo:
220+
"""
221+
Context manager for echoing variable assignments (in CPython)
222+
"""
223+
224+
def __init__(self, msg, indent=' '):
225+
self.msg = msg
226+
self.indent = indent
227+
self.parent_frame = inspect.currentframe().f_back
228+
229+
def __enter__(self):
230+
print(self.msg)
231+
self.locals_on_entry = self.parent_frame.f_locals.copy()
232+
233+
def __exit__(self, exc_t, exc_v, tb):
234+
new_locals = {k: v for k, v in self.parent_frame.f_locals.items() if k not in self.locals_on_entry}
235+
print(textwrap.indent(pprint.pformat(new_locals), self.indent))
236+
237+
191238
if __name__ == "__main__":
192239
size_x, size_y = get_terminal_size()
193240
print('width =', size_x, 'height =', size_y)

0 commit comments

Comments
 (0)