Best Python code snippet using fMBT_python
stairs_test.py
Source:stairs_test.py  
1# -*- coding: utf-8 -*-2from random import choice, sample3import hypothesis.strategies as st4from hypothesis import given, example, note5from stairs_solution import *6from util import list1_equals_list2_except_order, list1_is_subset_of_list27def test_enumerate_conditional_permutations():8    assert enumerate_conditional_permutations(1, [1, 2, 3], lambda l: sum(l) == 1) == [[1]]9    assert list1_equals_list2_except_order(enumerate_conditional_permutations(2, [1, 2, 3], lambda l: sum(l) == 2), [[1, 1], [2]])10    assert list1_equals_list2_except_order(enumerate_conditional_permutations(3, [1, 2, 3], lambda l: sum(l) == 3), [[1, 1, 1], [2, 1], [1, 2], [3]])11    assert list1_equals_list2_except_order(enumerate_conditional_permutations(4, [1, 2, 3], lambda l: sum(l) == 4), [[1, 1, 1, 1], [2, 1, 1], [12        1, 2, 1], [3, 1], [1, 1, 2], [2, 2], [1, 3]])13    assert enumerate_conditional_permutations(5, [2, 4], lambda l: sum(l) == 5) == []14    assert enumerate_conditional_permutations(1, [2, 4], lambda l: sum(l) == 1) == []15    assert enumerate_conditional_permutations(1, [1, 2, 3], lambda l: sum(l) == 0) == []16def test_count_solutions_recursive():17    assert count_solutions_recursive(1, tuple([1, 2, 3])) == 118    assert count_solutions_recursive(2, tuple([1, 2, 3])) == 219    assert count_solutions_recursive(3, tuple([1, 2, 3])) == 420    assert count_solutions_recursive(4, tuple([1, 2, 3])) == 721    assert count_solutions_recursive(8, tuple([1, 2, 3])) == 8122    assert count_solutions_recursive(5, tuple([2, 4])) == 023    assert count_solutions_recursive(1, tuple([2, 4])) == 024    assert count_solutions_recursive(0, tuple([1, 2, 3])) == 125def test_count_solutions_dp_bottom_up():26    assert count_solutions_dp_bottom_up(1, [1, 2, 3]) == 127    assert count_solutions_dp_bottom_up(2, [1, 2, 3]) == 228    assert count_solutions_dp_bottom_up(3, [1, 2, 3]) == 429    assert count_solutions_dp_bottom_up(4, [1, 2, 3]) == 730    assert count_solutions_dp_bottom_up(8, [1, 2, 3]) == 8131    assert count_solutions_dp_bottom_up(5, [2, 4]) == 032    assert count_solutions_dp_bottom_up(1, [2, 4]) == 033    assert count_solutions_dp_bottom_up(0, [1, 2, 3]) == 134def test_count_solutions_dp_top_down():35    assert count_solutions_dp_top_down(1, [1, 2, 3]) == 136    assert count_solutions_dp_top_down(2, [1, 2, 3]) == 237    assert count_solutions_dp_top_down(3, [1, 2, 3]) == 438    assert count_solutions_dp_top_down(4, [1, 2, 3]) == 739    assert count_solutions_dp_top_down(8, [1, 2, 3]) == 8140    assert count_solutions_dp_top_down(5, [2, 4]) == 041    assert count_solutions_dp_top_down(1, [2, 4]) == 042    assert count_solutions_dp_top_down(0, [1, 2, 3]) == 143def test_enumerate_solutions_recursive():44    assert enumerate_solutions_recursive(1, [1, 2, 3]) == [[1]]45    assert enumerate_solutions_recursive(2, [1, 2, 3]) == [[1, 1], [2]]46    assert enumerate_solutions_recursive(3, [1, 2, 3]) == [[1, 1, 1], [2, 1], [1, 2], [3]]47    assert enumerate_solutions_recursive(4, [1, 2, 3]) == [[1, 1, 1, 1], [2, 1, 1], [48        1, 2, 1], [3, 1], [1, 1, 2], [2, 2], [1, 3]]49    assert enumerate_solutions_recursive(5, [2, 4]) == []50    assert enumerate_solutions_recursive(1, [2, 4]) == []51    assert enumerate_solutions_recursive(0, [1, 2, 3]) == [[]]52def test_enumerate_solutions():53    assert enumerate_solutions(1, [1, 2, 3]) == [[1]]54    assert enumerate_solutions(2, [1, 2, 3]) == [[1, 1], [2]]55    assert enumerate_solutions(3, [1, 2, 3]) == [[1, 1, 1], [2, 1], [1, 2], [3]]56    assert enumerate_solutions(4, [1, 2, 3]) == [[1, 1, 1, 1], [2, 1, 1], [57        1, 2, 1], [3, 1], [1, 1, 2], [2, 2], [1, 3]]58    assert enumerate_solutions(5, [2, 4]) == []59    assert enumerate_solutions(1, [2, 4]) == []60    assert enumerate_solutions(0, [1, 2, 3]) == [[]]61def test_stairs_8():62    stairs = 863    steps = tuple([1, 2, 3])64    assert count_solutions_recursive(stairs, steps) == 8165    assert count_solutions_dp_bottom_up(stairs, steps) == 8166    assert count_solutions_dp_top_down(stairs, steps) == 8167    assert enumerate_optimal_solutions(stairs, steps) == [[3, 3, 2], [3, 2, 3], [2, 3, 3]]68    assert enumerate_unique_optimal_solutions(stairs, steps) == [[2, 3, 3]]69    assert find_any_optimal_solution(stairs, steps) == [3, 3, 2]70    assert len(enumerate_conditional_permutations(stairs, steps, lambda l: sum(l) == stairs)) == 8171def test_enumerate_optimal_solutions():72    assert enumerate_optimal_solutions(1, [1, 3]) == [[1]]73    assert enumerate_optimal_solutions(2, [1, 3]) == [[1, 1]]74    assert enumerate_optimal_solutions(3, [1, 3]) == [[3]]75    assert enumerate_optimal_solutions(4, [1, 3]) == [[3, 1], [1, 3]]76    assert enumerate_optimal_solutions(8, [1, 4, 5]) == [[4, 4]]77    assert enumerate_optimal_solutions(8, [1, 2, 3]) == [[3, 3, 2], [3, 2, 3], [2, 3, 3]]78    assert enumerate_optimal_solutions(1, [2, 4]) == []79    assert enumerate_optimal_solutions(3, [2, 4]) == []80    assert enumerate_optimal_solutions(7, [2, 4]) == []81    assert enumerate_optimal_solutions(3, [2, 5]) == []82    assert enumerate_optimal_solutions(4, [2]) == [[2, 2]]83    assert enumerate_optimal_solutions(0, [1, 2, 3]) == [[]]84def test_find_any_optimal_solution():85    assert find_any_optimal_solution(1, [1, 3]) == [1]86    assert find_any_optimal_solution(2, [1, 3]) == [1, 1]87    assert find_any_optimal_solution(3, [1, 3]) == [3]88    assert find_any_optimal_solution(4, [1, 3]) == [3, 1]89    assert find_any_optimal_solution(8, [1, 4, 5]) == [4, 4]90    assert find_any_optimal_solution(8, [1, 2, 3]) == [3, 3, 2]91    assert find_any_optimal_solution(1, [2, 4]) == []92    assert find_any_optimal_solution(3, [2, 4]) == []93    assert find_any_optimal_solution(7, [2, 4]) == []94    assert find_any_optimal_solution(3, [2, 5]) == []95    assert find_any_optimal_solution(4, [2]) == [2, 2]96    assert find_any_optimal_solution(0, [1, 2, 3]) == []97def test_enumerate_unique_optimal_solutions():98    assert enumerate_unique_optimal_solutions(1, [1, 3]) == [[1]]99    assert enumerate_unique_optimal_solutions(2, [1, 3]) == [[1, 1]]100    assert enumerate_unique_optimal_solutions(3, [1, 3]) == [[3]]101    assert enumerate_unique_optimal_solutions(4, [1, 3]) == [[1, 3]]102    assert enumerate_unique_optimal_solutions(8, [1, 4, 5]) == [[4, 4]]103    assert enumerate_unique_optimal_solutions(8, [1, 2, 3]) == [[2, 3, 3]]104    assert enumerate_unique_optimal_solutions(12, [1, 3, 5]) == [[1, 1, 5, 5], [1, 3, 3, 5], [3, 3, 3, 3]]105    assert enumerate_unique_optimal_solutions(1, [2, 4]) == []106    assert enumerate_unique_optimal_solutions(3, [2, 4]) == []107    assert enumerate_unique_optimal_solutions(7, [2, 4]) == []108    assert enumerate_unique_optimal_solutions(3, [2, 5]) == []109    assert enumerate_unique_optimal_solutions(4, [2]) == [[2, 2]]110    assert enumerate_unique_optimal_solutions(0, [1, 2, 3]) == [[]]111def test_list_subset():112    assert list1_is_subset_of_list2([[1, 1, 1, 4]], [[1, 1, 1, 4], [2, 1, 4]])113# ==== PROPERTY-BASED TESTING ======114def stairs_strategy(max_stairs):115    """116     Generator of tuples of number of stairs 'n' and list of steps 'steps':117        - 'n' is a value such that 0 < n <= max_stairs118        - steps[i] is value such that 0 < steps[i] <= n for all 0 < i <= n119    """120    n = choice(range(1, max_stairs + 1))121    i = choice(range(1, n + 1))122    return n, tuple(sample(range(1, n + 1), i))123@given(st.builds(stairs_strategy, st.integers(5, 25)))124def test_solution_comparison(t):125    n = t[0]126    elems = t[1]127    assert count_solutions_recursive(n, elems) == len(enumerate_solutions_recursive(n, elems))128    assert enumerate_solutions(n, elems) == enumerate_solutions_recursive(n, elems)129    assert list1_is_subset_of_list2(enumerate_optimal_solutions(n, elems), enumerate_solutions(n, elems))130    assert list1_is_subset_of_list2(enumerate_unique_optimal_solutions(n, elems), enumerate_optimal_solutions(n, elems))131    assert count_solutions_recursive(n, elems) == count_solutions_dp_bottom_up(n, elems)132    assert count_solutions_recursive(n, elems) == count_solutions_dp_top_down(n, elems)133    assert find_any_optimal_solution(n, elems) in enumerate_solutions_recursive(n, elems)134@given(st.builds(stairs_strategy, st.integers(4, 7)))135def test_enumerate_conditional_permutations_vs_enumerate_solutions(t):136    n = t[0]137    elems = t[1]138    list1 = enumerate_solutions(n, elems)139    list2 = enumerate_conditional_permutations(n, elems, lambda l: sum(l) == n)140    note(f"result:{list1_equals_list2_except_order(list1, list2)}")...network.py
Source:network.py  
...11#12#13#14class NetDeviceEnumerator:15    def enumerate(self):16        if System.WS_LINUX == System.name():17            return self.enumerate_linux()18        elif System.WS_FREEBSD == System.name():19            return self.enumerate_freebsd()20        return self.debug_enumerate()21    def enumerate_freebsd(self):22        raise Exception("NetDeviceEnumerator::enumerate_freebsd - not implemented on system '%s'" % System.name())23    def debug_enumerate(self):24        d1 = ActualNetDevice()25        d1.name   = "ens160"26        d1.mac    = "11:0C:29:2A:77:E3"27        d1.state  = "UP"28        d1.ip4.config_type     = CONFIG_TYPE_STATIC29        d1.ip4.address         = "192.168.1.14"30        d1.ip4.netmask         = "255.255.255.0"31        d1.ip4.gateway         = "192.168.1.1"32        d1.ip4.dns_srv1        = "192.168.1.4"33        d1.ip4.dns_srv2        = "192.168.1.5"34        d1.ip4.dns_search      = "diladele.lan"35        d1.ip6.config_type     = CONFIG_TYPE_NONE36        d2 = ActualNetDevice()37        d2.name   = "ens260"...preprocess.py
Source:preprocess.py  
...6def pre_normalization(data, zaxis=[0, 1], xaxis=[8, 4]):7    N, C, T, V, M = data.shape8    s = np.transpose(data, [0, 4, 2, 3, 1])  # N, C, T, V, M  to  N, M, T, V, C9    print('pad the null frames with the previous frames')10    for i_s, skeleton in enumerate(tqdm(s)):  # pad11        if skeleton.sum() == 0:12            print(i_s, ' has no skeleton')13        for i_p, person in enumerate(skeleton):14            if person.sum() == 0:15                continue16            if person[0].sum() == 0:17                index = (person.sum(-1).sum(-1) != 0)18                tmp = person[index].copy()19                person *= 020                person[:len(tmp)] = tmp21            for i_f, frame in enumerate(person):22                if frame.sum() == 0:23                    if person[i_f:].sum() == 0:24                        rest = len(person) - i_f25                        num = int(np.ceil(rest / i_f))26                        pad = np.concatenate([person[0:i_f] for _ in range(num)], 0)[:rest]27                        s[i_s, i_p, i_f:] = pad28                        break29    print('sub the center joint #1 (spine joint in ntu and neck joint in kinetics)')30    for i_s, skeleton in enumerate(tqdm(s)):31        if skeleton.sum() == 0:32            continue33        main_body_center = skeleton[0][:, 1:2, :].copy()34        for i_p, person in enumerate(skeleton):35            if person.sum() == 0:36                continue37            mask = (person.sum(-1) != 0).reshape(T, V, 1)38            s[i_s, i_p] = (s[i_s, i_p] - main_body_center) * mask39    print('parallel the bone between hip(jpt 0) and spine(jpt 1) of the first person to the z axis')40    for i_s, skeleton in enumerate(tqdm(s)):41        if skeleton.sum() == 0:42            continue43        joint_bottom = skeleton[0, 0, zaxis[0]]44        joint_top = skeleton[0, 0, zaxis[1]]45        axis = np.cross(joint_top - joint_bottom, [0, 0, 1])46        angle = angle_between(joint_top - joint_bottom, [0, 0, 1])47        matrix_z = rotation_matrix(axis, angle)48        for i_p, person in enumerate(skeleton):49            if person.sum() == 0:50                continue51            for i_f, frame in enumerate(person):52                if frame.sum() == 0:53                    continue54                for i_j, joint in enumerate(frame):55                    s[i_s, i_p, i_f, i_j] = np.dot(matrix_z, joint)56    print(57        'parallel the bone between right shoulder(jpt 8) and left shoulder(jpt 4) of the first person to the x axis')58    for i_s, skeleton in enumerate(tqdm(s)):59        if skeleton.sum() == 0:60            continue61        joint_rshoulder = skeleton[0, 0, xaxis[0]]62        joint_lshoulder = skeleton[0, 0, xaxis[1]]63        axis = np.cross(joint_rshoulder - joint_lshoulder, [1, 0, 0])64        angle = angle_between(joint_rshoulder - joint_lshoulder, [1, 0, 0])65        matrix_x = rotation_matrix(axis, angle)66        for i_p, person in enumerate(skeleton):67            if person.sum() == 0:68                continue69            for i_f, frame in enumerate(person):70                if frame.sum() == 0:71                    continue72                for i_j, joint in enumerate(frame):73                    s[i_s, i_p, i_f, i_j] = np.dot(matrix_x, joint)74    data = np.transpose(s, [0, 4, 2, 3, 1])75    return data76if __name__ == '__main__':77    data = np.load('../data/ntu/xview/val_data.npy')78    pre_normalization(data)...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!!
