def __ImportModule( module_name, asName=None ):
"""
Import a python module as needed into the global namespace. This can be accomplished
programatically and inside a method or class.
@param module_name : name of the module to load x.y.z
@return module : python module object that was loaded
"""
module = __import__(module_name)
for layer in module_name.split('.')[1:]:
module = getattr(module,layer)
if asName:
for x in range( len(inspect.stack()) ):
inspect.currentframe(x).f_globals[asName]=module
return module
python类currentframe()的实例源码
def mycallersname():
"""Returns the name of the caller of the caller of this function
(hence the name of the caller of the function in which
"mycallersname()" textually appears). Returns None if this cannot
be determined."""
# http://docs.python.org/library/inspect.html#the-interpreter-stack
import inspect
frame = inspect.currentframe()
if not frame:
return None
frame_,filename_,lineno_,funname,linelist_,listi_ = (
inspect.getouterframes(frame)[2])
return funname
def call_contextual_template(template):
# this is the magic line
frame = inspect.currentframe().f_back
# again, we don't care about efficiency, it's not the point here
d = dict(frame.f_globals)
d.update(frame.f_locals)
return call_template(template,d)
def ipsh():
ipshell = InteractiveShellEmbed(config=cfg, banner1=banner_msg, exit_msg=exit_msg)
frame = inspect.currentframe().f_back
msg = 'Stopped at {0.f_code.co_filename} at line {0.f_lineno}'.format(frame)
# Go back one level!
# This is needed because the call to ipshell is inside the function ipsh()
ipshell(msg, stack_depth=2)
def current_line_number():
"""Returns the current line number in our program.
:return: current line number
:rtype: int
"""
import inspect
return inspect.currentframe().f_back.f_lineno
def run(self):
# Explicit request for old-style install? Just do it
if self.old_and_unmanageable or self.single_version_externally_managed:
return orig.install.run(self)
if not self._called_from_setup(inspect.currentframe()):
# Run in backward-compatibility mode to support bdist_* commands.
orig.install.run(self)
else:
self.do_egg_install()
def run(self):
# Explicit request for old-style install? Just do it
if self.old_and_unmanageable or self.single_version_externally_managed:
return orig.install.run(self)
if not self._called_from_setup(inspect.currentframe()):
# Run in backward-compatibility mode to support bdist_* commands.
orig.install.run(self)
else:
self.do_egg_install()
def run(self):
# Explicit request for old-style install? Just do it
if self.old_and_unmanageable or self.single_version_externally_managed:
return orig.install.run(self)
if not self._called_from_setup(inspect.currentframe()):
# Run in backward-compatibility mode to support bdist_* commands.
orig.install.run(self)
else:
self.do_egg_install()
def run(self):
# Explicit request for old-style install? Just do it
if self.old_and_unmanageable or self.single_version_externally_managed:
return orig.install.run(self)
if not self._called_from_setup(inspect.currentframe()):
# Run in backward-compatibility mode to support bdist_* commands.
orig.install.run(self)
else:
self.do_egg_install()
def f(format_string):
caller_frame = inspect.currentframe().f_back
caller_globals = caller_frame.f_globals
caller_locals = caller_frame.f_locals
lf = LiteralFormatter()
return lf.format(format_string, caller_globals, caller_locals)
def run(self):
# Explicit request for old-style install? Just do it
if self.old_and_unmanageable or self.single_version_externally_managed:
return orig.install.run(self)
if not self._called_from_setup(inspect.currentframe()):
# Run in backward-compatibility mode to support bdist_* commands.
orig.install.run(self)
else:
self.do_egg_install()
def mycallersname():
"""Returns the name of the caller of the caller of this function
(hence the name of the caller of the function in which
"mycallersname()" textually appears). Returns None if this cannot
be determined."""
# http://docs.python.org/library/inspect.html#the-interpreter-stack
import inspect
frame = inspect.currentframe()
if not frame:
return None
frame_,filename_,lineno_,funname,linelist_,listi_ = (
inspect.getouterframes(frame)[2])
return funname
def _prepare_debuglog_message(message, caller_level=2):
# Por si acaso se le mete una cadena de tipo str,
# este módulo es capaz de detectar eso y convertirla a UTF8
if type(message) == str:
message = unicode(message, "UTF-8")
# Hora
now = timezone.now()
# Contexto desde el que se ha llamado
curframe = inspect.currentframe()
# Objeto frame que llamó a dlprint
calframes = inspect.getouterframes(curframe, caller_level)
caller_frame = calframes[2][0]
caller_name = calframes[2][3]
# Ruta del archivo que llamó a dlprint
filename_path = caller_frame.f_code.co_filename
filename = filename_path.split("/")[-1]
# Obtención del mensaje
return u"DjangoVirtualPOS: {0} {1} \"{2}\" at {3}:{5} in {6} ({4}:{5})\n".format(
now.strftime("%Y-%m-%d %H:%M:%S %Z"), settings.DOMAIN, message,
filename, filename_path, caller_frame.f_lineno, caller_name
)
# Prints the debug message
def fully_aligned_distance(p1, p2):
"""compares each one to its' best mapping"""
word2word = align_yields(p1, p2)
nodes1 = set(node for node in p1.layer(layer1.LAYER_ID).all if is_comparable(node))
nodes2 = set(node for node in p2.layer(layer1.LAYER_ID).all if is_comparable(node))
first = align_nodes(nodes1, nodes2, word2word)
word2word = reverse_mapping(word2word)
second = align_nodes(nodes2, nodes1, word2word)
count1 = len(set((i, j) for (i, j) in first.items() if compare(i, j)))
count2 = len(set((i, j) for (i, j) in second.items() if compare(i, j)))
print(inspect.currentframe().f_code.co_name, " returns ", two_sided_f(count1, count2, len(nodes1), len(nodes2)))
return two_sided_f(count1, count2, len(nodes1), len(nodes2))
def token_distance(p1, p2, map_by=buttom_up_by_levels_align):
"""compares considering only the main relation of each node"""
count1 = token_matches(p1, p2, map_by)
count2 = token_matches(p2, p1, map_by)
nodes1 = set(node for node in p1.layer(layer1.LAYER_ID).all
if is_comparable(node) and label(node) in MAIN_RELATIONS)
nodes2 = set(node for node in p2.layer(layer1.LAYER_ID).all
if is_comparable(node) and label(node) in MAIN_RELATIONS)
print(inspect.currentframe().f_code.co_name)
print("counts", count1, count2)
print("lens", len(nodes1), len(nodes2))
print(two_sided_f(count1, count2, len(nodes1), len(nodes2)))
return two_sided_f(count1, count2, len(nodes1), len(nodes2))
def run(self):
# Explicit request for old-style install? Just do it
if self.old_and_unmanageable or self.single_version_externally_managed:
return orig.install.run(self)
if not self._called_from_setup(inspect.currentframe()):
# Run in backward-compatibility mode to support bdist_* commands.
orig.install.run(self)
else:
self.do_egg_install()
def get_oplog_cursor_since(self, caller, ts=None):
frame = getframeinfo(currentframe().f_back)
comment = "%s:%s;%s:%i" % (caller.__name__, frame.function, frame.filename, frame.lineno)
if not ts:
ts = self.get_oplog_tail_ts()
query = {'ts': {'$gte': ts}}
logging.debug("Querying oplog on %s with query: %s" % (self.uri, query))
# http://api.mongodb.com/python/current/examples/tailable.html
return self.get_oplog_rs().find(query, cursor_type=CursorType.TAILABLE_AWAIT, oplog_replay=True).comment(comment)
def __enter__(self):
parent = inspect.currentframe().f_back
try:
if parent.f_code.co_flags & inspect.CO_NEWLOCALS:
raise RuntimeError('timers only work when invoked at the module/script level')
self._with_start = parent.f_lasti
finally:
del parent
gc_enabled = gc.isenabled()
gc.disable()
self._gc_enabled = gc_enabled
self._start_time = self.time_function()
return self
def run(self):
# Explicit request for old-style install? Just do it
if self.old_and_unmanageable or self.single_version_externally_managed:
return orig.install.run(self)
if not self._called_from_setup(inspect.currentframe()):
# Run in backward-compatibility mode to support bdist_* commands.
orig.install.run(self)
else:
self.do_egg_install()
def run(self):
# Explicit request for old-style install? Just do it
if self.old_and_unmanageable or self.single_version_externally_managed:
return orig.install.run(self)
if not self._called_from_setup(inspect.currentframe()):
# Run in backward-compatibility mode to support bdist_* commands.
orig.install.run(self)
else:
self.do_egg_install()