diff options
-rw-r--r-- | askbot/migrations/0053_create_threads_for_questions.py | 4 | ||||
-rw-r--r-- | askbot/utils/console.py | 34 |
2 files changed, 37 insertions, 1 deletions
diff --git a/askbot/migrations/0053_create_threads_for_questions.py b/askbot/migrations/0053_create_threads_for_questions.py index 56f826b9..22365692 100644 --- a/askbot/migrations/0053_create_threads_for_questions.py +++ b/askbot/migrations/0053_create_threads_for_questions.py @@ -3,12 +3,14 @@ import datetime from south.db import db from south.v2 import DataMigration from django.db import models +from askbot.utils.console import ProgressBar class Migration(DataMigration): def forwards(self, orm): "Write your forwards methods here." - for question in orm.Question.objects.iterator(): + num_questions = orm.Question.objects.count() + for question in ProgressBar(orm.Question.objects.iterator(), num_questions): thread = orm.Thread.objects.create(favourite_count=question.favourite_count) question.thread = thread question.save() diff --git a/askbot/utils/console.py b/askbot/utils/console.py index 496bbd65..f132e7d0 100644 --- a/askbot/utils/console.py +++ b/askbot/utils/console.py @@ -74,3 +74,37 @@ def print_progress(elapsed, total, nowipe = False): in-place""" output = '%6.2f%%' % (100 * float(elapsed)/float(total)) print_action(output, nowipe) + +class ProgressBar(object): + """A wrapper for an iterator, that prints + a progress bar along the way of iteration + """ + def __init__(self, iterable, length): + self.iterable = iterable + self.length = length + self.counter = float(0) + self.barlen = 60 + self.progress = '' + + def __iter__(self): + return self + + def next(self): + + sys.stdout.write('\b'*len(self.progress)) + + if self.length < self.barlen: + sys.stdout.write('-'*(self.barlen/self.length)) + elif int(self.counter) % (self.length / self.barlen) == 0: + sys.stdout.write('-') + + self.progress = ' %.2f%%' % (100 * (self.counter/self.length)) + sys.stdout.write(self.progress) + sys.stdout.flush() + + self.counter += 1 + try: + return self.iterable.next() + except StopIteration: + sys.stdout.write('\n') + raise |