Revamping the FlatBuffers docs.
[platform/upstream/flatbuffers.git] / samples / sample_binary.py
1 #!/usr/bin/python
2 # Copyright 2015 Google Inc. All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # To run this file, use `python_sample.sh`.
17
18 # Append paths to the `flatbuffers` and `MyGame` modules. This is necessary
19 # to facilitate executing this script in the `samples` folder, and to root
20 # folder (where it gets placed when using `cmake`).
21 import os
22 import sys
23 sys.path.append(os.path.join(os.path.dirname(__file__), '../python'))
24
25 import flatbuffers
26 import MyGame.Sample.Color
27 import MyGame.Sample.Equipment
28 import MyGame.Sample.Monster
29 import MyGame.Sample.Vec3
30 import MyGame.Sample.Weapon
31
32 # Example of how to use FlatBuffers to create and read binary buffers.
33
34 def main():
35   builder = flatbuffers.Builder(0)
36
37   # Create some weapons for our Monster ('Sword' and 'Axe').
38   weapon_one = builder.CreateString('Sword')
39   weapon_two = builder.CreateString('Axe')
40
41   MyGame.Sample.Weapon.WeaponStart(builder)
42   MyGame.Sample.Weapon.WeaponAddName(builder, weapon_one)
43   MyGame.Sample.Weapon.WeaponAddDamage(builder, 3)
44   sword = MyGame.Sample.Weapon.WeaponEnd(builder)
45
46   MyGame.Sample.Weapon.WeaponStart(builder)
47   MyGame.Sample.Weapon.WeaponAddName(builder, weapon_two)
48   MyGame.Sample.Weapon.WeaponAddDamage(builder, 5)
49   axe = MyGame.Sample.Weapon.WeaponEnd(builder)
50
51   # Serialize the FlatBuffer data.
52   name = builder.CreateString('Orc')
53
54   MyGame.Sample.Monster.MonsterStartInventoryVector(builder, 10)
55   # Note: Since we prepend the bytes, this loop iterates in reverse order.
56   for i in reversed(range(0, 10)):
57     builder.PrependByte(i)
58   inv = builder.EndVector(10)
59
60   MyGame.Sample.Monster.MonsterStartWeaponsVector(builder, 2)
61   # Note: Since we prepend the data, prepend the weapons in reverse order.
62   builder.PrependUOffsetTRelative(axe)
63   builder.PrependUOffsetTRelative(sword)
64   weapons = builder.EndVector(2)
65
66   pos = MyGame.Sample.Vec3.CreateVec3(builder, 1.0, 2.0, 3.0)
67
68   MyGame.Sample.Monster.MonsterStart(builder)
69   MyGame.Sample.Monster.MonsterAddPos(builder, pos)
70   MyGame.Sample.Monster.MonsterAddHp(builder, 300)
71   MyGame.Sample.Monster.MonsterAddName(builder, name)
72   MyGame.Sample.Monster.MonsterAddInventory(builder, inv)
73   MyGame.Sample.Monster.MonsterAddColor(builder,
74                                         MyGame.Sample.Color.Color().Red)
75   MyGame.Sample.Monster.MonsterAddWeapons(builder, weapons)
76   MyGame.Sample.Monster.MonsterAddEquippedType(
77       builder, MyGame.Sample.Equipment.Equipment().Weapon)
78   MyGame.Sample.Monster.MonsterAddEquipped(builder, axe)
79   orc = MyGame.Sample.Monster.MonsterEnd(builder)
80
81   builder.Finish(orc)
82
83   # We now have a FlatBuffer that we could store on disk or send over a network.
84
85   # ...Saving to file or sending over a network code goes here...
86
87   # Instead, we are going to access this buffer right away (as if we just
88   # received it).
89
90   buf = builder.Output()
91
92   # Note: We use `0` for the offset here, since we got the data using the
93   # `builder.Output()` method. This simulates the data you would store/receive
94   # in your FlatBuffer. If you wanted to read from the `builder.Bytes` directly,
95   # you would need to pass in the offset of `builder.Head()`, as the builder
96   # actually constructs the buffer backwards.
97   monster = MyGame.Sample.Monster.Monster.GetRootAsMonster(buf, 0)
98
99   # Note: We did not set the `Mana` field explicitly, so we get a default value.
100   assert monster.Mana() == 150
101   assert monster.Hp() == 300
102   assert monster.Name() == 'Orc'
103   assert monster.Color() == MyGame.Sample.Color.Color().Red
104   assert monster.Pos().X() == 1.0
105   assert monster.Pos().Y() == 2.0
106   assert monster.Pos().Z() == 3.0
107
108   # Get and test the `inventory` FlatBuffer `vector`.
109   for i in xrange(monster.InventoryLength()):
110     assert monster.Inventory(i) == i
111
112   # Get and test the `weapons` FlatBuffer `vector` of `table`s.
113   expected_weapon_names = ['Sword', 'Axe']
114   expected_weapon_damages = [3, 5]
115   for i in xrange(monster.WeaponsLength()):
116     assert monster.Weapons(i).Name() == expected_weapon_names[i]
117     assert monster.Weapons(i).Damage() == expected_weapon_damages[i]
118
119   # Get and test the `equipped` FlatBuffer `union`.
120   assert monster.EquippedType() == MyGame.Sample.Equipment.Equipment().Weapon
121
122   # An example of how you can appropriately convert the table depending on the
123   # FlatBuffer `union` type. You could add `elif` and `else` clauses to handle
124   # the other FlatBuffer `union` types for this field.
125   if monster.EquippedType() == MyGame.Sample.Equipment.Equipment().Weapon:
126     # `monster.Equipped()` returns a `flatbuffers.Table`, which can be used
127     # to initialize a `MyGame.Sample.Weapon.Weapon()`, in this case.
128     union_weapon = MyGame.Sample.Weapon.Weapon()
129     union_weapon.Init(monster.Equipped().Bytes, monster.Equipped().Pos)
130
131     assert union_weapon.Name() == "Axe"
132     assert union_weapon.Damage() == 5
133
134   print 'The FlatBuffer was successfully created and verified!'
135
136 if __name__ == '__main__':
137   main()