...

Text file src/github.com/google/flatbuffers/docs/source/PythonUsage.md

Documentation: github.com/google/flatbuffers/docs/source

     1Use in Python    {#flatbuffers_guide_use_python}
     2=============
     3
     4## Before you get started
     5
     6Before diving into the FlatBuffers usage in Python, it should be noted that the
     7[Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to general
     8FlatBuffers usage in all of the supported languages (including Python). This
     9page is designed to cover the nuances of FlatBuffers usage, specific to
    10Python.
    11
    12You should also have read the [Building](@ref flatbuffers_guide_building)
    13documentation to build `flatc` and should be familiar with
    14[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
    15[Writing a schema](@ref flatbuffers_guide_writing_schema).
    16
    17## FlatBuffers Python library code location
    18
    19The code for the FlatBuffers Python library can be found at
    20`flatbuffers/python/flatbuffers`. You can browse the library code on the
    21[FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/
    22python).
    23
    24## Testing the FlatBuffers Python library
    25
    26The code to test the Python library can be found at `flatbuffers/tests`.
    27The test code itself is located in [py_test.py](https://github.com/google/
    28flatbuffers/blob/master/tests/py_test.py).
    29
    30To run the tests, use the [PythonTest.sh](https://github.com/google/flatbuffers/
    31blob/master/tests/PythonTest.sh) shell script.
    32
    33*Note: This script requires [python](https://www.python.org/) to be
    34installed.*
    35
    36## Using the FlatBuffers Python library
    37
    38*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
    39example of how to use FlatBuffers in Python.*
    40
    41There is support for both reading and writing FlatBuffers in Python.
    42
    43To use FlatBuffers in your own code, first generate Python classes from your
    44schema with the `--python` option to `flatc`. Then you can include both
    45FlatBuffers and the generated code to read or write a FlatBuffer.
    46
    47For example, here is how you would read a FlatBuffer binary file in Python:
    48First, import the library and the generated code. Then read a FlatBuffer binary
    49file into a `bytearray`, which you pass to the `GetRootAsMonster` function:
    50
    51~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.py}
    52    import MyGame.Example as example
    53    import flatbuffers
    54
    55    buf = open('monster.dat', 'rb').read()
    56    buf = bytearray(buf)
    57    monster = example.GetRootAsMonster(buf, 0)
    58~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    59
    60Now you can access values like this:
    61
    62~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.py}
    63    hp = monster.Hp()
    64    pos = monster.Pos()
    65~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    66
    67## Support for Numpy arrays
    68
    69The Flatbuffers python library also has support for accessing scalar
    70vectors as numpy arrays. This can be orders of magnitude faster than
    71iterating over the vector one element at a time, and is particularly
    72useful when unpacking large nested flatbuffers. The generated code for
    73a scalar vector will have a method `<vector name>AsNumpy()`. In the
    74case of the Monster example, you could access the inventory vector
    75like this:
    76
    77~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.py}
    78    inventory = monster.InventoryAsNumpy()
    79    # inventory is a numpy array of type np.dtype('uint8')
    80~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    81
    82instead of
    83
    84~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.py}
    85    inventory = []
    86    for i in range(monster.InventoryLength()):
    87        inventory.append(int(monster.Inventory(i)))
    88~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    89
    90Numpy is not a requirement. If numpy is not installed on your system,
    91then attempting to access one of the `*asNumpy()` methods will result
    92in a `NumpyRequiredForThisFeature` exception.
    93
    94## Text Parsing
    95
    96There currently is no support for parsing text (Schema's and JSON) directly
    97from Python, though you could use the C++ parser through SWIG or ctypes. Please
    98see the C++ documentation for more on text parsing.
    99
   100<br>

View as plain text