codekingpro/portable-devtools
115k
1# Copyright 2014 Google Inc. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15from . import number_types as N16from . import packer17from .compat import memoryview_type18from .compat import NumpyRequiredForThisFeature, import_numpy19 20np = import_numpy()21 22FILE_IDENTIFIER_LENGTH = 423 24 25def Get(packer_type, buf, head):26 """Get decodes a value at buf[head] using `packer_type`."""27 return packer_type.unpack_from(memoryview_type(buf), head)[0]28 29 30def GetVectorAsNumpy(numpy_type, buf, count, offset):31 """GetVecAsNumpy decodes values starting at buf[head] as32 33 `numpy_type`, where `numpy_type` is a numpy dtype.34 """35 if np is not None:36 # TODO: could set .flags.writeable = False to make users jump through37 # hoops before modifying...38 return np.frombuffer(buf, dtype=numpy_type, count=count, offset=offset)39 else:40 raise NumpyRequiredForThisFeature('Numpy was not found.')41 42 43def Write(packer_type, buf, head, n):44 """Write encodes `n` at buf[head] using `packer_type`."""45 packer_type.pack_into(buf, head, n)46 