def to_bit_sequence(self, event):
""" Creates an array of bits containing the details in the event
dictionary. Once created, the array is cached to speed up future writes.
Parameters
----------
event: dict
A dictionary describing the current component event. It should have
3 keys: name, action, and metadata.
Returns
-------
The array of bits
"""
if event["metadata"] is None:
nbytes = self.action_bytes + self.name_bytes
metadata_array = []
else:
nbytes = self.metadata_bytes + self.action_bytes + self.name_bytes
try:
metadata_array = np.fromstring(event["metadata"],
dtype=np.uint16).astype(np.uint8)[:self.metadata_bytes]
except TypeError:
metadata_array = np.array(map(ord,
event["metadata"].ljust(self.metadata_bytes)[:self.metadata_bytes]),
dtype=np.uint8)
int8_array = np.zeros(nbytes, dtype="uint8")
int8_array[:self.name_bytes] = map(ord, event["name"].ljust(self.name_bytes)[:self.name_bytes])
int8_array[self.name_bytes:self.name_bytes + self.action_bytes] = map(ord, event["action"].ljust(self.action_bytes)[:self.action_bytes])
int8_array[self.name_bytes + self.action_bytes:] = metadata_array
sequence = ([True] +
np.unpackbits(int8_array).astype(bool).tolist() +
[False])
key = (event["name"], event["action"], event["metadata"])
self.map_to_bit[key] = sequence
return sequence
评论列表
文章目录