Skip to content
Open
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
23 changes: 18 additions & 5 deletions data_compression/coordinate_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,21 @@ def compress(self, original: float | str) -> int:
original: The value to compress.

Returns:
The compressed integer, or -1 if not found in the original list.
The compressed integer.

Raises:
KeyError: If ``original`` was not part of the input list.

>>> arr = [100, 10, 52, 83]
>>> cc = CoordinateCompressor(arr)
>>> cc.compress(100)
3
>>> cc.compress(7) # Value not in the original list
-1
Traceback (most recent call last):
...
KeyError: 7
"""
return self.coordinate_map.get(original, -1)
return self.coordinate_map[original]

def decompress(self, num: int) -> int | float | str:
"""
Expand All @@ -108,14 +113,22 @@ def decompress(self, num: int) -> int | float | str:
Returns:
The original value.

Raises:
IndexError: If ``num`` is not a valid compressed coordinate.

>>> arr = [100, 10, 52, 83]
>>> cc = CoordinateCompressor(arr)
>>> cc.decompress(0)
10
>>> cc.decompress(5) # Compressed coordinate out of range
-1
Traceback (most recent call last):
...
IndexError: compressed coordinate 5 is out of range
"""
return self.reverse_map[num] if 0 <= num < len(self.reverse_map) else -1
if not 0 <= num < len(self.reverse_map):
msg = f"compressed coordinate {num} is out of range"
raise IndexError(msg)
return self.reverse_map[num]


if __name__ == "__main__":
Expand Down