Best Python code snippet using lisa_python
test_search_space.py
Source:test_search_space.py  
...39        super().__init__(*args, **kwargs)40        id = f"{'.'.join(self.id().split('.')[-2:])}"41        self._log = get_logger(id)42    def test_supported_intrange(self) -> None:43        self._verify_matrix(44            expected_meet=[45                [True, True, True, False, True, True, False, True, False, False],46                [True, True, False, False, True, True, False, False, False, False],47            ],48            expected_min=[49                [12, 10, 15, False, 10, 10, False, 15, False, False],50                [12, 10, False, False, 10, 10, False, False, False, False],51            ],52            requirements=[53                IntRange(min=10, max=15),54                IntRange(min=10, max=15, max_inclusive=False),55            ],56            capabilities=[57                IntRange(12),58                IntRange(10),59                IntRange(15),60                IntRange(20),61                IntRange(5, 11),62                IntRange(5, 10),63                IntRange(5, 10, max_inclusive=False),64                IntRange(15, 20),65                IntRange(1, 5),66                IntRange(20, 100),67            ],68        )69    def test_supported_countspace(self) -> None:70        expected_meet = [71            [True, True, True, True, True, True, True, True, True, True, True],72            [False, True, False, False, False, True, True, True, False, False, False],73            [False, False, True, False, False, True, False, True, True, False, False],74            [False, False, False, False, True, False, False, True, True, True, True],75            [False, True, True, False, False, True, True, True, True, False, False],76            [False, True, False, False, False, True, True, True, True, False, False],77            [False, True, True, False, True, True, True, True, True, True, True],78        ]79        expected_min: List[List[Any]] = [80            [None, 10, 15, 18, 25, 10, 10, 10, 12, 21, 21],81            [False, 10, False, False, False, 10, 10, 10, False, False, False],82            [False, False, 15, False, False, 15, False, 15, 15, False, False],83            [False, False, False, False, 25, False, False, 25, 25, 25, 25],84            [False, 10, 15, False, False, 10, 10, 10, 12, False, False],85            [False, 10, False, False, False, 10, 10, 10, 12, False, False],86            [False, 10, 15, False, 25, 10, 10, 10, 12, 21, 21],87        ]88        self._verify_matrix(89            expected_meet=expected_meet,90            expected_min=expected_min,91            requirements=[92                None,93                10,94                15,95                25,96                IntRange(min=10, max=15),97                IntRange(min=10, max=15, max_inclusive=False),98                [IntRange(min=10, max=15), IntRange(min=20, max=80)],99            ],100            capabilities=[101                None,102                10,103                15,104                18,105                25,106                IntRange(min=10, max=15),107                IntRange(min=10, max=15, max_inclusive=False),108                [IntRange(min=10, max=15), IntRange(min=20, max=80)],109                [IntRange(min=12, max=30)],110                [IntRange(min=21, max=25)],111                IntRange(min=21, max=25),112            ],113        )114    def test_supported_set_space(self) -> None:115        set_aa = set(["aa"])116        set_aa_bb = set(["aa", "bb"])117        set_aa_bb_cc = set(["aa", "bb", "cc"])118        set_aa_cc = set(["aa", "cc"])119        set_cc = set(["cc"])120        self._verify_matrix(121            expected_meet=[122                [True, True, True, True, True],123                [True, True, True, True, True],124                [False, False, False, True, False],125                [True, False, True, False, False],126                [True, False, True, False, False],127            ],128            expected_min=[129                [None, None, None, None, None],130                [None, None, None, None, None],131                [False, False, False, set_aa_bb, False],132                [None, False, None, False, False],133                [None, False, None, False, False],134            ],135            requirements=[136                SetSpace[str](is_allow_set=True),137                SetSpace[str](is_allow_set=False),138                SetSpace[str](items=set_aa_bb, is_allow_set=True),139                SetSpace[str](items=set_aa_bb),140                SetSpace[str](items=set_aa_bb, is_allow_set=False),141            ],142            capabilities=[143                SetSpace[str](is_allow_set=True),144                SetSpace[str](items=set_aa, is_allow_set=True),145                SetSpace[str](items=set_cc, is_allow_set=True),146                SetSpace[str](items=set_aa_bb_cc, is_allow_set=True),147                SetSpace[str](items=set_aa_cc, is_allow_set=True),148            ],149        )150    def test_generate_min_capability_not_supported(self) -> None:151        requirement = IntRange(min=5)152        capability = IntRange(max=4)153        with self.assertRaises(expected_exception=LisaException) as cm:154            requirement.generate_min_capability(capability)155        self.assertIn("doesn't support", str(cm.exception))156    def test_int_range_validation(self) -> None:157        with self.assertRaises(expected_exception=LisaException) as cm:158            IntRange(min=6, max=4)159        self.assertIn("shouldn't be greater than", str(cm.exception))160        # no exception161        IntRange(min=5, max=5)162        with self.assertRaises(expected_exception=LisaException) as cm:163            IntRange(min=5, max=5, max_inclusive=False)164        self.assertIn("shouldn't be equal to", str(cm.exception))165    def _verify_matrix(166        self,167        expected_meet: List[List[bool]],168        expected_min: List[List[Any]],169        requirements: List[T],170        capabilities: List[T],171    ) -> None:172        for r_index, requirement in enumerate(requirements):173            for c_index, capability in enumerate(capabilities):174                extra_msg = (175                    f"index: [{r_index},{c_index}], "176                    f"requirement: {requirement}, capability: {capability}"177                )178                if isinstance(requirement, RequirementMixin):179                    self._assert_check(...description.py
Source:description.py  
...103        """104        if value is None:105            return106        107        self._matrix = self._verify_matrix(value)108        self._matrix_cumsum = cumsum(self._matrix, axis=1)109    def _verify_matrix(self, value: list[list[float]] | ndarray) -> ndarray:110        """returns verified copy of the matrix111        112        values are normalized so the sum of every row is equal to 1.0113        raises ValueError 114        """115        try:116            value = array(value, dtype=float32)117            if value.shape != self.shape:118                raise ValueError('Matrix dimension should be equal to the original dimension')119            120            value /= value.sum(1)[:, newaxis]121            if not allclose(value.sum(1), 1.0):122                raise ValueError('Matrix should be a right-stochastic matrix')123            ...gauss.py
Source:gauss.py  
1from helpers import _print_highlighted_matrix2import numpy as np3import numpy.typing as npt4def _verify_matrix(M: npt.ArrayLike) -> bool:5    n: int = M.shape[0]6    return all([M[i][i] != 0 for i in range(n)])7        8def _pivot_matrix(M: npt.ArrayLike, row: int, col: int) -> npt.ArrayLike:9    M[[row, col]] = M[[col, row]]10    return M11def gaussian_elim(A: npt.ArrayLike, b: npt.ArrayLike, verbose: bool=False) -> npt.ArrayLike:12    # recast arrays as floats for 13    # potential decimal calculations 14    # ahead15    A = A.astype('float64')16    b = b.astype('float64')17    18    # concatenate arrays to create19    # an augmented matrix20    aug_A = np.insert(A, len(A), b, axis=1)21    if verbose:22        print(aug_A)23        24    # extract num rows and num cols25    rows: int = aug_A.shape[0]26    cols: int = aug_A.shape[1]27    28    # check that our matrix is valid29    while not _verify_matrix(aug_A):30        # swap rows to achieve valid matrix31        for i in range(rows):32            if aug_A[i][i] == 0:33                print(f"0 pivot element found. Swapping rows {i} and {i-1}")34                aug_A = _pivot_matrix(aug_A, i, i-1)35            36    37    # for each column in the matrix (minus solution set)38    for i in range(cols-1):39        # iterate over each row40        # but only need to start at row below triangle41        # we are creating... i.e. full_col + 142        for j in range(i+1,rows):43            ...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!!
