def topic_frame(self, item, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): item = item.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) size = self._topic_count() dtss = (_DateTimeStamp * size)() labs = [_BUF_(label_str_max + 1) for _ in range(size)] strs = [_BUF_(data_str_max + 1) for _ in range(size)] labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs]) strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetTopicFrameStrings", self._name, item.encode("ascii"), strs_array, size, data_str_max + 1, labs_array if labels else _ppchar_(), label_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), arg_list=[ _str_, _str_, _ppchar_, _ulong_, _ulong_, _ppchar_, _ulong_, _PTR_(_DateTimeStamp) ]) if labels: l_map = map(_cast_cstr, labs_array) _nt_ = _gen_namedtuple(_str_clean(item)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(map(_cast_cstr, strs_array), adj_dts)) else: return _nt_(*map(_cast_cstr, strs_array)) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(map(_cast_cstr, strs_array), adj_dts)) else: return list(map(_cast_cstr, strs_array))
def get(self, item, topic, date_time=False, indx=0, check_indx=True, data_str_max=STR_DATA_SZ): item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if indx < 0: indx += self._block_size if indx >= self._block_size: raise TOSDB_IndexError("invalid index value passed to get()") if check_indx and indx >= self.stream_occupancy(item, topic): raise TOSDB_DataError("data not available at this index yet " + "(disable check_indx to avoid this error)") dts = _DateTimeStamp() tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] == "String": ret_str = _BUF_(data_str_max + 1) _lib_call("TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, ret_str, data_str_max + 1, _pointer(dts) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _pchar_, _uint32_, _PTR_(_DateTimeStamp))) if date_time : return (ret_str.value.decode(), TOSDB_DateTime(dts)) else: return ret_str.value.decode() else: val = tytup[1]() _lib_call("TOSDB_Get"+tytup[0], self._name, item.encode("ascii"), topic.encode("ascii"), indx, _pointer(val), _pointer(dts) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _PTR_(tytup[1]), _PTR_(_DateTimeStamp))) if date_time: return (val.value, TOSDB_DateTime(dts)) else: return val.value
def type_string(topic): """ Returns a platform-dependent string of the type of a particular 'topic' topic: string representing a TOS data field('LAST','ASK', etc) """ s = _BUF_(MAX_STR_SZ + 1) _lib_call("TOSDB_GetTypeString", topic.upper().encode("ascii"), s, (MAX_STR_SZ + 1)) return s.value.decode()
def topic_frame(self, item, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): item = item.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item( item) size = self._topic_count() dtss = (_DateTimeStamp * size)() labs = [_BUF_(label_str_max + 1) for _ in range(size)] strs = [_BUF_(data_str_max + 1) for _ in range(size)] labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs]) strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetTopicFrameStrings", self._name, item.encode("ascii"), strs_array, size, data_str_max + 1, labs_array if labels else _ppchar_(), label_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _ppchar_, _uint32_, _uint32_, _ppchar_, _uint32_, _PTR_(_DateTimeStamp))) if labels: l_map = map(_cast_cstr, labs_array) _nt_ = _gen_namedtuple(_str_clean(item)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(map(_cast_cstr, strs_array), adj_dts)) else: return _nt_(*map(_cast_cstr, strs_array)) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(map(_cast_cstr, strs_array), adj_dts)) else: return list(map(_cast_cstr, strs_array))
def type_string(topic): """ Returns a platform-dependent string of the type of a particular 'topic' topic: string representing a TOS data field('LAST','ASK', etc) """ tystr = _BUF_(MAX_STR_SZ + 1) _lib_call("TOSDB_GetTypeString", topic.upper().encode("ascii"), tystr, (MAX_STR_SZ + 1)) return tystr.value.decode()
def type_string( topic ): """ Returns a platform-dependent string of the type of a particular 'topic' topic: string representing a TOS data field('LAST','ASK', etc) """ tystr = _BUF_( MAX_STR_SZ + 1 ) err = _lib_call( "TOSDB_GetTypeString", topic.upper().encode("ascii"), tystr, (MAX_STR_SZ + 1) ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] returned" + "from library call","TOSDB_GetTypeString" ) return tystr.value.decode()
def items(self, str_max=MAX_STR_SZ): size = self._item_count() strs = [_BUF_(str_max + 1) for _ in range(size)] strs_array = (_pchar_* size)(*[ _cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetItemNames", self._name, strs_array, size, str_max + 1, arg_types=(_str_, _ppchar_, _uint32_, _uint32_)) return [_cast_cstr(s) for s in strs_array]
def topics_precached(self, str_max=MAX_STR_SZ): size = self._topic_precached_count() strs = [_BUF_(str_max + 1) for _ in range(size)] strs_array = (_pchar_* size)(*[ _cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetPreCachedTopicNames", self._name, strs_array, size, str_max + 1, arg_types=(_str_, _ppchar_, _uint32_, _uint32_)) return [_cast_cstr(s) for s in strs_array]
def items(self, str_max=MAX_STR_SZ): size = self._item_count() strs = [_BUF_(str_max + 1) for _ in range(size)] strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetItemNames", self._name, strs_array, size, str_max + 1, arg_list=[_str_, _ppchar_, _ulong_, _ulong_]) return [_cast_cstr(s) for s in strs_array]
def topics(self, str_max=MAX_STR_SZ): size = self._topic_count() strs = [_BUF_(str_max + 1) for _ in range(size)] strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetTopicNames", self._name, strs_array, size, str_max + 1, arg_types=(_str_, _ppchar_, _uint32_, _uint32_)) return [_cast_cstr(s) for s in strs_array]
def type_string(topic): """ Returns a platform-dependent string of the type of a particular 'topic' topic: string representing a TOS data field('LAST','ASK', etc) """ tystr = _BUF_(MAX_STR_SZ + 1) err = _lib_call("TOSDB_GetTypeString", topic.upper().encode("ascii"), tystr, (MAX_STR_SZ + 1)) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] returned" + "from library call", "TOSDB_GetTypeString") return tystr.value.decode()
def type_string(topic): """ Returns a platform-dependent string of the type of a particular topic type_string(topic) topic :: str :: topic string ('LAST','ASK', etc) returns -> str throws TOSDB_CLibError """ s = _BUF_(MAX_STR_SZ + 1) _lib_call("TOSDB_GetTypeString", topic.upper().encode("ascii"), s, (MAX_STR_SZ + 1)) return s.value.decode()
def _get_string(self, item, topic, date_time, indx, data_str_max): dt = _DateTimeStamp() s = _BUF_(data_str_max + 1) _lib_call("TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, s, data_str_max + 1, _pointer(dt) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _pchar_, _uint32_, _PTR_(_DateTimeStamp))) s = s.value.decode() return (s, TOSDB_DateTime(dt)) if date_time else s
def topics( self, str_max = MAX_STR_SZ ): """ Returns the topics currently in the block (and not pre-cached). str_max: the maximum length of topic strings returned returns -> list of strings """ size = self._topic_count() # store char buffers strs = [ _BUF_(str_max + 1) for _ in range(size)] # cast char buffers into (char*)[ ] strs_array = ( _pchar_* size)( *[ _cast(s, _pchar_) for s in strs] ) err =_lib_call( "TOSDB_GetTopicNames", self._name, strs_array, size, str_max + 1, arg_list = [ _str_, _ppchar_, _ulong_, _ulong_ ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] returned" + "from library call", "TOSDB_GetTopicNames" ) else: return [ _cast(ptr, _str_).value.decode() for ptr in strs_array ]
def topics(self, str_max=MAX_STR_SZ): """ Returns the topics currently in the block (and not pre-cached). str_max: the maximum length of topic strings returned returns -> list of strings """ size = self._topic_count() # store char buffers strs = [_BUF_(str_max + 1) for _ in range(size)] # cast char buffers into (char*)[ ] strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) err = _lib_call("TOSDB_GetTopicNames", self._name, strs_array, size, str_max + 1, arg_list=[_str_, _ppchar_, _ulong_, _ulong_]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] returned" + "from library call", "TOSDB_GetTopicNames") else: return [_cast(ptr, _str_).value.decode() for ptr in strs_array]
def get(self, item, topic, date_time=False, indx=0, check_indx=True, data_str_max=STR_DATA_SZ): """ Return a single data-point from the data-stream item: any item string in the block topic: any topic string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object indx: index of data-points [0 to block_size), [-block_size to -1] check_indx: throw if datum doesn't exist at that particular index data_str_max: the maximum size of string data returned """ item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if indx < 0: indx += self._block_size if indx >= self._block_size: raise TOSDB_IndexError("invalid index value passed to get()") if check_indx and indx >= self.stream_occupancy(item, topic): raise TOSDB_DataError("data not available at this index yet " + "(disable check_indx to avoid this error)") dts = _DateTimeStamp() tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] == "String": ret_str = _BUF_(data_str_max + 1) err =_lib_call( "TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, ret_str, data_str_max + 1, ( _pointer(dts) if date_time \ else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _str_, _long_, _pchar_, _ulong_, _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_GetString") if date_time: return (ret_str.value.decode(), TOSDB_DateTime(dts)) else: return ret_str.value.decode() else: val = tytup[1]() err = _lib_call( "TOSDB_Get"+tytup[0], self._name, item.encode("ascii"), topic.encode("ascii"), indx, _pointer(val), ( _pointer(dts) if date_time \ else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _str_, _long_, _PTR_(tytup[1]), _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_Get" + tytup[0]) if date_time: return (val.value, TOSDB_DateTime(dts)) else: return val.value
def stream_snapshot(self, item, topic, date_time=False, end=-1, beg=0, smart_size=True, data_str_max=STR_DATA_SZ): """ Return multiple data-points(a snapshot) from the data-stream item: any item string in the block topic: any topic string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object end: index of least recent data-point ( end of the snapshot ) beg: index of most recent data-point ( beginning of the snapshot ) smart_size: limits amount of returned data by data-stream's occupancy data_str_max: the maximum length of string data returned if date_time is True: returns-> list of 2tuple else: returns -> list """ item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if end < 0: end += self._block_size if beg < 0: beg += self._block_size if smart_size: end = min(end, self.stream_occupancy(item, topic) - 1) size = (end - beg) + 1 if beg < 0 or end < 0 \ or beg >= self._block_size or end >= self._block_size \ or size <= 0: raise TOSDB_IndexError("invalid 'beg' and/or 'end' index value(s)") dtss = (_DateTimeStamp * size)() tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] == "String": # store char buffers strs = [_BUF_(data_str_max + 1) for _ in range(size)] # cast char buffers into (char*)[ ] strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) err = _lib_call("TOSDB_GetStreamSnapshotStrings", self._name, item.encode("ascii"), topic.encode("ascii"), strs_array, size, data_str_max + 1, (dtss if date_time else _PTR_(_DateTimeStamp)()), end, beg, arg_list=[ _str_, _str_, _str_, _ppchar_, _ulong_, _ulong_, _PTR_(_DateTimeStamp), _long_, _long_ ]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_GetStreamSnapshotStrings") if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return [ _ for _ in \ zip( map(lambda x: _cast(x,_str_).value.decode(), strs_array ), adj_dts ) ] else: return [_cast(ptr, _str_).value.decode() for ptr in strs_array] else: num_array = (tytup[1] * size)() err = _lib_call("TOSDB_GetStreamSnapshot" + tytup[0] + "s", self._name, item.encode("ascii"), topic.encode("ascii"), num_array, size, (dtss if date_time else _PTR_(_DateTimeStamp)()), end, beg, arg_list=[ _str_, _str_, _str_, _PTR_(tytup[1]), _ulong_, _PTR_(_DateTimeStamp), _long_, _long_ ]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_GetStreamSnapshot" + tytup[0] + "s") if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return [_ for _ in zip(num_array, adj_dts)] else: return [_ for _ in num_array]
def stream_snapshot_from_marker(self, item, topic, date_time=False, beg=0, margin_of_safety=100, throw_if_data_lost=True, data_str_max=STR_DATA_SZ): """ Return multiple data-points(a snapshot) from the data-stream, ending where the last call began It's likely the stream will grow between consecutive calls. This call guarantees to pick up where the last get(), stream_snapshot(), or stream_snapshot_from_marker() call ended (under a few assumptions, see below). Internally the stream maintains a 'marker' that tracks the position of the last value pulled; the act of retreiving data and moving the marker can be thought of as a single, 'atomic' operation. There are three states to be aware of: 1) a 'beg' value that is greater than the marker (even if beg = 0) 2) a marker that moves through the entire stream and hits the bound 3) passing a buffer that is too small for the whole range State (1) can be caused by passing in a beginning index that is past the current marker, or by passing in 0 when the marker has yet to move. 'None' will be returned. State (2) occurs when the marker doesn't get reset before it hits the bound (block_size); as the oldest data is popped of the back of the stream it is lost (the marker can't grow past the end of the stream). State (3) occurs when an inadequately small buffer is used. The call handles buffer sizing for you by calling down to get the marker index, adjusting by 'beg' and 'margin_of_safety'. The latter helps assure the marker doesn't outgrow the buffer by the time the low-level retrieval operation completes. The default value indicates that over 100 push operations would have to take place during this call(highly unlikely). In either case (state (2) or (3)) if throw_if_data_lost is True a TOSDB_DataError will be thrown, otherwise the available data will be returned as normal. item: any item string in the block topic: any topic string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object beg: index of most recent data-point ( beginning of the snapshot ) margin_of_safety: (True/False) error margin for async stream growth throw_if_data_loss: (True/False) how to handle error states (see above) data_str_max: the maximum length of string data returned if beg > internal marker value: returns -> None if date_time is True: returns-> list of 2tuple else: returns -> list """ item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if beg < 0: beg += self._block_size if beg < 0 or beg >= self._block_size: raise TOSDB_IndexError("invalid 'beg' index value") if margin_of_safety < MIN_MARGIN_OF_SAFETY: raise TOSDB_ValueError("margin_of_safety < MIN_MARGIN_OF_SAFETY") is_dirty = _uint_() err = _lib_call("TOSDB_IsMarkerDirty", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(is_dirty), arg_list=[_str_, _str_, _str_, _PTR_(_uint_)]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_IsMarkerDirty") if is_dirty and throw_if_data_lost: raise TOSDB_DataError("marker is already dirty") mpos = _longlong_() err2 = _lib_call("TOSDB_GetMarkerPosition", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(mpos), arg_list=[_str_, _str_, _str_, _PTR_(_longlong_)]) if err2: raise TOSDB_CLibError( "error value [ " + str(err2) + " ] " + "returned from library call", "TOSDB_GetMarkerPosition") cur_sz = mpos.value - beg + 1 if cur_sz < 0: return None safe_sz = cur_sz + margin_of_safety dtss = (_DateTimeStamp * safe_sz)() tbits = type_bits(topic) tytup = _type_switch(tbits) get_size = _long_() if tytup[0] == "String": # store char buffers strs = [_BUF_(data_str_max + 1) for _ in range(safe_sz)] # cast char buffers into (char*)[ ] strs_array = (_pchar_ * safe_sz)(*[cast(s, _pchar_) for s in strs]) err3 = _lib_call("TOSDB_GetStreamSnapshotStringsFromMarker", self._name, item.encode("ascii"), topic.encode("ascii"), strs_array, safe_sz, data_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), beg, _pointer(get_size), arg_list=[ _str_, _str_, _str_, _ppchar_, _ulong_, _ulong_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_) ]) if err3: raise TOSDB_CLibError( "error value [ " + str(err3) + " ] returned from library call", "TOSDB_GetStreamSnapshotStrings" \ + "FromMarker") get_size = get_size.value if get_size == 0: return None elif get_size < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: get_size *= -1 if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss[:get_size]] return [ _ for _ in \ zip( map( lambda x : cast(x, _str_).value.decode(), strs_array[:get_size] ), adj_dts ) ] else: return [ cast(ptr, _str_).value.decode() for ptr in strs_array[:get_size] ] else: num_array = (tytup[1] * safe_sz)() err3 = _lib_call( "TOSDB_GetStreamSnapshot" \ + tytup[0] + "sFromMarker" , self._name, item.encode("ascii"), topic.encode("ascii"), num_array, safe_sz, dtss if date_time else _PTR_(_DateTimeStamp)(), beg, _pointer( get_size ), arg_list = [ _str_, _str_, _str_, _PTR_(tytup[1]), _ulong_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_) ] ) if err3: raise TOSDB_CLibError( "error value of [ " + str(err3) + " ] returned from library call", "TOSDB_GetStreamSnapshot" \ + tytup[0] + "sFromMarker" ) get_size = get_size.value if get_size == 0: return None elif get_size < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: get_size *= -1 if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss[:get_size]] return [_ for _ in zip(num_array[:get_size], adj_dts)] else: return [_ for _ in num_array[:get_size]]
def item_frame(self, topic, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): """ Return all the most recent item values for a particular topic. topic: any topic string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object labels: (True/False) pull the item labels with the values data_str_max: the maximum length of string data returned label_str_max: the maximum length of item label strings returned if labels and date_time are True: returns-> namedtuple of 2tuple if labels is True: returns -> namedtuple if date_time is True: returns -> list of 2tuple else returns-> list """ topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_topic(topic) size = self._item_count() dtss = (_DateTimeStamp * size)() # store char buffers labs = [_BUF_(label_str_max + 1) for _ in range(size)] # cast char buffers int (char*)[ ] labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs]) tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] is "String": # store char buffers strs = [_BUF_(data_str_max + 1) for _ in range(size)] strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) # cast char buffers int (char*)[ ] err = _lib_call("TOSDB_GetItemFrameStrings", self._name, topic.encode("ascii"), strs_array, size, data_str_max + 1, (labs_array if labels else _ppchar_()), label_str_max + 1, (dtss if date_time else _PTR_(_DateTimeStamp)()), arg_list=[ _str_, _str_, _ppchar_, _ulong_, _ulong_, _ppchar_, _ulong_, _PTR_(_DateTimeStamp) ]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_GetItemFrameStrings") s_map = map(lambda x: _cast(x, _str_).value.decode(), strs_array) if labels: l_map = map(lambda x: _cast(x, _str_).value.decode(), labs_array) _nt_ = _gen_namedtuple( _str_clean(topic)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(s_map, adj_dts)) else: return _nt_(*s_map) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(s_map, adj_dts)) else: return list(s_map) else: num_array = (tytup[1] * size)() err = _lib_call("TOSDB_GetItemFrame" + tytup[0] + "s", self._name, topic.encode("ascii"), num_array, size, (labs_array if labels else _ppchar_()), label_str_max + 1, (dtss if date_time else _PTR_(_DateTimeStamp)()), arg_list=[ _str_, _str_, _PTR_(tytup[1]), _ulong_, _ppchar_, _ulong_, _PTR_(_DateTimeStamp) ]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_GetItemFrame" + tytup[0] + "s") if labels: l_map = map(lambda x: _cast(x, _str_).value.decode(), labs_array) _nt_ = _gen_namedtuple( _str_clean(topic)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(num_array, adj_dts)) else: return _nt_(*num_array) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(num_array, adj_dts)) else: return [_ for _ in num_array]
def topic_frame(self, item, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): """ Return all the most recent topic values for a particular item: item: any item string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object labels: (True/False) pull the topic labels with the values data_str_max: the maximum length of string data returned label_str_max: the maximum length of topic label strings returned if labels and date_time are True: returns-> namedtuple of 2tuple if labels is True: returns -> namedtuple if date_time is True: returns -> list of 2tuple else returns-> list """ item = item.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) size = self._topic_count() dtss = (_DateTimeStamp * size)() # cast char buffers int (char*)[ ] labs = [_BUF_(label_str_max + 1) for _ in range(size)] strs = [_BUF_(data_str_max + 1) for _ in range(size)] # cast char buffers int (char*)[ ] labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs]) strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) err = _lib_call("TOSDB_GetTopicFrameStrings", self._name, item.encode("ascii"), strs_array, size, data_str_max + 1, (labs_array if labels else _ppchar_()), label_str_max + 1, (dtss if date_time else _PTR_(_DateTimeStamp)()), arg_list=[ _str_, _str_, _ppchar_, _ulong_, _ulong_, _ppchar_, _ulong_, _PTR_(_DateTimeStamp) ]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_GetTopicFrameStrings") s_map = map(lambda x: _cast(x, _str_).value.decode(), strs_array) if labels: l_map = map(lambda x: _cast(x, _str_).value.decode(), labs_array) _nt_ = _gen_namedtuple(_str_clean(item)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(s_map, adj_dts)) else: return _nt_(*s_map) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(s_map, adj_dts)) else: return list(s_map)
c_long as _long_, \ c_longlong as _longlong_, \ c_char_p as _str_, \ c_char as _char_, \ c_ubyte as _uchar_, \ c_int as _int_, \ c_void_p as _pvoid_, \ c_uint as _uint_, \ c_uint32 as _uint32_, \ c_uint8 as _uint8_ _pchar_ = _PTR_(_char_) _ppchar_ = _PTR_(_pchar_) _cast_cstr = lambda x: _cast(x, _str_).value.decode() _gen_str_buffers = lambda sz, n: [_BUF_(sz) for _ in range(n)] _gen_str_buffers_ptrs = lambda bufs: (_pchar_ * len(bufs))( *[_cast(b, _pchar_) for b in bufs]) _map_cstr = _partial(map, _cast_cstr) _map_dt = _partial(map, TOSDB_DateTime) _zip_cstr_dt = lambda cstr, dt: zip(_map_cstr(cstr), _map_dt(dt)) DLL_BASE_NAME = "tos-databridge" DLL_DEPENDS1_NAME = "_tos-databridge" SYS_ARCH_TYPE = "x64" if (_log(_maxsize * 2, 2) > 33) else "x86" MIN_MARGIN_OF_SAFETY = 10 _REGEX_NON_ALNUM = _compile("[\W+]") _REGEX_LETTER = _compile("[a-zA-Z]") _VER_SFFX = '[\d]{1,2}.[\d]{1,2}'
def stream_snapshot_from_marker(self, item, topic, date_time=False, beg=0, margin_of_safety=100, throw_if_data_lost=True, data_str_max=STR_DATA_SZ): item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if beg < 0: beg += self._block_size if beg < 0 or beg >= self._block_size: raise TOSDB_IndexError("invalid 'beg' index value") if margin_of_safety < MIN_MARGIN_OF_SAFETY: raise TOSDB_ValueError("margin_of_safety < MIN_MARGIN_OF_SAFETY") is_dirty = _uint_() _lib_call("TOSDB_IsMarkerDirty", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(is_dirty), arg_types=(_str_, _str_, _str_, _PTR_(_uint_))) if is_dirty and throw_if_data_lost: raise TOSDB_DataError("marker is already dirty") mpos = _longlong_() _lib_call("TOSDB_GetMarkerPosition", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(mpos), arg_types=(_str_, _str_, _str_, _PTR_(_longlong_))) cur_sz = mpos.value - beg + 1 if cur_sz < 0: return None safe_sz = cur_sz + margin_of_safety dtss = (_DateTimeStamp * safe_sz)() tbits = type_bits(topic) tytup = _type_switch(tbits) get_size = _long_() if tytup[0] == "String": strs = [_BUF_(data_str_max + 1) for _ in range(safe_sz)] strs_array = (_pchar_ * safe_sz)(*[_cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetStreamSnapshotStringsFromMarker", self._name, item.encode("ascii"), topic.encode("ascii"), strs_array, safe_sz, data_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), beg, _pointer(get_size), arg_types=(_str_, _str_, _str_, _ppchar_, _uint32_, _uint32_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_))) get_size = get_size.value if get_size == 0: return None elif get_size < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: get_size *= -1 if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss[:get_size]] return [ sd for sd in zip(map(_cast_cstr, strs_array[:get_size]), adj_dts) ] else: return [_cast_cstr(s) for s in strs_array[:get_size]] else: num_array = (tytup[1] * safe_sz)() _lib_call("TOSDB_GetStreamSnapshot" + tytup[0] + "sFromMarker", self._name, item.encode("ascii"), topic.encode("ascii"), num_array, safe_sz, dtss if date_time else _PTR_(_DateTimeStamp)(), beg, _pointer(get_size), arg_types=(_str_, _str_, _str_, _PTR_(tytup[1]), _uint32_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_))) get_size = get_size.value if get_size == 0: return None elif get_size < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: get_size *= -1 if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss[:get_size]] return [nd for nd in zip(num_array[:get_size], adj_dts)] else: return [n for n in num_array[:get_size]]
def stream_snapshot(self, item, topic, date_time=False, end=-1, beg=0, smart_size=True, data_str_max=STR_DATA_SZ): item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if end < 0: end += self._block_size if beg < 0: beg += self._block_size size = (end - beg) + 1 if beg < 0 \ or end < 0 \ or beg >= self._block_size \ or end >= self._block_size \ or size <= 0: raise TOSDB_IndexError("invalid 'beg' and/or 'end' index value(s)") if smart_size: so = self.stream_occupancy(item, topic) if so == 0 or so <= beg: return [] end = min(end, so - 1) beg = min(beg, so - 1) size = (end - beg) + 1 dtss = (_DateTimeStamp * size)() tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] == "String": strs = [_BUF_( data_str_max +1) for _ in range(size)] strs_array = (_pchar_ * size)(*[ _cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetStreamSnapshotStrings", self._name, item.encode("ascii"), topic.encode("ascii"), strs_array, size, data_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), end, beg, arg_types=(_str_, _str_, _str_, _ppchar_, _uint32_, _uint32_, _PTR_(_DateTimeStamp), _long_, _long_)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return [sd for sd in zip(map(_cast_cstr, strs_array), adj_dts)] else: return [_cast_cstr(s) for s in strs_array] else: num_array = (tytup[1] * size)() _lib_call("TOSDB_GetStreamSnapshot"+tytup[0]+"s", self._name, item.encode("ascii"), topic.encode("ascii"), num_array, size, dtss if date_time else _PTR_(_DateTimeStamp)(), end, beg, arg_types=(_str_, _str_, _str_, _PTR_(tytup[1]), _uint32_, _PTR_(_DateTimeStamp), _long_, _long_)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return [nd for nd in zip(num_array,adj_dts)] else: return [n for n in num_array]
def get( self, item, topic, date_time = False, indx = 0, check_indx = True, data_str_max = STR_DATA_SZ ): """ Return a single data-point from the data-stream item: any item string in the block topic: any topic string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object indx: index of data-points [0 to block_size), [-block_size to -1] check_indx: throw if datum doesn't exist at that particular index data_str_max: the maximum size of string data returned """ item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if indx < 0: indx += self._block_size if indx >= self._block_size: raise TOSDB_IndexError( "invalid index value passed to get()" ) if check_indx and indx >= self.stream_occupancy( item, topic ): raise TOSDB_DataError( "data not available at this index yet " + "(disable check_indx to avoid this error)" ) dts = _DateTimeStamp() tbits = type_bits( topic ) tytup = _type_switch( tbits ) if tytup[0] == "String": ret_str = _BUF_( data_str_max + 1 ) err =_lib_call( "TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, ret_str, data_str_max + 1, ( _pointer(dts) if date_time \ else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _str_, _long_, _pchar_, _ulong_, _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_GetString" ) if date_time : return (ret_str.value.decode(), TOSDB_DateTime( dts )) else: return ret_str.value.decode() else: val = tytup[1]() err = _lib_call( "TOSDB_Get"+tytup[0], self._name, item.encode("ascii"), topic.encode("ascii"), indx, _pointer(val), ( _pointer(dts) if date_time \ else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _str_, _long_, _PTR_(tytup[1]), _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_Get"+tytup[0] ) if date_time: return (val.value, TOSDB_DateTime( dts )) else: return val.value
def stream_snapshot( self, item, topic, date_time = False, end = -1, beg = 0, smart_size = True, data_str_max = STR_DATA_SZ ): """ Return multiple data-points(a snapshot) from the data-stream item: any item string in the block topic: any topic string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object end: index of least recent data-point ( end of the snapshot ) beg: index of most recent data-point ( beginning of the snapshot ) smart_size: limits amount of returned data by data-stream's occupancy data_str_max: the maximum length of string data returned if date_time is True: returns-> list of 2tuple else: returns -> list """ item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if end < 0: end += self._block_size if beg < 0: beg += self._block_size if smart_size: end = min(end, self.stream_occupancy( item, topic ) - 1 ) size = (end - beg) + 1 if beg < 0 or end < 0 \ or beg >= self._block_size or end >= self._block_size \ or size <= 0: raise TOSDB_IndexError("invalid 'beg' and/or 'end' index value(s)") dtss = (_DateTimeStamp * size)() tbits = type_bits( topic ) tytup = _type_switch( tbits ) if tytup[0] == "String": # store char buffers strs = [ _BUF_( data_str_max +1 ) for _ in range(size)] # cast char buffers into (char*)[ ] strs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in strs] ) err = _lib_call( "TOSDB_GetStreamSnapshotStrings", self._name, item.encode("ascii"), topic.encode("ascii"), strs_array, size, data_str_max + 1, ( dtss if date_time else _PTR_(_DateTimeStamp)() ), end, beg, arg_list = [ _str_, _str_, _str_, _ppchar_, _ulong_, _ulong_, _PTR_(_DateTimeStamp), _long_, _long_ ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_GetStreamSnapshotStrings" ) if date_time: adj_dts = [ TOSDB_DateTime(x) for x in dtss ] return [ _ for _ in \ zip( map(lambda x: _cast(x,_str_).value.decode(), strs_array ), adj_dts ) ] else: return [ _cast(ptr,_str_).value.decode() for ptr in strs_array ] else: num_array = (tytup[1] * size)() err = _lib_call( "TOSDB_GetStreamSnapshot"+tytup[0]+"s", self._name, item.encode("ascii"), topic.encode("ascii"), num_array, size, ( dtss if date_time else _PTR_(_DateTimeStamp)() ), end, beg, arg_list = [ _str_, _str_, _str_, _PTR_(tytup[1]), _ulong_, _PTR_(_DateTimeStamp), _long_, _long_ ] ) if err: raise TOSDB_CLibError("error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_GetStreamSnapshot"+tytup[0]+"s") if date_time: adj_dts = [ TOSDB_DateTime(x) for x in dtss ] return [ _ for _ in zip(num_array,adj_dts) ] else: return [ _ for _ in num_array ]
def stream_snapshot_from_marker( self, item, topic, date_time = False, beg = 0, margin_of_safety = 100, throw_if_data_lost = True, data_str_max = STR_DATA_SZ ): """ Return multiple data-points(a snapshot) from the data-stream, ending where the last call began It's likely the stream will grow between consecutive calls. This call guarantees to pick up where the last get(), stream_snapshot(), or stream_snapshot_from_marker() call ended (under a few assumptions, see below). Internally the stream maintains a 'marker' that tracks the position of the last value pulled; the act of retreiving data and moving the marker can be thought of as a single, 'atomic' operation. There are three states to be aware of: 1) a 'beg' value that is greater than the marker (even if beg = 0) 2) a marker that moves through the entire stream and hits the bound 3) passing a buffer that is too small for the whole range State (1) can be caused by passing in a beginning index that is past the current marker, or by passing in 0 when the marker has yet to move. 'None' will be returned. State (2) occurs when the marker doesn't get reset before it hits the bound (block_size); as the oldest data is popped of the back of the stream it is lost (the marker can't grow past the end of the stream). State (3) occurs when an inadequately small buffer is used. The call handles buffer sizing for you by calling down to get the marker index, adjusting by 'beg' and 'margin_of_safety'. The latter helps assure the marker doesn't outgrow the buffer by the time the low-level retrieval operation completes. The default value indicates that over 100 push operations would have to take place during this call(highly unlikely). In either case (state (2) or (3)) if throw_if_data_lost is True a TOSDB_DataError will be thrown, otherwise the available data will be returned as normal. item: any item string in the block topic: any topic string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object beg: index of most recent data-point ( beginning of the snapshot ) margin_of_safety: (True/False) error margin for async stream growth throw_if_data_loss: (True/False) how to handle error states (see above) data_str_max: the maximum length of string data returned if beg > internal marker value: returns -> None if date_time is True: returns-> list of 2tuple else: returns -> list """ item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if beg < 0: beg += self._block_size if beg < 0 or beg >= self._block_size: raise TOSDB_IndexError("invalid 'beg' index value") if margin_of_safety < MIN_MARGIN_OF_SAFETY: raise TOSDB_ValueError("margin_of_safety < MIN_MARGIN_OF_SAFETY") is_dirty = _uint_() err = _lib_call( "TOSDB_IsMarkerDirty", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(is_dirty), arg_list = [ _str_, _str_, _str_, _PTR_(_uint_) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_IsMarkerDirty" ) if is_dirty and throw_if_data_lost: raise TOSDB_DataError("marker is already dirty") mpos = _longlong_() err2 = _lib_call( "TOSDB_GetMarkerPosition", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(mpos), arg_list = [ _str_, _str_, _str_, _PTR_(_longlong_) ]) if err2: raise TOSDB_CLibError( "error value [ "+ str(err2) + " ] " + "returned from library call", "TOSDB_GetMarkerPosition" ) cur_sz = mpos.value - beg + 1 if cur_sz < 0: return None safe_sz = cur_sz + margin_of_safety dtss = (_DateTimeStamp * safe_sz)() tbits = type_bits( topic ) tytup = _type_switch( tbits ) get_size = _long_() if tytup[0] == "String": # store char buffers strs = [ _BUF_( data_str_max +1 ) for _ in range(safe_sz) ] # cast char buffers into (char*)[ ] strs_array = (_pchar_ * safe_sz)(*[ cast(s,_pchar_) for s in strs]) err3 = _lib_call( "TOSDB_GetStreamSnapshotStringsFromMarker", self._name, item.encode("ascii"), topic.encode("ascii"), strs_array, safe_sz, data_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), beg, _pointer( get_size ), arg_list = [ _str_, _str_, _str_, _ppchar_, _ulong_, _ulong_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_) ] ) if err3: raise TOSDB_CLibError( "error value [ " + str(err3) + " ] returned from library call", "TOSDB_GetStreamSnapshotStrings" \ + "FromMarker") get_size = get_size.value if get_size == 0: return None elif get_size < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: get_size *= -1 if date_time: adj_dts = [ TOSDB_DateTime( x ) for x in dtss[:get_size] ] return [ _ for _ in \ zip( map( lambda x : cast(x, _str_).value.decode(), strs_array[:get_size] ), adj_dts ) ] else: return [ cast(ptr,_str_).value.decode() for ptr in strs_array[:get_size] ] else: num_array = (tytup[1] * safe_sz)() err3 = _lib_call( "TOSDB_GetStreamSnapshot" \ + tytup[0] + "sFromMarker" , self._name, item.encode("ascii"), topic.encode("ascii"), num_array, safe_sz, dtss if date_time else _PTR_(_DateTimeStamp)(), beg, _pointer( get_size ), arg_list = [ _str_, _str_, _str_, _PTR_(tytup[1]), _ulong_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_) ] ) if err3: raise TOSDB_CLibError( "error value of [ " + str(err3) + " ] returned from library call", "TOSDB_GetStreamSnapshot" \ + tytup[0] + "sFromMarker" ) get_size = get_size.value if get_size == 0: return None elif get_size < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: get_size *= -1 if date_time: adj_dts = [ TOSDB_DateTime( x ) for x in dtss[:get_size] ] return [ _ for _ in zip( num_array[:get_size], adj_dts ) ] else: return [ _ for _ in num_array[:get_size] ]
def item_frame( self, topic, date_time = False, labels = True, data_str_max = STR_DATA_SZ, label_str_max = MAX_STR_SZ ): """ Return all the most recent item values for a particular topic. topic: any topic string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object labels: (True/False) pull the item labels with the values data_str_max: the maximum length of string data returned label_str_max: the maximum length of item label strings returned if labels and date_time are True: returns-> namedtuple of 2tuple if labels is True: returns -> namedtuple if date_time is True: returns -> list of 2tuple else returns-> list """ topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_topic(topic) size = self._item_count() dtss = (_DateTimeStamp * size)() # store char buffers labs = [ _BUF_(label_str_max+1) for _ in range(size)] # cast char buffers int (char*)[ ] labs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in labs] ) tbits = type_bits( topic ) tytup = _type_switch( tbits ) if tytup[0] is "String": # store char buffers strs = [ _BUF_( data_str_max + 1 ) for _ in range(size)] strs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in strs] ) # cast char buffers int (char*)[ ] err = _lib_call( "TOSDB_GetItemFrameStrings", self._name, topic.encode("ascii"), strs_array, size, data_str_max + 1, ( labs_array if labels else _ppchar_() ), label_str_max + 1, ( dtss if date_time else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _ppchar_, _ulong_, _ulong_, _ppchar_, _ulong_, _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_GetItemFrameStrings" ) s_map = map( lambda x: _cast(x, _str_).value.decode(), strs_array ) if labels: l_map = map(lambda x: _cast(x, _str_).value.decode(),labs_array) _nt_ = _gen_namedtuple(_str_clean(topic)[0], _str_clean(*l_map)) if date_time: adj_dts = [ TOSDB_DateTime(x) for x in dtss ] return _nt_( *zip(s_map,adj_dts) ) else: return _nt_( *s_map ) else: if date_time: adj_dts = [ TOSDB_DateTime(x) for x in dtss ] return list( zip(s_map,adj_dts) ) else: return list( s_map ) else: num_array = (tytup[1] * size)() err = _lib_call( "TOSDB_GetItemFrame"+tytup[0]+"s", self._name, topic.encode("ascii"), num_array, size, ( labs_array if labels else _ppchar_() ), label_str_max + 1, ( dtss if date_time else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _PTR_(tytup[1]), _ulong_, _ppchar_, _ulong_, _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_GetItemFrame" + tytup[0] + "s" ) if labels: l_map = map(lambda x: _cast(x,_str_).value.decode(), labs_array) _nt_ = _gen_namedtuple(_str_clean(topic)[0], _str_clean(*l_map)) if date_time: adj_dts = [ TOSDB_DateTime(x) for x in dtss ] return _nt_( *zip(num_array,adj_dts) ) else: return _nt_( *num_array ) else: if date_time: adj_dts = [ TOSDB_DateTime(x) for x in dtss ] return list( zip(num_array,adj_dts) ) else: return [ _ for _ in num_array ]
def get(self, item, topic, date_time=False, indx=0, check_indx=True, data_str_max=STR_DATA_SZ): item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if indx < 0: indx += self._block_size if indx >= self._block_size: raise TOSDB_IndexError("invalid index value passed to get()") if check_indx and indx >= self.stream_occupancy(item, topic): raise TOSDB_DataError("data not available at this index yet " + "(disable check_indx to avoid this error)") dts = _DateTimeStamp() tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] == "String": ret_str = _BUF_(data_str_max + 1) _lib_call("TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, ret_str, data_str_max + 1, _pointer(dts) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _pchar_, _uint32_, _PTR_(_DateTimeStamp))) if date_time: return (ret_str.value.decode(), TOSDB_DateTime(dts)) else: return ret_str.value.decode() else: val = tytup[1]() _lib_call("TOSDB_Get" + tytup[0], self._name, item.encode("ascii"), topic.encode("ascii"), indx, _pointer(val), _pointer(dts) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _PTR_(tytup[1]), _PTR_(_DateTimeStamp))) if date_time: return (val.value, TOSDB_DateTime(dts)) else: return val.value
def stream_snapshot_from_marker(self, item, topic, date_time=False, beg=0, margin_of_safety=100, throw_if_data_lost=True, data_str_max=STR_DATA_SZ): item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if beg < 0: beg += self._block_size if beg < 0 or beg >= self._block_size: raise TOSDB_IndexError("invalid 'beg' index value") if margin_of_safety < MIN_MARGIN_OF_SAFETY: raise TOSDB_ValueError("margin_of_safety < MIN_MARGIN_OF_SAFETY") is_dirty = _uint_() _lib_call("TOSDB_IsMarkerDirty", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(is_dirty), arg_types=(_str_, _str_, _str_, _PTR_(_uint_))) if is_dirty and throw_if_data_lost: raise TOSDB_DataError("marker is already dirty") mpos = _longlong_() _lib_call("TOSDB_GetMarkerPosition", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(mpos), arg_types=(_str_, _str_, _str_, _PTR_(_longlong_))) cur_sz = mpos.value - beg + 1 if cur_sz < 0: return None safe_sz = cur_sz + margin_of_safety dtss = (_DateTimeStamp * safe_sz)() tbits = type_bits(topic) tytup = _type_switch(tbits) get_size = _long_() if tytup[0] == "String": strs = [ _BUF_( data_str_max +1) for _ in range(safe_sz) ] strs_array = (_pchar_ * safe_sz)(*[_cast(s,_pchar_) for s in strs]) _lib_call("TOSDB_GetStreamSnapshotStringsFromMarker", self._name, item.encode("ascii"), topic.encode("ascii"), strs_array, safe_sz, data_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), beg, _pointer(get_size), arg_types=(_str_, _str_, _str_, _ppchar_, _uint32_, _uint32_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_))) get_size = get_size.value if get_size == 0: return None elif get_size < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: get_size *= -1 if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss[:get_size]] return [sd for sd in zip(map(_cast_cstr, strs_array[:get_size]), adj_dts)] else: return [_cast_cstr(s) for s in strs_array[:get_size]] else: num_array = (tytup[1] * safe_sz)() _lib_call("TOSDB_GetStreamSnapshot" + tytup[0] + "sFromMarker", self._name, item.encode("ascii"), topic.encode("ascii"), num_array, safe_sz, dtss if date_time else _PTR_(_DateTimeStamp)(), beg, _pointer(get_size), arg_types=(_str_, _str_, _str_, _PTR_(tytup[1]), _uint32_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_))) get_size = get_size.value if get_size == 0: return None elif get_size < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: get_size *= -1 if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss[:get_size]] return [nd for nd in zip(num_array[:get_size], adj_dts)] else: return [n for n in num_array[:get_size]]
def stream_snapshot(self, item, topic, date_time=False, end=-1, beg=0, smart_size=True, data_str_max=STR_DATA_SZ): item = item.upper() topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item(item) self._valid_topic(topic) if end < 0: end += self._block_size if beg < 0: beg += self._block_size size = (end - beg) + 1 if beg < 0 \ or end < 0 \ or beg >= self._block_size \ or end >= self._block_size \ or size <= 0: raise TOSDB_IndexError("invalid 'beg' and/or 'end' index value(s)") if smart_size: so = self.stream_occupancy(item, topic) if so == 0 or so <= beg: return [] end = min(end, so - 1) beg = min(beg, so - 1) size = (end - beg) + 1 dtss = (_DateTimeStamp * size)() tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] == "String": strs = [_BUF_(data_str_max + 1) for _ in range(size)] strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetStreamSnapshotStrings", self._name, item.encode("ascii"), topic.encode("ascii"), strs_array, size, data_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), end, beg, arg_types=(_str_, _str_, _str_, _ppchar_, _uint32_, _uint32_, _PTR_(_DateTimeStamp), _long_, _long_)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return [sd for sd in zip(map(_cast_cstr, strs_array), adj_dts)] else: return [_cast_cstr(s) for s in strs_array] else: num_array = (tytup[1] * size)() _lib_call("TOSDB_GetStreamSnapshot" + tytup[0] + "s", self._name, item.encode("ascii"), topic.encode("ascii"), num_array, size, dtss if date_time else _PTR_(_DateTimeStamp)(), end, beg, arg_types=(_str_, _str_, _str_, _PTR_(tytup[1]), _uint32_, _PTR_(_DateTimeStamp), _long_, _long_)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return [nd for nd in zip(num_array, adj_dts)] else: return [n for n in num_array]
def item_frame(self, topic, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_topic(topic) size = self._item_count() dtss = (_DateTimeStamp * size)() labs = [_BUF_(label_str_max+1) for _ in range(size)] labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs]) tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] is "String": strs = [_BUF_( data_str_max + 1) for _ in range(size)] strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetItemFrameStrings", self._name, topic.encode("ascii"), strs_array, size, data_str_max + 1, labs_array if labels else _ppchar_(), label_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _ppchar_, _uint32_, _uint32_, _ppchar_, _uint32_, _PTR_(_DateTimeStamp))) if labels: l_map = map(_cast_cstr, labs_array) _nt_ = _gen_namedtuple(_str_clean(topic)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(map(_cast_cstr, strs_array),adj_dts)) else: return _nt_(*map(_cast_cstr, strs_array)) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(map(_cast_cstr, strs_array),adj_dts)) else: return list(map(_cast_cstr, strs_array)) else: num_array = (tytup[1] * size)() _lib_call("TOSDB_GetItemFrame"+tytup[0]+"s", self._name, topic.encode("ascii"), num_array, size, labs_array if labels else _ppchar_(), label_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _PTR_(tytup[1]), _uint32_, _ppchar_, _uint32_, _PTR_(_DateTimeStamp))) if labels: l_map = map(_cast_cstr, labs_array) _nt_ = _gen_namedtuple(_str_clean(topic)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(num_array,adj_dts)) else: return _nt_(*num_array) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(num_array,adj_dts)) else: return [n for n in num_array]
def item_frame(self, topic, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): topic = topic.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_topic(topic) size = self._item_count() dtss = (_DateTimeStamp * size)() labs = [_BUF_(label_str_max + 1) for _ in range(size)] labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs]) tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] is "String": strs = [_BUF_(data_str_max + 1) for _ in range(size)] strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) _lib_call("TOSDB_GetItemFrameStrings", self._name, topic.encode("ascii"), strs_array, size, data_str_max + 1, labs_array if labels else _ppchar_(), label_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _ppchar_, _uint32_, _uint32_, _ppchar_, _uint32_, _PTR_(_DateTimeStamp))) if labels: l_map = map(_cast_cstr, labs_array) _nt_ = _gen_namedtuple( _str_clean(topic)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(map(_cast_cstr, strs_array), adj_dts)) else: return _nt_(*map(_cast_cstr, strs_array)) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(map(_cast_cstr, strs_array), adj_dts)) else: return list(map(_cast_cstr, strs_array)) else: num_array = (tytup[1] * size)() _lib_call("TOSDB_GetItemFrame" + tytup[0] + "s", self._name, topic.encode("ascii"), num_array, size, labs_array if labels else _ppchar_(), label_str_max + 1, dtss if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _PTR_(tytup[1]), _uint32_, _ppchar_, _uint32_, _PTR_(_DateTimeStamp))) if labels: l_map = map(_cast_cstr, labs_array) _nt_ = _gen_namedtuple( _str_clean(topic)[0], _str_clean(*l_map)) if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return _nt_(*zip(num_array, adj_dts)) else: return _nt_(*num_array) else: if date_time: adj_dts = [TOSDB_DateTime(x) for x in dtss] return list(zip(num_array, adj_dts)) else: return [n for n in num_array]
def topic_frame( self, item, date_time = False, labels = True, data_str_max = STR_DATA_SZ, label_str_max = MAX_STR_SZ ): """ Return all the most recent topic values for a particular item: item: any item string in the block date_time: (True/False) attempt to retrieve a TOSDB_DateTime object labels: (True/False) pull the topic labels with the values data_str_max: the maximum length of string data returned label_str_max: the maximum length of topic label strings returned if labels and date_time are True: returns-> namedtuple of 2tuple if labels is True: returns -> namedtuple if date_time is True: returns -> list of 2tuple else returns-> list """ item = item.upper() if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") self._valid_item( item ) size = self._topic_count() dtss = (_DateTimeStamp * size)() # cast char buffers int (char*)[ ] labs = [ _BUF_( label_str_max + 1 ) for _ in range(size)] strs = [ _BUF_( data_str_max + 1 ) for _ in range(size)] # cast char buffers int (char*)[ ] labs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in labs] ) strs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in strs] ) err = _lib_call( "TOSDB_GetTopicFrameStrings", self._name, item.encode("ascii"), strs_array, size, data_str_max + 1, ( labs_array if labels else _ppchar_() ), label_str_max + 1, ( dtss if date_time else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _ppchar_, _ulong_, _ulong_, _ppchar_, _ulong_, _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_GetTopicFrameStrings" ) s_map = map( lambda x: _cast(x, _str_).value.decode(), strs_array ) if labels: l_map = map( lambda x: _cast(x, _str_).value.decode(), labs_array ) _nt_ = _gen_namedtuple( _str_clean(item)[0], _str_clean( *l_map ) ) if date_time: adj_dts = [TOSDB_DateTime( x ) for x in dtss] return _nt_( *zip( s_map, adj_dts ) ) else: return _nt_( *s_map ) else: if date_time: adj_dts = [TOSDB_DateTime( x ) for x in dtss] return list( zip(s_map, adj_dts) ) else: return list( s_map )