-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportmagic.py
38 lines (33 loc) · 1.15 KB
/
importmagic.py
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
import sys
import inspect
import pprint
_classes = {}
_functions = {}
_objects = {}
if __name__ != "__main__" and __name__ not in globals():
module = sys.modules["__main__"]
if hasattr(module, "__file__"):
items = [line for line in inspect.getsource(module).split("\n") if __name__ in line][-1].split("from "+__name__+" import")
print(items)
module = __import__(module.__name__, globals(), None, [], 0)
members = [item for item in inspect.getsource(module)]
pprint.pprint(members)
for name, obj in members:
if inspect.isbuiltin(obj):
globals()[name] = obj
elif inspect.ismodule(obj):
globals()[name] = obj
elif inspect.isfunction(obj):
globals()[name] = obj
_functions[name] = obj
elif inspect.isclass(obj):
globals()[name] = obj
_classes[name] = obj
else:
globals()[name] = obj
_objects[name] = obj
else:
print("nope")
print("_classes", _classes)
print("_functions", _functions)
print("_objects", _objects)