From e77600638da54b4e4a5ab3bdbeef64d8f8085c6c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 25 Aug 2020 15:47:06 +0200 Subject: [PATCH] Travis CI: Identify our ten slowest pytests (#2350) * Travis CI: Identify our ten slowest tests https://howchoo.com/g/mtblodnjzjc/how-to-measure-unit-test-execution-times-in-pytest helps us to find the individual tests that are slowing down our Travis CI checks. * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- .travis.yml | 2 +- matrix/matrix_operation.py | 40 ++++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index 21103c357..a03f8161f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,6 @@ before_script: - pip install -r requirements.txt # fast fail on black, flake8, validate_filenames script: - mypy --ignore-missing-imports . - - pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=. . + - pytest --doctest-modules --durations=10 --cov-report=term-missing:skip-covered --cov=. . after_success: - scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py index b5a724ad6..42a94da12 100644 --- a/matrix/matrix_operation.py +++ b/matrix/matrix_operation.py @@ -27,9 +27,11 @@ def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]: >>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]]) [[-1, -0.5], [-1, -1.5]] """ - if _check_not_integer(matrix_a)\ - and _check_not_integer(matrix_b)\ - and _verify_matrix_sizes(matrix_a, matrix_b): + if ( + _check_not_integer(matrix_a) + and _check_not_integer(matrix_b) + and _verify_matrix_sizes(matrix_a, matrix_b) + ): return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)] @@ -60,8 +62,9 @@ def multiply(matrix_a: List[list], matrix_b: List[list]) -> List[list]: f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) " f"and ({rows[1]},{cols[1]})" ) - return [[sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)] - for i in matrix_a] + return [ + [sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)] for i in matrix_a + ] def identity(n: int) -> List[list]: @@ -95,8 +98,8 @@ def minor(matrix: List[list], row: int, column: int) -> List[list]: >>> minor([[1, 2], [3, 4]], 1, 1) [[1]] """ - minor = matrix[: row] + matrix[row + 1:] - return [row[:column] + row[column + 1:] for row in minor] + minor = matrix[:row] + matrix[row + 1 :] + return [row[:column] + row[column + 1 :] for row in minor] def determinant(matrix: List[list]) -> int: @@ -109,8 +112,10 @@ def determinant(matrix: List[list]) -> int: if len(matrix) == 1: return matrix[0][0] - return sum(x * determinant(minor(matrix, 0, i)) * (-1) ** i - for i, x in enumerate(matrix[0])) + return sum( + x * determinant(minor(matrix, 0, i)) * (-1) ** i + for i, x in enumerate(matrix[0]) + ) def inverse(matrix: List[list]) -> List[list]: @@ -124,9 +129,10 @@ def inverse(matrix: List[list]) -> List[list]: if det == 0: return None - matrix_minor = [[determinant(minor(matrix, i, j)) - for j in range(len(matrix))] - for i in range(len(matrix))] + matrix_minor = [ + [determinant(minor(matrix, i, j)) for j in range(len(matrix))] + for i in range(len(matrix)) + ] cofactors = [ [x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])] @@ -159,14 +165,10 @@ def _verify_matrix_sizes(matrix_a: List[list], matrix_b: List[list]) -> Tuple[li def main(): matrix_a = [[12, 10], [3, 9]] matrix_b = [[3, 4], [7, 4]] - matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], - [31, 32, 33, 34], [41, 42, 43, 44]] + matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]] matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] - print( - f"Add Operation, {add(matrix_a, matrix_b) = } \n") - print( - f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n", - ) + print(f"Add Operation, {add(matrix_a, matrix_b) = } \n") + print(f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",) print(f"Identity: {identity(5)}\n") print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n") print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n")