|
34 | 34 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
35 | 35 | # MA 02110-1301, USA.
|
36 | 36 | #
|
| 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 | +# |
37 | 62 |
|
38 | 63 | # stdlib
|
| 64 | +import inspect |
39 | 65 | import os
|
40 | 66 | import platform
|
| 67 | +import pprint |
41 | 68 | import shlex
|
42 | 69 | import struct
|
43 | 70 | import subprocess
|
44 | 71 | import sys
|
| 72 | +import textwrap |
45 | 73 |
|
46 | 74 |
|
47 | 75 | def clear():
|
@@ -188,6 +216,25 @@ def ioctl_GWINSZ(fd):
|
188 | 216 | return int(cr[1]), int(cr[0])
|
189 | 217 |
|
190 | 218 |
|
| 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 | + |
191 | 238 | if __name__ == "__main__":
|
192 | 239 | size_x, size_y = get_terminal_size()
|
193 | 240 | print('width =', size_x, 'height =', size_y)
|
0 commit comments