teachcompute.ext_test_case

Various helpers to help develop the package.

ExtTestCase

class teachcompute.ext_test_case.ExtTestCase(methodName='runTest')[source][source]
assertAlmostEqual(expected: ndarray, value: ndarray, atol: float = 0, rtol: float = 0)[source][source]

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is more than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

If the two objects compare equal then they will automatically compare almost equal.

assertIns(sub: Tuple[Any, ...], s: str)[source][source]

Checks that one of the substrings in sub is part of s.

assertNotAlmostEqual(expected: ndarray, value: ndarray, atol: float = 0, rtol: float = 0)[source][source]

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is less than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

Objects that are equal automatically fail.

capture(fct: Callable)[source][source]

Runs a function and capture standard output and error.

Paramètres:

fct – function to run

Renvoie:

result of fct, output, error

classmethod tearDownClass()[source][source]

Hook method for deconstructing the class fixture after running all tests in the class.

tryCall(fct: Callable, msg: str | None = None, none_if: str | None = None) Any | None[source][source]

Calls the function, catch any error.

Paramètres:
  • fct – function to call

  • msg – error message to display if failing

  • none_if – returns None if this substring is found in the error message

Renvoie:

output of fct

ignore_warnings

teachcompute.ext_test_case.ignore_warnings(warns: List[Warning]) Callable[source][source]

Catches warnings.

Paramètres:

warns – warnings to ignore

measure_time

teachcompute.ext_test_case.measure_time(stmt: str | Callable, context: Dict[str, Any] | None = None, repeat: int = 10, number: int = 50, warmup: int = 1, div_by_number: bool = True, max_time: float | None = None) Dict[str, str | int | float][source][source]

Measures a statement and returns the results as a dictionary.

Paramètres:
  • stmt – string or callable

  • context – variable to know in a dictionary

  • repeat – average over repeat experiment

  • number – number of executions in one row

  • warmup – number of iteration to do before starting the real measurement

  • div_by_number – divide by the number of executions

  • max_time – execute the statement until the total goes beyond this time (approximatively), repeat is ignored, div_by_number must be set to True

Renvoie:

dictionary

<<<

from pprint import pprint
from math import cos
from teachcompute.ext_test_case import measure_time

res = measure_time(lambda: cos(0.5))
pprint(res)

>>>

    {'average': 1.1280000001079315e-07,
     'context_size': 64,
     'deviation': 3.2496154767962686e-09,
     'max_exec': 1.2200000014672697e-07,
     'min_exec': 1.099999997222767e-07,
     'number': 50,
     'repeat': 10,
     'ttime': 1.1280000001079315e-06,
     'warmup_time': 1.3899999999011925e-05}

See Timer.repeat for a better understanding of parameter repeat and number. The function returns a duration corresponding to number times the execution of the main statement.

measure_time_dim

teachcompute.ext_test_case.measure_time_dim(stmt, contexts, repeat=10, number=50, div_by_number=True, verbose=0)[source][source]

Measures a statement multiple time with function measure_time_dim().

Paramètres:
  • stmt – string

  • contexts – variable to know in a dictionary, every context must include field “x_name”, which is copied in the result

  • repeat – average over repeat experiment

  • number – number of executions in one row

  • div_by_number – divide by the number of executions

  • verbose – if > 0, use tqdm to display progress

Renvoie:

yield dictionary

<<<

import pprint
import numpy
from teachcompute.ext_test_case import measure_time_dim

res = list(
    measure_time_dim(
        "cos(x)",
        contexts=[
            dict(cos=numpy.cos, x=numpy.arange(10), x_name=10),
            dict(cos=numpy.cos, x=numpy.arange(100), x_name=100),
        ],
    )
)
pprint.pprint(res)

>>>

    [{'average': 2.4791999999251857e-06,
      'context_size': 232,
      'deviation': 5.737959219052686e-07,
      'max_exec': 3.729999999677602e-06,
      'min_exec': 1.6119999997954436e-06,
      'number': 50,
      'repeat': 10,
      'ttime': 2.4791999999251856e-05,
      'warmup_time': 4.410000002508241e-05,
      'x_name': 10},
     {'average': 5.61699999980192e-06,
      'context_size': 232,
      'deviation': 1.4388929774562115e-06,
      'max_exec': 9.524000000737943e-06,
      'min_exec': 4.132000000254265e-06,
      'number': 50,
      'repeat': 10,
      'ttime': 5.61699999980192e-05,
      'warmup_time': 2.9500000039206498e-05,
      'x_name': 100}]

See Timer.repeat for a better understanding of parameter repeat and number. The function returns a duration corresponding to number times the execution of the main statement.