In anticipation of creating large data structure, it appeared to be helpful if the users could get an idea of how much memory (RAM in particular) would be used while attempting to create a large, multidimensional array. In order to convert the calculated size into a precise, human-readable format, the follow code was devised. In order to convert a number into an equivalent representation of bytes, just call the "convert" function while providing the number as its argument. The other functions are currently public in case anyone else finds them useful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | """Module for byte-to-string conversion.
This module provides several utility functions along with another
function that can convert numbers into byte-size representations."""
################################################################################
__version__ = "$Revision: 2 $"
__date__ = "8 October 2009"
__author__ = "Stephen Chappell <Noctis.Skytower@gmail.com>"
__credits__ = """\
T. Hansen, for his encouraging example as an excellent programmer.
S. Spencer, for reminding me to strive for quality in all things.
J. Sparks, for helping to reignite a dedication to writing code."""
################################################################################
import sys as _sys
################################################################################
def convert(number):
"Convert bytes into human-readable representation."
assert 0 < number < 1 << 110, 'number out of range'
ordered = reversed(tuple(format_bytes(partition_number(number, 1 << 10))))
cleaned = ', '.join(item for item in ordered if item[0] != '0')
return cleaned
################################################################################
def partition_number(number, base):
"Continually divide number by base until zero."
div, mod = divmod(number, base)
yield mod
while div:
div, mod = divmod(div, base)
yield mod
def format_bytes(parts):
"Format partitioned bytes into human-readable strings."
for power, number in enumerate(parts):
yield '{} {}'.format(number, format_suffix(power, number))
def format_suffix(power, number):
"Compute the suffix for a certain power of bytes."
return (PREFIX[power] + 'byte').capitalize() + 's'[number == 1:]
################################################################################
PREFIX = ' kilo mega giga tera peta exa zetta yotta bronto geop'.split(' ')
################################################################################
if __name__ == '__main__':
_sys.stdout.write('Content-Type: text/plain\n\n')
_sys.stdout.write(open(_sys.argv[0]).read())
|
Those last three lines should not be there...
The last three lines are for servers that run *.py files as CGI scripts.
If this code is running on a server, you can examine it as open source.
Trying this for the first time, am I forgetting something?
If you are not using the latest version of Python (3.2.1), try changing
yield '{} {}'.format(number, format_suffix(power, number))
toyield '{0} {1}'.format(number, format_suffix(power, number))
instead.another variant