def _get_data_from_section(self, index): """ Get a dataset from an index within the data file. :param index: The desired index within the data file. :return: The data object at that index. """ try: header = self.metaheader[str(index)] except KeyError as ke: raise exceptions.DatatypeException( "Metaheader error: entry no. {} not found!".format(index))\ from ke self.file.seek(self.offset + header["start byte"]) num_bytes = header["end byte"] - header["start byte"] section = self.file.read(num_bytes) datatype = header['datatype'] if datatype == consts.Datatypes.EVENT.value: result = EventData.from_string(section) elif datatype == consts.Datatypes.POINT.value: result = PointData.from_string(section) elif datatype == consts.Datatypes.STACK.value: result = StackData.from_string(section) else: raise exceptions.DatatypeException( "Header error: datatype '{}' not recognised!".format(datatype)) return result
def from_string(string): """ Converts a standard representation into a :class:`PointDatum` object. Tolerant to whitespace/trailing linebreaks. :param string: The string to convert. :raises: exceptions.DatatypeException :return: The resulting :class:`PointDatum` object. """ try: x, y, info = string.strip().split(consts.field_separator) return PointDatum(x=float(x), y=float(y), info=info) except IndexError as ie: raise exceptions.DatatypeException( "PointDatum - not enough values in datatype string " "('{}')".format(string)) from ie except ValueError as ve: raise exceptions.DatatypeException( "PointDatum - could not convert datatype string " "('{}')".format(string)) from ve
def from_string(string): """ Converts a standard representation to a :class:`EventDatum` object. Tolerant to whitespace/trailing linebreaks. :param string: The string to convert. :raises: exceptions.DatatypeException :return: The resulting :class:`EventDatum` object. """ try: time, type_, datum, connected = \ string.strip().split(consts.field_separator) return EventDatum(time=int(time), type=type_, specific_datum=ast.literal_eval(datum), connected=ast.literal_eval(connected)) except IndexError as ie: raise exceptions.DatatypeException( "EventDatum - not enough values in datatype string " "('{}')".format(string)) from ie except ValueError as ve: raise exceptions.DatatypeException( "EventDatum - could not convert datatype string " "('{}')".format(string)) from ve
def get_interface_from_index(self, index): """ Get an interface name from an index in a file. :param index: The index within the file. :return: The corresponding interface name. """ try: header = self.metaheader[str(index)] except KeyError as ke: raise exceptions.DatatypeException( "Metaheader error: entry no. {} not found!".format(index)) \ from ke return header['interface']
def from_string(string): """ Converts a standard representation into a :class:`StackDatum` object. Tolerant to whitespace/trailing linebreaks. :param string: The string to convert. :raises: exceptions.DatatypeException :return: The resulting :class:`StackDatum` object. """ try: separated = string.strip().split(consts.field_separator) weight = separated[0] stack_tuple = tuple(separated[1:]) return StackDatum(weight=int(weight), stack=stack_tuple) except ValueError as ve: raise exceptions.DatatypeException( "StackDatum - could not convert datatype string " "('{}')".format(string)) from ve