[Python] Improve python API (#8781)

* Fix generate_code script path

* [Python] Make StartVector public

Make StartVector vector public since it is already being used in
generated code

* [Python] Improve vector creation for Python API

Makes Python API for vectors cleaner like Rust and Swift

---------

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Jacob Abrams
2026-01-20 17:01:20 -08:00
committed by GitHub
parent 0d67abde45
commit af8997b567
20 changed files with 622 additions and 88 deletions

View File

@@ -422,7 +422,6 @@ class Builder(object):
off2 = self.Offset() - off + N.UOffsetTFlags.bytewidth
self.PlaceUOffsetT(off2)
## @cond FLATBUFFERS_INTERNAL
def StartVector(self, elemSize, numElems, alignment):
"""StartVector initializes bookkeeping for writing a new vector.
@@ -438,8 +437,6 @@ class Builder(object):
self.Prep(alignment, elemSize * numElems) # In case alignment > int.
return self.Offset()
## @endcond
def EndVector(self, numElems=None):
"""EndVector writes data necessary to finish vector construction."""
@@ -556,6 +553,21 @@ class Builder(object):
self.vectorNumElems = x.size
return self.EndVector()
def CreateVectorOfTables(self, offsets):
"""CreateVectorOfTables writes a vector of offsets such as tables or strings.
Args:
offsets: Iterable of offsets returned from previous builder operations.
Each element should be an integer compatible with UOffsetT.
"""
offsets = list(offsets)
self.StartVector(N.UOffsetTFlags.bytewidth, len(offsets),
N.UOffsetTFlags.bytewidth)
for off in reversed(offsets):
self.PrependUOffsetTRelative(off)
return self.EndVector()
## @cond FLATBUFFERS_INTERNAL
def assertNested(self):
"""Check that we are in the process of building an object."""