I would like to save the state of itertools.product() after my program quits. Is it possible to do this with pickling? What I am planning to do is to generate permutations and if the process is interrupted (KeyboardInterrupt), I can resume the process the next time I run the program.
def trywith(itr):
try:
for word in itr:
time.sleep(1)
print("".join(word))
except KeyboardInterrupt:
f=open("/root/pickle.dat","wb")
pickle.dump((itr),f)
f.close()
if os.path.exists("/root/pickle.dat"):
f=open("/root/pickle.dat","rb")
itr=pickle.load(f)
trywith(itr)
else:
try:
itr=itertools.product('abcd',repeat=3)
for word in itr:
time.sleep(1)
print("".join(word))
except KeyboardInterrupt:
f=open("/root/pickle.dat","wb")
pickle.dump((itr),f)
f.close()