1# 2# Copyright (C) 2013 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17"""Various formatting functions.""" 18 19 20def NumToPercent(num, total, min_precision=1, max_precision=5): 21 """Returns the percentage (string) of |num| out of |total|. 22 23 If the percentage includes a fraction, it will be computed down to the least 24 precision that yields a non-zero and ranging between |min_precision| and 25 |max_precision|. Values are always rounded down. All arithmetic operations 26 are integer built-ins. Examples (using default precision): 27 28 (1, 1) => 100% 29 (3, 10) => 30% 30 (3, 9) => 33.3% 31 (3, 900) => 0.3% 32 (3, 9000000) => 0.00003% 33 (3, 900000000) => 0% 34 (5, 2) => 250% 35 36 Args: 37 num: the value of the part 38 total: the value of the whole 39 min_precision: minimum precision for fractional percentage 40 max_precision: maximum precision for fractional percentage 41 Returns: 42 Percentage string, or None if percent cannot be computed (i.e. total is 43 zero). 44 45 """ 46 if total == 0: 47 return None 48 49 percent = 0 50 precision = min(min_precision, max_precision) 51 factor = 10 ** precision 52 while precision <= max_precision: 53 percent = num * 100 * factor / total 54 if percent: 55 break 56 factor *= 10 57 precision += 1 58 59 whole, frac = divmod(percent, factor) 60 while frac and not frac % 10: 61 frac /= 10 62 precision -= 1 63 64 return '%d%s%%' % (whole, '.%0*d' % (precision, frac) if frac else '') 65 66 67def BytesToHumanReadable(size, precision=1, decimal=False): 68 """Returns a human readable representation of a given |size|. 69 70 The returned string includes unit notations in either binary (KiB, MiB, etc) 71 or decimal (kB, MB, etc), based on the value of |decimal|. The chosen unit is 72 the largest that yields a whole (or mixed) number. It may contain up to 73 |precision| fractional digits. Values are always rounded down. Largest unit 74 is an exabyte. All arithmetic operations are integer built-ins. Examples 75 (using default precision and binary units): 76 77 4096 => 4 KiB 78 5000 => 4.8 KiB 79 500000 => 488.2 KiB 80 5000000 => 4.7 MiB 81 82 Args: 83 size: the size in bytes 84 precision: the number of digits past the decimal point 85 decimal: whether to compute/present decimal or binary units 86 Returns: 87 Readable size string, or None if no conversion is applicable (i.e. size is 88 less than the smallest unit). 89 90 """ 91 constants = ( 92 (('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'), 1024), 93 (('kB', 'MB', 'GB', 'TB', 'PB', 'EB'), 1000) 94 ) 95 suffixes, base = constants[decimal] 96 exp, magnitude = 0, 1 97 while exp < len(suffixes): 98 next_magnitude = magnitude * base 99 if size < next_magnitude: 100 break 101 exp += 1 102 magnitude = next_magnitude 103 104 if exp != 0: 105 whole = size / magnitude 106 frac = (size % magnitude) * (10 ** precision) / magnitude 107 while frac and not frac % 10: 108 frac /= 10 109 return '%d%s %s' % (whole, '.%d' % frac if frac else '', suffixes[exp - 1]) 110