1 import os, glob, inspect
2 from types import StringTypes
4 def find_local_modules(filepath):
6 for f in glob.glob(os.path.dirname(filepath)+"/*.py"):
7 name = os.path.basename(f)[:-3]
13 if not elt or isinstance(elt, list):
15 if isinstance(elt, StringTypes):
17 if isinstance(elt, (tuple, set, frozenset)):
21 # FROM: https://gist.github.com/techtonik/2151727
22 # Public Domain, i.e. feel free to copy/paste
23 # Considered a hack in Python 2
27 def caller_name(skip=2):
28 """Get a name of a caller in the format module.class.method
30 `skip` specifies how many levels of stack to skip while getting caller
31 name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.
33 An empty string is returned if skipped levels exceed stack height
35 stack = inspect.stack()
37 if len(stack) < start + 1:
39 parentframe = stack[start][0]
42 module = inspect.getmodule(parentframe)
43 # `modname` can be None when frame is executed directly in console
44 # TODO(techtonik): consider using __main__
46 name.append(module.__name__)
48 if 'self' in parentframe.f_locals:
49 # I don't know any way to detect call from the object method
50 # XXX: there seems to be no way to detect static method call - it will
51 # be just a function call
52 name.append(parentframe.f_locals['self'].__class__.__name__)
53 codename = parentframe.f_code.co_name
54 if codename != '<module>': # top level usually
55 name.append( codename ) # function or a method
59 def is_sublist(x, y, shortcut=None):
60 if not shortcut: shortcut = []
61 if x == []: return (True, shortcut)
62 if y == []: return (False, None)
64 return is_sublist(x[1:],y[1:], shortcut)
66 return is_sublist(x, y[1:], shortcut + [y[0]])