我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用builtins.isinstance()。
def _get_callable_argspec_py2(func): argspec = inspect.getargspec(func) if argspec.keywords is not None: msg = "variable length keyword argument '{0}' found"\ .format(argspec.keywords) raise FyppFatalError(msg) vararg = argspec.varargs args = argspec.args tuplearg = False for elem in args: tuplearg = tuplearg or isinstance(elem, list) if tuplearg: msg = 'tuple argument(s) found' raise FyppFatalError(msg) defaults = {} if argspec.defaults is not None: for ind, default in enumerate(argspec.defaults): iarg = len(args) - len(argspec.defaults) + ind defaults[args[iarg]] = default return args, defaults, vararg
def _formatted_exception(exc): error_header_formstr = '{file}:{line}: ' error_body_formstr = 'error: {errormsg} [{errorclass}]' if not isinstance(exc, FyppError): return error_body_formstr.format( errormsg=str(exc), errorclass=exc.__class__.__name__) out = [] if exc.fname is not None: if exc.span[1] > exc.span[0] + 1: line = '{0}-{1}'.format(exc.span[0] + 1, exc.span[1]) else: line = '{0}'.format(exc.span[0] + 1) out.append(error_header_formstr.format(file=exc.fname, line=line)) out.append(error_body_formstr.format(errormsg=exc.msg, errorclass=exc.__class__.__name__)) if exc.cause is not None: out.append('\n' + _formatted_exception(exc.cause)) out.append('\n') return ''.join(out)
def pow(x, y, z=_SENTINEL): """ pow(x, y[, z]) -> number With two arguments, equivalent to x**y. With three arguments, equivalent to (x**y) % z, but may be more efficient (e.g. for ints). """ # Handle newints if isinstance(x, newint): x = long(x) if isinstance(y, newint): y = long(y) if isinstance(z, newint): z = long(z) try: if z == _SENTINEL: return _builtin_pow(x, y) else: return _builtin_pow(x, y, z) except ValueError: if z == _SENTINEL: return _builtin_pow(x+0j, y) else: return _builtin_pow(x+0j, y, z) # ``future`` doesn't support Py3.0/3.1. If we ever did, we'd add this: # callable = __builtin__.callable
def get_logo_button(self, index): """ Return the button of the station specified by its index :param index: button index in the playlist :return: current station button """ try: self.button = self.buttons[str(index)] except: pass if not self.is_button_defined(): return b = self.factory.create_station_button(self.button.state, self.bounding_box, self.switch_mode) b.components[1].content = self.button.state.icon_base img = b.components[1].content if isinstance(img, tuple): img = img[1] bb = self.bounding_box logo_height = int((200 * bb.h)/228) img = self.util.scale_image(img, (logo_height, logo_height)) b.components[1].content = img b.components[1].content_x = bb.x + bb.w/2 - img.get_size()[0]/2 b.components[1].content_y = bb.y + bb.h/2 - img.get_size()[1]/2 return b
def parsefile(self, fobj): '''Parses file or a file like object. Args: fobj (str or file): Name of a file or a file like object. ''' if isinstance(fobj, str): if fobj == STDIN: self._includefile(None, sys.stdin, STDIN, os.getcwd()) else: inpfp = _open_input_file(fobj) self._includefile(None, inpfp, fobj, os.path.dirname(fobj)) inpfp.close() else: self._includefile(None, fobj, FILEOBJ, os.getcwd())
def _func_import(self, name, *_, **__): module = self._scope.get(name, None) if module is not None and isinstance(module, types.ModuleType): return module else: msg = "Import of module '{0}' via '__import__' not allowed"\ .format(name) raise ImportError(msg)
def loadData(self, data): if isinstance(data, dict): # If 'data' is preferences on users for training self.prefs = data elif isinstance(data, str): # If 'data' is a file path of training data self.prefs = tool.loadData(data) self.itemList = {} for user in self.prefs: for item in self.prefs[user]: self.itemList[item] = None
def loadData(self, data): if isinstance(data, dict): # If 'data' is preferences on users for training self.prefsOnUser = data self.prefs = tool.transposePrefs(self.prefsOnUser) elif isinstance(data, str): # If 'data' is a file path of training data self.prefsOnUser = tool.loadData(data) self.prefs = tool.transposePrefs(self.prefsOnUser) self.itemList = self.prefs.keys()