diff --git a/aprslib/base91.py b/aprslib/base91.py index 6ce29f4..283f152 100644 --- a/aprslib/base91.py +++ b/aprslib/base91.py @@ -51,21 +51,19 @@ def to_decimal(text): 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. - With optional padding to a specific length. + With optional parameter for fix with output """ text = [] if not isinstance(number, int_type): raise TypeError("Expected number to be int, got %s", type(number)) - elif not isinstance(padding, int_type): - raise TypeError("Expected padding to be int, got %s", type(number)) + elif not isinstance(width, int_type): + raise TypeError("Expected width to be int, got %s", type(number)) elif number < 0: raise ValueError("Expected number to be positive integer") - elif padding < 1: - raise ValueError("Expected padding to be >0") elif number > 0: max_n = ceil(log(number) / log(91)) @@ -73,9 +71,4 @@ def from_decimal(number, padding=1): quotient, number = divmod(number, 91**n) text.append(chr(33 + quotient)) - text = "".join(text).lstrip('!') - - # add padding if necessary - text = '!' * (padding-len(text)) + text - - return text + return "".join(text).lstrip('!').rjust(max(1, width), '!') diff --git a/tests/test_base91.py b/tests/test_base91.py index 6e2fc7a..f6924ef 100644 --- a/tests/test_base91.py +++ b/tests/test_base91.py @@ -34,23 +34,21 @@ class a_FromDecimal(unittest.TestCase): for n in testData: 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] for n in testData: 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] for n in testData: self.assertEqual(n, len(base91.from_decimal(0, n))) - def test_invalid_padding(self): - testData = [0, -1, -100] - - for n in testData: - self.assertRaises(ValueError, base91.from_decimal, 0, padding=n) + def test_negative_width(self): + for n in [0, -1, -100]: + self.assertEqual(base91.from_decimal(0, width=n), '!') class b_ToDecimal(unittest.TestCase):