Skip to content

Commit cebb69c

Browse files
authored
Merge pull request #34 from saxbophone/josh/33-custom-error-classes
Create custom error classes
2 parents 5caf130 + 333d299 commit cebb69c

File tree

6 files changed

+48
-22
lines changed

6 files changed

+48
-22
lines changed

basest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
absolute_import, division, print_function, unicode_literals
55
)
66

7-
from . import core, encoders
7+
from . import core, encoders, exceptions
88

99

10-
__all__ = ['core', 'encoders']
10+
__all__ = ['core', 'encoders', 'exceptions']

basest/core/encode.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
absolute_import, division, print_function, unicode_literals
55
)
66

7+
from ..exceptions import ImproperUsageError
78
from .utils import ints_to_symbols, symbols_to_ints, validate_symbol_tables
89

910

@@ -38,10 +39,10 @@ def encode_raw(input_base, output_base, input_ratio, output_ratio, input_data):
3839
Special validation: if the output base is larger than the input base, then
3940
the length of the input data MUST be an exact multiple of the input ratio.
4041
Otherwise, the data will be corrupted if we continue, so we will raise
41-
ValueError instead.
42+
ImproperUsageError instead.
4243
'''
4344
if input_base < output_base and input_length % input_ratio != 0:
44-
raise ValueError(
45+
raise ImproperUsageError(
4546
'Input data length must be exact multiple of input ratio when '
4647
'output base is larger than input base'
4748
)

basest/core/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
absolute_import, division, print_function, unicode_literals
55
)
66

7+
from ..exceptions import InvalidSymbolTableError
8+
79

810
def ints_to_symbols(ints, symbol_table):
911
"""
@@ -39,12 +41,12 @@ def validate_symbol_tables(symbol_table, padding_symbol, other_symbol_table):
3941
"""
4042
Validates two symbol tables (the padding symbol being used alongside the
4143
first one).
42-
Raises ValueError if either of the symbol tables (or padding symbol) fail
43-
validation.
44+
Raises InvalidSymbolTableError if either of the symbol tables (or padding
45+
symbol) fail validation.
4446
"""
4547
# first check that they all do not contain None
4648
if None in (symbol_table + [padding_symbol] + other_symbol_table):
47-
raise ValueError(
49+
raise InvalidSymbolTableError(
4850
'None cannot be used in symbol tables nor for padding'
4951
)
5052
# if that check passes, validate tables (and padding) for uniqueness
@@ -53,4 +55,4 @@ def validate_symbol_tables(symbol_table, padding_symbol, other_symbol_table):
5355
(not _symbol_table_is_unique(symbol_table, padding_symbol)) or
5456
(not _symbol_table_is_unique(other_symbol_table))
5557
):
56-
raise ValueError('Unique symbol tables required')
58+
raise InvalidSymbolTableError('Unique symbol tables required')

basest/exceptions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class ImproperUsageError(ValueError):
6+
"""
7+
This exception is raised when an attempt is made to encode data using a
8+
larger output base than the input base AND when the length of the input
9+
data is not exactly divisible by the input ratio.
10+
11+
This cannot be allowed because such options would cause data corruption.
12+
"""
13+
pass
14+
15+
16+
class InvalidSymbolTableError(ValueError):
17+
"""
18+
This exception is raised when the symbol table and/or padding symbol
19+
supplied to an encoding/decoding operation are invalid.
20+
"""
21+
pass

tests/core/test_encode_decode.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ddt import data, ddt, unpack
1010

1111
from basest.core import decode, encode
12+
from basest.exceptions import InvalidSymbolTableError
1213

1314

1415
base64_alphabet = [
@@ -140,9 +141,9 @@ def test_encode_rejects_non_unique_symbol_tables(
140141
):
141142
"""
142143
When a non-unique input or output symbol table is passed to encode(),
143-
ValueError should be raised.
144+
InvalidSymbolTableError should be raised.
144145
"""
145-
with self.assertRaises(ValueError):
146+
with self.assertRaises(InvalidSymbolTableError):
146147
encode(
147148
len(input_symbol_table), input_symbol_table,
148149
len(output_symbol_table), output_symbol_table,
@@ -156,9 +157,9 @@ def test_encode_rejects_output_symbol_table_containing_padding_symbol(
156157
):
157158
"""
158159
When the output symbol table passed to encode() contains the padding
159-
symbol, ValueError should be raised.
160+
symbol, InvalidSymbolTableError should be raised.
160161
"""
161-
with self.assertRaises(ValueError):
162+
with self.assertRaises(InvalidSymbolTableError):
162163
encode(1, ['a'], 1, ['b'], 'b', 1, 1, [])
163164

164165
@data(
@@ -175,9 +176,9 @@ def test_encode_rejects_none_used_in_symbol_tables_and_padding(
175176
):
176177
"""
177178
When any of the symbol tables or the padding symbol passed to encode()
178-
are or contain None, ValueError should be raised.
179+
are or contain None, InvalidSymbolTableError should be raised.
179180
"""
180-
with self.assertRaises(ValueError):
181+
with self.assertRaises(InvalidSymbolTableError):
181182
encode(
182183
len(input_symbol_table), input_symbol_table,
183184
len(output_symbol_table), output_symbol_table,
@@ -294,9 +295,9 @@ def test_decode_rejects_non_unique_symbol_tables(
294295
):
295296
"""
296297
When a non-unique input or output symbol table is passed to decode(),
297-
ValueError should be raised.
298+
InvalidSymbolTableError should be raised.
298299
"""
299-
with self.assertRaises(ValueError):
300+
with self.assertRaises(InvalidSymbolTableError):
300301
decode(
301302
len(input_symbol_table), input_symbol_table,
302303
padding_symbol,
@@ -310,9 +311,9 @@ def test_decode_rejects_input_symbol_table_containing_padding_symbol(
310311
):
311312
"""
312313
When the input symbol table passed to decode() contains the padding
313-
symbol, ValueError should be raised.
314+
symbol, InvalidSymbolTableError should be raised.
314315
"""
315-
with self.assertRaises(ValueError):
316+
with self.assertRaises(InvalidSymbolTableError):
316317
decode(1, ['a'], 'a', 1, ['b'], 1, 1, [])
317318

318319
@data(
@@ -329,9 +330,9 @@ def test_decode_rejects_none_used_in_symbol_tables_and_padding(
329330
):
330331
"""
331332
When any of the symbol tables or the padding symbol passed to decode()
332-
are or contain None, ValueError should be raised.
333+
are or contain None, InvalidSymbolTableError should be raised.
333334
"""
334-
with self.assertRaises(ValueError):
335+
with self.assertRaises(InvalidSymbolTableError):
335336
decode(
336337
len(input_symbol_table), input_symbol_table,
337338
padding_symbol,

tests/core/test_encode_decode_raw.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ddt import data, ddt, unpack
1010

1111
from basest.core import decode_raw, encode_raw
12+
from basest.exceptions import ImproperUsageError
1213

1314

1415
@ddt
@@ -80,9 +81,9 @@ def test_encode_raw_invalid_input_ratio(
8081
input ratio. This is because such an action normally can be solved with
8182
padding, however padding can only be used successfully on the 'smaller'
8283
side of the transformation, in any other case data corruption occurs.
83-
If this is attempted, then ValueError should be raised.
84+
If this is attempted, then ImproperUsageError should be raised.
8485
"""
85-
with self.assertRaises(ValueError):
86+
with self.assertRaises(ImproperUsageError):
8687
encode_raw(
8788
input_base=input_base, output_base=output_base,
8889
input_ratio=input_ratio, output_ratio=output_ratio,

0 commit comments

Comments
 (0)