Python Losing Class Reference

Asked

Viewed 53 times

0

I need help with a class reference problem. My class is losing the reference in the middle of a While and I’m not getting to figure out why. Gives the following message:

Erroinstance '' has been Deleted, or its Row is otherwise not present.

I will post the code. In summary it reads a file and depending on the record type record it may have 1 or more records. The fact is that when leaving an IF and entering the next one the class that was instantiated loses the reference. I imagine it’s not something complex, but I’m not able to find the point. In the code is the comment #Here comes the error to inform the place where it is not working. If someone can give me a force I thank. Follow the code, thank you:

def importFile(self, url, file):  
    try:            
        url = url+"/"+file            
        fileImport = open(url, 'r')
        lines = fileImport.readlines()            
        fileImport.close()         
        banco = Banco()

        i = 0        

        while i < len(lines):                                                                                                
            line = lines[i].replace("\n","").split("|") # ler a linha do fileOriginal, retira os \n e troca , por .

            if line[0] == '00000':
                id_02000 = 0
                id_03110 = 0                
                id_03111 = 0
                id_03112 = 0
                id_03120 = 0
                id_03121 = 0
                id_03122 = 0
                id_03130 = 0
                id_03131 = 0
                id_03132 = 0
                id_04000 = 0
                pgdasd = Pgdasd()
                pgdasd = pgdasd.setPgdasd_00000(line)                          
                id_declaracao = pgdasd.PGDASD_00000_ID_DECLARACAO                    
                banco.insere(pgdasd, i+1)               

            if line[0] == '03000':#Pode repetir                                
                pgdasd_03000 = Pgdasd_03000()
                pgdasd_03000.setPgdasd_03000(line, id_declaracao)                    

                banco.insere(pgdasd_03000, i+1)

            if line[0] == '03100':#Se repete
                pgdasd_03100 = Pgdasd_03100() #Essa classe é usada no próximo IF
                pgdasd_03100.setPgdasd_03100(line, id_declaracao, pgdasd_03000)                    

                banco.insere(pgdasd_03100, i+1)

            if line[0] == '03110':#Se repete
                id_03110 = id_03110+1
                pgdasd_03110 = Pgdasd_03110()
                pgdasd_03110.setPgdasd_03110(line, id_declaracao, pgdasd_03000, pgdasd_03100, id_03110) #Aqui dá o erro

                banco.insere(pgdasd_03110, i+1)                

            i = i + 1  
        banco.fechaSessao()
        print("Arquivo "+url+" importado com sucesso")             
    except Exception as e:                 
        exit()

Classes:

Base = declarative_base()

class Pgdasd_03100(Base):
__tablename__  = 'pgdasd_03100'
__table_args__ = SCHEMA

PGDASD_00000_ID_DECLARACAO   = Column(String, primary_key = True)
PGDASD_03000_CNPJ            = Column(String, primary_key = True)    
PGDASD_03100_TIPO            = Column(String(2), primary_key = True)
PGDASD_03100_VLTOTAL         = Column(Numeric(14,2))


def setPgdasd_03100(self, line, id_declaracao, pgdasd_03000):                       
    self.PGDASD_00000_ID_DECLARACAO = id_declaracao
    self.PGDASD_03000_CNPJ          = pgdasd_03000.PGDASD_03000_CNPJ
    self.PGDASD_03100_TIPO          = line[1]
    self.PGDASD_03100_VLTOTAL       = line[2]

    return self

class Pgdasd_03110(Base):
__tablename__  = 'pgdasd_03110'    
__table_args__ = SCHEMA    

PGDASD_00000_ID_DECLARACAO   = Column(String, primary_key = True)
PGDASD_03000_CNPJ            = Column(String, primary_key = True)    
PGDASD_03100_TIPO            = Column(String, primary_key = True)
PGDASD_03110_ID              = Column(Integer, primary_key = True)
PGDASD_03110_UF              = Column(String(2))
PGDASD_03110_COD_TOM         = Column(String(4))
PGDASD_03110_VALOR           = Column(Numeric(14,2))    


def setPgdasd_03110(self, line, id_declaracao, pgdasd_03000, pgdasd_03100, id_03110):

    self.PGDASD_00000_ID_DECLARACAO   = id_declaracao
    self.PGDASD_03000_CNPJ            = pgdasd_03000.PGDASD_03000_CNPJ 
    self.PGDASD_03100_TIPO            = pgdasd_03100.PGDASD_03100_TIPO

    self.PGDASD_03110_ID              = id_03110            
    self.PGDASD_03110_UF              = line[1]
    self.PGDASD_03110_COD_TOM         = line[2]
    self.PGDASD_03110_VALOR           = line[3]        

    return self
  • Where do these classes come from? some orm? da para vc criar um minimum, complete and verifiable example? Eventually the simple attempt to create an example of this, solves the problem :-)

  • Yes, it is an ORM (sqlalchemy). I will post 2 example classes to see if it helps in understanding. Ah, and thank you for the idea of creating a minimum viable example.

  • Note that the interesting thing would be that the example not only reproduces the problem but can also be executed by those who want to help.

  • In this case, Sidon. It’s a little complicated because the code is very big, it involves a lot of things. I posted the part that is giving problem, because maybe I am wrong in something that is not so complex, maybe only matter of logic.

  • But it is possible to 'compress' the context, by the way, this simple exercise will make you discover what is the problem that, by the error msg, very likely is related to the classes of the ORM, rows deleted? See if that link helping. :-)

No answers

Browser other questions tagged

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