Bottom Page

Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Empty attribute class dictionary after saving it in a class object dictionary
#1
I will try to explain what problem I have:

I have an attribute class dictionary called "diccpuntscontrol" which is annoying me because for some unknown reason it's not working properly.

Basically there is a problem when recovering the data of the whole class which previously has been saved on the dictionary "Dicc_Curses".

class Cursa:
    Dicc_Curses = {}
    def __init__ (self, nomcursa, nummaxparticipants, numpuntscontrol, estatcursa = "creada", diccpuntscontrol = None, diccparticipants = None):
        self.nomcursa = nomcursa
        self.nummaxparticipants = nummaxparticipants
        self.numpuntscontrol = numpuntscontrol
        self.estatcursa = estatcursa
        if diccpuntscontrol is None:
            self.diccpuntscontrol = {}
        else:
            self.diccpuntscontrol = diccpuntscontrol
        if diccparticipants is None:
            self.diccparticipants = {}
        else:
            self.diccparticipants = diccparticipants

    def guardar_cursa (nomcursa, cursa):
        Cursa.Dicc_Curses[nomcursa] = cursa

    def get_cursa (nomcursa):
        return Cursa.Dicc_Curses[nomcursa]

    def print (self):
        print (self.diccpuntscontrol)

# How I save the race, of course this belongs to another part of the code, just copying here if it helps
c = Cursa (nomcursa, nummaxcorredors, 2, "creada", ppc_dicc)
# ppc_dicc is the dictionary that should become diccpuntscontrol and it is NOT empty
Cursa.guardar_cursa (nomcursa, c)

# How do I restore the data of the race, of course this belongs to another part of the code, just copying here if it helps
c = Cursa.get_cursa (nomcursa)
c.print()
# c.print is here for testing purposes only
Output:
{}
The saving function "guardar_cursa" saves the object "cursa" and the name of the race "nomcursa" in "Dicc_Curses".

The recovering function "get_cursa" searches Dicc_Curses and let's you get the data of the race. All of the class variables except "diccpuntscontrol" show properly but for some unknown reason "diccpuntscontrol" is always {} when it shouldn't be. Wall

If it helps also, I made a testing code to see whether the problems gets fixed somehow and surprisingly here works properly:

class Cursa:
    Dicc_Curses = {}
    def __init__ (self, nomcursa, diccpuntscontrol = None):
        self.nomcursa = nomcursa
        if diccpuntscontrol is None:
            self.diccpuntscontrol = {}
        else:
            self.diccpuntscontrol = diccpuntscontrol
    
    def guardar_cursa (nomcursa, cursa):
        Cursa.Dicc_Curses[nomcursa] = cursa
        
    def get_cursa (nomcursa):
        return Cursa.Dicc_Curses[nomcursa]

    def print (self):
        print (self.diccpuntscontrol)


c = Cursa ("nomcursa", {'S': {}, 'A': {}})
c.print()
Cursa.guardar_cursa ("nomcursa", c)

d = Cursa.get_cursa ("nomcursa")
d.print()
Output:
{'S': {}, 'A': {}} {'S': {}, 'A': {}}
Quote
#2
It is working - at a cursor glance - properly; you defined get_cursar and guardar_cursar wrong, they should have been defined as staticmethod. Though classmethod would have served you better
    @classmethod
    def guardar_cursa (cls, nomcursa, cursa):
        cls.Dicc_Curses[nomcursa] = cursa
         
    @classmethod
    def get_cursa (cls, nomcursa):
        return cls.Dicc_Curses[nomcursa]


In Python, the first argument of a method is always a reference to an object; self is just a convention, so - without a proper decorator - nomcursa is interpreted as self substitution

PS Please, don't use long concatenated words as variable name; they are unreadable, use underscores

PPS When I said "properly" - I meant that it works exactly as you defined it; not the way you intended it to work.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Quote
#3
If I do what you said the compiler complains, that's why I avoided using self in this functions since I do not really needed because I can call the dictionary saving objects without self.
Error:
Cursa.guardar_cursa (nomcursa, c) TypeError: guardar_cursa() missing 1 required positional argument: 'cursa'
Also, if supposedly my code works properly why all the class variables are printed correctly except "diccpuntscontrol" and why in one program it doesn't work and in the testing program it works.
Quote
#4
OK. spoon-feeding time - that would have worked in your previous code
    
    @staticmethod
    def guardar_cursa (nomcursa, cursa):
        Cursa.Dicc_Curses[nomcursa] = cursa
    @staticmethod     
    def get_cursa (nomcursa):
        return Cursa.Dicc_Curses[nomcursa]
You have to learn about classmethod and staticmethod - and how they differ from regular methods.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Quote
#5
My bad, I didn't know that I had to copy the @. I thought it was you telling me this was in x method. Anyway, this doesn't fix the issue unluckily.

None of both methods.
Quote
#6
(May-20-2018, 01:23 PM)3dimensions Wrote: My bad, I didn't know that I had to copy the @. I thought it was you telling me this was in x method. Anyway, this doesn't fix the issue unluckily.

None of both methods.

Stupid me - thinking that your code makes sense. There's no connection between Cursa.Dicc_Curses and self.diccpuntscontrol. Forget everything that I said
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Quote
#7
Okay, I found the issue but it makes no sense. For some unknown reason, "diccpuntscontrol" is not saving properly because the dictionary is returned from a class function. Making copies of said dictionary won't work either.

This class declaration will print that diccpuntscontrol = {}
ppc_dicc = # Call functions that returns a dictionary
print (ppc_dicc) # To check that the dictionary is not empty and perfectly ok
c = Cursa (nomcursa, nummaxcorredors, 2, "creada", ppc_dicc)
Meanwhile this one will print properly:
c = Cursa (nomcursa, nummaxcorredors, 2, "creada", {"Test": "Hello"})
And this one too:
ppc_dicc = {"Test": "Hello"}
c = Cursa (nomcursa, nummaxcorredors, 2, "creada", ppc_dicc)
How does this make sense????? Wall


The class itself:
class PPC:
    Dicc_PPC = {}

    def guardar_ppc (self, nomppc):
        PPC.Dicc_PPC[nomppc] = {}

    def reset_dicc (self):
        PPC.Dicc_PPC.clear()

    def get_dicc (self):
        return PPC.Dicc_PPC

Found a temporary solution which will let me continue working on the program but if someone has a proper answer to fix this issue post it please
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Dictionary for Excel Data Ranjirock 0 56 10 hours ago
Last Post: Ranjirock
  Attribute Error Adit99 5 177 Yesterday, 11:44 AM
Last Post: ichabod801
  Basic one: Aggregating from a dictionary Mustey 1 121 Aug-12-2019, 07:33 PM
Last Post: buran
  class returns NoneType Object istemihan 0 58 Aug-12-2019, 11:47 AM
Last Post: istemihan
  class random var write to array storzo 5 148 Aug-02-2019, 03:26 PM
Last Post: storzo
  AttributeError: 'area' object has no attribute 'areaofcircle' shane1236 5 160 Aug-02-2019, 06:44 AM
Last Post: shane1236
  Parsing json - dictionary with in dictionary krish216 6 288 Jul-30-2019, 10:01 PM
Last Post: cvsae
  Create XML from dictionary pygrrrl 7 249 Jul-29-2019, 08:33 PM
Last Post: pygrrrl
  Dictionary help Tridium 3 114 Jul-29-2019, 09:41 AM
Last Post: Tridium
  How do you import a dictionary from a .csv file? SteampunkMaverick12 2 150 Jul-29-2019, 09:20 AM
Last Post: sudheerm

Forum Jump:


Users browsing this thread: 1 Guest(s)