Created
July 23, 2009 00:12
-
-
Save andreyvit/152407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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