Python - Pytest Typeerror

Asked

Viewed 66 times

-1

I am assembling the pytest of the function calculate from the class Sum of a calculator, but as the function calculate receives an "object" as argument, it is giving error. Follows the codes.

Inputs:

def main():
    operation = input('''
Por favor, selecione a opção que gostaria de realizar:
SOMA para adição
SUBTRAÇÃO para subtração
''')

    number_1 = int(input('Por favor, inserir o primeiro número: '))
    number_2 = int(input('Por favor, inserir o segundo número: '))

    parametros = ParametrosCalculo(operation, number_1, number_2)
    resultado = Calculadora().preparar(parametros).calcular()

    print(f'O seu resultado é: {resultado}')

if __name__ == '__main__':
    main()
class ParametrosCalculo(object):
    def __init__(self, operacao, p1, p2):
        self.operacao = operacao
        self.parametro1 = p1
        self.parametro2 = p2

class Operacao(object):
    def __init__(self, p1, p2):
        self.p1 = p1
        self.p2 = p2

    def calcular(self):
        raise Exception('Operacao não é!')

class Soma(Operacao):
    def calcular(self):
        return self.p1 + self.p2

class Subtracao(Operacao):
    def calcular(self):
        return self.p1 - self.p2

class FabricaOperacoes(object):
    @staticmethod
    def criar(parametros_operacao: ParametrosCalculo):
        if parametros_operacao.operacao == 'soma':
            return Soma(parametros_operacao.parametro1, parametros_operacao.parametro2)
        if parametros_operacao.operacao == 'subtracao':
            return Subtracao(parametros_operacao.parametro1, parametros_operacao.parametro2)

        raise Exception('Operacao inválida!')

class Calculadora(object):
    def __init__(self):
        self.preparada = False

    def preparar(self, parametros_calculo):
        self.parametros_calculo = parametros_calculo
        self.operacao = FabricaOperacoes.criar(parametros_calculo)
        self.preparada = True

        return self

    def calcular(self):
        return self.operacao.calcular()

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

0

Your calculating function does not take two arguments. If I am understanding your right code, you have to create an object Operacao, and pass this object to the construction of the object Soma.

When building the object Soma, call calculates: soma.calcular()

I don’t know what your goal is with this code. But it seems to me very complicated. It can be simplified.

Instead of having classes like Soma, Subtracao, etc. These classes can be methods of the class Operacao, and the methods Soma, Subtracao, etc could receive the parameters for the operation. The way you are doing, Voce has to create an object operacao and soma every time Voce wants to do an operation, and Voce will have a stack overflow if it does many of these operations.

  • Yes, to test the calculation() I need to create the object that comes from the Operation class, but I’m having trouble with the code... I have to create the object within the test function?

  • I think it would be Soma(Operacao).calcular()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.