summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJoshua Smith <102520999+joshua-smith8@users.noreply.github.com>2022-09-22 19:08:09 +0100
committerGitHub <noreply@github.com>2022-09-22 11:08:09 -0700
commit413115858c1e89b340b0e4465a005d0f0ac45478 (patch)
tree4e4e0f97a1def0f7a7efc69d86fd3333d93325f8 /python
parent88046190eee8089cca5226ec3707d112f622fbd9 (diff)
downloadflatbuffers-413115858c1e89b340b0e4465a005d0f0ac45478.tar.gz
flatbuffers-413115858c1e89b340b0e4465a005d0f0ac45478.tar.bz2
flatbuffers-413115858c1e89b340b0e4465a005d0f0ac45478.zip
[Python] Python fixed size array (#7529)
* feat: Added support for fixed sized arrays to python Problem: We encountered that using fixed arrays from C++ to python that python would not read those arrays correctly due to no size information being encoded in the byte array itself. Fix: Encode the sizes within the generated python file during code generation. Specfically we add GetArrayAsNumpy to the python version of table, which takes as input the length of the vector. When generating the python message files we include this length from the VectorType().fixed_length. * fix: added digit support for camel case to snake case conversion Problem: When including a number in the message name we would encounter cases where SnakeCase would not add the appropirate breaks. e.g. Int32Stamped -> int_32stamped rather than int_32_stamped. Fix: To fix this we can add the condition that we check if the current character is not lower and not a digit, that we check if the previous character was a lower or digit. If it was a lower or digit then we add the break. * fix: Array support for structures Problem: The python generated code for handling non-struct and struct vectors and arrays was inconsistent. The calls to populate the obj api was creating incorrect code. Solution: To fix this the VectorOfStruct and VectorOfNonStruct was rewritten to handle array cases and bring the two methods in line which each other. Testing: PythonTesting.sh now correctly runs and generates the code for array_test.fbs. Minor modifications were done on the test to use the new index accessor for struct arrays and the script correctly sources the location of the python code. * chore: clang format changes * Added code generated by scripts/generate_code. Modified GetArrayOfNonStruct slightly to allow for function overloading allowing the user to get a single element of an array or the whole array. * Added new_line parameter to OffsetPrefix to allow optional new lines to be added. This allows us to use the GenIndents method that automatically adds new lines instead. * Reupload of generated code from the scripts/generate_code.py * Removed new line in GetVectorAsNumpy. * Updated Array lengths to use Length methods where possible. Added fallthrough for GenTypePointer. Added digit check to CamelToSnake method. Added and modified tests for ToSnakeCase and CamelToSnake. * Added range check on the getter methods for vector and array types. Renamed == as is for python
Diffstat (limited to 'python')
-rw-r--r--python/flatbuffers/table.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/python/flatbuffers/table.py b/python/flatbuffers/table.py
index adc76ca8..d5336ca6 100644
--- a/python/flatbuffers/table.py
+++ b/python/flatbuffers/table.py
@@ -113,6 +113,15 @@ class Table(object):
numpy_dtype = N.to_numpy_type(flags)
return encode.GetVectorAsNumpy(numpy_dtype, self.Bytes, length, offset)
+ def GetArrayAsNumpy(self, flags, off, length):
+ """
+ GetArrayAsNumpy returns the array with fixed width that starts at `Vector(offset)`
+ with length `length` as a numpy array with the type specified by `flags`. The
+ array is a `view` into Bytes so modifying the returned will modify Bytes in place.
+ """
+ numpy_dtype = N.to_numpy_type(flags)
+ return encode.GetVectorAsNumpy(numpy_dtype, self.Bytes, length, off)
+
def GetVOffsetTSlot(self, slot, d):
"""
GetVOffsetTSlot retrieves the VOffsetT that the given vtable location
@@ -125,5 +134,5 @@ class Table(object):
off = self.Offset(slot)
if off == 0:
- return d
+ return d
return off