vendredi 17 juin 2016

Object doesn't load attributes the second time its called but does the first time

I have a object that when initialized with an id (oznaka), will read from a file and load all the atributes like duzina, sirina... using the citaj2 method. The daj_sve just prints all the attributes in a needed way.

class Paket(object):

    def __init__(self, oznaka):
        a = open("paketi.txt","r").read().split("n")
        k = len(a)
        for i in range(k):
            s = a[i].split("|")
            if s[0] == str(oznaka):
                self.oznaka = oznaka
                self.duzina = citaj2(oznaka,1,"paketi.txt")
                self.sirina = citaj2(oznaka,2,"paketi.txt")
                self.visina = citaj2(oznaka,3,"paketi.txt")
                self.opis = citaj2(oznaka,5,"paketi.txt")
                self.tezina = citaj2(oznaka,4, "paketi.txt")
                self.relacija = citaj2(oznaka,7, "paketi.txt")
                self.status = citaj2(oznaka,8,"paketi.txt")

    def izmeni_status(self, status):
        self.status = status
        izmena("paketi.txt",self.oznaka,7,str(status))

    def daj_sve(self):
        print("Opis paketa: " + self.opis + "n" + "Karakteristike: " + "n"
            + "Visina: " + self.visina + "   " + "Duzina: " + self.duzina + "n"
            + "Sirina: " + self.sirina + "   " + "Tezina: "+ self.tezina + "n"
            + "Odrediste: " + self.relacija)

The citaj2 definition: (oznaka = id, sta_oces = the spot I need in a line)

def citaj2(oznaka,sta_oces,fajl):
    with open(fajl, 'r') as f:
        a = f.readlines()
        for i in a:
            x = i.split("|")
            if x[0] == oznaka:
                return(x[sta_oces])

Now all that is used in below, where zahtevi contains ID's I need to print using the daj_sve method.

for i in zahtevi:
    print(i)
    paket = Paket(str(i))
    paket.daj_sve()

So now I get this error on the second i in zahtevi that he tries to print, and he says that the he doesn't have an attribute but on the previous insturction he created that object with that attribute.

Output when run:

1233                                <-- id or i
Opis paketa: neki tamo ku*ac        <--This is the correct print
Karakteristike:
Visina: 4   Duzina: 2
Sirina: 3   Tezina: 23
Odrediste: neutovaren
1334                                <-- new id that has the same atributes
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from menu import pocetak
  File "C:UsersWhal4sDesktoptim6asdtim6menu.py", line 165, in     <module>
    zahtevi_za_transport()
  File "C:UsersWhal4sDesktoptim6asdtim6menu.py", line 161, in     zahtevi_za_transport
    paket.daj_sve()
  File "C:UsersWhal4sDesktoptim6asdtim6Klase.py", line 73, in daj_sve
    print("Opis paketa: " + self.opis + "n" + "Karakteristike: " + "n" +     "Visina: " + self.visina + "   " + "Duzina: " + self.duzina + "n" + "Sirina: "     + self.sirina + "   " + "Tezina: "+ self.tezina + "n" + "Odrediste: " +     self.relacija)
AttributeError: 'Paket' object has no attribute 'opis'

I did test the citaj2 method on the other object id and it works. I mean the object creates itself but it says it has no attribute. I can understand that, but it clearly works the first time and the second time it does not, and they have the same attributes except id.

Aucun commentaire:

Enregistrer un commentaire