def serialize(entity: SpacyBatchResponseData, builder: Builder) -> Any: responses_offsets = list(map(lambda r: SpacyResponseMapper.serialize(r, builder), entity.responses)) SpacyBatchResponseClass.SpacyBatchResponseStartResponsesVector(builder, len(entity.responses)) for offset in reversed(responses_offsets): builder.PrependUOffsetTRelative(offset) responses_vector_offset = builder.EndVector(len(entity.responses)) SpacyBatchResponseClass.SpacyBatchResponseStart(builder) SpacyBatchResponseClass.SpacyBatchResponseAddResponses(builder, responses_vector_offset) return SpacyBatchResponseClass.SpacyBatchResponseEnd(builder)
def _create_schema(builder: flatbuffers.Builder, root_object: Object.Object) -> Schema.Schema: Schema.SchemaStartObjectsVector(builder, 1) builder.PrependUOffsetTRelative(root_object) objects = builder.EndVector(1) Schema.SchemaStart(builder) Schema.SchemaAddObjects(builder, objects) Schema.SchemaAddRootTable(builder, root_object) return Schema.SchemaEnd(builder)
def serialize(entity: TesseractMessageEntity) -> bytearray: builder = Builder(1024) payload_offset = entity.message.serialize(builder) TesseractMessageStart(builder) TesseractMessageAddMessageType(builder, entity.type) TesseractMessageAddMessage(builder, payload_offset) message_offset = TesseractMessageEnd(builder) builder.Finish(message_offset) return builder.Output()
def serialize(entity: NERSpacyResponseData, builder: Builder) -> Any: text_offset = builder.CreateString(entity.text) label_offset = builder.CreateString(entity.label) NERSpacyResponseClass.NERSpacyResponseStart(builder) NERSpacyResponseClass.NERSpacyResponseAddText(builder, text_offset) NERSpacyResponseClass.NERSpacyResponseAddStartChar(builder, entity.start_char) NERSpacyResponseClass.NERSpacyResponseAddEndChar(builder, entity.end_char) NERSpacyResponseClass.NERSpacyResponseAddLabel(builder, label_offset) return NERSpacyResponseClass.NERSpacyResponseEnd(builder)
def _create_object(builder: flatbuffers.Builder, name: str, fields: Sequence[Field.Field]) -> Object.Object: name = builder.CreateString(name) Object.ObjectStartFieldsVector(builder, len(fields)) for field in reversed(fields): builder.PrependUOffsetTRelative(field) fields_vector = builder.EndVector(len(fields)) Object.ObjectStart(builder) Object.ObjectAddName(builder, name) Object.ObjectAddFields(builder, fields_vector) return Object.ObjectEnd(builder)
def serialize(entity: SpacyRequestData, builder: Builder) -> Any: text_offset = builder.CreateString(entity.text) SpacyRequestClass.SpacyRequestStartTypeVector(builder, len(entity.types)) for x in reversed(entity.types): builder.PrependByte(x.value) types_offset = builder.EndVector(len(entity.types)) SpacyRequestClass.SpacyRequestStart(builder) SpacyRequestClass.SpacyRequestAddText(builder, text_offset) SpacyRequestClass.SpacyRequestAddType(builder, types_offset) return SpacyRequestClass.SpacyRequestEnd(builder)
def _serialise_doublearray(builder: flatbuffers.Builder, data: np.ndarray, source: int): ArrayDoubleStartValueVector(builder, len(data)) for single_value in reversed(data): builder.PrependFloat64(single_value) array_offset = builder.EndVector(len(data)) ArrayDoubleStart(builder) ArrayDoubleAddValue(builder, array_offset) value_position = ArrayDoubleEnd(builder) LogData.LogDataStart(builder) LogData.LogDataAddSourceName(builder, source) LogData.LogDataAddValue(builder, value_position) LogData.LogDataAddValueType(builder, Value.ArrayDouble)
def serialize(entity: DEPSpacyResponseData, builder: Builder) -> Any: lang_offset_offset = builder.CreateString(entity.lang) relation_offset = builder.CreateString(entity.relation) source_offset = builder.CreateString(entity.source) source_pos_offset = builder.CreateString(entity.source_pos) source_tag_offset = builder.CreateString(entity.source_tag) source_base_offset = builder.CreateString(entity.source_base) target_offset = builder.CreateString(entity.target) target_pos_offset = builder.CreateString(entity.target_pos) target_tag_offset = builder.CreateString(entity.target_tag) target_base_offset = builder.CreateString(entity.target_base) DEPSpacyResponseClass.DEPSpacyResponseStart(builder) DEPSpacyResponseClass.DEPSpacyResponseAddLang(builder, lang_offset_offset) DEPSpacyResponseClass.DEPSpacyResponseAddRelation(builder, relation_offset) DEPSpacyResponseClass.DEPSpacyResponseAddSource(builder, source_offset) DEPSpacyResponseClass.DEPSpacyResponseAddSourcePos(builder, source_pos_offset) DEPSpacyResponseClass.DEPSpacyResponseAddSourceIndex(builder, entity.source_index) DEPSpacyResponseClass.DEPSpacyResponseAddSourceTag(builder, source_tag_offset) DEPSpacyResponseClass.DEPSpacyResponseAddSourceBase(builder, source_base_offset) DEPSpacyResponseClass.DEPSpacyResponseAddTarget(builder, target_offset) DEPSpacyResponseClass.DEPSpacyResponseAddTargetPos(builder, target_pos_offset) DEPSpacyResponseClass.DEPSpacyResponseAddTargetIndex(builder, entity.target_index) DEPSpacyResponseClass.DEPSpacyResponseAddTargetTag(builder, target_tag_offset) DEPSpacyResponseClass.DEPSpacyResponseAddTargetBase(builder, target_base_offset) return DEPSpacyResponseClass.DEPSpacyResponseEnd(builder)
def packmsg(): b=Builder(0) #Create the field and add it directly. Vec2DStart(b) p1= CreatePoint2D(b, 3, 5) Vec2DAddP1(b, p1) p2= CreatePoint2D(b, 9, 15) Vec2DAddP2(b, p2) #Vec2DAddP1(b, CreatePoint2D(b, 3, 5)) #Vec2DAddP2(b, CreatePoint2D(b, 9, 15)) pos=Vec2DEnd(b) b.Finish(pos) return b.Output()
def serialize(data: EmbeddingResponseData, builder: flatbuffers.Builder): length = len(data.vector) vector_offsets = list( map(lambda r: EmbeddingElementMapper.serialize(r, builder), data.vector)) EmbeddingResponse.EmbeddingResponseStartVectorVector(builder, length) for offset in reversed(vector_offsets): builder.PrependUOffsetTRelative(offset) vector_offsets_vector = builder.EndVector(length) EmbeddingResponse.EmbeddingResponseStart(builder) EmbeddingResponse.EmbeddingResponseAddVector(builder, vector_offsets_vector) return EmbeddingResponse.EmbeddingResponseEnd(builder)
def serialize(data: MostSimilarResponseData, builder: flatbuffers.Builder): length = len(data.similarities) similarities_offsets = list( map(lambda r: MostSimilarityMapper.serialize(r, builder), data.similarities)) MostSimilarResponse.MostSimilarResponseStartSimilaritiesVector( builder, length) for offset in reversed(similarities_offsets): builder.PrependUOffsetTRelative(offset) similarities_offsets_vector = builder.EndVector(length) MostSimilarResponse.MostSimilarResponseStart(builder) MostSimilarResponse.MostSimilarResponseAddSimilarities( builder, similarities_offsets_vector) return MostSimilarResponse.MostSimilarResponseEnd(builder)
def to_fb(self, builder: flatbuffers.Builder): anim_name_str = builder.CreateString(self.anim_name) CozmoAnim.FaceAnimation.FaceAnimationStart(builder) CozmoAnim.FaceAnimation.FaceAnimationAddTriggerTimeMs(builder, self.trigger_time_ms) CozmoAnim.FaceAnimation.FaceAnimationAddAnimName(builder, anim_name_str) fbkf = CozmoAnim.FaceAnimation.FaceAnimationEnd(builder) return fbkf
def _encode_value(self, builder: flatbuffers.Builder, value: Any, type_: BaseType) -> int: if type_ == BaseType.String: return builder.CreateString(value) elif type_ in _number_type_by_base_type.keys(): return value raise NotImplementedError(f'Unsupported field type: {type_}')
def _serialise_stringarray(builder: flatbuffers.Builder, data: np.ndarray, source: int): string_offsets = [ builder.CreateString(string_item) for string_item in reversed(data) ] ArrayStringStartValueVector(builder, len(data)) for string_offset in string_offsets: builder.PrependSOffsetTRelative(string_offset) string_array_offset = builder.EndVector(len(data)) ArrayStringStart(builder) ArrayStringAddValue(builder, string_array_offset) value_position = ArrayStringEnd(builder) LogData.LogDataStart(builder) LogData.LogDataAddSourceName(builder, source) LogData.LogDataAddValue(builder, value_position) LogData.LogDataAddValueType(builder, Value.ArrayString)
def serialize(self, builder: Builder): text_offset = builder.CreateString(self.text) TesseractOcrResponseStart(builder) TesseractOcrResponseAddText(builder, text_offset) TesseractOcrResponseAddConfidence(builder, self.confidence) return TesseractOcrResponseEnd(builder)
def serialize(self, builder: Builder): image_offset = builder.CreateByteVector(self.image) TesseractOcrRequestStart(builder) TesseractOcrRequestAddImage(builder, image_offset) return TesseractOcrRequestEnd(builder)
def to_fb(self, builder: flatbuffers.Builder): clips_arr = [] for clip in self.clips: fbclip = clip.to_fb(builder) clips_arr.append(fbclip) CozmoAnim.AnimClips.AnimClipsStartClipsVector(builder, len(clips_arr)) for i in reversed(range(len(clips_arr))): builder.PrependUOffsetTRelative(clips_arr[i]) clips_vector = builder.EndVector(len(clips_arr)) CozmoAnim.AnimClips.AnimClipsStart(builder) CozmoAnim.AnimClips.AnimClipsAddClips(builder, clips_vector) fbclips = CozmoAnim.AnimClips.AnimClipsEnd(builder) return fbclips
def to_fb(self, builder: flatbuffers.Builder): event_id_str = builder.CreateString(self.event_id) CozmoAnim.Event.EventStart(builder) CozmoAnim.Event.EventAddTriggerTimeMs(builder, self.trigger_time_ms) CozmoAnim.Event.EventAddEventId(builder, event_id_str) fbkf = CozmoAnim.Event.EventEnd(builder) return fbkf
def serialize(data: MostSimilarityData, builder: flatbuffers.Builder): text = builder.CreateString(data.text) MostSimilarity.MostSimilarityStart(builder) MostSimilarity.MostSimilarityAddText(builder, text) MostSimilarity.MostSimilarityAddProbability(builder, data.probability) return MostSimilarity.MostSimilarityEnd(builder)
def write_to_flatbuffer(self, builder: Builder): name = builder.CreateString(self.name) if self.bot: loadout = self.loadout_config.write_to_flatbuffer(builder) if self.rlbot_controlled: variety = PlayerClass.RLBotPlayer RLBotPlayer.RLBotPlayerStart(builder) player = RLBotPlayer.RLBotPlayerEnd(builder) else: variety = PlayerClass.PsyonixBotPlayer PsyonixBotPlayer.PsyonixBotPlayerStart(builder) PsyonixBotPlayer.PsyonixBotPlayerAddBotSkill( builder, self.bot_skill) player = PsyonixBotPlayer.PsyonixBotPlayerEnd(builder) else: loadout = LoadoutConfig().write_to_flatbuffer(builder) variety = PlayerClass.HumanPlayer HumanPlayer.HumanPlayerStart(builder) player = HumanPlayer.HumanPlayerEnd(builder) PlayerConfigurationFlat.PlayerConfigurationStart(builder) PlayerConfigurationFlat.PlayerConfigurationAddName(builder, name) PlayerConfigurationFlat.PlayerConfigurationAddLoadout(builder, loadout) PlayerConfigurationFlat.PlayerConfigurationAddTeam(builder, self.team) PlayerConfigurationFlat.PlayerConfigurationAddVariety(builder, player) PlayerConfigurationFlat.PlayerConfigurationAddVarietyType( builder, variety) PlayerConfigurationFlat.PlayerConfigurationAddSpawnId( builder, self.spawn_id) return PlayerConfigurationFlat.PlayerConfigurationEnd(builder)
def _create_field(builder: flatbuffers.Builder, name: str, type_: FieldType, offset: int) -> Field.Field: name = builder.CreateString(name) Field.FieldStart(builder) Field.FieldAddName(builder, name) Field.FieldAddType(builder, type_) Field.FieldAddOffset(builder, offset) return Field.FieldEnd(builder)
def simple_request(request_type, client_id=0, text=None, data=None): builder = Builder(50) if text is not None: text = builder.CreateString(text) # Create Simple Request Data SimpleRequestData.SimpleRequestDataStart(builder) SimpleRequestData.SimpleRequestDataAddType(builder, request_type) if text is not None: SimpleRequestData.SimpleRequestDataAddText(builder, text) if data is not None: SimpleRequestData.SimpleRequestDataAddData( builder, data, ) contentOffset = SimpleRequestData.SimpleRequestDataEnd(builder) # Create Message Data MessageData.MessageDataStart(builder) MessageData.MessageDataAddVersion( builder, CreateProtocolVersion(builder, *PROTO_VERSION)) MessageData.MessageDataAddSenderId(builder, client_id) MessageData.MessageDataAddContentType(builder, Content.SimpleRequestData) MessageData.MessageDataAddContent(builder, contentOffset) end = MessageData.MessageDataEnd(builder) builder.Finish(end) if os.environ.get("DEBUG_TRANSPORT"): print('->', "SimpleRequestData", "%-23s" % requestlookup[request_type], data, text, sep='\t') return set_file_identifier(builder.Output())
def serialize(data: GensimFastTextMostSimilarResponseData, builder: flatbuffers.Builder): length = len(data.responses) responses_offsets = list( map(lambda r: MostSimilarResponseMapper.serialize(r, builder), data.responses)) GensimFastTextMostSimilarResponse.GensimFastTextMostSimilarResponseStartResponsesVector( builder, length) for offset in reversed(responses_offsets): builder.PrependUOffsetTRelative(offset) responses_offsets_vector = builder.EndVector(length) GensimFastTextMostSimilarResponse.GensimFastTextMostSimilarResponseStart( builder) GensimFastTextMostSimilarResponse.GensimFastTextMostSimilarResponseAddResponses( builder, responses_offsets_vector) return GensimFastTextMostSimilarResponse.GensimFastTextMostSimilarResponseEnd( builder)
def serialize(entity: SpacyResponseData, builder: Builder) -> Any: ner_offsets = list(map(lambda n: NERSpacyResponseMapper.serialize(n, builder), entity.ner)) dep_offsets = list(map(lambda d: DEPSpacyResponseMapper.serialize(d, builder), entity.dep)) SpacyResponseClass.SpacyResponseStartNerVector(builder, len(entity.ner)) for x in reversed(ner_offsets): builder.PrependUOffsetTRelative(x) ner_vector_offset = builder.EndVector(len(entity.ner)) SpacyResponseClass.SpacyResponseStartDepVector(builder, len(entity.dep)) for x in reversed(dep_offsets): builder.PrependUOffsetTRelative(x) dep_vector_offset = builder.EndVector(len(entity.dep)) SpacyResponseClass.SpacyResponseStart(builder) SpacyResponseClass.SpacyResponseAddNer(builder, ner_vector_offset) SpacyResponseClass.SpacyResponseAddDep(builder, dep_vector_offset) return SpacyResponseClass.SpacyResponseEnd(builder)
def serialize(data: SimilarityRequestData, builder: flatbuffers.Builder): text1 = builder.CreateString(data.text1) text2 = builder.CreateString(data.text2) SimilarityRequestStart(builder) SimilarityRequestAddText1(builder, text1) SimilarityRequestAddText2(builder, text2) return SimilarityRequestEnd(builder)
def serialize(data: GensimFastTextEmbeddingRequestData, builder: flatbuffers.Builder): length = len(data.requests) request_offsets = list( map(lambda r: EmbeddingRequestMapper.serialize(r, builder), data.requests)) GensimFastTextEmbeddingRequest.GensimFastTextEmbeddingRequestStartRequestsVector( builder, length) for offset in reversed(request_offsets): builder.PrependUOffsetTRelative(offset) request_offsets_vector = builder.EndVector(length) GensimFastTextEmbeddingRequest.GensimFastTextEmbeddingRequestStart( builder) GensimFastTextEmbeddingRequest.GensimFastTextEmbeddingRequestAddRequests( builder, request_offsets_vector) return GensimFastTextEmbeddingRequest.GensimFastTextEmbeddingRequestEnd( builder)
def to_fb(self, builder: flatbuffers.Builder): radius_mm_str = builder.CreateString(str(self.radius_mm)) CozmoAnim.BodyMotion.BodyMotionStart(builder) CozmoAnim.BodyMotion.BodyMotionAddTriggerTimeMs(builder, self.trigger_time_ms) CozmoAnim.BodyMotion.BodyMotionAddDurationTimeMs(builder, self.duration_ms) CozmoAnim.BodyMotion.BodyMotionAddRadiusMm(builder, radius_mm_str) CozmoAnim.BodyMotion.BodyMotionAddSpeed(builder, self.speed) fbkf = CozmoAnim.BodyMotion.BodyMotionEnd(builder) return fbkf
def to_flatbuffer(self, player_index: int) -> Builder: builder = Builder(100) ControllerState.ControllerStateStart(builder) ControllerState.ControllerStateAddSteer(builder, self.steer) ControllerState.ControllerStateAddThrottle(builder, self.throttle) ControllerState.ControllerStateAddPitch(builder, self.pitch) ControllerState.ControllerStateAddYaw(builder, self.yaw) ControllerState.ControllerStateAddRoll(builder, self.roll) ControllerState.ControllerStateAddJump(builder, self.jump) ControllerState.ControllerStateAddBoost(builder, self.boost) ControllerState.ControllerStateAddHandbrake(builder, self.handbrake) cs_offset = ControllerState.ControllerStateEnd(builder) PlayerInput.PlayerInputStart(builder) PlayerInput.PlayerInputAddPlayerIndex(builder, player_index) PlayerInput.PlayerInputAddControllerState(builder, cs_offset) pi_offset = PlayerInput.PlayerInputEnd(builder) builder.Finish(pi_offset) return builder
def handle_hidingspots_layer(g: minidom.Node, objs: GameObjects, builder: flatbuffers.Builder): for o in g.getElementsByTagName('object'): x, y, rotation = get_pos(o) poly = 0 width = 0 height = 0 isCircle = False if not o.getElementsByTagName('polygon'): width, height = get_dim(o) x += (math.cos(math.radians(rotation)) * width / 2.0 - math.sin(math.radians(rotation)) * height / 2.0) y += (math.cos(math.radians(rotation)) * height / 2.0 + math.sin(math.radians(rotation)) * width / 2.0) if o.getElementsByTagName('ellipse'): assertCircleness(o) isCircle = True else: polyverts = getPolygonVertices(o) FlatBuffGenerated.HidingSpot.HidingSpotStartPolyvertsVector( builder, len(polyverts)) for v in polyverts: vert_x, vert_y = v.split(",") vert_x_f = float(vert_x) * GLOBAL_SCALE vert_y_f = float(vert_y) * GLOBAL_SCALE FlatBuffGenerated.Vec2.CreateVec2(builder, vert_x_f, vert_y_f) poly = builder.EndVector(len(polyverts)) name = builder.CreateString(o.getAttribute('name')) FlatBuffGenerated.HidingSpot.HidingSpotStart(builder) pos = FlatBuffGenerated.Vec2.CreateVec2(builder, x * GLOBAL_SCALE, y * GLOBAL_SCALE) FlatBuffGenerated.HidingSpot.HidingSpotAddPos(builder, pos) FlatBuffGenerated.HidingSpot.HidingSpotAddRotation(builder, rotation) size = FlatBuffGenerated.Vec2.CreateVec2(builder, width * GLOBAL_SCALE, height * GLOBAL_SCALE) FlatBuffGenerated.HidingSpot.HidingSpotAddSize(builder, size) FlatBuffGenerated.HidingSpot.HidingSpotAddIsCircle(builder, isCircle) FlatBuffGenerated.HidingSpot.HidingSpotAddPolyverts(builder, poly) FlatBuffGenerated.HidingSpot.HidingSpotAddName(builder, name) objs.hidingspots.append( FlatBuffGenerated.HidingSpot.HidingSpotEnd(builder))
def _encode_vector(self, builder: flatbuffers.Builder, value: Collection, element_type: BaseType) -> int: if element_type == BaseType.UByte or element_type == BaseType.Byte: return builder.CreateByteVector(value) elements_encoded = [ self._encode_value(builder, element, element_type) for element in value ] element_bytesize = _number_type_by_base_type[element_type].bytewidth vector_element_type = _number_type_by_base_type[element_type] builder.StartVector(element_bytesize, len(elements_encoded), element_bytesize) for index, element in enumerate(reversed(elements_encoded)): if vector_element_type == flatbuffers.number_types.UOffsetTFlags: builder.PrependUOffsetTRelative(element) else: builder.Prepend(_number_type_by_base_type[element_type], element) return builder.EndVector(len(elements_encoded))