Thread: SF.net SVN: fclient: [96] trunk/sandbox/fcp/fcp_lib/numbers.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <ju...@us...> - 2008-02-01 14:41:45
|
Revision: 96
http://fclient.svn.sourceforge.net/fclient/?rev=96&view=rev
Author: jurner
Date: 2008-02-01 06:41:38 -0800 (Fri, 01 Feb 2008)
Log Message:
-----------
comb over
Modified Paths:
--------------
trunk/sandbox/fcp/fcp_lib/numbers.py
Modified: trunk/sandbox/fcp/fcp_lib/numbers.py
===================================================================
--- trunk/sandbox/fcp/fcp_lib/numbers.py 2008-01-30 17:17:10 UTC (rev 95)
+++ trunk/sandbox/fcp/fcp_lib/numbers.py 2008-02-01 14:41:38 UTC (rev 96)
@@ -14,51 +14,63 @@
@return: (str) formated number
>>> format_num_bytes(100)
- '100 b'
+ '100B'
>>> format_num_bytes(1000)
- '1.00 kb'
+ '1.00KB'
>>> format_num_bytes(1024, conform=False)
- '1.00 kb'
+ '1.00KiB'
>>> format_num_bytes(1000, short=False)
- '1.00 Kilobyte'
+ '1.00Kilobyte'
"""
-
- if short:
- names = ('b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb')
- else:
- names = ('Byte',
- 'Kilobyte',
- 'Megabyte',
- 'Gigabyte',
- 'Terabyte',
- 'Petabyte',
- 'Exabyte',
- 'Zettabyte',
- 'Yottabyte'
- )
if conform:
factor = 1000
+ if short:
+ names = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
+ else:
+ names = ('Byte',
+ 'Kilobyte',
+ 'Megabyte',
+ 'Gigabyte',
+ 'Terabyte',
+ 'Petabyte',
+ 'Exabyte',
+ 'Zettabyte',
+ 'Yottabyte'
+ )
else:
factor = 1024
-
- num = float(num)
+ if short:
+ names = ('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
+ else:
+ names = ('Byte',
+ 'Kibibyte',
+ 'Mebibyte',
+ 'Gibibyte',
+ 'Tebibyte',
+ 'Pebibyte',
+ 'Exibyte',
+ 'Zebibyte',
+ 'Yobiabyte'
+ )
+
name = names[0]
if num >= factor:
+ num = float(num)
for tmp_name in names[1: ]:
num /= factor
name = tmp_name
if num < factor:
break
else:
- return '%i %s' % (num, name)
+ return '%i%s' % (num, name)
-
-
- return '%01.2f %s' % (num, name)
+ return '%01.2f%s' % (num, name)
+
+
#***************************************************************
#
#***************************************************************
@@ -233,7 +245,6 @@
num = pat.sub('', num)
return num
-
#*****************************************************************
#
#****************************************************************
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ju...@us...> - 2008-02-16 10:09:53
|
Revision: 218
http://fclient.svn.sourceforge.net/fclient/?rev=218&view=rev
Author: jurner
Date: 2008-02-16 02:09:58 -0800 (Sat, 16 Feb 2008)
Log Message:
-----------
comb over
Modified Paths:
--------------
trunk/sandbox/fcp/fcp_lib/numbers.py
Modified: trunk/sandbox/fcp/fcp_lib/numbers.py
===================================================================
--- trunk/sandbox/fcp/fcp_lib/numbers.py 2008-02-16 10:09:26 UTC (rev 217)
+++ trunk/sandbox/fcp/fcp_lib/numbers.py 2008-02-16 10:09:58 UTC (rev 218)
@@ -6,11 +6,19 @@
#***************************************************************
#
#***************************************************************
-def format_num_bytes(num, short=True, conform=True):
+class ByteSizeNames:
+ Binary = ('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
+ Common = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
+
+
+
+def format_num_bytes(num, binary=False, names=None, format_strings=('%i%s', '%01.2f%s') ):
"""Formats a number representing a number of bytes to a human readable string
@param num: (int) number to fomat
- @param short: use short names
- @param conform: if True factor is 1000, else factor is 1024
+ @param binary: if True conversion factor is 1024, else factor is 1000
+ @param names: (tuple) names to use as suffix or None to use default names (see L{ByteSizeNames})
+ @param format_strings: (tuple) format strings to be used or None to use the default formating.
+ The first member of the tuple is used to format bytes, the seconds anything > one kilobyte
@return: (str) formated number
>>> format_num_bytes(100)
@@ -19,43 +27,24 @@
>>> format_num_bytes(1000)
'1.00KB'
- >>> format_num_bytes(1024, conform=False)
+ >>> format_num_bytes(1024, binary=True)
'1.00KiB'
- >>> format_num_bytes(1000, short=False)
- '1.00Kilobyte'
+ >>> format_num_bytes(1000, names=('W', 'X', 'Y', 'Z'))
+ '1.00X'
+ >>> format_num_bytes(1000, format_strings=('%s%i', '%01.4f%s'))
+ '1.0000KB'
+
"""
- if conform:
+ if binary:
+ factor = 1024
+ if names is None:
+ names = ByteSizeNames.Binary
+ else:
factor = 1000
- if short:
- names = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
- else:
- names = ('Byte',
- 'Kilobyte',
- 'Megabyte',
- 'Gigabyte',
- 'Terabyte',
- 'Petabyte',
- 'Exabyte',
- 'Zettabyte',
- 'Yottabyte'
- )
- else:
- factor = 1024
- if short:
- names = ('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
- else:
- names = ('Byte',
- 'Kibibyte',
- 'Mebibyte',
- 'Gibibyte',
- 'Tebibyte',
- 'Pebibyte',
- 'Exibyte',
- 'Zebibyte',
- 'Yobiabyte'
- )
+ if names is None:
+ names = ByteSizeNames.Common
name = names[0]
if num >= factor:
@@ -66,11 +55,78 @@
if num < factor:
break
else:
- return '%i%s' % (num, name)
+ return format_strings[0] % (num, name)
+ return format_strings[1] % (num, name)
+
+
+NumBytesPat = re.compile('''\A (\d+ \. \d+) | (\d+)''', re.X)
+def num_bytes_to_bytes(num, binary=False, names=None):
+ """Converts a string containing a bytes size to an integer
+ @param num: (str) string to convert
+ @param binary: if True conversion factor is 1024, else factor is 1000
+ @param names: (tuple) names to use as suffix or None to use default names (see L{ByteSizeNames})
+
+ @return: (int) number of bytes
+ @note: postfixes are handled case sensitive
+
+ >>> num_bytes_to_bytes('1000B')
+ 1000
+
+ >>> num_bytes_to_bytes('1GB')
+ 1000000000
+
+ >>> num_bytes_to_bytes('1.2KB')
+ 1200
+
+ >>> num_bytes_to_bytes('1.5678B')
+ 2
+
+ >>> num_bytes_to_bytes('1MiB', binary=True)
+ 1048576
+
+ >>> num_bytes_to_bytes('1X', names=('X', ))
+ 1
- return '%01.2f%s' % (num, name)
+ >>> num_bytes_to_bytes('foo')
+ Traceback (most recent call last):
+ ...
+ ValueError: No number found in input string
+
+ >>> num_bytes_to_bytes('1foo')
+ Traceback (most recent call last):
+ ...
+ ValueError: Unknown size postfix
+ """
+ if names is None:
+ if binary:
+ names = ByteSizeNames.Binary
+ else:
+ names = ByteSizeNames.Common
+ names = list(names)
+
+ match = NumBytesPat.match(num)
+ if match is None:
+ raise ValueError('No number found in input string')
+
+ isfloat, isint = match.groups()
+ if isfloat:
+ z = len(isfloat)
+ tmp_num = float(isfloat)
+ else:
+ z = len(isint)
+ tmp_num = int(isint)
+
+ postfix = num[z: ]
+ try:
+ exp = names.index(postfix)
+ except ValueError:
+ raise ValueError('Unknown size postfix')
+ factor = 1024 if binary else 1000
+
+ #TODO: 1.96B is returned rounded as 2. Should we complain?
+ return int(round(tmp_num * (factor ** exp)))
+
-
#***************************************************************
#
#***************************************************************
@@ -245,6 +301,7 @@
num = pat.sub('', num)
return num
+
#*****************************************************************
#
#****************************************************************
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|