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 in Python kartheek 3 141 7 hours ago
Last Post: snippsat
  Is it possible to check for a key error in a python dictionary? mrsenorchuck 0 81 Yesterday, 11:38 PM
Last Post: mrsenorchuck
  Unpacking dictionary from .xlsx as Kwargs etjkai 4 180 Dec-05-2019, 11:20 PM
Last Post: micseydel
  adding information to a dictionary. Justice_py 2 165 Dec-05-2019, 04:29 AM
Last Post: Justice_py
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 187 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Defining parent Class in Python 2.7.11 sarah 2 186 Dec-03-2019, 02:54 AM
Last Post: sarah
  using class functions in roguelike game trousers1 3 113 Dec-02-2019, 08:22 PM
Last Post: ichabod801
  Dictionary and tuples list comprehensions help paul41 2 156 Nov-29-2019, 06:59 PM
Last Post: perfringo
  Assert that dictionary contains a key phpMick 3 71 Nov-27-2019, 11:36 AM
Last Post: buran
  Calculating frequency of items in a dictionary markellefultz20 1 83 Nov-27-2019, 04:21 AM
Last Post: scidam

Forum Jump:


Users browsing this thread: 1 Guest(s)