keker123

Untitled

May 6th, 2023 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. import click
  2. from math import sqrt
  3. from functools import wraps
  4. from random import randint
  5.  
  6.  
  7. def retry_on_exception(attempts = 1):
  8.     def decorator(func):
  9.         @wraps(func)
  10.         def wrapper(*args, **kwargs):
  11.             for i in range(attempts):
  12.                 try:
  13.                     result = func(*args, **kwargs)
  14.                 except Exception as e:
  15.                     if i == attempts - 1:
  16.                         raise e
  17.                     else:
  18.                         continue
  19.                 else:
  20.                     return result
  21.         return wrapper
  22.     return decorator
  23.  
  24.  
  25. @retry_on_exception(3)
  26. def test_random_even():
  27.     num = randint(1, 10)
  28.     assert num % 2 == 0
  29.  
  30.  
  31. class EquationSolver:
  32.     @staticmethod
  33.     def solve_square(a, b, c):
  34.         D = b ** 2 - 4 * a * c
  35.         if D < 0:
  36.             return
  37.         elif D == 0:
  38.             yield -b / (2 * a)
  39.         else:
  40.             yield (-b + sqrt(D)) / (2 * a)
  41.             yield (-b - sqrt(D)) / (2 * a)
  42.  
  43.     @staticmethod
  44.     def solve_linear(a, b, c, d):
  45.         if a - c == 0:
  46.             return
  47.         yield (d - b) / (a - c)
  48.  
  49.  
  50. def test_linear_D_zero():
  51.     assert list(EquationSolver.solve_linear(1, 2, 2, 0)) == [2]
  52.  
  53.  
  54. def test_linear_normal():
  55.     assert list(EquationSolver.solve_linear(1, 2, 3, 4)) == [-1]
  56.  
  57.  
  58. def test_linear_zero():
  59.     assert list(EquationSolver.solve_linear(1, 2, 1, 4)) == []
  60.  
  61.  
  62. def test_linear_infinite():
  63.     assert list(EquationSolver.solve_linear(1, 2, 1, 2)) == []
  64.  
  65.  
  66. def test_square_normal():
  67.     assert list(EquationSolver.solve_square(1, 2, 1)) == [-1]
  68.     assert list(EquationSolver.solve_square(1, 2, -3)) == [1, -3]
  69.  
  70.  
  71. def test_square_complex():
  72.     assert list(EquationSolver.solve_square(1, 2, 3)) == []
  73.  
  74.  
  75. def test_square_no_B_C():
  76.     assert list(EquationSolver.solve_square(1, 0, 0)) == [0]
  77.  
  78.  
  79. def testAll():
  80.     print("Testing linear")
  81.     test_linear_D_zero()
  82.     test_linear_normal()
  83.     test_linear_zero()
  84.     test_linear_infinite()
  85.     print("Testing square")
  86.     test_square_normal()
  87.     test_square_complex()
  88.     test_square_no_B_C()
  89.     print("Test retry on exception")
  90.     test_random_even()
  91.     print("All tests passed")
  92.  
  93.  
  94. """
  95. 1. linear: читает параметры a, b, c, d из консоли и выводит результат работы solve_linear
  96. 2. square: читает параметры a, b, c из консоли и выводит результат работы solve_square
  97. 3. tests: запускает тесты
  98. """
  99.  
  100.  
  101. @click.group()
  102. def cli():
  103.     pass
  104.  
  105.  
  106. @cli.command()
  107. def linear():
  108.     a, b, c, d = map(float, input().split())
  109.     solves = EquationSolver.solve_linear(a, b, c, d)
  110.     try:
  111.         first = next(solves)
  112.         print("x = {}".format(first))
  113.     except StopIteration:
  114.         print("No solutions")
  115.     else:
  116.         for x in solves:
  117.             print("x = {}".format(x))
  118.  
  119.  
  120. @cli.command()
  121. def square():
  122.     a, b, c = map(float, input().split())
  123.     solves = EquationSolver.solve_square(a, b, c)
  124.     try:
  125.         first = next(solves)
  126.         print("x = {}".format(first))
  127.     except StopIteration:
  128.         print("No solutions")
  129.     else:
  130.         for x in solves:
  131.             print("x = {}".format(x))
  132.  
  133.  
  134. @cli.command()
  135. def tests():
  136.     print("Running tests...")
  137.     testAll()
  138.     print("Tests passed")
  139.  
  140.  
  141. if __name__ == '__main__':
  142.     test_random_even()
  143.     cli()
  144.  
Advertisement
Add Comment
Please, Sign In to add comment