4

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()
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
repzero
  • 8,254
  • 2
  • 18
  • 40
  • Have a look at http://stackoverflow.com/q/9864809/3001761 – jonrsharpe Nov 09 '14 at 18:34
  • Nice but I was looking for a simpler explanation on how to do this since I am still exploring python. – repzero Nov 09 '14 at 18:44
  • The short answer is no, you can't just pickle it; there isn't a much simpler solution, I don't think. – jonrsharpe Nov 09 '14 at 18:47
  • ok, appreciated jonrsharpe..I will see if I can try to understand and examine the inputs at http://stackoverflow.com/questions/9864809/using-itertools-product-and-want-to-seed-a-value. – repzero Nov 09 '14 at 18:53

1 Answers1

3

In Python 2, there is not pickle support for the various itertools.

However, in Python 3, pickling support was added, so the itertools.product() iterator ought to pickle just fine:

>>> import pickle
>>> import itertools
>>> it = itertools.product(range(2), repeat=3)
>>> next(it)
(0, 0, 0)
>>> next(it)
(0, 0, 1)
>>> next(it)
(0, 1, 0)
>>> p = pickle.dumps(it)
>>> del it
>>> it = pickle.loads(p)
>>> next(it)
(0, 1, 1)
>>> next(it)
(1, 0, 0)
>>> next(it)
(1, 0, 1)
>>> next(it)
(1, 1, 0)
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485