Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get(self, data_type, n=1):
try:
data_type = data_type.lower()
except AttributeError:
raise BinaryReaderException("Argument data_type has to be string")
data = self._read_data(data_type, n)
if n == 1:
return data[0]
else:
return data
def _read_data(self, data_type, n):
try:
num_of_bytes = self.DATA_TYPES[data_type]["num_of_bytes"]
except KeyError as e:
raise BinaryReaderException("Data type: '{}' is not valid. Valid types: [{}]".format(data_type,
", ".join(
self.DATA_TYPES.keys())))
substring = self._binary_data[self._pointer:self._pointer + num_of_bytes * n]
if len(substring) != num_of_bytes * n:
raise BinaryReaderException(
"Not possible to read {} bytes, length of binary data is {} bytes".format(num_of_bytes * n,
len(self)))
self._pointer += num_of_bytes * n
return struct.unpack("<{}".format(self.DATA_TYPES[data_type]["format_char"] * n), substring)
def __init__(self, message):
super(BinaryReaderException, self).__init__(message)
def _read_data(self, data_type, n):
try:
num_of_bytes = self.DATA_TYPES[data_type]["num_of_bytes"]
except KeyError as e:
raise BinaryReaderException("Data type: '{}' is not valid. Valid types: [{}]".format(data_type,
", ".join(
self.DATA_TYPES.keys())))
substring = self._binary_data[self._pointer:self._pointer + num_of_bytes * n]
if len(substring) != num_of_bytes * n:
raise BinaryReaderException(
"Not possible to read {} bytes, length of binary data is {} bytes".format(num_of_bytes * n,
len(self)))
self._pointer += num_of_bytes * n
return struct.unpack("<{}".format(self.DATA_TYPES[data_type]["format_char"] * n), substring)