Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions basest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
absolute_import, division, print_function, unicode_literals
)

from . import core, encoders
from . import core, encoders, exceptions


__all__ = ['core', 'encoders']
__all__ = ['core', 'encoders', 'exceptions']
5 changes: 3 additions & 2 deletions basest/core/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
absolute_import, division, print_function, unicode_literals
)

from ..exceptions import ImproperUsageError
from .utils import ints_to_symbols, symbols_to_ints, validate_symbol_tables


Expand Down Expand Up @@ -38,10 +39,10 @@ def encode_raw(input_base, output_base, input_ratio, output_ratio, input_data):
Special validation: if the output base is larger than the input base, then
the length of the input data MUST be an exact multiple of the input ratio.
Otherwise, the data will be corrupted if we continue, so we will raise
ValueError instead.
ImproperUsageError instead.
'''
if input_base < output_base and input_length % input_ratio != 0:
raise ValueError(
raise ImproperUsageError(
'Input data length must be exact multiple of input ratio when '
'output base is larger than input base'
)
Expand Down
10 changes: 6 additions & 4 deletions basest/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
absolute_import, division, print_function, unicode_literals
)

from ..exceptions import InvalidSymbolTableError


def ints_to_symbols(ints, symbol_table):
"""
Expand Down Expand Up @@ -39,12 +41,12 @@ def validate_symbol_tables(symbol_table, padding_symbol, other_symbol_table):
"""
Validates two symbol tables (the padding symbol being used alongside the
first one).
Raises ValueError if either of the symbol tables (or padding symbol) fail
validation.
Raises InvalidSymbolTableError if either of the symbol tables (or padding
symbol) fail validation.
"""
# first check that they all do not contain None
if None in (symbol_table + [padding_symbol] + other_symbol_table):
raise ValueError(
raise InvalidSymbolTableError(
'None cannot be used in symbol tables nor for padding'
)
# if that check passes, validate tables (and padding) for uniqueness
Expand All @@ -53,4 +55,4 @@ def validate_symbol_tables(symbol_table, padding_symbol, other_symbol_table):
(not _symbol_table_is_unique(symbol_table, padding_symbol)) or
(not _symbol_table_is_unique(other_symbol_table))
):
raise ValueError('Unique symbol tables required')
raise InvalidSymbolTableError('Unique symbol tables required')
21 changes: 21 additions & 0 deletions basest/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-


class ImproperUsageError(ValueError):
"""
This exception is raised when an attempt is made to encode data using a
larger output base than the input base AND when the length of the input
data is not exactly divisible by the input ratio.

This cannot be allowed because such options would cause data corruption.
"""
pass


class InvalidSymbolTableError(ValueError):
"""
This exception is raised when the symbol table and/or padding symbol
supplied to an encoding/decoding operation are invalid.
"""
pass
25 changes: 13 additions & 12 deletions tests/core/test_encode_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ddt import data, ddt, unpack

from basest.core import decode, encode
from basest.exceptions import InvalidSymbolTableError


base64_alphabet = [
Expand Down Expand Up @@ -140,9 +141,9 @@ def test_encode_rejects_non_unique_symbol_tables(
):
"""
When a non-unique input or output symbol table is passed to encode(),
ValueError should be raised.
InvalidSymbolTableError should be raised.
"""
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSymbolTableError):
encode(
len(input_symbol_table), input_symbol_table,
len(output_symbol_table), output_symbol_table,
Expand All @@ -156,9 +157,9 @@ def test_encode_rejects_output_symbol_table_containing_padding_symbol(
):
"""
When the output symbol table passed to encode() contains the padding
symbol, ValueError should be raised.
symbol, InvalidSymbolTableError should be raised.
"""
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSymbolTableError):
encode(1, ['a'], 1, ['b'], 'b', 1, 1, [])

@data(
Expand All @@ -175,9 +176,9 @@ def test_encode_rejects_none_used_in_symbol_tables_and_padding(
):
"""
When any of the symbol tables or the padding symbol passed to encode()
are or contain None, ValueError should be raised.
are or contain None, InvalidSymbolTableError should be raised.
"""
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSymbolTableError):
encode(
len(input_symbol_table), input_symbol_table,
len(output_symbol_table), output_symbol_table,
Expand Down Expand Up @@ -294,9 +295,9 @@ def test_decode_rejects_non_unique_symbol_tables(
):
"""
When a non-unique input or output symbol table is passed to decode(),
ValueError should be raised.
InvalidSymbolTableError should be raised.
"""
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSymbolTableError):
decode(
len(input_symbol_table), input_symbol_table,
padding_symbol,
Expand All @@ -310,9 +311,9 @@ def test_decode_rejects_input_symbol_table_containing_padding_symbol(
):
"""
When the input symbol table passed to decode() contains the padding
symbol, ValueError should be raised.
symbol, InvalidSymbolTableError should be raised.
"""
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSymbolTableError):
decode(1, ['a'], 'a', 1, ['b'], 1, 1, [])

@data(
Expand All @@ -329,9 +330,9 @@ def test_decode_rejects_none_used_in_symbol_tables_and_padding(
):
"""
When any of the symbol tables or the padding symbol passed to decode()
are or contain None, ValueError should be raised.
are or contain None, InvalidSymbolTableError should be raised.
"""
with self.assertRaises(ValueError):
with self.assertRaises(InvalidSymbolTableError):
decode(
len(input_symbol_table), input_symbol_table,
padding_symbol,
Expand Down
5 changes: 3 additions & 2 deletions tests/core/test_encode_decode_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ddt import data, ddt, unpack

from basest.core import decode_raw, encode_raw
from basest.exceptions import ImproperUsageError


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