Skip to content

Commit 66334f1

Browse files
committed
Wrote code to make failing tests pass
1 parent 2e7d342 commit 66334f1

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

basest/core/decode.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
)
66

77
from .encode import encode_raw
8-
from .utils import ints_to_symbols, symbols_to_ints
8+
from .utils import ints_to_symbols, symbol_table_is_unique, symbols_to_ints
99

1010

1111
def decode_raw(input_base, output_base, input_ratio, output_ratio, input_data):
@@ -54,6 +54,12 @@ def decode(
5454
Assumes standard base64-style padding using the given input padding symbol,
5555
but can handle unpadded input just fine.
5656
"""
57+
# validate the uniqueness of both symbol tables before continuing
58+
if (
59+
(not symbol_table_is_unique(input_symbol_table, input_padding)) or
60+
(not symbol_table_is_unique(output_symbol_table))
61+
):
62+
raise ValueError('Unique symbol tables required')
5763
# create workon copy of input data and convert symbols to raw ints
5864
# NOTE: input symbol table here includes the padding character
5965
input_workon = symbols_to_ints(

basest/core/encode.py

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

7-
from .utils import ints_to_symbols, symbols_to_ints
7+
from .utils import ints_to_symbols, symbol_table_is_unique, symbols_to_ints
88

99

1010
def _nearest_length(input_length, input_ratio):
@@ -96,6 +96,12 @@ def encode(
9696
Uses standard base64-style padding if needed, using the given padding
9797
symbol.
9898
"""
99+
# validate the uniqueness of both symbol tables before continuing
100+
if (
101+
(not symbol_table_is_unique(input_symbol_table)) or
102+
(not symbol_table_is_unique(output_symbol_table, output_padding))
103+
):
104+
raise ValueError('Unique symbol tables required')
99105
# create workon copy of input data and convert symbols to raw ints
100106
input_workon = symbols_to_ints(input_data, input_symbol_table)
101107
# use encode_raw() to encode the data

basest/core/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ def symbols_to_ints(symbols, symbol_table):
1919
convert them to an iterable of ints and return this.
2020
"""
2121
return [symbol_table.index(s) for s in symbols]
22+
23+
24+
def symbol_table_is_unique(symbol_table, padding_symbol=None):
25+
"""
26+
Returns True if the given symbol table and padding symbol are unique,
27+
otherwise returns False.
28+
"""
29+
# simple way of checking if a list of hashables is unique
30+
if len(symbol_table) != len(set(symbol_table)):
31+
return False
32+
else:
33+
# otherwise, check that padding_symbol isn't in the symbol table
34+
# NOTE: this assumes that `None` is never in the symbol table
35+
return padding_symbol not in symbol_table

0 commit comments

Comments
 (0)