- adds over.misc.update_dict

- fixes #4 over.misc.console is too hard to use
This commit is contained in:
Martinez 2017-02-16 11:14:16 +01:00
parent 8e0b27cb17
commit d0bac63fb3

View file

@ -54,22 +54,42 @@ def batch_gen(data, batch_size):
# --------------------------------------------------
def console(locals, globals=None, tab_completion=True):
def update_dict(A, B, overwrite=False):
"""
Shallow-copies items from B to A.
Iff `overwrite`, keys that already exist in A will be overwritten.
Otherwise, only keys that aren't in A will be set.
"""
for key, value in B.items():
if key not in A:
A[key] = value
# --------------------------------------------------
def console(override_environment=None, tab_completion=True):
"""
Opens up a Python console.
If no `override_environment` is passed, the environment of the calling stack frame (and all parent stack frames) will be used.
"""
import code
import copy
import inspect
import readline
import rlcompleter
environment = copy.copy(locals)
if override_environment:
environment = override_environment
else:
environment = {}
frame = inspect.currentframe()
if globals:
for key, value in globals.items():
if key not in environment:
environment[key] = value
while frame:
update_dict(environment, frame.f_locals)
frame = frame.f_back
if tab_completion:
readline.parse_and_bind("tab: complete")