Best Python code snippet using stestr_python
virsh_nodecpumap.py
Source:virsh_nodecpumap.py
1import os2import logging3from autotest.client import utils4from autotest.client.shared import error5from virttest import virsh6SYSFS_SYSTEM_PATH = "/sys/devices/system/cpu"7def get_present_cpu():8 """9 Get host present cpu10 :return: the host present cpu number11 """12 if os.path.exists("%s/cpu0" % SYSFS_SYSTEM_PATH):13 cmd = "ls %s | grep cpu[0-9] | wc -l" % SYSFS_SYSTEM_PATH14 cmd_result = utils.run(cmd, ignore_status=True)15 present = int(cmd_result.stdout.strip())16 else:17 present = None18 return present19def format_map(map_str, map_test, map_length):20 """21 Format cpu map str to tuple22 :param map_str: cpu map string23 :param map_test: template cpu map tuple24 :param map_length: cpu map tuple length25 :return: the cpu map tuple26 """27 cpu_map = ()28 if '-' in map_str:29 param = map_str.split('-')30 for i in range(map_length):31 if i in range(int(param[0]), int(param[1]) + 1):32 cpu_map += ('y',)33 else:34 cpu_map += (map_test[i],)35 else:36 for i in range(map_length):37 if i == int(map_str):38 cpu_map += ('y',)39 else:40 cpu_map += (map_test[i],)41 return cpu_map42def get_online_cpu():43 """44 Get host online cpu map and number45 :return: the host online cpu map tuple46 """47 cpu_map = ()48 map_test = ()49 cpu_map_list = []50 present = get_present_cpu()51 if not present:52 return None53 for i in range(present):54 map_test += ('-',)55 for i in range(present):56 if i == 0:57 cpu_map_list.append('y')58 else:59 cpu_map_list.append('-')60 if os.path.exists("%s/online" % SYSFS_SYSTEM_PATH):61 cmd = "cat %s/online" % SYSFS_SYSTEM_PATH62 cmd_result = utils.run(cmd, ignore_status=True)63 output = cmd_result.stdout.strip()64 if ',' in output:65 output1 = output.split(',')66 for i in range(len(output1)):67 cpu_map = format_map(output1[i], map_test, present)68 map_test = cpu_map69 else:70 cpu_map = format_map(output, map_test, present)71 else:72 for i in range(present):73 if i != 0:74 if os.path.exists("%s/cpu%s/online" % (SYSFS_SYSTEM_PATH, i)):75 cmd = "cat %s/cpu%s/online" % (SYSFS_SYSTEM_PATH, i)76 cmd_result = utils.run(cmd, ignore_status=True)77 output = cmd_result.stdout.strip()78 if int(output) == 1:79 cpu_map_list[i] = 'y'80 else:81 return None82 cpu_map = tuple(cpu_map_list)83 return cpu_map84def run(test, params, env):85 """86 Test the command virsh nodecpumap87 (1) Call virsh nodecpumap88 (2) Call virsh nodecpumap with an unexpected option89 """90 option = params.get("virsh_node_options")91 status_error = params.get("status_error")92 result = virsh.nodecpumap(option, ignore_status=True, debug=True)93 output = result.stdout.strip()94 status = result.exit_status95 # Check result96 if status_error == "yes":97 if status == 0:98 raise error.TestFail("Run successfully with wrong command!")99 else:100 logging.info("Run failed as expected")101 else:102 out_value = []103 out = output.split('\n')104 for i in range(3):105 out_value.append(out[i].split()[-1])106 present = get_present_cpu()107 if not present:108 raise error.TestNAError("Host cpu counting not supported")109 else:110 if present != int(out_value[0]):111 raise error.TestFail("Present cpu is not expected")112 cpu_map = get_online_cpu()113 if not cpu_map:114 raise error.TestNAError("Host cpu map not supported")115 else:116 if cpu_map != tuple(out_value[2]):117 logging.info(cpu_map)118 logging.info(tuple(out_value[2]))119 raise error.TestFail("Cpu map is not expected")120 online = 0121 for i in range(present):122 if cpu_map[i] == 'y':123 online += 1124 if online != int(out_value[1]):...
map函数.py
Source:map函数.py
...4# for i in num_l:5# ret.append(i**2)6#7# print(ret)8# def map_test(array):9# ret=[]10# for i in num_l:11# ret.append(i**2)12# return ret13#14# ret=map_test(num_l)15# rett=map_test(num1_l)16# print(ret)17# print(rett)18num_l=[1,2,10,5,3,7]19#lambda x:x+120def add_one(x):21 return x+122#lambda x:x-123def reduce_one(x):24 return x-125#lambda x:x**226def pf(x):27 return x**228def map_test(func,array):29 ret=[]30 for i in num_l:31 res=func(i) #add_one(i)32 ret.append(res)33 return ret34# print(map_test(add_one,num_l))35# print(map_test(lambda x:x+1,num_l))36# print(map_test(reduce_one,num_l))37# print(map_test(lambda x:x-1,num_l))38# print(map_test(pf,num_l))39# print(map_test(lambda x:x**2,num_l))40#ç»æçæ¬41def map_test(func,array): #func=lambda x:x+1 arrary=[1,2,10,5,3,7]42 ret=[]43 for i in array:44 res=func(i) #add_one(i)45 ret.append(res)46 return ret47print(map_test(lambda x:x+1,num_l))48res=map(lambda x:x+1,num_l)49print('å
ç½®å½æ°mapï¼å¤çç»æ',res)50# for i in res:51# print(i)52print(list(res))53print('ä¼ çæ¯æåå½æ°',list(map(reduce_one,num_l)))54msg='linhaifeng'...
test_map.py
Source:test_map.py
1from mock_command import mock_command2from ft.commands.map import Map3# test cases for Arithmetic operations4def test_map_add3():5 map_test = mock_command(Map)6 map_test.set_input("add", ["3"], ["3", "5"])7 map_test.assert_output(["6", "8"])8def test_map_sub3():9 map_test = mock_command(Map)10 map_test.set_input("sub", ["3"], ["1", "3", "4"])11 map_test.assert_output(["-2", "0", "1"])12def test_map_mul3():13 map_test = mock_command(Map)14 map_test.set_input("mul", ["3"], ["0", "-1", "3"])15 map_test.assert_output(["0", "-3", "9"])16def test_map_id():17 map_test = mock_command(Map)18 map_test.set_input("id", [], ["3", "5"])19 map_test.assert_output(["3", "5"])20def test_map_split_ext():21 map_test = mock_command(Map)22 map_test.set_input("split_ext", [], ["file.txt", "dir/image.jpg"])23 map_test.assert_output(["file\ttxt", "dir/image\tjpg"])24def test_map_format_string():25 map_test = mock_command(Map)26 map_test.set_input("format", ["{:>5}"], ["abc", "b"])27 map_test.assert_output([" abc", " b"])28def test_map_format_int():29 map_test = mock_command(Map)30 map_test.set_input("format", ["{:02x}"], ["3", "11", "255"])...
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!!