Conversation
| if arity <= 0: | ||
| raise MyError("Арность не может быть меньше или равна 0.") |
There was a problem hiding this comment.
А чем вам не угодила нулевая арность?
| for el in (a, b, c): | ||
| func = func(el) | ||
| assert func == a + b + c |
There was a problem hiding this comment.
А почему не просто func(a)(b)(c)?
| try: | ||
|
|
||
| def summ(a, b, c): | ||
| return a + b + c | ||
|
|
||
| curry(summ, -1) | ||
| assert False | ||
| except MyError as e: | ||
| assert isinstance(e, MyError) | ||
| assert str(e) == "Арность не может быть меньше или равна 0." |
There was a problem hiding this comment.
Для таких вещей есть pytest.raises: https://docs.pytest.org/en/7.1.x/how-to/assert.html#assertraises
There was a problem hiding this comment.
Я не до конца уловил вашу идею про универсальный тест. Вы могли бы сделать функцию, которая на случайных входах проверяет, что каррированная функция отрабатывает так же, как и изначальная. Проблема тут в типах, потому что генерировать случайные числа просто, а вот что-то более сложное -- надо постараться
import random
def fully_apply(f, args):
res = f
for arg in args:
res = res(arg)
return res
def generate_int_input(func):
[random.randint(1, 10**6) for _ in range(func.__code__.co_argcount)]
def curried_equiv(func):
func_c = curry(func, func.__code__.co_argcount)
input = generate_int_input(func)
assert fully_apply(func_c, input) == func(*input)
def test_summ():
def summ(a, b, c):
return a + b + c
curried_equiv(summ)Но функции должны работать только с int-ми (возвращать могут, впрочем, любой тип, главное, чтобы можно было два элемента сравнивать), и придумывать их вы должны тоже сами
Completed curry task and created tests for it. Created two functions: curry and uncurry. Raised some exceptions connected with arity and arguments. Tested all the exceptions raises, also tested how my functions work with other different functions. I had the idea to create one universal test, that has different quantity of arguments every time and puts them into for e.g. in summ function... and in the end checks the result... But I had some problems with arity, 'cause i couldn't check the number of argumets that summ function requires.. Could you tell me please, whether it is possible to create such a universal test?
P.S. I hope you'll accept the way I tryed to solve the curry task, because I think that my version of solving it is not the most beautiful and correct.. But I tryed:)