mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-06-09 14:04:10 +08:00
add doctests to b40 module
This commit is contained in:
@@ -34,25 +34,57 @@ B16_REGEX = '^[0-9a-f]*$'
|
||||
|
||||
def int_to_charset(val, charset):
|
||||
""" Turn a non-negative integer into a string.
|
||||
|
||||
>>> int_to_charset(0, B40_CHARS)
|
||||
'0'
|
||||
>>> int_to_charset(658093, B40_CHARS)
|
||||
'abcd'
|
||||
>>> int_to_charset(40, B40_CHARS)
|
||||
'10'
|
||||
>>> int_to_charset(149190078205533, B40_CHARS)
|
||||
'muneeb.id'
|
||||
>>> int_to_charset(-1, B40_CHARS)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: "val" must be a non-negative integer.
|
||||
"""
|
||||
if not val >= 0:
|
||||
if val < 0:
|
||||
raise ValueError('"val" must be a non-negative integer.')
|
||||
|
||||
if val == 0:
|
||||
return charset[0]
|
||||
|
||||
output = ""
|
||||
while val > 0:
|
||||
val, digit = divmod(val, len(charset))
|
||||
output += charset[digit]
|
||||
|
||||
# reverse the characters in the output and return
|
||||
return output[::-1]
|
||||
|
||||
|
||||
def charset_to_int(s, charset):
|
||||
""" Turn a string into a non-negative integer.
|
||||
|
||||
>>> charset_to_int('0', B40_CHARS)
|
||||
0
|
||||
>>> charset_to_int('10', B40_CHARS)
|
||||
40
|
||||
>>> charset_to_int('abcd', B40_CHARS)
|
||||
658093
|
||||
>>> charset_to_int('', B40_CHARS)
|
||||
0
|
||||
>>> charset_to_int('muneeb.id', B40_CHARS)
|
||||
149190078205533
|
||||
>>> charset_to_int('A', B40_CHARS)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: substring not found
|
||||
"""
|
||||
output = 0
|
||||
for char in s:
|
||||
output = output * len(charset) + charset.index(char)
|
||||
|
||||
return output
|
||||
|
||||
|
||||
@@ -68,6 +100,12 @@ def change_charset(s, original_charset, target_charset):
|
||||
|
||||
|
||||
def hexpad(x):
|
||||
"""
|
||||
>>> hexpad('123')
|
||||
'0123'
|
||||
>>> hexpad('1234')
|
||||
'1234'
|
||||
"""
|
||||
return ('0' * (len(x) % 2)) + x
|
||||
|
||||
|
||||
@@ -78,11 +116,28 @@ def charset_to_hex(s, original_charset):
|
||||
def hex_to_charset(s, destination_charset):
|
||||
if not virtualchain.lib.hashing.is_hex(s):
|
||||
raise ValueError("Value must be in hex format")
|
||||
|
||||
s = s.lower()
|
||||
return change_charset(s, B16_CHARS, destination_charset)
|
||||
|
||||
|
||||
def is_b40(s):
|
||||
"""
|
||||
>>> is_b40('abcd')
|
||||
True
|
||||
>>> is_b40(u'abcd')
|
||||
False
|
||||
>>> is_b40(None)
|
||||
False
|
||||
>>> is_b40(123)
|
||||
False
|
||||
>>> is_b40('1234567890')
|
||||
True
|
||||
>>> is_b40('abcd.efgh+-_')
|
||||
True
|
||||
>>> is_b40('')
|
||||
True
|
||||
"""
|
||||
return (isinstance(s, str) and (re.match(B40_REGEX, s) is not None))
|
||||
|
||||
|
||||
@@ -100,3 +155,9 @@ def bin_to_b40(s):
|
||||
|
||||
def b40_to_hex(s):
|
||||
return hexlify(b40_to_bin(s))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user