def end_progress(self): if not self.report_progress: return total_time = int(time.time() - self.start_time) sys.stdout.write( "\nFinished! Iterated over {n} object{s} in {duration}.\n".format( n=self.objects_done, s='s' if self.objects_done != 1 else '', duration=format_duration(total_time)))
def update_progress(self, direction, chunk=None, end_pk=None): if not self.report_progress: return if chunk is not None: self.chunks_done += 1 if self.objects_done != "???": # If the queryset is not being fetched as-is, e.g. its # .delete() is called, we can't know how many objects were # affected, so we just bum out and write "???". if chunk._result_cache is None: self.objects_done = "???" else: self.objects_done += len(chunk) try: percent_complete = 100 * (float(self.objects_done) / self.total) except (ZeroDivisionError, ValueError): percent_complete = 0.0 report = "{} {} processed {}/{} objects ({:.2f}%) in {} chunks".format( self.model_name, self.__class__.__name__, self.objects_done, self.total, percent_complete, self.chunks_done, ) if end_pk is not None: report += "; {dir} pk so far {end_pk}".format( dir="highest" if direction == 1 else "lowest", end_pk=end_pk, ) if self.objects_done != '???' and self.rate.avg_rate: n_remaining = self.total - self.objects_done s_remaining = max(0, int(n_remaining // self.rate.avg_rate)) report += ', {} remaining'.format( format_duration(s_remaining) ) # Add spaces to avoid problem with reverse iteration, see #177. spacing = " " * max(0, len(self.old_report) - len(report)) if self.old_report: # Reset line on successive outputs sys.stdout.write("\r") sys.stdout.write(report) sys.stdout.write(spacing) sys.stdout.flush() self.old_report = report
def update_progress(self, direction, chunk=None, end_pk=None): if not self.report_progress: return if chunk is not None: self.chunks_done += 1 if self.objects_done != "???": # If the queryset is not being fetched as-is, e.g. its # .delete() is called, we can't know how many objects were # affected, so we just bum out and write "???". if chunk._result_cache is None: self.objects_done = "???" else: self.objects_done += len(chunk) try: percent_complete = 100 * (float(self.objects_done) / self.total) except (ZeroDivisionError, ValueError): percent_complete = 0.0 report = "{} {} processed {}/{} objects ({:.2f}%) in {} chunks".format( self.model_name, self.__class__.__name__, self.objects_done, self.total, percent_complete, self.chunks_done, ) if end_pk is not None: report += "; {dir} pk so far {end_pk}".format( dir="highest" if direction == 1 else "lowest", end_pk=end_pk, ) if self.objects_done != '???' and self.rate.avg_rate: n_remaining = self.total - self.objects_done s_remaining = max(0, int(n_remaining // self.rate.avg_rate)) report += ', {} remaining'.format( format_duration(s_remaining), ) # Add spaces to avoid problem with reverse iteration, see #177. spacing = " " * max(0, len(self.old_report) - len(report)) if self.old_report: # Reset line on successive outputs sys.stdout.write("\r") sys.stdout.write(report) sys.stdout.write(spacing) sys.stdout.flush() self.old_report = report
def end_progress(self): if not self.report_progress: return total_time = time.time() - self.start_time sys.stdout.write( "\nFinished! Iterated over {n} object{s} in {duration}.\n".format( n=self.objects_done, s='s' if self.objects_done != 1 else '', duration=format_duration(total_time) ) )
def test_minutes(self): assert format_duration(60) == '1m0s' assert format_duration(61) == '1m1s' assert format_duration(120) == '2m0s' assert format_duration(3599) == '59m59s'
def test_seconds(self): assert format_duration(0) == '0s' assert format_duration(1) == '1s' assert format_duration(30) == '30s' assert format_duration(59) == '59s'
def test_hours(self): assert format_duration(3600) == '1h0m0s' assert format_duration(3601) == '1h0m1s'
def test_seconds(self): assert format_duration(0) == "0s" assert format_duration(1) == "1s" assert format_duration(30) == "30s" assert format_duration(59) == "59s"
def test_minutes(self): assert format_duration(60) == "1m0s" assert format_duration(61) == "1m1s" assert format_duration(120) == "2m0s" assert format_duration(3599) == "59m59s"