import random import string import sys def random_string(s): return ''.join([random.choice(string.letters) for i in range(s)]) def make_test_file(pickler, n): """Create a dictionary from 1-tuple of string to ints, and pickle it using module pickler. The strings are created randomly, giving a dictionary of maximum size n. """ # Since the dict is filled with random keys, its size may be smaller # than n, as some random keys may match. # for keys containing a 5 letter random string, and n = 1e6, # the actual dictionary size is expected to be only 0.1% smaller. d = dict([((random_string(5), ), i) for i in range(n)]) pickle = __import__(pickler) with open("test.pickle", 'wb') as handle: pickle.dump(d, handle, 2) if __name__ == "__main__": # usage: python2.x make_file.py pickler n pickler = sys.argv[1] # name of pickle module to use (pickle or cPickle) n = int(sys.argv[2]) # size of dictionary make_test_file(pickler, n)