Define function or method result return [closed]

1

Editing: A previous function generates a list like this:

['Responsável atual', 'Número do processo', 'Foro de tramitação', 'Data da intimação 05/10/2017', 'Data da ação 15/08/2011']

This is the previous list in "Data". Data is my final report. It contains hundreds of processes listed. I get one by one of these processes, access and ADD the information from "Parties". That is, I INCREASE this list with complementary information, which are, for example:

['Exequente', 'União', '(CNPJ do exequente)', 'Executado', '(CNPJ fictício) 00.345.123/0001-00', 'Nome da empresa executada']

My role PERCORREES this list, MEETS the target CNPJ (or CPF) and TREAT this information to determine who will be the new responsible. So he has several elif, as he considers number 3 (just before the bar). If there is 0, 9 or 8 it IGNOR and picks up the previous one (in this case, 2). If there is 0, 9, or 8 ignore and pick up the previous one and so on, so it's a bit confusing to determine who is responsible. In the end, when I have determined who is responsible according to this criterion, I need to add all of this new information to that list in the previous list (ie add "Parts" + "distribution" to "Data") to complete the report ...

#############################

I have a function:

def PegaDadosPartes(dados):
      distribuicaoPara = VerificaDist(partes)
      dados.append(str(distribuicaoPara) + " @")
      for x in range(len(partes)):
          dados.append(partes[x] + '@')
      del partes[0:len(partes)]
      return dados

It receives a dataset (list 'parts') from another function, and feeds another list ('data') with this information, BEING the variable 'distribution' calls the function 'CheckDist (parts)' to get their value.

This value depends on a series of treatments. Among them, check if there are CNPJ's or CPF's in 'parts', and treat differently case by case, like this:

def VerificaDist(partes):
      posicao = []
      temCnpj = 0
      for x in range(len(partes)):
            verificaOutroCnpj = re.search("\d{2}.\d{3}.\d{3}/\d{4}-\d{2}", partes[x]) 
            if verificaOutroCnpj:
                  posicao.append("CNPJ " + verificaOutroCnpj.group())
                  temCnpj = temCnpj + 1
            verificaCpf = re.search("\d{3}.\d{3}.\d{3}-\d{2}", partes[x]) #Se não achou CNPJ, busca CPF.
            if verificaCpf:
                  posicao.append("CPF " + verificaCpf.group())
      if temCnpj > 0:
            Caso1(posicao) #Primeiro caso: consta CNPJ.
      else:
            Caso2(posicao) #Segundo caso: consta CPF.

In both cases, the treatment functions will "slice" the variable because the treatment criteria are different. In the CNPJ the digit before the "/" determines the person in charge. In the CPF, the eighth digit defines this. The final function does this:

def TrataDistribuicao(alvos): #("Alvos" são as partes "fatiadas" da informação)
      analisa = open('C:\Regras.txt', 'r') #É o arquivo que contém as regras de designação de responsável, tipo "Se o dígito é 8 responsável fulano, se 7 cicrano" e assim por diante...
      regrasDeDistribuicao = analisa.readlines()
      for x in range(len(regrasDeDistribuicao)):
            regrasDeDistribuicao[x] = regrasDeDistribuicao[x].split(',')
      for x in range(len(regrasDeDistribuicao)):
            if alvos[0] == regrasDeDistribuicao[x][0]:
                  distribuicaoPara = str("Distribuir para " + regrasDeDistribuicao[x][1] )
            elif alvos[1] == regrasDeDistribuicao[x][0]:
                  distribuicaoPara = str("Distribuir para " + regrasDeDistribuicao[x][1])
            elif alvos[2] == regrasDeDistribuicao[x][0]:
                  distribuicaoPara = str("Distribuir para " + regrasDeDistribuicao[x][1] )
            elif alvos[3] == regrasDeDistribuicao[x][0]:
                  distribuicaoPara = str("Distribuir para " + regrasDeDistribuicao[x][1])
analisa.close()

It happens that, at the end of all this, I'm not able to "set" the variable "distributionTo". How to do?

    
asked by anonymous 13.10.2017 / 20:09

1 answer

0

I added a return distribuicaoPara to each subsequent function, predicting all possible variable definition, and the value was finally returned! Thank you all.

I also changed the loop. It looks like this:

def TrataDistribuicao(alvos):
analisa = open('C:\Regras.txt', 'r')
regrasDeDistribuicao = analisa.readlines()
for x in range(len(regrasDeDistribuicao)):
    regrasDeDistribuicao[x] = regrasDeDistribuicao[x].split(',')

resultado = None #Acrescentei essa variável para servir de critério pra saída do laço.
for x in range(len(alvos)):
    for y in range(len(regrasDeDistribuicao)):
        if alvos[x] == regrasDeDistribuicao[y][0] and resultado == None:
            resultado = ("Distribuir para " + regrasDeDistribuicao[y][1] + " considerado " + dados[0])

if resultado == None:
    distribuicaoPara = "CNPJ ou CPF não coincidente com nenhuma situação previamente prevista. Verificação manual."
    return distribuicaoPara
else:
    distribuicaoPara = resultado
    return distribuicaoPara
analisa.close()
    
13.10.2017 / 22:38