Skip to content

Instantly share code, notes, and snippets.

@andreyvit
Created July 23, 2009 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreyvit/152407 to your computer and use it in GitHub Desktop.
Save andreyvit/152407 to your computer and use it in GitHub Desktop.
# This code is a part of CrashKit, a web-based crash collection and analysis software
# (http://crashkitapp.appspot.com). Please consider it to be in public domain.
def get_package_name(frame):
shortest_package = frame.f_globals.get('__name__')
if shortest_package is None: # afaik this only happens while importing a module
for folder in sys.path:
folder = os.path.abspath(folder).replace('\\', '/')
if not folder.endswith('/'):
folder += '/'
if filename.startswith(folder):
package = filename[len(folder):]
if package.endswith('.py'): package = package[:-3]
if package.endswith('.pyc'): package = package[:-4]
package = package.replace('/', '.')
if shortest_package is None or len(package) < len(shortest_package):
shortest_package = package
return shortest_package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment