xref: /aosp_15_r20/external/flatbuffers/python/flatbuffers/encode.py (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker# Copyright 2014 Google Inc. All rights reserved.
2*890232f2SAndroid Build Coastguard Worker#
3*890232f2SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*890232f2SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*890232f2SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*890232f2SAndroid Build Coastguard Worker#
7*890232f2SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*890232f2SAndroid Build Coastguard Worker#
9*890232f2SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*890232f2SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*890232f2SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*890232f2SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*890232f2SAndroid Build Coastguard Worker# limitations under the License.
14*890232f2SAndroid Build Coastguard Worker
15*890232f2SAndroid Build Coastguard Workerfrom . import number_types as N
16*890232f2SAndroid Build Coastguard Workerfrom . import packer
17*890232f2SAndroid Build Coastguard Workerfrom .compat import memoryview_type
18*890232f2SAndroid Build Coastguard Workerfrom .compat import import_numpy, NumpyRequiredForThisFeature
19*890232f2SAndroid Build Coastguard Worker
20*890232f2SAndroid Build Coastguard Workernp = import_numpy()
21*890232f2SAndroid Build Coastguard Worker
22*890232f2SAndroid Build Coastguard WorkerFILE_IDENTIFIER_LENGTH=4
23*890232f2SAndroid Build Coastguard Worker
24*890232f2SAndroid Build Coastguard Workerdef Get(packer_type, buf, head):
25*890232f2SAndroid Build Coastguard Worker    """ Get decodes a value at buf[head] using `packer_type`. """
26*890232f2SAndroid Build Coastguard Worker    return packer_type.unpack_from(memoryview_type(buf), head)[0]
27*890232f2SAndroid Build Coastguard Worker
28*890232f2SAndroid Build Coastguard Worker
29*890232f2SAndroid Build Coastguard Workerdef GetVectorAsNumpy(numpy_type, buf, count, offset):
30*890232f2SAndroid Build Coastguard Worker    """ GetVecAsNumpy decodes values starting at buf[head] as
31*890232f2SAndroid Build Coastguard Worker    `numpy_type`, where `numpy_type` is a numpy dtype. """
32*890232f2SAndroid Build Coastguard Worker    if np is not None:
33*890232f2SAndroid Build Coastguard Worker        # TODO: could set .flags.writeable = False to make users jump through
34*890232f2SAndroid Build Coastguard Worker        #       hoops before modifying...
35*890232f2SAndroid Build Coastguard Worker        return np.frombuffer(buf, dtype=numpy_type, count=count, offset=offset)
36*890232f2SAndroid Build Coastguard Worker    else:
37*890232f2SAndroid Build Coastguard Worker        raise NumpyRequiredForThisFeature('Numpy was not found.')
38*890232f2SAndroid Build Coastguard Worker
39*890232f2SAndroid Build Coastguard Worker
40*890232f2SAndroid Build Coastguard Workerdef Write(packer_type, buf, head, n):
41*890232f2SAndroid Build Coastguard Worker    """ Write encodes `n` at buf[head] using `packer_type`. """
42*890232f2SAndroid Build Coastguard Worker    packer_type.pack_into(buf, head, n)
43