Created
December 11, 2016 18:51
-
-
Save tmthydvnprt/b23f244b215c09465cc51b5af225de9d to your computer and use it in GitHub Desktop.
Convert a directory of pickle objects to be cross platform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import glob | |
| import cPickle as pickle | |
| class ProgressBar(object): | |
| """Implements a comand-line progress bar""" | |
| def __init__(self, iterations): | |
| """Create a progress bar""" | |
| self.iterations = iterations | |
| self.prog_bar = '[]' | |
| self.fill_char = '*' | |
| self.width = 40 | |
| self.__update_amount(0) | |
| def animate(self, iterate): | |
| """Animate progress""" | |
| print '\r', self, | |
| sys.stdout.flush() | |
| self.update_iteration(iterate + 1) | |
| return self | |
| def update_iteration(self, elapsed_iter): | |
| """Increment progress""" | |
| self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0) | |
| self.prog_bar = '%s %s of %s complete' % (self.prog_bar, elapsed_iter, self.iterations) | |
| return self | |
| def __update_amount(self, new_amount): | |
| """Update amount of progress""" | |
| percent_done = int(round((new_amount / 100.0) * 100.0)) | |
| all_full = self.width - 2 | |
| num_hashes = int(round((percent_done / 100.0) * all_full)) | |
| self.prog_bar = '[%s%s]' % ((self.fill_char * num_hashes), ' ' * (all_full - num_hashes)) | |
| pct_place = (len(self.prog_bar) // 2) - len(str(percent_done)) | |
| pct_string = '%s%%' % (percent_done) | |
| self.prog_bar = '%s%s%s' % (self.prog_bar[0:pct_place], pct_string, self.prog_bar[pct_place + len(pct_string):]) | |
| return self | |
| def __str__(self): | |
| """String representation""" | |
| return str(self.prog_bar) | |
| pickle_files = glob.glob('/path/to/pickle/directory/*.pkl') | |
| p = ProgressBar(len(pickle_files)) | |
| for i, pickle_file in enumerate(pickle_files): | |
| with open(pickle_file, 'r') as f: | |
| data = pickle.load(f) | |
| with open(pickle_file, 'wb') as f: | |
| pickle.dump(data, f, protocol=0) | |
| p.animate(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment