我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用inspect.getmodulename()。
def from_command_line(cls, *args, **keys): params = list() for name, param in cls.params(): if name not in keys: params.append((name, param)) bot_name = inspect.getmodulename(inspect.stack()[1][1]) if "ABUSEHELPER_CONF_FROM_STDIN" in os.environ: defaults = dict(pickle.load(sys.stdin)) defaults.setdefault("bot_name", bot_name) added = cls._from_dict(params, **defaults) else: added = cls._from_sys_argv(params, bot_name=bot_name) added.update(keys) return cls(*args, **added)
def modulesInPackage(self, packageName, package): docless = [] directory = path.dirname(package.__file__) for modfile in glob.glob(path.join(directory, '*.py')): moduleName = inspect.getmodulename(modfile) if moduleName == '__init__': # These are tested by test_packages. continue elif moduleName in ('spelunk_gnome','gtkmanhole'): # argh special case pygtk evil argh. How does epydoc deal # with this? continue try: module = reflect.namedModule('.'.join([packageName, moduleName])) except Exception, e: # print moduleName, "misbehaved:", e pass else: if not inspect.getdoc(module): docless.append(modfile) return docless
def get_modules(dir): modules = {} # Get all files in the given directory try: files = os.listdir(dir) except: # I guess not then return modules # WHich is empty for file in files: fname = os.path.join(dir, file) if os.path.isfile(fname): module = inspect.getmodulename(fname) if module: modules[module] = fname elif os.path.isdir(fname): dir_modules = __try_import_dir(fname) # Extract modules from dir for m, f in dir_modules.items(): modules[m] = f return modules
def __init__(self, context=None, **kwargs): """ When creating the placeholder, a process-specific context should be given. Parameters ---------- context : str Context is used to indicate the process that generated the processing statistics. Returns ------- Metrics Instance where processing statistics can be added or modified. """ # If context is not given, determine it from calling function. if context is None: previous_frame = inspect.getouterframes(inspect.currentframe())[1] module = inspect.getmodulename(previous_frame[1]) context = module + '.' + previous_frame[3] self.context = context for arg, value in kwargs.items(): setattr(self, arg, value)
def __get__(self, instance, owner): """This method is called from class. Args: owner(object): class instance Returns: logging.LoggerAdapter: logger adaptor Notes: In case using logger for module level use get() method. __get__() won't be called from module level. """ if self.for_exception: caller_frame = inspect.stack()[2] module_name = inspect.getmodulename(caller_frame[1]) func_name = caller_frame[3] try: class_name = caller_frame[0].f_locals["self"].__class__.__name__ except KeyError: class_name = "" _logger_adaptor = self._get_logger(module_name, class_name, func_name) else: _logger_adaptor = self._get_logger(owner.__module__, owner.__name__) return _logger_adaptor
def __get__(self, instance, owner): """This method is called from class. Args: owner (owner): class instance. Returns: logging.LoggerAdapter: logger adaptor. Note: In case using logger for module level use get() method. __get__() won't be called from module level. """ if self.for_exception: caller_frame = inspect.stack()[2] module_name = inspect.getmodulename(caller_frame[1]) func_name = caller_frame[3] try: class_name = caller_frame[0].f_locals["self"].__class__.__name__ except KeyError: class_name = "" _logger_adaptor = self._get_logger(module_name, class_name, func_name) else: _logger_adaptor = self._get_logger(owner.__module__, owner.__name__) return _logger_adaptor
def __get__(self, instance, owner): """This method is called from class. Note: In case using logger for module level use get() method. __get__() won't be called from module level. """ if self.for_exception: caller_frame = inspect.stack()[2] module_name = inspect.getmodulename(caller_frame[1]) func_name = caller_frame[3] try: class_name = caller_frame[0].f_locals["self"].__class__.__name__ except KeyError: class_name = "" _logger_adaptor = self._get_logger(module_name, class_name, func_name) else: _logger_adaptor = self._get_logger(owner.__module__, owner.__name__) return _logger_adaptor
def omap(fun, arglist): print banner("Starting omap") N_tasks = len(arglist) jobname = str(npr.RandomState().randint(10**12)) working_dir = path.join(root_working_dir, jobdir(jobname)) module_path = path.join(os.getcwd(), inspect.getsourcefile(fun)) module_name = inspect.getmodulename(module_path) run_signal_path = path.join('..', run_signal(jobname)) fun_name = fun.__name__ slurm_str = slurm_template.format(jobname=jobname, N_tasks=N_tasks, other_options=slurm_options, module_name=module_name, fun_name=fun_name) with temp_dir(working_dir): shutil.copy(module_path, ".") with open(arg_fname, 'w') as f: pickle.dump(arglist, f) with open(slurm_fname, 'w') as f: f.write(slurm_str) with open(run_signal_path, 'w'): pass print "Submitting {0} tasks (output in {1})".format(N_tasks, working_dir) while path.exists(run_signal_path): time.sleep(1) print "Tasks submitted" return collect_results(jobname)
def __str__(self): stack_iter = iter(self._stack) for stack in stack_iter: # Find the caller of AssertThat(...). if stack[3] == 'AssertThat': caller = next(stack_iter) return ('{0}({1}) created in module {2}, line {3}, in {4}:\n' ' {5}' .format(self.__class__.__name__, self._GetSubject(), inspect.getmodulename(caller[1]), # Module name. caller[2], # Line number. caller[3], # Function name. caller[4][0].strip())) # Code snippet. # The subject was not created by AssertThat(). return '{0}({1})'.format(self.__class__.__name__, self._GetSubject())
def get_parser(description=None): """Gets an ArgumentParser, defaulting to the caller's __doc__ as the description.""" if description is None: caller_frame = inspect.stack()[1] caller_path = caller_frame[1] caller_module_name = inspect.getmodulename(caller_path) try: caller_module = importlib.import_module(caller_module_name) doc = caller_module.__doc__ except ImportError: logging.error( 'Could not auto-detect __doc__ for parser description for module %r, ' 'derived from caller path %r.', caller_module_name, caller_path) doc = None else: doc = description return argparse.ArgumentParser( description=doc, formatter_class=argparse.RawDescriptionHelpFormatter)
def iter_modules(self, prefix=''): if self.path is None or not os.path.isdir(self.path): return yielded = {} import inspect filenames = os.listdir(self.path) filenames.sort() # handle packages before same-named modules for fn in filenames: modname = inspect.getmodulename(fn) if modname=='__init__' or modname in yielded: continue path = os.path.join(self.path, fn) ispkg = False if not modname and os.path.isdir(path) and '.' not in fn: modname = fn for fn in os.listdir(path): subname = inspect.getmodulename(fn) if subname=='__init__': ispkg = True break else: continue # not a package if modname and '.' not in modname: yielded[modname] = 1 yield prefix + modname, ispkg
def iter_zipimport_modules(importer, prefix=''): dirlist = zipimport._zip_directory_cache[importer.archive].keys() dirlist.sort() _prefix = importer.prefix plen = len(_prefix) yielded = {} import inspect for fn in dirlist: if not fn.startswith(_prefix): continue fn = fn[plen:].split(os.sep) if len(fn)==2 and fn[1].startswith('__init__.py'): if fn[0] not in yielded: yielded[fn[0]] = 1 yield fn[0], True if len(fn)!=1: continue modname = inspect.getmodulename(fn[0]) if modname=='__init__': continue if modname and '.' not in modname and modname not in yielded: yielded[modname] = 1 yield prefix + modname, False
def __init__(self, name=None, router=None, load_env=True, log_config=LOGGING): if log_config: logging.config.dictConfig(log_config) # Only set up a default log handler if the # end-user application didn't set anything up. if not (logging.root.handlers and log.level == logging.NOTSET and log_config): formatter = logging.Formatter( "%(asctime)s: %(levelname)s: %(message)s") handler = logging.StreamHandler() handler.setFormatter(formatter) log.addHandler(handler) log.setLevel(logging.INFO) # Get name from previous stack frame if name is None: frame_records = stack()[1] name = getmodulename(frame_records[1]) self.name = name self.config = Config(load_env=load_env) self.router = router or PathRouter() self.debug = None self.static_handler = None
def param_defaults(cls, **defaults): result = dict() for name, param in cls.params(): if param.has_default(): result[name] = param.default elif name == "bot_name": modulename = inspect.getmodulename(inspect.getfile(cls)) result["bot_name"] = modulename result.update(defaults) return result
def iter_modules(self, prefix=''): if self.path is None or not os.path.isdir(self.path): return yielded = {} import inspect try: filenames = os.listdir(self.path) except OSError: # ignore unreadable directories like import does filenames = [] filenames.sort() # handle packages before same-named modules for fn in filenames: modname = inspect.getmodulename(fn) if modname=='__init__' or modname in yielded: continue path = os.path.join(self.path, fn) ispkg = False if not modname and os.path.isdir(path) and '.' not in fn: modname = fn try: dircontents = os.listdir(path) except OSError: # ignore unreadable directories like import does dircontents = [] for fn in dircontents: subname = inspect.getmodulename(fn) if subname=='__init__': ispkg = True break else: continue # not a package if modname and '.' not in modname: yielded[modname] = 1 yield prefix + modname, ispkg
def disable_for_loaddata(signal_handler): """The signals to update denormalized data should not be called during loaddata management command (can raise an IntegrityError)""" def wrapper(*args, **kwargs): for fr in inspect.stack(): if inspect.getmodulename(fr[1]) == 'loaddata': return signal_handler(*args, **kwargs) return wrapper # end
def __init__(self, name=None, router=None, error_handler=None): if name is None: frame_records = stack()[1] name = getmodulename(frame_records[1]) self.name = name self.router = router or Router() # ?? self.error_handler = error_handler or Handler(self) # ???? self.config = Config() # ????? self.request_middleware = deque() # ????? self.response_middleware = deque() # ????? self.blueprints = {} # ?? self._blueprint_order = [] self.loop = None self.debug = None # Register alternative method names self.go_fast = self.run # -------------------------------------------------------------------- # # Registration # -------------------------------------------------------------------- # # # ???????: # - ????. ?????, ???? # - ??????, ??: sanic.router.Router() ?????? # # Decorator
def download(self, dload_url, torrent_name, load): """ Torrent download method. Used to download .torrent file. Torrent is downloaded in ~/Downloads/torrench/ """ try: mod = inspect.getmodulename(inspect.stack()[1][1]) modules_to_exclude = ['linuxtracker', 'distrowatch'] self.logger.debug("Download begins...") home = os.path.expanduser(os.path.join('~', 'Downloads')) downloads_dir = os.path.join(home, 'torrench') self.logger.debug("Default download directory: %s", (downloads_dir)) if mod not in modules_to_exclude: downloads_dir = os.path.join(downloads_dir, mod) if not os.path.exists(downloads_dir): self.logger.debug("download directory does not exist.") os.makedirs(downloads_dir) self.logger.debug("created directory: %s", (downloads_dir)) torrent_file = os.path.join(downloads_dir, torrent_name) with open(torrent_file, "wb") as file: print("Downloading torrent...") response = requests.get(dload_url) file.write(response.content) self.logger.debug("Download complete!") print("Download complete!") print("\nSaved in %s\n" % (downloads_dir)) self.logger.debug("Saved in %s", (downloads_dir)) # Load torrent to client if load == 1: self.load_torrent(torrent_file) except KeyboardInterrupt as e: self.logger.exception(e) print("\nAborted!\n")
def iter_zipimport_modules(importer, prefix=''): dirlist = sorted(zipimport._zip_directory_cache[importer.archive]) _prefix = importer.prefix plen = len(_prefix) yielded = {} import inspect for fn in dirlist: if not fn.startswith(_prefix): continue fn = fn[plen:].split(os.sep) if len(fn)==2 and fn[1].startswith('__init__.py'): if fn[0] not in yielded: yielded[fn[0]] = 1 yield fn[0], True if len(fn)!=1: continue modname = inspect.getmodulename(fn[0]) if modname=='__init__': continue if modname and '.' not in modname and modname not in yielded: yielded[modname] = 1 yield prefix + modname, False
def __init__(self, name=None, router=None, error_handler=None, load_env=True, request_class=None, strict_slashes=False, log_config=None, configure_logging=True): # Get name from previous stack frame if name is None: frame_records = stack()[1] name = getmodulename(frame_records[1]) # logging if configure_logging: logging.config.dictConfig(log_config or LOGGING_CONFIG_DEFAULTS) self.name = name self.router = router or Router() self.request_class = request_class self.error_handler = error_handler or ErrorHandler() self.config = Config(load_env=load_env) self.request_middleware = deque() self.response_middleware = deque() self.blueprints = {} self._blueprint_order = [] self.configure_logging = configure_logging self.debug = None self.sock = None self.strict_slashes = strict_slashes self.listeners = defaultdict(list) self.is_running = False self.is_request_stream = False self.websocket_enabled = False self.websocket_tasks = set() # Register alternative method names self.go_fast = self.run
def current_method(): mod = inspect.getmodulename(inspect.stack()[1][1]) func = inspect.stack()[1][3] return mod, func
def _iter_file_finder_modules(importer, prefix=''): if importer.path is None or not os.path.isdir(importer.path): return yielded = {} import inspect try: filenames = os.listdir(importer.path) except OSError: # ignore unreadable directories like import does filenames = [] filenames.sort() # handle packages before same-named modules for fn in filenames: modname = inspect.getmodulename(fn) if modname=='__init__' or modname in yielded: continue path = os.path.join(importer.path, fn) ispkg = False if not modname and os.path.isdir(path) and '.' not in fn: modname = fn try: dircontents = os.listdir(path) except OSError: # ignore unreadable directories like import does dircontents = [] for fn in dircontents: subname = inspect.getmodulename(fn) if subname=='__init__': ispkg = True break else: continue # not a package if modname and '.' not in modname: yielded[modname] = 1 yield prefix + modname, ispkg