def BytesToFixedWidthString(num_bytes, decimal_places=1): """Adjusts proper width for printing num_bytes in readable format. Args: num_bytes: The number of bytes we must display. decimal_places: The standard number of decimal places. Returns: String of fixed width representing num_bytes. """ human_readable = HumanReadableWithDecimalPlaces(num_bytes, decimal_places=decimal_places) number_format = human_readable.split() if int(round(float(number_format[0]))) >= 1000: # If we are in the [1000:1024) range for the whole part of the number, # we must remove the decimal part. last_character = len(number_format[0]) - decimal_places - 1 number_format[0] = number_format[0][:last_character] return "%9s" % (" ".join(number_format))
def BytesToFixedWidthString(num_bytes, decimal_places=1): """Adjusts proper width for printing num_bytes in readable format. Args: num_bytes: The number of bytes we must display. decimal_places: The standard number of decimal places. Returns: String of fixed width representing num_bytes. """ human_readable = HumanReadableWithDecimalPlaces(num_bytes, decimal_places=decimal_places) number_format = human_readable.split() if int(round(float(number_format[0]))) >= 1000: # If we are in the [1000:1024) range for the whole part of the number, # we must remove the decimal part. last_character = len(number_format[0]) - decimal_places - 1 number_format[0] = number_format[0][:last_character] return '%9s' % (' '.join(number_format))
def PrintFinalSummaryMessage(self, stream=sys.stderr): """Prints a final message to indicate operation succeeded. Args: stream: Stream to print messages. Usually sys.stderr, but customizable for testing. """ string_to_print = ('Operation completed over %s objects' % DecimalShort(self.num_objects)) if self.total_size: string_to_print += ( '/%s' % HumanReadableWithDecimalPlaces(self.total_size)) remaining_width = self.console_width - len(string_to_print) if not self.quiet_mode: stream.write(('\n' + string_to_print + '.' + (max(remaining_width, 0) * ' ') + '\n'))
def test_UIHumanReadableWithDecimalPlaces(self): """Tests HumanReadableWithDecimalPlaces for UI.""" self.assertEqual('1.0 GiB', HumanReadableWithDecimalPlaces(1024**3 + 1024**2 * 10, 1)) self.assertEqual('1.0 GiB', HumanReadableWithDecimalPlaces(1024**3), 1) self.assertEqual('1.01 GiB', HumanReadableWithDecimalPlaces(1024**3 + 1024**2 * 10, 2)) self.assertEqual('1.000 GiB', HumanReadableWithDecimalPlaces(1024**3 + 1024**2*5, 3)) self.assertEqual('1.10 GiB', HumanReadableWithDecimalPlaces(1024**3 + 1024**2 * 100, 2)) self.assertEqual('1.100 GiB', HumanReadableWithDecimalPlaces(1024**3 + 1024**2 * 100, 3)) self.assertEqual('10.00 MiB', HumanReadableWithDecimalPlaces(1024**2 *10, 2)) # The test below is good for rounding. self.assertEqual('2.01 GiB', HumanReadableWithDecimalPlaces(2157969408, 2)) self.assertEqual('2.0 GiB', HumanReadableWithDecimalPlaces(2157969408, 1)) self.assertEqual('0 B', HumanReadableWithDecimalPlaces(0, 0)) self.assertEqual('0.00 B', HumanReadableWithDecimalPlaces(0, 2)) self.assertEqual('0.00000 B', HumanReadableWithDecimalPlaces(0, 5))