Best Python code snippet using slash
ABuChecker.py
Source:ABuChecker.py
1# -*- encoding:utf-8 -*-2"""3 æ£æ¥ç±»ï¼æ£æ¥å½æ°å¯¹è±¡ãå½æ°åæ°ãå½æ°è¿åå¼4"""5from __future__ import print_function6from __future__ import absolute_import7from __future__ import division8from toolz import valmap9from functools import wraps10from ..CheckBu.ABuFuncUtil import *11from ..CheckBu.ABuChecks import *12from ..CheckBu.ABuProcessor import arg_process, return_process13__author__ = 'å¤ç«'14__weixin__ = 'abu_quant'15class _NoInstances(six.with_metaclass(ABCMeta, type)):16 """é»æ¢å®ä¾å"""17 def __call__(cls, *args, **kwargs):18 raise TypeError("Can't instantiate directly")19class FuncChecker(six.with_metaclass(ABCMeta, _NoInstances)):20 """å½æ°ç¸å
³çæ£æ¥ç±»"""21 @staticmethod22 def check_iscallable(func):23 """24 æ£æ¥ä¼ å
¥åæ°å¯¹è±¡æ¯å¦æ¯å½æ°ï¼ä¸æ¯å½æ°raise CheckError25 :param func: ä¼ å
¥åæ°å¯¹è±¡26 """27 if not callable(func):28 raise CheckError('%s is not callable' % get_func_name(func))29class ArgChecker(six.with_metaclass(ABCMeta, _NoInstances)):30 """å½æ°åæ°ç¸å
³çæ£æ¥ç±»"""31 @staticmethod32 def check_type(*ty_args, **ty_kwargs):33 """34 ãè£
饰å¨ã35 æ£æ¥è¾å
¥åæ°ç±»åï¼æ£æ¥å¤±è´¥raise CheckError36 :param ty_args: ç±»åtuple37 :param ty_kwargs: ç±»ådict38 :return: 39 """40 # æ£æ¥æ¯å¦æä¸åè§çtupleåæ°41 for ty in ty_args:42 if not isinstance(ty, (type, tuple)):43 raise TypeError(44 "check_type() expected a type or tuple of types"45 ", but got {type_} instead.".format(46 type_=ty,47 )48 )49 # æ£æ¥æ¯å¦æä¸åè§çdictåæ°50 for name, ty in six.iteritems(ty_kwargs):51 if not isinstance(ty, (type, tuple)):52 raise TypeError(53 "check_type() expected a type or tuple of types for "54 "argument '{name}', but got {type_} instead.".format(55 name=name, type_=ty,56 )57 )58 # å°type_checkä½ç¨å¨å½æ°åæ°ä¸59 return arg_process(*map(type_check, list(ty_args)), **valmap(type_check, ty_kwargs))60 @staticmethod61 def check_bound(*bd_args, **bd_kwargs):62 """63 ãè£
饰å¨ã64 æ£æ¥è¾å
¥åæ°æ¯å¦å¨æä¸èå´å
ï¼æ£æ¥å¤±è´¥raise CheckError65 ä¼ å
¥åæ°å½¢å¼åºä¸º`` (min_value, max_value)``.66 ``None`` å¯ä»¥ä½ä¸º ``min_value`` æ ``max_value``ï¼ç¸å½äºæ£è´æ ç©·67 :param bd_args: tupleèå´åæ°68 :param bd_kwargs: dictèå´åæ°69 :return: 70 """71 # å°bound_valid_and_checkä½ç¨å¨å½æ°åæ°ä¸72 return arg_process(*map(bound_valid_and_check, list(bd_args)),73 **valmap(bound_valid_and_check, bd_kwargs))74 @staticmethod75 def _check_default(check_no, *names, **_unused):76 """77 ãè£
饰å¨ã78 æ£æ¥å½æ°åæ°æ¯å¦ææè
没æé»è®¤å¼ï¼æ£æ¥å¤±è´¥raise CheckError79 :param staticmethod: æ£æ¥æ or 没æ80 :param names: å¾
æ£æ¥åæ°çå称81 :param _unused: å±è½dictåæ°82 :return: 83 """84 # å±è½dictåæ°85 if _unused:86 raise TypeError("_check_default() doesn't accept dict processors")87 check_err_msg = '' if check_no else 'no '88 def decorate(func):89 # è·åé»è®¤åæ°åå
¸90 arg_defaults = get_arg_defaults(func)91 for name in names:92 if name not in arg_defaults:93 # ä¼ å
¥äºå¹¶ä¸åå¨çåæ°å94 raise TypeError(get_func_name(func) + ' has no argument named ' + name)95 if bool(check_no) != (isinstance(arg_defaults[name], ArgNoDefault)):96 # æ£æ¥å¤±è´¥97 raise CheckError(98 'In ' + get_func_name(func) + ' argument {} has {}default'.format(name, check_err_msg))99 @wraps(func)100 def wrapper(*args, **kwargs):101 # ç´æ¥è¿å被è£
饰å½æ°ç»æ102 return func(*args, **kwargs)103 return wrapper104 return decorate105 @staticmethod106 def check_hasdefault(*names, **_unused):107 """108 ãè£
饰å¨ã109 æ£æ¥å½æ°åæ°æ¯å¦æé»è®¤å¼ï¼æ£æ¥å¤±è´¥raise CheckError110 :param names: å¾
æ£æ¥åæ°çå称111 :param _unused: å±è½dictåæ°112 :return: 113 """114 return ArgChecker._check_default(False, *names, **_unused)115 @staticmethod116 def check_nodefault(*names, **_unused):117 """118 ãè£
饰å¨ã119 æ£æ¥å½æ°åæ°æ¯å¦æ²¡æé»è®¤å¼ï¼æ£æ¥å¤±è´¥raise CheckError120 :param names: å¾
æ£æ¥åæ°çå称121 :param _unused: å±è½dictåæ°122 :return: 123 """124 return ArgChecker._check_default(True, *names, **_unused)125 @staticmethod126 def check_hasargs(func):127 """128 ãè£
饰å¨ã129 æ£æ¥å½æ°æ¯å¦æ*argsåæ°ï¼æ£æ¥å¤±è´¥raise CheckError130 :param func: ä¼ å
¥å½æ°å¯¹è±¡131 :return: 132 """133 # 解å
å½æ°åæ°åé»è®¤å¼134 argspec = getargspec(func)135 spec_args = argspec.args if argspec.args else []136 defaults = argspec.defaults if argspec.defaults else ()137 if len(spec_args) - len(defaults) == 0:138 # å½æ°æ²¡ætupleåæ°139 raise CheckError(get_func_name(func) + ' has no args')140 @wraps(func)141 def wrapper(*args_inner, **kwargs):142 # ç´æ¥è¿å被è£
饰å½æ°ç»æ143 return func(*args_inner, **kwargs)144 return wrapper145 @staticmethod146 def check_haskwargs(func):147 """148 ãè£
饰å¨149 æ£æ¥å½æ°æ¯å¦æ**kwargsåæ°ï¼æ£æ¥å¤±è´¥raise CheckError150 :param func: ä¼ å
¥å½æ°å¯¹è±¡151 :return: 152 """153 # 解å
å½æ°åæ°åé»è®¤å¼154 argspec = getargspec(func)155 defaults = argspec.defaults if argspec.defaults else ()156 if not defaults:157 # å½æ°æ²¡ædictåæ°158 raise CheckError(get_func_name(func) + ' has no kwargs')159 @wraps(func)160 def wrapper(*args, **kwargs):161 # ç´æ¥è¿å被è£
饰å½æ°ç»æ162 return func(*args, **kwargs)163 return wrapper164 @staticmethod165 def check_subset(*ss_args, **ss_kwargs):166 """167 ãè£
饰å¨ã168 æ£æ¥è¾å
¥åæ°æ¯å¦æ¯æä¸éåçåéï¼æ£æ¥å¤±è´¥raise CheckError169 :param ss_args: åæ°éåtuple170 :param ss_kwargs: åæ°éådict171 :return: 172 """173 # æ£æ¥æ¯å¦æä¸åè§çtupleåæ°174 for ss in ss_args:175 if not isinstance(ss, (list, set, type(None))):176 raise TypeError(177 "check_subset() expected a list or set or None of values"178 ", but got {subset_} or tuple instead.".format(179 subset_=str(type(ss)),180 )181 )182 # æ£æ¥æ¯å¦æä¸åè§çdictåæ°183 for name, ss in six.iteritems(ss_kwargs):184 if not isinstance(ss, (list, set, type(None))):185 raise TypeError(186 "check_subset() expected a list or set of values for "187 "argument '{name_}', but got {subset_} or tuple instead.".format(188 name_=name, subset_=str(type(ss)),189 )190 )191 # å°subset_checkå½æ°ä½ç¨å¨å½æ°åæ°ä¸192 return arg_process(*map(subset_check, list(ss_args)), **valmap(subset_check, ss_kwargs))193class ReturnChecker(six.with_metaclass(ABCMeta, _NoInstances)):194 """å½æ°è¿åå¼ç¸å
³çæ£æ¥ç±»"""195 @staticmethod196 def check_type(*types, **_unused):197 """198 ãè£
饰å¨ã199 æ£æ¥è¿åå¼ç±»åï¼æ£æ¥å¤±è´¥raise CheckError200 :param types: ç±»åtuple201 :param _unused: å±è½dictåæ°202 :return: 203 """204 # å±è½dictåæ°205 if _unused:206 raise TypeError("check_type() doesn't accept dict processors")207 # æ£æ¥æ¯å¦æä¸åæ ¼çtupleåæ°208 for type_ in types:209 if not isinstance(type_, (type, tuple, type(None))): # tuple or å
置类å210 raise TypeError(211 "check_return_type() expected a type or tuple of types, but got {type_msg} instead.".format(212 type_msg=type_,213 )214 )215 # å°type_checkå½æ°ä½ç¨å¨å½æ°è¿åå¼ä¸216 return return_process(*map(type_check, list(types)))217 @staticmethod218 def check_bound(*bounds, **_unused):219 """220 ãè£
饰å¨ã221 æ£æ¥è¿ååæ°æ¯å¦å¨æä¸èå´å
ï¼æ£æ¥å¤±è´¥raise CheckError222 ä¼ å
¥åæ°å½¢å¼åºä¸º`` (min_value, max_value)``.223 ``None`` å¯ä»¥ä½ä¸º ``min_value`` æ ``max_value``ï¼ç¸å½äºæ£è´æ ç©·224 :param bounds: tupleèå´åæ°225 :param _unused: å±è½dictåæ°226 :return: 227 """228 # å±è½dictåæ°229 if _unused:230 raise TypeError("check_bound() doesn't accept dict processors")231 # å°bound_valid_and_checkå½æ°ä½ç¨å¨å½æ°è¿åå¼ä¸232 return return_process(*map(bound_valid_and_check, list(bounds)))233 @staticmethod234 def check_subset(*ss_args, **_unused):235 """236 ãè£
饰å¨ã237 æ£æ¥è¾å
¥åæ°æ¯å¦æ¯æä¸éåçåéï¼æ£æ¥å¤±è´¥raise CheckError238 :param ss_args: åæ°éåtuple239 :param _unused: å±è½dictåæ°240 :return: 241 """242 # å±è½dictåæ°243 if _unused:244 raise TypeError("check_subset() doesn't accept dict processors")245 # æ£æ¥ä¼ å
¥çtupleåæ°246 for ss in ss_args:247 if not isinstance(ss, (list, set, type(None))):248 raise TypeError(249 "check_subset() expected a list or set or None of values"250 ", but got {subset_} or tuple instead.".format(251 subset_=str(type(ss)),252 )253 )254 # å°subset_checkå½æ°ä½ç¨å¨å½æ°è¿åå¼ä¸...
libwebp.py
Source:libwebp.py
1# This file was automatically generated by SWIG (http://www.swig.org).2# Version 2.0.43#4# Do not make changes to this file unless you know what you are doing--modify5# the SWIG interface file instead.6from sys import version_info7if version_info >= (2,6,0):8 def swig_import_helper():9 from os.path import dirname10 import imp11 fp = None12 try:13 fp, pathname, description = imp.find_module('_libwebp', [dirname(__file__)])14 except ImportError:15 import _libwebp16 return _libwebp17 if fp is not None:18 try:19 _mod = imp.load_module('_libwebp', fp, pathname, description)20 finally:21 fp.close()22 return _mod23 _libwebp = swig_import_helper()24 del swig_import_helper25else:26 import _libwebp27del version_info28try:29 _swig_property = property30except NameError:31 pass # Python < 2.2 doesn't have 'property'.32def _swig_setattr_nondynamic(self,class_type,name,value,static=1):33 if (name == "thisown"): return self.this.own(value)34 if (name == "this"):35 if type(value).__name__ == 'SwigPyObject':36 self.__dict__[name] = value37 return38 method = class_type.__swig_setmethods__.get(name,None)39 if method: return method(self,value)40 if (not static):41 self.__dict__[name] = value42 else:43 raise AttributeError("You cannot add attributes to %s" % self)44def _swig_setattr(self,class_type,name,value):45 return _swig_setattr_nondynamic(self,class_type,name,value,0)46def _swig_getattr(self,class_type,name):47 if (name == "thisown"): return self.this.own()48 method = class_type.__swig_getmethods__.get(name,None)49 if method: return method(self)50 raise AttributeError(name)51def _swig_repr(self):52 try: strthis = "proxy of " + self.this.__repr__()53 except: strthis = ""54 return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)55try:56 _object = object57 _newclass = 158except AttributeError:59 class _object : pass60 _newclass = 061def WebPGetDecoderVersion():62 """WebPGetDecoderVersion() -> int"""63 return _libwebp.WebPGetDecoderVersion()64def WebPGetInfo(*args):65 """WebPGetInfo(uint8_t data) -> (width, height)"""66 return _libwebp.WebPGetInfo(*args)67def WebPDecodeRGB(*args):68 """WebPDecodeRGB(uint8_t data) -> (rgb, width, height)"""69 return _libwebp.WebPDecodeRGB(*args)70def WebPDecodeRGBA(*args):71 """WebPDecodeRGBA(uint8_t data) -> (rgb, width, height)"""72 return _libwebp.WebPDecodeRGBA(*args)73def WebPDecodeARGB(*args):74 """WebPDecodeARGB(uint8_t data) -> (rgb, width, height)"""75 return _libwebp.WebPDecodeARGB(*args)76def WebPDecodeBGR(*args):77 """WebPDecodeBGR(uint8_t data) -> (rgb, width, height)"""78 return _libwebp.WebPDecodeBGR(*args)79def WebPDecodeBGRA(*args):80 """WebPDecodeBGRA(uint8_t data) -> (rgb, width, height)"""81 return _libwebp.WebPDecodeBGRA(*args)82def WebPGetEncoderVersion():83 """WebPGetEncoderVersion() -> int"""84 return _libwebp.WebPGetEncoderVersion()85def wrap_WebPEncodeRGB(*args):86 """private, do not call directly."""87 return _libwebp.wrap_WebPEncodeRGB(*args)88def wrap_WebPEncodeBGR(*args):89 """private, do not call directly."""90 return _libwebp.wrap_WebPEncodeBGR(*args)91def wrap_WebPEncodeRGBA(*args):92 """private, do not call directly."""93 return _libwebp.wrap_WebPEncodeRGBA(*args)94def wrap_WebPEncodeBGRA(*args):95 """private, do not call directly."""96 return _libwebp.wrap_WebPEncodeBGRA(*args)97def wrap_WebPEncodeLosslessRGB(*args):98 """private, do not call directly."""99 return _libwebp.wrap_WebPEncodeLosslessRGB(*args)100def wrap_WebPEncodeLosslessBGR(*args):101 """private, do not call directly."""102 return _libwebp.wrap_WebPEncodeLosslessBGR(*args)103def wrap_WebPEncodeLosslessRGBA(*args):104 """private, do not call directly."""105 return _libwebp.wrap_WebPEncodeLosslessRGBA(*args)106def wrap_WebPEncodeLosslessBGRA(*args):107 """private, do not call directly."""108 return _libwebp.wrap_WebPEncodeLosslessBGRA(*args)109_UNUSED = 1110def WebPEncodeRGB(rgb, width, height, stride, quality_factor):111 """WebPEncodeRGB(uint8_t rgb, int width, int height, int stride, float quality_factor) -> lossy_webp"""112 webp = wrap_WebPEncodeRGB(113 rgb, _UNUSED, _UNUSED, width, height, stride, quality_factor)114 if len(webp[0]) == 0:115 return None116 return webp[0]117def WebPEncodeRGBA(rgb, width, height, stride, quality_factor):118 """WebPEncodeRGBA(uint8_t rgb, int width, int height, int stride, float quality_factor) -> lossy_webp"""119 webp = wrap_WebPEncodeRGBA(120 rgb, _UNUSED, _UNUSED, width, height, stride, quality_factor)121 if len(webp[0]) == 0:122 return None123 return webp[0]124def WebPEncodeBGR(rgb, width, height, stride, quality_factor):125 """WebPEncodeBGR(uint8_t rgb, int width, int height, int stride, float quality_factor) -> lossy_webp"""126 webp = wrap_WebPEncodeBGR(127 rgb, _UNUSED, _UNUSED, width, height, stride, quality_factor)128 if len(webp[0]) == 0:129 return None130 return webp[0]131def WebPEncodeBGRA(rgb, width, height, stride, quality_factor):132 """WebPEncodeBGRA(uint8_t rgb, int width, int height, int stride, float quality_factor) -> lossy_webp"""133 webp = wrap_WebPEncodeBGRA(134 rgb, _UNUSED, _UNUSED, width, height, stride, quality_factor)135 if len(webp[0]) == 0:136 return None137 return webp[0]138def WebPEncodeLosslessRGB(rgb, width, height, stride):139 """WebPEncodeLosslessRGB(uint8_t rgb, int width, int height, int stride) -> lossless_webp"""140 webp = wrap_WebPEncodeLosslessRGB(rgb, _UNUSED, _UNUSED, width, height, stride)141 if len(webp[0]) == 0:142 return None143 return webp[0]144def WebPEncodeLosslessRGBA(rgb, width, height, stride):145 """WebPEncodeLosslessRGBA(uint8_t rgb, int width, int height, int stride) -> lossless_webp"""146 webp = wrap_WebPEncodeLosslessRGBA(rgb, _UNUSED, _UNUSED, width, height, stride)147 if len(webp[0]) == 0:148 return None149 return webp[0]150def WebPEncodeLosslessBGR(rgb, width, height, stride):151 """WebPEncodeLosslessBGR(uint8_t rgb, int width, int height, int stride) -> lossless_webp"""152 webp = wrap_WebPEncodeLosslessBGR(rgb, _UNUSED, _UNUSED, width, height, stride)153 if len(webp[0]) == 0:154 return None155 return webp[0]156def WebPEncodeLosslessBGRA(rgb, width, height, stride):157 """WebPEncodeLosslessBGRA(uint8_t rgb, int width, int height, int stride) -> lossless_webp"""158 webp = wrap_WebPEncodeLosslessBGRA(rgb, _UNUSED, _UNUSED, width, height, stride)159 if len(webp[0]) == 0:160 return None161 return webp[0]...
SAC.py
Source:SAC.py
1def inWords(n):2 return 4 * n3def openSAC(file):4 pass5# floats (word 0-69)6HdrFloats = ["DELTA", "DEPMIN", "DEPMAX", "SCALE", "ODELTA",7 "B", "E", "O", "A", "_INTERNAL",8 "T0", "T1", "T2", "T3", "T4",9 "T5", "T6", "T7", "T8", "T9",10 "F", "RESP0", "RESP1", "RESP2", "RESP3",11 "RESP4", "RESP5", "RESP6", "RESP7", "RESP8",12 "RESP9", "STLA", "STLO", "STEL", "STDP",13 "EVLA", "EVLO", "EVEL", "EVDP", "MAG",14 "USER0", "USER1", "USER2", "USER3", "USER4",15 "USER5", "USER6", "USER7", "USER8", "USER9",16 "DIST", "AZ", "BAZ", "GCARC", "_INTERNAL",17 "_INTERNAL", "DEPMEN", "CMPAZ", "CMPINC", "XMINIMUM",18 "XMAXIMUM", "YMINIMUM", "YMAXIMUM", "_UNUSED", "_UNUSED",19 "_UNUSED", "_UNUSED", "_UNUSED", "_UNUSED", "_UNUSED"]20assert len(HdrFloats) == 7021# integers and enumerated (words 70-104)22HdrInts = ["NZYEAR", "NZJDAY", "NZHOUR", "NZMIN", "NZSEC",23 "NZMSEC", "NVHDR", "NORID", "NEVID", "NPTS",24 "_INTERNAL", "NWFID", "NXSIZE", "NYSIZE", "_UNUSED",25 "IFTYPE", "IDEP", "IZTYPE", "_UNUSED", "IINST",26 "ISTREG", "IEVREG", "IEVTYP", "IQUAL", "ISYNTH",27 "IMAGTYP", "IMAGSRC", "_UNUSED", "_UNUSED", "_UNUSED",28 "_UNUSED", "_UNUSED", "_UNUSED", "_UNUSED", "_UNUSED"]29assert len(HdrInts) == 3530# logical (boolean) (words 105-109)31HdrLogicals = ["LEVEN", "LPSPOL", "LOVROK", "LCALDA", "_UNUSED"]32assert len(HdrLogicals) == 533# alphanumeric (each is 2 words [8 ASCII characters] big)34# (KEVNM takes two slots as KEVNM_1, KEVNM_2)35HdrTexts = ["KSTNM", "KEVNM_1", "KEVNM_2",36 "KHOLE", "KO", "KA",37 "KT0", "KT1", "KT2",38 "KT3", "KT4", "KT5",39 "KT6", "KT7", "KT8",40 "KT9", "KF", "KUSER0",41 "KUSER1", "KUSER2", "KCMPNM",42 "KNETWK", "KDATRD", "KINST"]43assert len(HdrTexts) == 2444import struct45HEADER_SIZE = inWords(158)46def headerVersion(raw_header):47 """Returns a tuple of the (version, big-endian) or (None, None)48 49 nvhdr should be loaded from a SAC file at NVHDR_LOCATION.50 """51 _valid_version = range(1, 20)52 def valid_version(x):53 return x in _valid_version54 nvhdr_string = raw_header[inWords(76):inWords(77)]55 if len(nvhdr_string) != 4:56 raise ValueError, "nvhdr_string must be 4 bytes long"57 le_ver = struct.unpack("<i", nvhdr_string)[0]58 be_ver = struct.unpack(">i", nvhdr_string)[0]59 if valid_version(le_ver) and valid_version(be_ver):60 # can't decide (I don't even know if this can happen)61 return (None, None)62 if valid_version(le_ver):63 return (le_ver, False)64 if valid_version(be_ver):65 return (be_ver, True)66 else:67 return (None, None)68class Header():69 def __init__(self, raw_header):70 data = {}71 self.data = data72 def header_range(start, end):73 return raw_header[inWords(start):inWords(end+1)]74 required_size = inWords(158)75 if len(raw_header) != required_size:76 raise ValueError, "header size error (size=%d)" % len(raw_header)77 (version, big_endian) = headerVersion(raw_header)78 if not version:79 raise ValueError, "invalid header format"80 if big_endian:81 endian = ">"82 else:83 endian = "<"84 self.version = version85 self.big_endian = big_endian86 floats = struct.unpack(endian + "70f", header_range(0, 69))87 for (name, val) in zip(HdrFloats, floats):88 data[name] = val89 # integers, enumerated, and logicals90 ints = struct.unpack(endian + "40i", header_range(70, 109))91 for (name, val) in zip(HdrInts + HdrLogicals, ints):92 data[name] = val93 94 text_data = header_range(110, 157)95 for (name, index) in zip(HdrTexts, range(0, 4 * 24, 4)):96 data[name] = text_data[index:index+4]97 data["KEVNM"] = data["KEVNM_1"] + data["KEVNM_2"]98class Reader():99 def __init__(self, file):100 header_data = file.read(inWords(158))101 self.header = Header(header_data)102 npts = self.header.data["NPTS"]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!