Best Python code snippet using stestr_python
type_check.py
Source:type_check.py
1# Author: Xinshuo Weng2# email: xinshuo.weng@gmail.com3# this file includes functions checking the datatype and equality of input variables4import os, numpy as np, sys5from PIL import Image6############################################################# basic and customized datatype7# note:8# the tuple with length of 1 is equivalent to just the single element, it is not a tuple anymore9# the boolean value True and False are the scalar value 1 and 0 respectively10def isstring(string_test):11 if sys.version_info[0] < 3:12 return isinstance(string_test, basestring)13 else:14 return isinstance(string_test, str)15def islist(list_test):16 return isinstance(list_test, list)17def islogical(logical_test):18 return isinstance(logical_test, bool)19def isnparray(nparray_test):20 return isinstance(nparray_test, np.ndarray)21def istuple(tuple_test):22 return isinstance(tuple_test, tuple)23def isfunction(func_test):24 return callable(func_test)25def isdict(dict_test):26 return isinstance(dict_test, dict)27def isext(ext_test):28 '''29 check if it is an extension, only '.something' is an extension, multiple extension is not a valid extension30 '''31 return isstring(ext_test) and ext_test[0] == '.' and len(ext_test) > 1 and ext_test.count('.') == 132def isrange(range_test):33 '''34 check if it is a data range: such as [0, 1], (0, 1), array([0, 1]), the min must not bigger than the max35 '''36 return is2dpts(range_test) and range_test[0] <= range_test[1]37def isscalar(scalar_test):38 try: return isinteger(scalar_test) or isfloat(scalar_test)39 except TypeError: return False40############################################################# value41def isinteger(integer_test):42 if isnparray(integer_test): return False43 try: return isinstance(integer_test, int) or int(integer_test) == integer_test44 except ValueError: return False45 except TypeError: return False46def isfloat(float_test):47 return isinstance(float_test, float)48def ispositiveinteger(integer_test):49 return isinteger(integer_test) and integer_test > 050def isnonnegativeinteger(integer_test):51 return isinteger(integer_test) and integer_test >= 052def ifconfscalar(scalar_test):53 return isscalar(scalar_test) and scalar_test >= 0 and scalar_test <= 154def isuintnparray(nparray_test):55 return isnparray(nparray_test) and nparray_test.dtype == 'uint8'56def isfloatnparray(nparray_test):57 return isnparray(nparray_test) and nparray_test.dtype == 'float32'58def isnannparray(nparray_test):59 return isnparray(nparray_test) and bool(np.isnan(nparray_test).any())60############################################################# list61def islistoflist(list_test):62 if not islist(list_test): return False63 return all(islist(tmp) for tmp in list_test) and len(list_test) > 064def islistofstring(list_test):65 if not islist(list_test): return False66 return all(isstring(tmp) for tmp in list_test) and len(list_test) >= 067def islistofimage(list_test):68 if not islist(list_test): return False69 return all(isimage(tmp) for tmp in list_test) and len(list_test) >= 070def islistofdict(list_test):71 if not islist(list_test): return False72 return all(isdict(tmp) for tmp in list_test) and len(list_test) >= 073def islistofscalar(list_test):74 if not islist(list_test): return False75 return all(isscalar(tmp) for tmp in list_test) and len(list_test) >= 076def islistofpositiveinteger(list_test):77 if not islist(list_test): return False78 return all(ispositiveinteger(tmp) for tmp in list_test) and len(list_test) >= 079def islistofnonnegativeinteger(list_test):80 if not islist(list_test): return False81 return all(isnonnegativeinteger(tmp) for tmp in list_test) and len(list_test) >= 082############################################################# path 83# note:84# empty path is not valid, a path of whitespace ' ' is valid85def is_path_valid(pathname):86 try: 87 if not isstring(pathname) or not pathname: return False88 except TypeError: return False89 else: return True90def is_path_creatable(pathname):91 '''92 if any previous level of parent folder exists, returns true93 '''94 if not is_path_valid(pathname): return False95 pathname = os.path.normpath(pathname)96 pathname = os.path.dirname(os.path.abspath(pathname))97 # recursively to find the previous level of parent folder existing98 while not is_path_exists(pathname): 99 pathname_new = os.path.dirname(os.path.abspath(pathname))100 if pathname_new == pathname: return False101 pathname = pathname_new102 return os.access(pathname, os.W_OK)103def is_path_exists(pathname):104 try: return is_path_valid(pathname) and os.path.exists(pathname)105 except OSError: return False106def is_path_exists_or_creatable(pathname):107 try: return is_path_exists(pathname) or is_path_creatable(pathname)108 except OSError: return False109def isfile(pathname):110 if is_path_valid(pathname):111 pathname = os.path.normpath(pathname)112 name = os.path.splitext(os.path.basename(pathname))[0]113 ext = os.path.splitext(pathname)[1]114 return len(name) > 0 and len(ext) > 0115 else: return False;116def isfolder(pathname):117 '''118 if '.' exists in the subfolder, the function still justifies it as a folder. e.g., /mnt/dome/adhoc_0.5x/abc is a folder119 if '.' exists after all slashes, the function will not justify is as a folder. e.g., /mnt/dome/adhoc_0.5x is NOT a folder120 '''121 if is_path_valid(pathname):122 pathname = os.path.normpath(pathname)123 if pathname == './': return True124 name = os.path.splitext(os.path.basename(pathname))[0]125 ext = os.path.splitext(pathname)[1]126 return len(name) > 0 and len(ext) == 0127 else: return False128############################################################# images129def isimsize(size_test):130 '''131 shape check for images132 '''133 return is2dpts(size_test)134def ispilimage(image_test):135 return isinstance(image_test, Image.Image)136def iscolorimage_dimension(image_test):137 '''138 dimension check for RGB color images (or RGBA)139 '''140 if ispilimage(image_test): image_test = np.array(image_test)141 return isnparray(image_test) and image_test.ndim == 3 and (image_test.shape[2] == 3 or image_test.shape[2] == 4)142def isgrayimage_dimension(image_test):143 '''144 dimension check for gray images145 '''146 if ispilimage(image_test): image_test = np.array(image_test)147 return isnparray(image_test) and (image_test.ndim == 2 or (image_test.ndim == 3 and image_test.shape[2] == 1))148def isimage_dimension(image_test):149 '''150 dimension check for images151 '''152 return iscolorimage_dimension(image_test) or isgrayimage_dimension(image_test)153def isuintimage(image_test):154 '''155 value check for uint8 images156 '''157 if ispilimage(image_test): image_test = np.array(image_test)158 if not isimage_dimension(image_test): return False159 return image_test.dtype == 'uint8' # if uint8, must in [0, 255]160def isfloatimage(image_test):161 '''162 value check for float32 images163 '''164 if ispilimage(image_test): image_test = np.array(image_test)165 if not isimage_dimension(image_test): return False166 if not image_test.dtype == 'float32': return False167 item_check_le = (image_test <= 1.0)168 item_check_se = (image_test >= 0.0)169 return bool(item_check_le.all()) and bool(item_check_se.all())170def isnpimage(image_test):171 '''172 check if it is an uint8 or float32 numpy valid image173 '''174 return isnparray(image_test) and (isfloatimage(image_test) or isuintimage(image_test))175def isimage(image_test):176 return isnpimage(image_test) or ispilimage(image_test)177############################################################# geometry178def is2dpts(pts_test):179 '''180 2d point coordinate, numpy array or list or tuple with 2 elements181 '''182 return (isnparray(pts_test) or islist(pts_test) or istuple(pts_test)) and np.array(pts_test).size == 2183def is3dpts(pts_test):184 '''185 numpy array or list or tuple with 3 elements186 '''187 return (isnparray(pts_test) or islist(pts_test) or istuple(pts_test)) and np.array(pts_test).size == 3188def is2dhomopts(pts_test):189 '''190 2d homogeneous point coordinate, numpy array or list or tuple with 3 elements191 '''192 return is3dpts(pts_test)193def is2dptsarray(pts_test):194 '''195 numpy array with [2, N], N >= 0196 '''197 return isnparray(pts_test) and pts_test.shape[0] == 2 and len(pts_test.shape) == 2 and pts_test.shape[1] >= 0198def is3dptsarray(pts_test):199 '''200 numpy array with [3, N], N >= 0201 '''202 return isnparray(pts_test) and pts_test.shape[0] == 3 and len(pts_test.shape) == 2 and pts_test.shape[1] >= 0 203def is4dptsarray(pts_test):204 '''205 numpy array with [4, N], N >= 0206 '''207 return isnparray(pts_test) and pts_test.shape[0] == 4 and len(pts_test.shape) == 2 and pts_test.shape[1] >= 0 208def is2dptsarray_occlusion(pts_test):209 '''210 numpy array with [3, N], N >= 0. The third row represents occlusion, which contains only 1 or 0 or -1211 '''212 return is3dptsarray(pts_test) and bool((np.logical_or(np.logical_or(pts_test[2, :] == 0, pts_test[2, :] == 1), pts_test[2, :] == -1)).all())213def is2dptsarray_confidence(pts_test):214 '''215 numpy array with [3, N], N >= 0, the third row represents confidence, which contains a floating value bwtween [-1, 2] (as sometimes is 1.01 or -0.01)216 '''217 return is3dptsarray(pts_test) and bool((pts_test[2, :] >= -1).all()) and bool((pts_test[2, :] <= 2).all())218def is2dptsarray_homogeneous(pts_test):219 '''220 numpy array with [3, N], N >= 0221 '''222 return is3dptsarray(pts_test)223def is3dptsarray_homogeneous(pts_test):224 '''225 numpy array with [4, N], N >= 0226 '''227 return isnparray(pts_test) and pts_test.shape[0] == 4 and len(pts_test.shape) == 2 and pts_test.shape[1] >= 0 228def is3dhomopts(pts_test):229 '''230 numpy array or list or tuple with 3 elements231 '''232 return (isnparray(pts_test) or islist(pts_test) or istuple(pts_test)) and np.array(pts_test).size == 4233def is2dhomoline(line_test):234 '''235 numpy array or list or tuple with 3 elements236 '''237 return is2dhomopts(line_test)238def islinesarray(line_test):239 return is3dptsarray_homogeneous(line_test)240def isbbox(bbox_test):241 return isnparray(bbox_test) and islinesarray(bbox_test.transpose()) # N x 4242def iscenterbbox(bbox_test):...
test.py
Source:test.py
1print('--- numbic---')2print(2+3*4)3print((2+3)*4)4print(2**10) # å¹³æ¹5print(6/3)6print(7/3)7print(7//3) # åæ´é¤8print(7 % 3) # remain9print(3/6)10print(3//6)11print(3 % 6)12print(2**100)13# ------------------14print('--- bool ---')15print(5 > 1 and 5 < 10)16print(not (5 < 3))17# -----18print('---- Collection Data Types')19list_test = [1, 3, True, 5, 3]20list_test2 = [2, 3, 3, 4]21print('list->', list_test)22print('list slicing->', list_test[1:4])23print('list in ->', 1 in list_test)24print('list len ->', len(list_test))25print('list concatentation ->', list_test + list_test2)26list_test.append('appended')27print('list append ->', list_test)28print('list repeat -> ', list_test * 3)29print('range ->', range(10))30print('range to List->', list(range(10)))31print('range 5 -10->', range(5, 10))32print('range 5-10,asc step->', list(range(10, 5, -1)))33print('-------- tuple -----')34tuple_test = {35 3, 6, 'cat', 4, 5, False36}37tuple_test_2 = {38 3, 6, False39}40print('tuple', tuple_test)41print('tuple <= ->', tuple_test_2 <= tuple_test)42print('----- dictionary')43dict = {44 'iowa': 'dfsdf',45 'widfdf': 'dfsdf'46}47print(dict)48print('get it =>', dict.get('iowa'))49print('get it =>', dict['iowa'])...
hackerrank_list.py
Source:hackerrank_list.py
1if __name__ == '__main__':2 N = int(raw_input())3 list_test = []4 for i in range(N):5 command = raw_input()6 command_array = command.split(" ")7 if command_array[0] == "print":8 print list_test9 elif command_array[0] == "reverse":10 if list_test:11 list_test.reverse()12 elif command_array[0] == "sort":13 if list_test:14 list_test.sort()15 elif command_array[0] == "pop":16 if list_test:17 list_test.pop()18 elif command_array[0] == "remove":19 for i in range(1,len(command_array)):20 if int(command_array[i]) in list_test:21 list_test.remove(int(command_array[i])) 22 elif command_array[0] == "append":23 for i in range(1,len(command_array)): 24 list_test.append(int(command_array[i])) 25 elif command_array[0] == "insert": 26 list_test.insert(int(command_array[1]), int(command_array[2]))...
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!!