在Python中访问Flatbuffers联合

时间:2018-12-06 16:52:04

标签: python python-3.x flatbuffers

我的架构文件中具有以下定义:

union UGeometry { Polygon, Point, Linestring }

table Point {
    point:Vec2;
}

table Polygon {
    points:[Vec2List]; 
}

table Geometry {
    g:UGeometry;
}

(删除了一些样板代码以进行类型检查和其他操作)

表“几何”存储类型为Point,Polygon和LineString的几何。我可以像往常一样使用C ++和Javascript访问此文件在Javascript中,我使用以下命令来获取Polygon类型:

var rawPolygon = flatBufGeometry.g( new storage.Polygon() );

但是,我在生成的Python代码中找不到这种访问器。以下内容无效:

rawPolygon = rawGeometry.G()(storage.Polygon.Polygon())

如何使用Python访问表中的Flatbuffers联合对象?

1 个答案:

答案 0 :(得分:0)

这里是Google Monster.fbs的示例,因为所有flatbuffer具有相似的结构并生成了python文件。

function goSubmit(){ 
    var flag = 0 
    var element = document.getElementById('submit_id');
    var name = element.querySelector('#Signature').value
    var date = element.querySelector("#date").value

    if(!name){
        M.toast({html: 'Name required!'})
        flag = 1
    }
    if(flag)
        return
    {//submit
        var data = {}
        data.email = email_first
        data.candidate_code = candidate_code
        data.date = date
        data.signature = name


        for(let i = 0 ; i <= numbers; i ++){
            var number =  i + 1
            var name_property = 'name of reference ' + number
            var email_property = 'email of reference ' + number
            var explaination_property = 'explaination ' + number
            data[name_property] = names[i]
            data[email_property] = emails[i]
            data[explaination_property] = explainations[i]

            data.reference = names[i];
            data.email_ref = emails[i];
            data.explaination = explainations[i];

        }

        google.script.run.appendData(data);

    }
}

要访问武器,请尝试

union Equipment { Weapon } // Optionally add more tables.


table Monster {
  pos:Vec3;
  mana:short = 150;
  hp:short = 100;
  name:string;
  friendly:bool = false (deprecated);
  inventory:[ubyte];
  color:Color = Blue;
  weapons:[Weapon];
  equipped:Equipment;
  path:[Vec3];
}

table Weapon {
  name:string;
  damage:short;
}

root_type Monster;

来源: https://github.com/google/flatbuffers/blob/master/samples/monster.fbs https://github.com/google/flatbuffers/blob/master/samples/sample_binary.py