Skip to content

Commit

Permalink
Make invalid value explicit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Diamond committed Sep 29, 2023
1 parent 8767f92 commit e8921c1
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions python/fusion_engine_client/parsers/file_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,15 @@ class FileIndex(object):

_DTYPE = np.dtype([('time', '<f8'), ('type', '<u2'), ('offset', '<u8'), ('message_index', '<u8')])

def __init__(self, index_path: str = None, data_path: str = None, delete_on_error=True,
data: Union[np.ndarray, list] = None, t0: Timestamp = None, enum_class = MessageType):
def __init__(self,
index_path: str = None,
data_path: str = None,
delete_on_error=True,
data: Union[np.ndarray,
list] = None,
t0: Timestamp = None,
enum_class=MessageType,
enum_class_invalid=MessageType.INVALID):
"""!
@brief Construct a new @ref FileIndex instance.
Expand All @@ -139,8 +146,10 @@ def __init__(self, index_path: str = None, data_path: str = None, delete_on_erro
`.p1log` file. For internal use.
@param t0 The P1 time corresponding with the start of the `.p1log` file, if known. For internal use.
@param enum_class The @ref IntEnum class used to identify the messages.
@param enum_class_invalid The value of enum_class to use for invalid entries.
"""
self.enum_class = enum_class
self.enum_class_invalid = enum_class_invalid
if data is None:
self._data = None
else:
Expand Down Expand Up @@ -195,7 +204,7 @@ def load(self, index_path, data_path=None, delete_on_error=True):
if not os.path.exists(data_path):
# If the user didn't explicitly set data_path and the default file doesn't exist, it is not considered
# an error.
if self._data['type'][-1] == self.enum_class.INVALID:
if self._data['type'][-1] == self.enum_class_invalid:
self._data = self._data[:-1]
return
elif not os.path.exists(data_path):
Expand All @@ -220,7 +229,7 @@ def load(self, index_path, data_path=None, delete_on_error=True):
# Get the last entry in the index. If its message type is INVALID, it's a special marker at the end of the
# index file indicating the size of the binary data file when the index was created. If it exists, we can
# use it to check if the data file size has changed.
if self.type[-1] == self.enum_class.INVALID:
if self.type[-1] == self.enum_class_invalid:
expected_data_file_size = self.offset[-1]
self._data = self._data[:-1]

Expand Down Expand Up @@ -271,9 +280,9 @@ def save(self, index_path: str, data_path: str):
if len(self._data) > 0:
# Append an EOF marker at the end of the data if data_path is specified.
data = self._data
if data['type'][-1] != self.enum_class.INVALID and data_path is not None:
if data['type'][-1] != self.enum_class_invalid and data_path is not None:
file_size_bytes = os.stat(data_path).st_size
data = np.append(data, np.array((np.nan, self.enum_class.INVALID, file_size_bytes, -1),
data = np.append(data, np.array((np.nan, self.enum_class_invalid, file_size_bytes, -1),
dtype=FileIndex._DTYPE))

raw_data = FileIndex._to_raw(data)
Expand Down Expand Up @@ -489,6 +498,7 @@ class FileIndexBuilder(object):
This class can be used to construct a @ref FileIndex and a corresponding `.p1i` file when reading a `.p1log` file.
"""

def __init__(self):
self.raw_data = []

Expand Down

0 comments on commit e8921c1

Please sign in to comment.