-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilPythonCode.py
More file actions
118 lines (89 loc) · 2.9 KB
/
utilPythonCode.py
File metadata and controls
118 lines (89 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
A simple python interaction console.
Use interact() to open the console.
"""
from code import InteractiveConsole as _console
from sys import version_info as _version, modules as _modules
from importlib import reload as _reload
class _namedFunction:
def __init__(self, func=lambda: None, label=''):
self.__func = func
self.__label = label
self.docs = 'No help entry found for this command.'
def __repr__(self):
return '{}\n'.format(self.__label)
def __call__(self, *args, **kwargs):
return self.__func(*args, **kwargs)
def setHelpDoc(self, theDoc):
self.docs = theDoc
return self
## Command clear ##
_clear_doc = \
"""
clear() clears the screen (with 100 empty lines).
""".strip()
clear = _namedFunction(lambda: print('\n'*100), 'Type clear() to clean the screen.').setHelpDoc(_clear_doc)
## Command reload ##
_reload_doc = \
"""
Reload a module.
Parameters:
theMod - the module (str / module).
Optional Parameters:
fromList - the list to import (from <theMod> import <fromList>).
toNamespace - when <fromList> is specified, save the import names into <toNamespace> (usually globals() / locals()).
Examples:
reload(math)
reload(math, fromList=['sin'], toNamespace=globals())
""".strip()
def _reloadModule(theMod, fromList=[], toNamespace=globals()):
if type(theMod) == str:
theMod = _modules[theMod]
_reload(theMod)
if type(toNamespace).__name__ == 'module':
toNamespace = vars(toNamespace)
fromNamespace = vars(theMod)
for name in fromList:
toNamespace[name] = fromNamespace[name]
return theMod
reload = _namedFunction(_reloadModule, 'Type reload(...) to reload a module, help_extra(reload) for help.').setHelpDoc(_reload_doc)
## Command help ##
_help_msg = \
"""
Type help_extra(<obj>) for help about command <obj>.
List of extra commands:
help_extra - show help about console add-on commands.
clear - clean the screen.
reload - reload a module.
""".strip()
def _loadHelp(obj=None):
if obj == None:
print(_help_msg +'\n')
else:
if not isinstance(obj, _namedFunction):
print('"{}" is not an add-on command.\n'.format(obj))
else:
print(obj.docs +'\n')
help_extra = _namedFunction(_loadHelp, _help_msg).setHelpDoc(_help_msg)
__all__ = ['clear', 'reload', 'help_extra']
def interact(startMsg=None, extraVars={}):
"""
Open a Python interactive console.
Parameters:
startMsg - the startup message.
extraVars - load the <extraVars> into console variables.
"""
if startMsg == None:
cprt = 'Type "help", "help_extra", "copyright", "credits" or "license" for more information.'
msgs = [
'Python v{} Interactive Console'.format('.'.join(map(str, _version[0:3]))),
cprt,
]
startMsg = '\n'.join(msgs)
elif startMsg != '':
startMsg = '{}\n'.format(startMsg)
extraFeature = {name: globals()[name] for name in __all__}
console = _console({**extraFeature, **extraVars})
console.interact(banner=startMsg, exitmsg='')
if __name__ == '__main__':
interact()