This repository has been archived by the owner on Jun 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve yatsm pixel embedded terminal
- Loading branch information
Showing
2 changed files
with
95 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" Functions for interpreter access in YATSM CLI | ||
Function ``open_interpreter`` can be called to open up a Python or IPython | ||
interpreter with access to a variety of objects in local memory. | ||
TODO: | ||
* Add more useful functions for exploring data | ||
""" | ||
import code | ||
import sys | ||
import textwrap | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from yatsm import __version__ | ||
|
||
_funcs = locals() | ||
|
||
|
||
def open_interpreter(model, message=None, funcs=None): | ||
""" Opens an (I)Python interpreter | ||
Args: | ||
model (YATSM model): Pass YATSM model to work with | ||
message (str, optional): Additional message to pass to user in banner | ||
funcs (dict of callable, optional): Functions available in (I)Python | ||
session | ||
""" | ||
local = dict(_funcs, model=model, np=np, plt=plt) | ||
if funcs: | ||
local.update(funcs) | ||
|
||
banner = """\ | ||
YATSM {yver} Interactive Interpreter (Python {pver}) | ||
Type "help(model)" for info on YATSM model methods. | ||
NumPy and matplotlib.pyplot are already imported as "np" and "plt". | ||
""".format( | ||
yver=__version__, | ||
pver='.'.join(map(str, sys.version_info[:3])), | ||
funcs='\n\t'.join([k for k in local]) | ||
) | ||
banner = textwrap.dedent(banner) | ||
if isinstance(message, str): | ||
banner += '\n' + message | ||
|
||
try: | ||
import IPython | ||
IPython.InteractiveShell.banner1 = banner | ||
IPython.start_ipython(argv=[], user_ns=local) | ||
except: | ||
code.interact(banner, local=local) |
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