SF.net SVN: fclient: [123] trunk/fclient/fclient_lib/pyex/numbers.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <ju...@us...> - 2008-02-02 18:31:33
|
Revision: 123
http://fclient.svn.sourceforge.net/fclient/?rev=123&view=rev
Author: jurner
Date: 2008-02-02 10:30:56 -0800 (Sat, 02 Feb 2008)
Log Message:
-----------
some new methods
Modified Paths:
--------------
trunk/fclient/fclient_lib/pyex/numbers.py
Modified: trunk/fclient/fclient_lib/pyex/numbers.py
===================================================================
--- trunk/fclient/fclient_lib/pyex/numbers.py 2008-02-02 18:30:26 UTC (rev 122)
+++ trunk/fclient/fclient_lib/pyex/numbers.py 2008-02-02 18:30:56 UTC (rev 123)
@@ -2,6 +2,7 @@
"""
+import re
#***************************************************************
#
#***************************************************************
@@ -13,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)
+
+
#***************************************************************
#
#***************************************************************
@@ -134,6 +147,107 @@
#*****************************************************************
#
#****************************************************************
+HEX_PREFIXES = ('0x', '0X', '&H', '&h')
+NUM_DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz"
+
+
+def to_base(to_base, num, base=10, hex_prefixes=HEX_PREFIXES):
+ """Converts a number to the desired base
+ @param to_base: base to convert the number to (2-36 and 256)
+ @param num: number to convert
+ @param base: base of the number to convert
+ @param hex_prefixes: prefixes for hexadecimal numbers to be recognized by the function
+
+ @return: (str) converted number
+
+
+ >>> to_base(2, 10)
+ '1010'
+ >>> to_base(10, '1010', base=2)
+ '10'
+ >>> to_base(2, '1010', base=2)
+ '1010'
+
+ >>> to_base(10, '0xFF', base=16)
+ '255'
+
+ >>> to_base(256, '0x61', base=16)
+ 'a'
+ >>> to_base(2, 'a', base=256)
+ '1100001'
+
+
+ >>> result = to_base(2, 'Hello World!', 256)
+ >>> result
+ '10010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001'
+ >>> result = to_base(256, result, base=2)
+ >>> result
+ 'Hello World!'
+
+ """
+ if num < 0:
+ raise ValueError('negative numbers not supported: %r' % num)
+ if to_base < 2 or to_base > 36 and not to_base == 256:
+ raise ValueError('bes must be in range 2-36 or 256, found: %r' % to_base)
+ if base < 2 or base > 36 and not base == 256:
+ raise ValueError('bes must be in range 2-36 or 256, found: %r' % base)
+
+ if base == 256:
+ tmp_num = 0
+ for char in num:
+ tmp_num = (tmp_num << 8) + ord(char)
+ num = tmp_num
+ else:
+ # let int() handle it
+ if base == 16:
+ num = strip_hex_prefix(num, hex_prefixes=hex_prefixes)
+ num = int('%s' % num, base)
+
+ if to_base == 10 and base != 256:
+ return str(num)
+
+ out = ''
+ if to_base == 256:
+ while num:
+ num, tail = divmod(num, to_base)
+ out += chr(tail)
+ return out[-1::-1] if out else '\x00'
+ else:
+ while num:
+ num, tail = divmod(num, to_base)
+ out += NUM_DIGITS[tail]
+ return out[-1::-1] if out else '0'
+
+
+
+def strip_hex_prefix(num, hex_prefixes=HEX_PREFIXES):
+ """Strips prefixes from hexadecimal numbers
+ @param num: number to strip prefix from
+ @param hex_prefixes: list containing prefixes to strip
+ @return: (str) stripped number
+
+ >>> strip_hex_prefix('0xFF')
+ 'FF'
+ >>> strip_hex_prefix('&HFF')
+ 'FF'
+ >>> strip_hex_prefix(10)
+ '10'
+ >>> strip_hex_prefix('')
+ ''
+
+ """
+ if not isinstance(num, basestring):
+ num = '%s' % num
+ else:
+ pat = re.compile(
+ '\A(%s)' % '|'.join([re.escape(i) for i in hex_prefixes])
+ )
+ num = pat.sub('', num)
+ return num
+
+#*****************************************************************
+#
+#****************************************************************
if __name__ == '__main__':
import doctest
doctest.testmod()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|