I want to write a simple tutorial in my Python module foo
.
If the module is imported as foo
I would like the tutorial to call it foo
:
>>> import foo
>>> foo.tutorial()
Please run foo.baz().
However, when the module is imported as bar
- I want the tutorial to call it bar
:
>>> import foo as bar
>>> bar.tutorial()
Please run bar.baz().
I know that neither __name__
nor __package__
variables would change their value on import ... as ...
.
To be different from Is it possible to detect way of import of a module in python? - it will be enough for me to know the first alias the module is imported with.
It's not pretty, foolproof, or production worthy, but:
import inspect
import sys
thismod = sys.modules[__name__]
def imported_as():
imported_frame = inspect.currentframe().f_back.f_back
for k, v in imported_frame.f_locals.items():
if v is thismod:
return k
for k, v in imported_frame.f_globals.items():
if v is thismod:
return k
def tutorial():
print('please call {}.baz()'.format(imported_as()))
A tiny explanation:
Why you should really never do this:
from
imports will break this inspect.currentframe
is super magical and should generally be avoided in production code Here is a similar solution to Anthony's. I believe it better handles multiple imports. from
imports are still an issue.
import inspect
import re
import readline
def tutorial():
frame = inspect.currentframe().f_back
if "__file__" in frame.f_locals:
f = inspect.getframeinfo(frame)
m = re.search(r"(([^\.]*).*)\.tutorial", f.code_context[0].strip())
if m:
parent,path = m.group(2),m.group(1)
if inspect.ismodule(frame.f_locals[parent]):
print "Please run {}.baz()".format(path)
return
print "Verify import and run foo.baz()"
else:
loc = frame.f_locals
glo = frame.f_globals
local_foo = [(k, v.__name__)for k,v in loc.items() if inspect.ismodule(v) and v.__name__=="foo"]
global_foo = [(k, v.__name__)for k,v in glo.items() if inspect.ismodule(v) and v.__name__=="foo"]
if local_foo and set(local_foo)==set(global_foo):
print "Please run {}.baz()".format(global_foo[-1][0])
elif local_foo and global_foo:
print "Please run {}.baz() in local scope or {}.baz()".format(local_foo[-1][0], global_foo[-1][0])
elif local_foo:
print "Please run {}.baz() in local scope".format(local_foo[-1][0])
elif global_foo:
print "Please run {}.baz()".format(global_foo[-1][0])
else:
n = readline.get_current_history_length()
h = [str(readline.get_history_item(i)) for i in range(n)] + [readline.get_line_buffer()]
h.reverse()
for i in h:
matches = re.findall(r"([A-Za-z0-9_-]*(\.[A-Za-z0-9_-]*)*)\.tutorial", i)
for match in matches:
if _baz_check(glo, match[0]):
print "Please run {}.baz()".format(match[0])
return
print "Verify import and run foo.baz()"
def _baz_check(d, path):
path = path.split(".")
if path[0] not in d:
return False
cur = d[path[0]]
for i in range(1,len(path)):
if not hasattr(cur, path[i]):
return False
cur = getattr(cur, path[i])
return hasattr(cur, "__name__") and cur.__name__ == "foo" and hasattr(cur, "baz")
def baz():
print "Baz"
import foo
foo.tutorial()
import foo as bar
bar.tutorial()
def f():
import foo as local_foo
local_foo.tutorial()
f()
from foo import tutorial
tutorial()
import mod
mod.foo.tutorial()
from mod import foo as dum
dum.tutorial()
Please run foo.baz()
Please run bar.baz()
Please run local_foo.baz()
Verify import and run foo.baz()
Please run mod.foo.baz()
Please run dum.baz()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.