我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用inspect.html()。
def group(s, n): # See # http://www.python.org/doc/2.6/library/functions.html#zip return zip(*[iter(s)]*n)
def interleave_planes(ipixels, apixels, ipsize, apsize): """ Interleave (colour) planes, e.g. RGB + A = RGBA. Return an array of pixels consisting of the `ipsize` elements of data from each pixel in `ipixels` followed by the `apsize` elements of data from each pixel in `apixels`. Conventionally `ipixels` and `apixels` are byte arrays so the sizes are bytes, but it actually works with any arrays of the same type. The returned array is the same type as the input arrays which should be the same type as each other. """ itotal = len(ipixels) atotal = len(apixels) newtotal = itotal + atotal newpsize = ipsize + apsize # Set up the output buffer # See http://www.python.org/doc/2.4.4/lib/module-array.html#l2h-1356 out = array(ipixels.typecode) # It's annoying that there is no cheap way to set the array size :-( out.extend(ipixels) out.extend(apixels) # Interleave in the pixel data for i in range(ipsize): out[i:newtotal:newpsize] = ipixels[i:itotal:ipsize] for i in range(apsize): out[i+ipsize:newtotal:newpsize] = apixels[i:atotal:apsize] return out
def mycallersname(): """Returns the name of the caller of the caller of this function (hence the name of the caller of the function in which "mycallersname()" textually appears). Returns None if this cannot be determined.""" # http://docs.python.org/library/inspect.html#the-interpreter-stack import inspect frame = inspect.currentframe() if not frame: return None frame_,filename_,lineno_,funname,linelist_,listi_ = ( inspect.getouterframes(frame)[2]) return funname
def _enhex(s): """Convert from binary string (bytes) to hex string (str).""" import binascii return bytestostr(binascii.hexlify(s)) # Copies of PngSuite test files taken # from http://www.schaik.com/pngsuite/pngsuite_bas_png.html # on 2009-02-19 by drj and converted to hex. # Some of these are not actually in PngSuite (but maybe they should # be?), they use the same naming scheme, but start with a capital # letter.
def group(s, n): # See # http://www.python.org/doc/2.6/library/functions.html#zip return list(zip(*[iter(s)]*n))
def get_bound_args(func, *args, **kwargs): """ https://docs.python.org/3/library/inspect.html#inspect.BoundArguments def f(a, b, c=5, d=6): pass get_bound_args(f, 3, 6, d=100) -> {'a':3, 'b':6, 'c':5, 'd':100} Returns: OrderedDict of bound arguments """ arginfo = inspect.signature(func).bind(*args, **kwargs) arginfo.apply_defaults() return arginfo.arguments
def has_parameter_kind(func, kind): """ https://docs.python.org/3/library/inspect.html#inspect.signature Returns: whether the func has a parameter kind """ P = inspect.Parameter assert kind in [P.POSITIONAL_ONLY, P.POSITIONAL_OR_KEYWORD, P.VAR_POSITIONAL, P.KEYWORD_ONLY, P.VAR_KEYWORD] return any(param for param in inspect.signature(func).parameters.values() if param.kind == kind)
def write_pnm(file, width, height, pixels, meta): """Write a Netpbm PNM/PAM file.""" bitdepth = meta['bitdepth'] maxval = 2**bitdepth - 1 # Rudely, the number of image planes can be used to determine # whether we are L (PGM), LA (PAM), RGB (PPM), or RGBA (PAM). planes = meta['planes'] # Can be an assert as long as we assume that pixels and meta came # from a PNG file. assert planes in (1,2,3,4) if planes in (1,3): if 1 == planes: # PGM # Could generate PBM if maxval is 1, but we don't (for one # thing, we'd have to convert the data, not just blat it # out). fmt = 'P5' else: # PPM fmt = 'P6' file.write('%s %d %d %d\n' % (fmt, width, height, maxval)) if planes in (2,4): # PAM # See http://netpbm.sourceforge.net/doc/pam.html if 2 == planes: tupltype = 'GRAYSCALE_ALPHA' else: tupltype = 'RGB_ALPHA' file.write('P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\n' 'TUPLTYPE %s\nENDHDR\n' % (width, height, planes, maxval, tupltype)) # Values per row vpr = planes * width # struct format fmt = '>%d' % vpr if maxval > 0xff: fmt = fmt + 'H' else: fmt = fmt + 'B' for row in pixels: file.write(struct.pack(fmt, *row)) file.flush()
def _function_contents(func): """Return the signature contents of a function. The signature is as follows (should be byte/chars): < _code_contents (see above) from func.__code__ > ,( comma separated _object_contents for function argument defaults) ,( comma separated _object_contents for any closure contents ) See also: https://docs.python.org/3/reference/datamodel.html func.__code__ - The code object representing the compiled function body. func.__defaults__ - A tuple containing default argument values for those arguments that have defaults, or None if no arguments have a default value func.__closure__ - None or a tuple of cells that contain bindings for the function's free variables. """ contents = [_code_contents(func.__code__, func.__doc__)] # The function contents depends on the value of defaults arguments if func.__defaults__: function_defaults_contents = [_object_contents(cc) for cc in func.__defaults__] defaults = bytearray(b',(') defaults.extend(bytearray(b',').join(function_defaults_contents)) defaults.extend(b')') contents.append(defaults) else: contents.append(b',()') # The function contents depends on the closure captured cell values. closure = func.__closure__ or [] try: closure_contents = [_object_contents(x.cell_contents) for x in closure] except AttributeError: closure_contents = [] contents.append(b',(') contents.append(bytearray(b',').join(closure_contents)) contents.append(b')') retval = bytearray(b'').join(contents) return retval