flatbuffers/tests/MyGame/Example/ArrayTable.py

91 lines
2.7 KiB
Python
Raw Normal View History

# automatically generated by the FlatBuffers compiler, do not modify
# namespace: Example
import flatbuffers
Flatbuffers Python Object API (#5616) * Flatbuffers Python Object API Implement the logic to generate the Python object API that can unpack the data from a buf class into an object class, and pack the data of an object class to a buf class. * Fix the build issues Remove unused parameters and replace auto in the for-loop statement with std::string to make it compatible with VS2010. * Fix the build issues. * Add support for Array type Added logic to handle Array type in Python Object API. Updated the generated code accordingly. * Fix the old style casting from int to char * Fixed another conversion from int to char * Fixed the import for typing Importing typing may cause errors when a machine do not have the moduel typing installed. This PR fixes the issue by guarding "import typing" with the "try/except" statement. * Fix issue of iterating the vector of import list * Update the generated examples using generate_code.sh * Fix the import order for typing The import list was stored in unordered_set, so that each generated codes may have different import order. Therefore, it failed in the consistency test where two generated copies need to have exactly the same apperance. * Optimize unpack using numpy Use numpy to unpack vector whenever it is possible to improve unpack performance. Also, added codegen command for Python specificly in generate_code.sh, because --no-includes cannot be turn on for Python. * Fix the import order * Update generate_code.bat for windows accordingly * Replace error message with pass Avoid printing error message for every Python2 users about typing. Replace it with pass.
2019-12-02 22:11:28 +00:00
from flatbuffers.compat import import_numpy
np = import_numpy()
class ArrayTable(object):
__slots__ = ['_tab']
@classmethod
def GetRootAs(cls, buf, offset=0):
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
x = ArrayTable()
x.Init(buf, n + offset)
return x
Python: Added support for file_identifiers (#5123) * Python: Added support for file_identifiers * Added tests. Fixed file_identifier code. * Python: Fixed excessive padding of file_identifier. Repaired tests. * Python: Made code compatible with python2.7 * Python: Typo fix in @endcond * whitespace normaalization * Stylistic change from if(not X is None) to if(X is not None). Added comment to type string. * Python: Added support for automatic code generation of file_identifiers. Added tests for said code generation. * converted sprintf to snprintf * Bugfix, added snprint deffinition for MSVC * changed snprint deffinition for MSVC to sprint_s * changed scanf to IntToStringHex. Renamed HasFileIdentifier to GenHasFileIdentifier. * Added updated genereated code to commit * Python bugix: flatc no longer produces HasFileIdentfier for shcemas with no file identifier * Added tests to verify `MonsterBufferHasIdentifier` returns false on no Identifier * Python: added tests for GetBufferIdentifier and BufferHasIdentifier Python: removed unessasary parenethesis in if statements Minor format changes. * Python : correceted instances of keyword arguments being called as positional arguments * fixed typos and grammer in comments * Minor style fixes * Indentation fix * Equals style changes * Python: Fixed Alignment Issues. Changed test code to test against atual output * Ran make(forgot to run make last commit) * Python: Style changes * Style changes * indentation and style * readded CONTRIBUTING.md * Formatting tweak Mostly to make CI run again * More formatting fixes * More formatting fixes * More formatting fixes * More formatting fixes * Formatting fix * More formatting fixes * Formatting * ran generate_code.sh
2019-07-26 18:06:25 +00:00
@classmethod
def GetRootAsArrayTable(cls, buf, offset=0):
"""This method is deprecated. Please switch to GetRootAs."""
return cls.GetRootAs(buf, offset)
@classmethod
Python: Added support for file_identifiers (#5123) * Python: Added support for file_identifiers * Added tests. Fixed file_identifier code. * Python: Fixed excessive padding of file_identifier. Repaired tests. * Python: Made code compatible with python2.7 * Python: Typo fix in @endcond * whitespace normaalization * Stylistic change from if(not X is None) to if(X is not None). Added comment to type string. * Python: Added support for automatic code generation of file_identifiers. Added tests for said code generation. * converted sprintf to snprintf * Bugfix, added snprint deffinition for MSVC * changed snprint deffinition for MSVC to sprint_s * changed scanf to IntToStringHex. Renamed HasFileIdentifier to GenHasFileIdentifier. * Added updated genereated code to commit * Python bugix: flatc no longer produces HasFileIdentfier for shcemas with no file identifier * Added tests to verify `MonsterBufferHasIdentifier` returns false on no Identifier * Python: added tests for GetBufferIdentifier and BufferHasIdentifier Python: removed unessasary parenethesis in if statements Minor format changes. * Python : correceted instances of keyword arguments being called as positional arguments * fixed typos and grammer in comments * Minor style fixes * Indentation fix * Equals style changes * Python: Fixed Alignment Issues. Changed test code to test against atual output * Ran make(forgot to run make last commit) * Python: Style changes * Style changes * indentation and style * readded CONTRIBUTING.md * Formatting tweak Mostly to make CI run again * More formatting fixes * More formatting fixes * More formatting fixes * More formatting fixes * Formatting fix * More formatting fixes * Formatting * ran generate_code.sh
2019-07-26 18:06:25 +00:00
def ArrayTableBufferHasIdentifier(cls, buf, offset, size_prefixed=False):
return flatbuffers.util.BufferHasIdentifier(buf, offset, b"\x41\x52\x52\x54", size_prefixed=size_prefixed)
# ArrayTable
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)
# ArrayTable
def A(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
x = o + self._tab.Pos
Flatbuffers Python Object API (#5616) * Flatbuffers Python Object API Implement the logic to generate the Python object API that can unpack the data from a buf class into an object class, and pack the data of an object class to a buf class. * Fix the build issues Remove unused parameters and replace auto in the for-loop statement with std::string to make it compatible with VS2010. * Fix the build issues. * Add support for Array type Added logic to handle Array type in Python Object API. Updated the generated code accordingly. * Fix the old style casting from int to char * Fixed another conversion from int to char * Fixed the import for typing Importing typing may cause errors when a machine do not have the moduel typing installed. This PR fixes the issue by guarding "import typing" with the "try/except" statement. * Fix issue of iterating the vector of import list * Update the generated examples using generate_code.sh * Fix the import order for typing The import list was stored in unordered_set, so that each generated codes may have different import order. Therefore, it failed in the consistency test where two generated copies need to have exactly the same apperance. * Optimize unpack using numpy Use numpy to unpack vector whenever it is possible to improve unpack performance. Also, added codegen command for Python specificly in generate_code.sh, because --no-includes cannot be turn on for Python. * Fix the import order * Update generate_code.bat for windows accordingly * Replace error message with pass Avoid printing error message for every Python2 users about typing. Replace it with pass.
2019-12-02 22:11:28 +00:00
from MyGame.Example.ArrayStruct import ArrayStruct
obj = ArrayStruct()
obj.Init(self._tab.Bytes, x)
return obj
return None
Keep methods with struct name and switch them to default (#6879) * Keep methods with struct name and switch them to default This PR can help fix the following two issues: 1): A set of simplified API (without struct name) was added in https://github.com/google/flatbuffers/pull/6336. It causes name conflict when merging all generated python file into a single one (the primary usage senario in Google). 2): Flatbuffers 2.0 generates absolute import path, which may cause name space conflicts. See more details in https://github.com/google/flatbuffers/issues/5840. The solution for both is to generate the merged Python code, similar C++. The merged code will not contain the simplied API, but only the method with struct name. For issue (1), it will mimic the exactly usage pattern for Google internal. For issue (2), users can generate the merged flatbuffer code, without worrying about the imports. The above idea will be implemented in the following steps: Step 1 (this PR): revert changes in https://github.com/google/flatbuffers/pull/6336 that set the simplified API as default. Remove statements that the original API will be deprecated, and reset the original API as default. Step 2 (the following PR): create a flag to generate the merged code. The Simplified API will be removed from the merged code, otherwise it will cause name conflict. * Update the generated sample code * Update the generated example code * Reverst the changes of GetRootAs * Update examples from grpc/example/generate.sh
2021-11-04 22:12:25 +00:00
def ArrayTableStart(builder): builder.StartObject(1)
def Start(builder):
return ArrayTableStart(builder)
def ArrayTableAddA(builder, a): builder.PrependStructSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(a), 0)
def AddA(builder, a):
return ArrayTableAddA(builder, a)
def ArrayTableEnd(builder): return builder.EndObject()
def End(builder):
return ArrayTableEnd(builder)
Flatbuffers Python Object API (#5616) * Flatbuffers Python Object API Implement the logic to generate the Python object API that can unpack the data from a buf class into an object class, and pack the data of an object class to a buf class. * Fix the build issues Remove unused parameters and replace auto in the for-loop statement with std::string to make it compatible with VS2010. * Fix the build issues. * Add support for Array type Added logic to handle Array type in Python Object API. Updated the generated code accordingly. * Fix the old style casting from int to char * Fixed another conversion from int to char * Fixed the import for typing Importing typing may cause errors when a machine do not have the moduel typing installed. This PR fixes the issue by guarding "import typing" with the "try/except" statement. * Fix issue of iterating the vector of import list * Update the generated examples using generate_code.sh * Fix the import order for typing The import list was stored in unordered_set, so that each generated codes may have different import order. Therefore, it failed in the consistency test where two generated copies need to have exactly the same apperance. * Optimize unpack using numpy Use numpy to unpack vector whenever it is possible to improve unpack performance. Also, added codegen command for Python specificly in generate_code.sh, because --no-includes cannot be turn on for Python. * Fix the import order * Update generate_code.bat for windows accordingly * Replace error message with pass Avoid printing error message for every Python2 users about typing. Replace it with pass.
2019-12-02 22:11:28 +00:00
import MyGame.Example.ArrayStruct
try:
from typing import Optional
except:
pass
class ArrayTableT(object):
# ArrayTableT
def __init__(self):
self.a = None # type: Optional[MyGame.Example.ArrayStruct.ArrayStructT]
@classmethod
def InitFromBuf(cls, buf, pos):
[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
2022-09-22 18:08:09 +00:00
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, 0)
Flatbuffers Python Object API (#5616) * Flatbuffers Python Object API Implement the logic to generate the Python object API that can unpack the data from a buf class into an object class, and pack the data of an object class to a buf class. * Fix the build issues Remove unused parameters and replace auto in the for-loop statement with std::string to make it compatible with VS2010. * Fix the build issues. * Add support for Array type Added logic to handle Array type in Python Object API. Updated the generated code accordingly. * Fix the old style casting from int to char * Fixed another conversion from int to char * Fixed the import for typing Importing typing may cause errors when a machine do not have the moduel typing installed. This PR fixes the issue by guarding "import typing" with the "try/except" statement. * Fix issue of iterating the vector of import list * Update the generated examples using generate_code.sh * Fix the import order for typing The import list was stored in unordered_set, so that each generated codes may have different import order. Therefore, it failed in the consistency test where two generated copies need to have exactly the same apperance. * Optimize unpack using numpy Use numpy to unpack vector whenever it is possible to improve unpack performance. Also, added codegen command for Python specificly in generate_code.sh, because --no-includes cannot be turn on for Python. * Fix the import order * Update generate_code.bat for windows accordingly * Replace error message with pass Avoid printing error message for every Python2 users about typing. Replace it with pass.
2019-12-02 22:11:28 +00:00
arrayTable = ArrayTable()
[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
2022-09-22 18:08:09 +00:00
arrayTable.Init(buf, pos+n)
Flatbuffers Python Object API (#5616) * Flatbuffers Python Object API Implement the logic to generate the Python object API that can unpack the data from a buf class into an object class, and pack the data of an object class to a buf class. * Fix the build issues Remove unused parameters and replace auto in the for-loop statement with std::string to make it compatible with VS2010. * Fix the build issues. * Add support for Array type Added logic to handle Array type in Python Object API. Updated the generated code accordingly. * Fix the old style casting from int to char * Fixed another conversion from int to char * Fixed the import for typing Importing typing may cause errors when a machine do not have the moduel typing installed. This PR fixes the issue by guarding "import typing" with the "try/except" statement. * Fix issue of iterating the vector of import list * Update the generated examples using generate_code.sh * Fix the import order for typing The import list was stored in unordered_set, so that each generated codes may have different import order. Therefore, it failed in the consistency test where two generated copies need to have exactly the same apperance. * Optimize unpack using numpy Use numpy to unpack vector whenever it is possible to improve unpack performance. Also, added codegen command for Python specificly in generate_code.sh, because --no-includes cannot be turn on for Python. * Fix the import order * Update generate_code.bat for windows accordingly * Replace error message with pass Avoid printing error message for every Python2 users about typing. Replace it with pass.
2019-12-02 22:11:28 +00:00
return cls.InitFromObj(arrayTable)
@classmethod
def InitFromObj(cls, arrayTable):
x = ArrayTableT()
x._UnPack(arrayTable)
return x
# ArrayTableT
def _UnPack(self, arrayTable):
if arrayTable is None:
return
if arrayTable.A() is not None:
self.a = MyGame.Example.ArrayStruct.ArrayStructT.InitFromObj(arrayTable.A())
# ArrayTableT
def Pack(self, builder):
Keep methods with struct name and switch them to default (#6879) * Keep methods with struct name and switch them to default This PR can help fix the following two issues: 1): A set of simplified API (without struct name) was added in https://github.com/google/flatbuffers/pull/6336. It causes name conflict when merging all generated python file into a single one (the primary usage senario in Google). 2): Flatbuffers 2.0 generates absolute import path, which may cause name space conflicts. See more details in https://github.com/google/flatbuffers/issues/5840. The solution for both is to generate the merged Python code, similar C++. The merged code will not contain the simplied API, but only the method with struct name. For issue (1), it will mimic the exactly usage pattern for Google internal. For issue (2), users can generate the merged flatbuffer code, without worrying about the imports. The above idea will be implemented in the following steps: Step 1 (this PR): revert changes in https://github.com/google/flatbuffers/pull/6336 that set the simplified API as default. Remove statements that the original API will be deprecated, and reset the original API as default. Step 2 (the following PR): create a flag to generate the merged code. The Simplified API will be removed from the merged code, otherwise it will cause name conflict. * Update the generated sample code * Update the generated example code * Reverst the changes of GetRootAs * Update examples from grpc/example/generate.sh
2021-11-04 22:12:25 +00:00
ArrayTableStart(builder)
Flatbuffers Python Object API (#5616) * Flatbuffers Python Object API Implement the logic to generate the Python object API that can unpack the data from a buf class into an object class, and pack the data of an object class to a buf class. * Fix the build issues Remove unused parameters and replace auto in the for-loop statement with std::string to make it compatible with VS2010. * Fix the build issues. * Add support for Array type Added logic to handle Array type in Python Object API. Updated the generated code accordingly. * Fix the old style casting from int to char * Fixed another conversion from int to char * Fixed the import for typing Importing typing may cause errors when a machine do not have the moduel typing installed. This PR fixes the issue by guarding "import typing" with the "try/except" statement. * Fix issue of iterating the vector of import list * Update the generated examples using generate_code.sh * Fix the import order for typing The import list was stored in unordered_set, so that each generated codes may have different import order. Therefore, it failed in the consistency test where two generated copies need to have exactly the same apperance. * Optimize unpack using numpy Use numpy to unpack vector whenever it is possible to improve unpack performance. Also, added codegen command for Python specificly in generate_code.sh, because --no-includes cannot be turn on for Python. * Fix the import order * Update generate_code.bat for windows accordingly * Replace error message with pass Avoid printing error message for every Python2 users about typing. Replace it with pass.
2019-12-02 22:11:28 +00:00
if self.a is not None:
a = self.a.Pack(builder)
Keep methods with struct name and switch them to default (#6879) * Keep methods with struct name and switch them to default This PR can help fix the following two issues: 1): A set of simplified API (without struct name) was added in https://github.com/google/flatbuffers/pull/6336. It causes name conflict when merging all generated python file into a single one (the primary usage senario in Google). 2): Flatbuffers 2.0 generates absolute import path, which may cause name space conflicts. See more details in https://github.com/google/flatbuffers/issues/5840. The solution for both is to generate the merged Python code, similar C++. The merged code will not contain the simplied API, but only the method with struct name. For issue (1), it will mimic the exactly usage pattern for Google internal. For issue (2), users can generate the merged flatbuffer code, without worrying about the imports. The above idea will be implemented in the following steps: Step 1 (this PR): revert changes in https://github.com/google/flatbuffers/pull/6336 that set the simplified API as default. Remove statements that the original API will be deprecated, and reset the original API as default. Step 2 (the following PR): create a flag to generate the merged code. The Simplified API will be removed from the merged code, otherwise it will cause name conflict. * Update the generated sample code * Update the generated example code * Reverst the changes of GetRootAs * Update examples from grpc/example/generate.sh
2021-11-04 22:12:25 +00:00
ArrayTableAddA(builder, a)
arrayTable = ArrayTableEnd(builder)
Flatbuffers Python Object API (#5616) * Flatbuffers Python Object API Implement the logic to generate the Python object API that can unpack the data from a buf class into an object class, and pack the data of an object class to a buf class. * Fix the build issues Remove unused parameters and replace auto in the for-loop statement with std::string to make it compatible with VS2010. * Fix the build issues. * Add support for Array type Added logic to handle Array type in Python Object API. Updated the generated code accordingly. * Fix the old style casting from int to char * Fixed another conversion from int to char * Fixed the import for typing Importing typing may cause errors when a machine do not have the moduel typing installed. This PR fixes the issue by guarding "import typing" with the "try/except" statement. * Fix issue of iterating the vector of import list * Update the generated examples using generate_code.sh * Fix the import order for typing The import list was stored in unordered_set, so that each generated codes may have different import order. Therefore, it failed in the consistency test where two generated copies need to have exactly the same apperance. * Optimize unpack using numpy Use numpy to unpack vector whenever it is possible to improve unpack performance. Also, added codegen command for Python specificly in generate_code.sh, because --no-includes cannot be turn on for Python. * Fix the import order * Update generate_code.bat for windows accordingly * Replace error message with pass Avoid printing error message for every Python2 users about typing. Replace it with pass.
2019-12-02 22:11:28 +00:00
return arrayTable