I found this very weird bug that I can’t understand.
First of all inside this function I unpickle:
tableOrders = pickle.load(open("\\\\VIERNES7-3\Documentos c\sharedTableOrders.p","rb"))
If I do pprint(tableOrders) I get:
{1: {'blink': False,
'canceled': 'no',
'orders': [{u'availability': u'si',
u'canceled': u'no',
u'category': u'Minutas',
u'kitchen': u'si',
u'name': u'Hamburguesa al Plato',
u'parilla': u'no',
u'price': 60,
u'ready': u'no'},
{u'availability': u'si',
u'canceled': u'no',
u'category': u'Minutas',
u'kitchen': u'si',
u'name': u'Hamburguesa al Plato',
u'parilla': u'no',
u'price': 60,
u'ready': u'no'}]}}
Now I iterate thought table orders in this manner:
count = 0
for x in tableOrders[table]["orders"]:
if (x["kitchen"] == "si" or x["category"] == "Bebidas") and x["ready"] == "no":
print count
print int(event.widget.curselection()[0])
if count == int(event.widget.curselection()[0]):
x["ready"] = "si"
event.widget.delete(int(event.widget.curselection()[0]))
break
count += 1
int(event.widget.curselection()[0]) Will be the selected element of a listbox (it seems to work correctly).
The weird thing is that after I do this I have:
{1: {'blink': False,
'canceled': 'no',
'orders': [{u'availability': u'si',
u'canceled': u'no',
u'category': u'Minutas',
u'kitchen': u'si',
u'name': u'Hamburguesa al Plato',
u'parilla': u'no',
u'price': 60,
u'ready': u'si'}, <-------------- MARKED AS "si"
{u'availability': u'si',
u'canceled': u'no',
u'category': u'Minutas',
u'kitchen': u'si',
u'name': u'Hamburguesa al Plato',
u'parilla': u'no',
u'price': 60,
u'ready': u'si'}]}} <-------------- MARKED AS "si"
So both “ready” are marked as “si”, and that is not what I was expecting since I had put a break there and it should only change the property “ready” if the count == the selected element.
I tried also clicking the third element of the listbox (that has an index of 2) and this is what I got from print count and print int(event.widget.curselection()[0]):
0
2
1
2
2
2
This is why I am confused since only when both are equal (2 == 2) the x["ready"] = "si" code should be executed.
I’m not sure where the problem is, I’m quite lost but maybe I understanding the looping or break incorrectly or I’m confusing how to handle the dictionary and x["ready"] = "si" does something other that what I was expecting it to do.
Just in case it wasn’t clear, if I click the first element of the listbox (and thus int(event.widget.curselection()[0]) is zero) I want the first element of the list to be x[“ready”] == “si”, if I click on the second one the second element of the list should have the value “ready” to “si” and so on.
I did not tag this question with gui because I have discarded the possibility that the problem is there to the best of my knowledge.
EDIT:
Since the problem seems to be when I’m using the pickle here is more relevant code:
l = []
for item in sorted(jMenu["menu"]["items"]):
if item["category"] == selectedCategory:
l.append(item)
pedido = l[int(widget.curselection()[0])]
##pedido is something like this:
##pedido = {u'category': u'Bebidas', u'price': 40, u'name': u'Coca Cola', u'availability': u'si'}
Then I do:
tableOrders.addFood(activeTable, pedido)
addFood is
def addFood(self, table, food):
if not table in self.tableList.keys():
self.tableList[table] = {"orders":[food], "blink": False, "canceled": "no"}#el canceled hay que sacarlo?
else:
self.tableList[table]["orders"].append(food)
And to pickle I do:
def dumpTableOrders(self):
tableOrders = pickle.load(open("sharedTableOrders.p","rb"))
#pprint(self.tableList)
for food in sorted(self.tableList[activeTable]["orders"]):
if not activeTable in tableOrders.keys():
tableOrders[activeTable] = {"orders":[food], "blink": False, "canceled": "no"}#el canceled hay que sacarlo?
else:
tableOrders[activeTable]["orders"].append(food)
self.tableList = {}
#pprint(tableOrders)
pickle.dump(tableOrders, open(r'sharedTableOrders.p',"wb"))
Problem is in the object you are pickling, surely there both objects are same e.g.
So look into the object you are picking and create copies there, alternative is to json dump it , that way you will not get same object again e.g.