user3577419
user3577419

Reputation: 87

Can I pause itertools on python, and resume later?

I need to create a list of strings with all the possible combinations of all letters uppercase and lowercase, with non repeating characters, of lenght 14, this is massive and I know it will take a lot of time and space. My code right now is this:

import itertools

filename = open("strings.txt", "w")

for com in itertools.permutations('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 14):
    filename.write("\n"+"1_"+"".join(com)+"\n"+"0_"+"".join(com))
    print ("".join(com))

pretty basic, it does the job and I have not found a faster way as of yet (tried a java algorithm I found that seemed faster but python was faster) Since this will take a long time, from time to time I need to turn off my computer, so I need to be able to save somewhere where I left and continue, else I will start from the beginning each time it crashes/turn off my pc / anything happen. Is there any way to do that?

Upvotes: 3

Views: 204

Answers (1)

rdas
rdas

Reputation: 21285

You can pickle that iterator object. Its internal state will be stored in the pickle file. When you resume it should start from where it left off.

Something like this:

import itertools
import os
import pickle
import time

# if the iterator was saved, load it
if os.path.exists('saved_iter.pkl'):
    with open('saved_iter.pkl', 'rb') as f:
        iterator = pickle.load(f)
# otherwise recreate it
else:
    iterator = itertools.permutations('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 14)

try:
    for com in iterator:
        # process the object from the iterator
        print(com)
        time.sleep(1.0)
except KeyboardInterrupt:
    # if the script is about to exit, save the iterator state
    with open('saved_iter.pkl', 'wb') as f:
        pickle.dump(iterator, f)

Which results in:

>python so_test.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'p')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'q')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'r')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 's')

>python so_test.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 't')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'u')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'v')
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'w')

Upvotes: 8

Related Questions