more refactor to base91.from_decimal
This commit is contained in:
parent
1bdf8f21ae
commit
7c87cd7eb7
|
|
@ -51,21 +51,19 @@ def to_decimal(text):
|
||||||
return decimal if text != '' else 0
|
return decimal if text != '' else 0
|
||||||
|
|
||||||
|
|
||||||
def from_decimal(number, padding=1):
|
def from_decimal(number, width=1):
|
||||||
"""
|
"""
|
||||||
Takes a decimal and returns base91 char string.
|
Takes a decimal and returns base91 char string.
|
||||||
With optional padding to a specific length.
|
With optional parameter for fix with output
|
||||||
"""
|
"""
|
||||||
text = []
|
text = []
|
||||||
|
|
||||||
if not isinstance(number, int_type):
|
if not isinstance(number, int_type):
|
||||||
raise TypeError("Expected number to be int, got %s", type(number))
|
raise TypeError("Expected number to be int, got %s", type(number))
|
||||||
elif not isinstance(padding, int_type):
|
elif not isinstance(width, int_type):
|
||||||
raise TypeError("Expected padding to be int, got %s", type(number))
|
raise TypeError("Expected width to be int, got %s", type(number))
|
||||||
elif number < 0:
|
elif number < 0:
|
||||||
raise ValueError("Expected number to be positive integer")
|
raise ValueError("Expected number to be positive integer")
|
||||||
elif padding < 1:
|
|
||||||
raise ValueError("Expected padding to be >0")
|
|
||||||
elif number > 0:
|
elif number > 0:
|
||||||
max_n = ceil(log(number) / log(91))
|
max_n = ceil(log(number) / log(91))
|
||||||
|
|
||||||
|
|
@ -73,9 +71,4 @@ def from_decimal(number, padding=1):
|
||||||
quotient, number = divmod(number, 91**n)
|
quotient, number = divmod(number, 91**n)
|
||||||
text.append(chr(33 + quotient))
|
text.append(chr(33 + quotient))
|
||||||
|
|
||||||
text = "".join(text).lstrip('!')
|
return "".join(text).lstrip('!').rjust(max(1, width), '!')
|
||||||
|
|
||||||
# add padding if necessary
|
|
||||||
text = '!' * (padding-len(text)) + text
|
|
||||||
|
|
||||||
return text
|
|
||||||
|
|
|
||||||
|
|
@ -34,23 +34,21 @@ class a_FromDecimal(unittest.TestCase):
|
||||||
for n in testData:
|
for n in testData:
|
||||||
self.assertRaises(ValueError, base91.from_decimal, n)
|
self.assertRaises(ValueError, base91.from_decimal, n)
|
||||||
|
|
||||||
def test_invalid_padding_type(self):
|
def test_invalid_width_type(self):
|
||||||
testData = ['0', '1', 1.0, None, [0], dict]
|
testData = ['0', '1', 1.0, None, [0], dict]
|
||||||
|
|
||||||
for n in testData:
|
for n in testData:
|
||||||
self.assertRaises(TypeError, base91.from_decimal, 0, padding=n)
|
self.assertRaises(TypeError, base91.from_decimal, 0, padding=n)
|
||||||
|
|
||||||
def test_valid_padding(self):
|
def test_valid_width(self):
|
||||||
testData = [1, 2, 5, 10, 100]
|
testData = [1, 2, 5, 10, 100]
|
||||||
|
|
||||||
for n in testData:
|
for n in testData:
|
||||||
self.assertEqual(n, len(base91.from_decimal(0, n)))
|
self.assertEqual(n, len(base91.from_decimal(0, n)))
|
||||||
|
|
||||||
def test_invalid_padding(self):
|
def test_negative_width(self):
|
||||||
testData = [0, -1, -100]
|
for n in [0, -1, -100]:
|
||||||
|
self.assertEqual(base91.from_decimal(0, width=n), '!')
|
||||||
for n in testData:
|
|
||||||
self.assertRaises(ValueError, base91.from_decimal, 0, padding=n)
|
|
||||||
|
|
||||||
|
|
||||||
class b_ToDecimal(unittest.TestCase):
|
class b_ToDecimal(unittest.TestCase):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue