I want the following counter to check the order num class inside a dictionary thats inside a list.
...code...
self.counter = count(1)
def submit(self):
if Database.xoomDatabase[0]["Num Orden"] == next(self.counter):
self.counter = Database.xoomDatabase["Num Orden"]
Database.xoomDatabase.append(ordenOrganiz)
The reason I'm doing this type of counter is because I'm dumping a pickle file everytime the app closes, that contains Database.xoomDatabase. When the app is executed, the pickle dump gets loaded and all the dictionaries inside of it get pushed back into Database.xoomDatabase. This is the dictionary that's saved on Database.xoomDatabase:
global ordenOrganiz
ordenOrganiz = {"Num Order": nicenum,
"Nombre": nombre,
"Email": email,
"Num Tel/Cel": num,
"Orden Creada:": fechacreacion1,
"Fecha de Entrega": fechaentrega}
print(ordenOrganiz["Nombre"])
return dict(ordenOrganiz)
My questions is: How can I start the counter in exactly the last "Order Num" the is loaded from the pickle dump file?
EDIT: This is how, with the help of Anand S Kumar, I got it it to work:
if len(Database.xoomDatabase) == 0:
newCount = 0
else:
newCount = max(Database.xoomDatabase, key = lambda x:x['Num Orden'])["Num Orden"]
nombre = contents1
nicenum = int(newCount) + 1
This loop checks if there are any saved dictionaries on the list. If there ar no dics., the count starts at 1. If there are already saved dics., the count will start from the last "Orden Num"(Order Number) saved into the pickle dump.
xoomDatabase = []
if path.isfile(file_path) == False:
open(file_path, "w").close()
else:
loadLis = open(file_path, "rb")
dalis = pickle.load(loadLis)
loadLis.close()
xoomDatabase.extend(dalis)
This loops check if there is any file to load, if there isn't, it cretes one. If there's already a saved pickle dump, then it will load the list with previously saved dicts.
You can create a class variable directly inside the class, and then access it using
Class.<variable>
and also in__init__()
function use thisClass.<variable>
to initialize the counter for each variable and increment the counter.Example -
And then at the start of the program, read back the data from the pickle dump and then take the largest counter and set it to counter as given in below example.
Example -
The above
max()
gives the largestNum Orden
from the list of dictionaries.