bulk code format fix (#8707)

This commit is contained in:
Derek Bailey
2025-09-23 21:50:27 -07:00
committed by GitHub
parent 0e047869da
commit caf3b494db
559 changed files with 38871 additions and 31276 deletions

View File

@@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from .builder import Builder
from .table import Table
from .compat import range_func as compat_range
from ._version import __version__
from . import util
from ._version import __version__
from .builder import Builder
from .compat import range_func as compat_range
from .table import Table

View File

@@ -14,4 +14,4 @@
# Placeholder, to be updated during the release process
# by the setup.py
__version__ = u"25.2.10"
__version__ = "25.2.10"

File diff suppressed because it is too large Load Diff

View File

@@ -12,8 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
""" A tiny version of `six` to help with backwards compability. Also includes
compatibility helpers for numpy. """
"""A tiny version of `six` to help with backwards compability.
Also includes compatibility helpers for numpy.
"""
import sys
@@ -25,62 +27,65 @@ PY3 = sys.version_info[0] == 3
PY34 = sys.version_info[0:2] >= (3, 4)
if PY3:
import importlib.machinery
string_types = (str,)
binary_types = (bytes,bytearray)
range_func = range
import importlib.machinery
string_types = (str,)
binary_types = (bytes, bytearray)
range_func = range
memoryview_type = memoryview
struct_bool_decl = "?"
else:
import imp
string_types = (unicode,)
if PY26 or PY27:
binary_types = (str, bytearray)
else:
binary_types = (str,)
range_func = xrange
if PY26 or (PY27 and not PY275):
memoryview_type = buffer
struct_bool_decl = "<b"
else:
memoryview_type = memoryview
struct_bool_decl = "?"
else:
import imp
string_types = (unicode,)
if PY26 or PY27:
binary_types = (str,bytearray)
else:
binary_types = (str,)
range_func = xrange
if PY26 or (PY27 and not PY275):
memoryview_type = buffer
struct_bool_decl = "<b"
else:
memoryview_type = memoryview
struct_bool_decl = "?"
# Helper functions to facilitate making numpy optional instead of required
def import_numpy():
"""
Returns the numpy module if it exists on the system,
otherwise returns None.
"""
if PY3:
numpy_exists = (
importlib.machinery.PathFinder.find_spec('numpy') is not None)
else:
try:
imp.find_module('numpy')
numpy_exists = True
except ImportError:
numpy_exists = False
"""Returns the numpy module if it exists on the system,
if numpy_exists:
# We do this outside of try/except block in case numpy exists
# but is not installed correctly. We do not want to catch an
# incorrect installation which would manifest as an
# ImportError.
import numpy as np
else:
np = None
otherwise returns None.
"""
if PY3:
numpy_exists = importlib.machinery.PathFinder.find_spec("numpy") is not None
else:
try:
imp.find_module("numpy")
numpy_exists = True
except ImportError:
numpy_exists = False
return np
if numpy_exists:
# We do this outside of try/except block in case numpy exists
# but is not installed correctly. We do not want to catch an
# incorrect installation which would manifest as an
# ImportError.
import numpy as np
else:
np = None
return np
class NumpyRequiredForThisFeature(RuntimeError):
"""
Error raised when user tries to use a feature that
requires numpy without having numpy installed.
"""
pass
"""Error raised when user tries to use a feature that
requires numpy without having numpy installed.
"""
pass
# NOTE: Future Jython support may require code here (look at `six`).

View File

@@ -15,28 +15,31 @@
from . import number_types as N
from . import packer
from .compat import memoryview_type
from .compat import import_numpy, NumpyRequiredForThisFeature
from .compat import NumpyRequiredForThisFeature, import_numpy
np = import_numpy()
FILE_IDENTIFIER_LENGTH=4
FILE_IDENTIFIER_LENGTH = 4
def Get(packer_type, buf, head):
""" Get decodes a value at buf[head] using `packer_type`. """
return packer_type.unpack_from(memoryview_type(buf), head)[0]
"""Get decodes a value at buf[head] using `packer_type`."""
return packer_type.unpack_from(memoryview_type(buf), head)[0]
def GetVectorAsNumpy(numpy_type, buf, count, offset):
""" GetVecAsNumpy decodes values starting at buf[head] as
`numpy_type`, where `numpy_type` is a numpy dtype. """
if np is not None:
# TODO: could set .flags.writeable = False to make users jump through
# hoops before modifying...
return np.frombuffer(buf, dtype=numpy_type, count=count, offset=offset)
else:
raise NumpyRequiredForThisFeature('Numpy was not found.')
"""GetVecAsNumpy decodes values starting at buf[head] as
`numpy_type`, where `numpy_type` is a numpy dtype.
"""
if np is not None:
# TODO: could set .flags.writeable = False to make users jump through
# hoops before modifying...
return np.frombuffer(buf, dtype=numpy_type, count=count, offset=offset)
else:
raise NumpyRequiredForThisFeature('Numpy was not found.')
def Write(packer_type, buf, head, n):
""" Write encodes `n` at buf[head] using `packer_type`. """
packer_type.pack_into(buf, head, n)
"""Write encodes `n` at buf[head] using `packer_type`."""
packer_type.pack_into(buf, head, n)

View File

@@ -36,6 +36,7 @@ class BitWidth(enum.IntEnum):
These are used in the lower 2 bits of a type field to determine the size of
the elements (and or size field) of the item pointed to (e.g. vector).
"""
W8 = 0 # 2^0 = 1 byte
W16 = 1 # 2^1 = 2 bytes
W32 = 2 # 2^2 = 4 bytes
@@ -81,12 +82,9 @@ class BitWidth(enum.IntEnum):
@staticmethod
def B(byte_width):
return {
1: BitWidth.W8,
2: BitWidth.W16,
4: BitWidth.W32,
8: BitWidth.W64
}[byte_width]
return {1: BitWidth.W8, 2: BitWidth.W16, 4: BitWidth.W32, 8: BitWidth.W64}[
byte_width
]
I = {1: 'b', 2: 'h', 4: 'i', 8: 'q'} # Integer formats
@@ -165,6 +163,7 @@ class Type(enum.IntEnum):
These are used as the upper 6 bits of a type field to indicate the actual
type.
"""
NULL = 0
INT = 1
UINT = 2
@@ -214,8 +213,10 @@ class Type(enum.IntEnum):
@staticmethod
def IsTypedVector(type_):
return Type.VECTOR_INT <= type_ <= Type.VECTOR_STRING_DEPRECATED or \
type_ == Type.VECTOR_BOOL
return (
Type.VECTOR_INT <= type_ <= Type.VECTOR_STRING_DEPRECATED
or type_ == Type.VECTOR_BOOL
)
@staticmethod
def IsTypedVectorElementType(type_):
@@ -306,7 +307,7 @@ class Buf:
def Find(self, sub):
"""Returns the lowest index where the sub subsequence is found."""
return self._buf[self._offset:].find(sub)
return self._buf[self._offset :].find(sub)
def Slice(self, offset):
"""Returns new `Buf` which starts from the given offset."""
@@ -314,11 +315,12 @@ class Buf:
def Indirect(self, offset, byte_width):
"""Return new `Buf` based on the encoded offset (indirect encoding)."""
return self.Slice(offset - _Unpack(U, self[offset:offset + byte_width]))
return self.Slice(offset - _Unpack(U, self[offset : offset + byte_width]))
class Object:
"""Base class for all non-trivial data accessors."""
__slots__ = '_buf', '_byte_width'
def __init__(self, buf, byte_width):
@@ -332,7 +334,8 @@ class Object:
class Sized(Object):
"""Base class for all data accessors which need to read encoded size."""
__slots__ = '_size',
__slots__ = ('_size',)
def __init__(self, buf, byte_width, size=0):
super().__init__(buf, byte_width)
@@ -343,7 +346,7 @@ class Sized(Object):
@property
def SizeBytes(self):
return self._buf[-self._byte_width:0]
return self._buf[-self._byte_width : 0]
def __len__(self):
return self._size
@@ -351,11 +354,12 @@ class Sized(Object):
class Blob(Sized):
"""Data accessor for the encoded blob bytes."""
__slots__ = ()
@property
def Bytes(self):
return self._buf[0:len(self)]
return self._buf[0 : len(self)]
def __repr__(self):
return 'Blob(%s, size=%d)' % (self._buf, len(self))
@@ -363,11 +367,12 @@ class Blob(Sized):
class String(Sized):
"""Data accessor for the encoded string bytes."""
__slots__ = ()
@property
def Bytes(self):
return self._buf[0:len(self)]
return self._buf[0 : len(self)]
def Mutate(self, value):
"""Mutates underlying string bytes in place.
@@ -383,9 +388,9 @@ class String(Sized):
encoded = value.encode('utf-8')
n = len(encoded)
if n <= len(self):
self._buf[-self._byte_width:0] = _Pack(U, n, self._byte_width)
self._buf[-self._byte_width : 0] = _Pack(U, n, self._byte_width)
self._buf[0:n] = encoded
self._buf[n:len(self)] = bytearray(len(self) - n)
self._buf[n : len(self)] = bytearray(len(self) - n)
return True
return False
@@ -398,6 +403,7 @@ class String(Sized):
class Key(Object):
"""Data accessor for the encoded key bytes."""
__slots__ = ()
def __init__(self, buf, byte_width):
@@ -406,7 +412,7 @@ class Key(Object):
@property
def Bytes(self):
return self._buf[0:len(self)]
return self._buf[0 : len(self)]
def __len__(self):
return self._buf.Find(0)
@@ -420,12 +426,14 @@ class Key(Object):
class Vector(Sized):
"""Data accessor for the encoded vector bytes."""
__slots__ = ()
def __getitem__(self, index):
if index < 0 or index >= len(self):
raise IndexError('vector index %s is out of [0, %d) range' % \
(index, len(self)))
raise IndexError(
'vector index %s is out of [0, %d) range' % (index, len(self))
)
packed_type = self._buf[len(self) * self._byte_width + index]
buf = self._buf.Slice(index * self._byte_width)
@@ -437,12 +445,16 @@ class Vector(Sized):
return [e.Value for e in self]
def __repr__(self):
return 'Vector(%s, byte_width=%d, size=%d)' % \
(self._buf, self._byte_width, self._size)
return 'Vector(%s, byte_width=%d, size=%d)' % (
self._buf,
self._byte_width,
self._size,
)
class TypedVector(Sized):
"""Data accessor for the encoded typed vector or fixed typed vector bytes."""
__slots__ = '_element_type', '_size'
def __init__(self, buf, byte_width, element_type, size=0):
@@ -461,7 +473,7 @@ class TypedVector(Sized):
@property
def Bytes(self):
return self._buf[:self._byte_width * len(self)]
return self._buf[: self._byte_width * len(self)]
@property
def ElementType(self):
@@ -469,8 +481,9 @@ class TypedVector(Sized):
def __getitem__(self, index):
if index < 0 or index >= len(self):
raise IndexError('vector index %s is out of [0, %d) range' % \
(index, len(self)))
raise IndexError(
'vector index %s is out of [0, %d) range' % (index, len(self))
)
buf = self._buf.Slice(index * self._byte_width)
return Ref(buf, self._byte_width, 1, self._element_type)
@@ -497,8 +510,12 @@ class TypedVector(Sized):
raise TypeError('unsupported element_type: %s' % self._element_type)
def __repr__(self):
return 'TypedVector(%s, byte_width=%d, element_type=%s, size=%d)' % \
(self._buf, self._byte_width, self._element_type, self._size)
return 'TypedVector(%s, byte_width=%d, element_type=%s, size=%d)' % (
self._buf,
self._byte_width,
self._element_type,
self._size,
)
class Map(Vector):
@@ -524,7 +541,9 @@ class Map(Vector):
@property
def Keys(self):
byte_width = _Unpack(U, self._buf[-2 * self._byte_width:-self._byte_width])
byte_width = _Unpack(
U, self._buf[-2 * self._byte_width : -self._byte_width]
)
buf = self._buf.Indirect(-3 * self._byte_width, self._byte_width)
return TypedVector(buf, byte_width, Type.KEY)
@@ -542,6 +561,7 @@ class Map(Vector):
class Ref:
"""Data accessor for the encoded data bytes."""
__slots__ = '_buf', '_parent_width', '_byte_width', '_type'
@staticmethod
@@ -556,12 +576,16 @@ class Ref:
self._type = type_
def __repr__(self):
return 'Ref(%s, parent_width=%d, byte_width=%d, type_=%s)' % \
(self._buf, self._parent_width, self._byte_width, self._type)
return 'Ref(%s, parent_width=%d, byte_width=%d, type_=%s)' % (
self._buf,
self._parent_width,
self._byte_width,
self._type,
)
@property
def _Bytes(self):
return self._buf[:self._parent_width]
return self._buf[: self._parent_width]
def _ConvertError(self, target_type):
raise TypeError('cannot convert %s to %s' % (self._type, target_type))
@@ -593,8 +617,9 @@ class Ref:
Returns:
Whether the value was mutated or not.
"""
return self.IsBool and \
_Mutate(U, self._buf, value, self._parent_width, BitWidth.W8)
return self.IsBool and _Mutate(
U, self._buf, value, self._parent_width, BitWidth.W8
)
@property
def IsNumeric(self):
@@ -602,8 +627,12 @@ class Ref:
@property
def IsInt(self):
return self._type in (Type.INT, Type.INDIRECT_INT, Type.UINT,
Type.INDIRECT_UINT)
return self._type in (
Type.INT,
Type.INDIRECT_INT,
Type.UINT,
Type.INDIRECT_UINT,
)
@property
def AsInt(self):
@@ -615,11 +644,11 @@ class Ref:
elif self._type is Type.INT:
return _Unpack(I, self._Bytes)
elif self._type is Type.INDIRECT_INT:
return _Unpack(I, self._Indirect()[:self._byte_width])
return _Unpack(I, self._Indirect()[: self._byte_width])
if self._type is Type.UINT:
return _Unpack(U, self._Bytes)
elif self._type is Type.INDIRECT_UINT:
return _Unpack(U, self._Indirect()[:self._byte_width])
return _Unpack(U, self._Indirect()[: self._byte_width])
elif self.IsString:
return len(self.AsString)
elif self.IsKey:
@@ -648,13 +677,15 @@ class Ref:
if self._type is Type.INT:
return _Mutate(I, self._buf, value, self._parent_width, BitWidth.I(value))
elif self._type is Type.INDIRECT_INT:
return _Mutate(I, self._Indirect(), value, self._byte_width,
BitWidth.I(value))
return _Mutate(
I, self._Indirect(), value, self._byte_width, BitWidth.I(value)
)
elif self._type is Type.UINT:
return _Mutate(U, self._buf, value, self._parent_width, BitWidth.U(value))
elif self._type is Type.INDIRECT_UINT:
return _Mutate(U, self._Indirect(), value, self._byte_width,
BitWidth.U(value))
return _Mutate(
U, self._Indirect(), value, self._byte_width, BitWidth.U(value)
)
else:
return False
@@ -674,7 +705,7 @@ class Ref:
elif self._type is Type.FLOAT:
return _Unpack(F, self._Bytes)
elif self._type is Type.INDIRECT_FLOAT:
return _Unpack(F, self._Indirect()[:self._byte_width])
return _Unpack(F, self._Indirect()[: self._byte_width])
elif self.IsString:
return float(self.AsString)
elif self.IsVector:
@@ -697,11 +728,21 @@ class Ref:
Whether the value was mutated or not.
"""
if self._type is Type.FLOAT:
return _Mutate(F, self._buf, value, self._parent_width,
BitWidth.B(self._parent_width))
return _Mutate(
F,
self._buf,
value,
self._parent_width,
BitWidth.B(self._parent_width),
)
elif self._type is Type.INDIRECT_FLOAT:
return _Mutate(F, self._Indirect(), value, self._byte_width,
BitWidth.B(self._byte_width))
return _Mutate(
F,
self._Indirect(),
value,
self._byte_width,
BitWidth.B(self._byte_width),
)
else:
return False
@@ -781,8 +822,11 @@ class Ref:
@property
def AsTypedVector(self):
if self.IsTypedVector:
return TypedVector(self._Indirect(), self._byte_width,
Type.ToTypedVectorElementType(self._type))
return TypedVector(
self._Indirect(),
self._byte_width,
Type.ToTypedVectorElementType(self._type),
)
else:
raise self._ConvertError('TYPED_VECTOR')
@@ -911,8 +955,11 @@ class Value:
if Type.IsInline(self._type):
return self._min_bit_width
for byte_width in 1, 2, 4, 8:
offset_loc = buf_size + _PaddingBytes(buf_size, byte_width) + \
elem_index * byte_width
offset_loc = (
buf_size
+ _PaddingBytes(buf_size, byte_width)
+ elem_index * byte_width
)
bit_width = BitWidth.U(offset_loc - self._value)
if byte_width == (1 << bit_width):
return bit_width
@@ -937,6 +984,7 @@ def InMap(func):
func(self, *args[1:], **kwargs)
else:
func(self, *args, **kwargs)
return wrapper
@@ -949,6 +997,7 @@ def InMapForString(func):
func(self, args[1])
else:
raise ValueError('invalid number of arguments')
return wrapper
@@ -978,10 +1027,12 @@ class Pool:
class Builder:
"""Helper class to encode structural data into flexbuffers format."""
def __init__(self,
share_strings=False,
share_keys=True,
force_min_bit_width=BitWidth.W8):
def __init__(
self,
share_strings=False,
share_keys=True,
force_min_bit_width=BitWidth.W8,
):
self._share_strings = share_strings
self._share_keys = share_keys
self._force_min_bit_width = force_min_bit_width
@@ -1034,7 +1085,7 @@ class Builder:
def _ReadKey(self, offset):
key = self._buf[offset:]
return key[:key.find(0)]
return key[: key.find(0)]
def _Align(self, alignment):
byte_width = 1 << alignment
@@ -1054,7 +1105,11 @@ class Builder:
def _WriteAny(self, value, byte_width):
fmt = {
Type.NULL: U, Type.BOOL: U, Type.INT: I, Type.UINT: U, Type.FLOAT: F
Type.NULL: U,
Type.BOOL: U,
Type.INT: I,
Type.UINT: U,
Type.FLOAT: F,
}.get(value.Type)
if fmt:
self._Write(fmt, value.Value, byte_width)
@@ -1157,11 +1212,9 @@ class Builder:
def _PushIndirect(self, value, type_, bit_width):
byte_width = self._Align(bit_width)
loc = len(self._buf)
fmt = {
Type.INDIRECT_INT: I,
Type.INDIRECT_UINT: U,
Type.INDIRECT_FLOAT: F
}[type_]
fmt = {Type.INDIRECT_INT: I, Type.INDIRECT_UINT: U, Type.INDIRECT_FLOAT: F}[
type_
]
self._Write(fmt, value, byte_width)
self._stack.append(Value(loc, type_, bit_width))
@@ -1362,10 +1415,12 @@ class Builder:
self._WriteScalarVector(Type.FLOAT, 8, elements, fixed=False)
elif elements.typecode in ('b', 'h', 'i', 'l', 'q'):
self._WriteScalarVector(
Type.INT, elements.itemsize, elements, fixed=False)
Type.INT, elements.itemsize, elements, fixed=False
)
elif elements.typecode in ('B', 'H', 'I', 'L', 'Q'):
self._WriteScalarVector(
Type.UINT, elements.itemsize, elements, fixed=False)
Type.UINT, elements.itemsize, elements, fixed=False
)
else:
raise ValueError('unsupported array typecode: %s' % elements.typecode)
else:
@@ -1375,10 +1430,9 @@ class Builder:
add(e)
@InMap
def FixedTypedVectorFromElements(self,
elements,
element_type=None,
byte_width=0):
def FixedTypedVectorFromElements(
self, elements, element_type=None, byte_width=0
):
"""Encodes sequence of elements of the same type as fixed typed vector.
Args:
@@ -1399,7 +1453,7 @@ class Builder:
if len(types) != 1:
raise TypeError('all elements must be of the same type')
type_, = types
(type_,) = types
if element_type is None:
element_type = {int: Type.INT, float: Type.FLOAT}.get(type_)
@@ -1410,7 +1464,7 @@ class Builder:
width = {
Type.UINT: BitWidth.U,
Type.INT: BitWidth.I,
Type.FLOAT: BitWidth.F
Type.FLOAT: BitWidth.F,
}[element_type]
byte_width = 1 << max(width(e) for e in elements)
@@ -1441,7 +1495,8 @@ class Builder:
keys = self._CreateVector(self._stack[start::2], typed=True, fixed=False)
values = self._CreateVector(
self._stack[start + 1::2], typed=False, fixed=False, keys=keys)
self._stack[start + 1 :: 2], typed=False, fixed=False, keys=keys
)
del self._stack[start:]
self._stack.append(values)
@@ -1521,7 +1576,8 @@ def GetRoot(buf):
raise ValueError('buffer is too small')
byte_width = buf[-1]
return Ref.PackedType(
Buf(buf, -(2 + byte_width)), byte_width, packed_type=buf[-2])
Buf(buf, -(2 + byte_width)), byte_width, packed_type=buf[-2]
)
def Dumps(obj):

View File

@@ -16,7 +16,7 @@ import collections
import struct
from . import packer
from .compat import import_numpy, NumpyRequiredForThisFeature
from .compat import NumpyRequiredForThisFeature, import_numpy
np = import_numpy()
@@ -26,156 +26,157 @@ np = import_numpy()
# These classes could be collections.namedtuple instances, but those are new
# in 2.6 and we want to work towards 2.5 compatability.
class BoolFlags(object):
bytewidth = 1
min_val = False
max_val = True
py_type = bool
name = "bool"
packer_type = packer.boolean
bytewidth = 1
min_val = False
max_val = True
py_type = bool
name = "bool"
packer_type = packer.boolean
class Uint8Flags(object):
bytewidth = 1
min_val = 0
max_val = (2**8) - 1
py_type = int
name = "uint8"
packer_type = packer.uint8
bytewidth = 1
min_val = 0
max_val = (2**8) - 1
py_type = int
name = "uint8"
packer_type = packer.uint8
class Uint16Flags(object):
bytewidth = 2
min_val = 0
max_val = (2**16) - 1
py_type = int
name = "uint16"
packer_type = packer.uint16
bytewidth = 2
min_val = 0
max_val = (2**16) - 1
py_type = int
name = "uint16"
packer_type = packer.uint16
class Uint32Flags(object):
bytewidth = 4
min_val = 0
max_val = (2**32) - 1
py_type = int
name = "uint32"
packer_type = packer.uint32
bytewidth = 4
min_val = 0
max_val = (2**32) - 1
py_type = int
name = "uint32"
packer_type = packer.uint32
class Uint64Flags(object):
bytewidth = 8
min_val = 0
max_val = (2**64) - 1
py_type = int
name = "uint64"
packer_type = packer.uint64
bytewidth = 8
min_val = 0
max_val = (2**64) - 1
py_type = int
name = "uint64"
packer_type = packer.uint64
class Int8Flags(object):
bytewidth = 1
min_val = -(2**7)
max_val = (2**7) - 1
py_type = int
name = "int8"
packer_type = packer.int8
bytewidth = 1
min_val = -(2**7)
max_val = (2**7) - 1
py_type = int
name = "int8"
packer_type = packer.int8
class Int16Flags(object):
bytewidth = 2
min_val = -(2**15)
max_val = (2**15) - 1
py_type = int
name = "int16"
packer_type = packer.int16
bytewidth = 2
min_val = -(2**15)
max_val = (2**15) - 1
py_type = int
name = "int16"
packer_type = packer.int16
class Int32Flags(object):
bytewidth = 4
min_val = -(2**31)
max_val = (2**31) - 1
py_type = int
name = "int32"
packer_type = packer.int32
bytewidth = 4
min_val = -(2**31)
max_val = (2**31) - 1
py_type = int
name = "int32"
packer_type = packer.int32
class Int64Flags(object):
bytewidth = 8
min_val = -(2**63)
max_val = (2**63) - 1
py_type = int
name = "int64"
packer_type = packer.int64
bytewidth = 8
min_val = -(2**63)
max_val = (2**63) - 1
py_type = int
name = "int64"
packer_type = packer.int64
class Float32Flags(object):
bytewidth = 4
min_val = None
max_val = None
py_type = float
name = "float32"
packer_type = packer.float32
bytewidth = 4
min_val = None
max_val = None
py_type = float
name = "float32"
packer_type = packer.float32
class Float64Flags(object):
bytewidth = 8
min_val = None
max_val = None
py_type = float
name = "float64"
packer_type = packer.float64
bytewidth = 8
min_val = None
max_val = None
py_type = float
name = "float64"
packer_type = packer.float64
class SOffsetTFlags(Int32Flags):
pass
pass
class UOffsetTFlags(Uint32Flags):
pass
pass
class VOffsetTFlags(Uint16Flags):
pass
pass
def valid_number(n, flags):
if flags.min_val is None and flags.max_val is None:
return True
return flags.min_val <= n <= flags.max_val
if flags.min_val is None and flags.max_val is None:
return True
return flags.min_val <= n <= flags.max_val
def enforce_number(n, flags):
if flags.min_val is None and flags.max_val is None:
return
if not flags.min_val <= n <= flags.max_val:
raise TypeError("bad number %s for type %s" % (str(n), flags.name))
if flags.min_val is None and flags.max_val is None:
return
if not flags.min_val <= n <= flags.max_val:
raise TypeError("bad number %s for type %s" % (str(n), flags.name))
def float32_to_uint32(n):
packed = struct.pack("<1f", n)
(converted,) = struct.unpack("<1L", packed)
return converted
packed = struct.pack("<1f", n)
(converted,) = struct.unpack("<1L", packed)
return converted
def uint32_to_float32(n):
packed = struct.pack("<1L", n)
(unpacked,) = struct.unpack("<1f", packed)
return unpacked
packed = struct.pack("<1L", n)
(unpacked,) = struct.unpack("<1f", packed)
return unpacked
def float64_to_uint64(n):
packed = struct.pack("<1d", n)
(converted,) = struct.unpack("<1Q", packed)
return converted
packed = struct.pack("<1d", n)
(converted,) = struct.unpack("<1Q", packed)
return converted
def uint64_to_float64(n):
packed = struct.pack("<1Q", n)
(unpacked,) = struct.unpack("<1d", packed)
return unpacked
packed = struct.pack("<1Q", n)
(unpacked,) = struct.unpack("<1d", packed)
return unpacked
def to_numpy_type(number_type):
if np is not None:
return np.dtype(number_type.name).newbyteorder('<')
else:
raise NumpyRequiredForThisFeature('Numpy was not found.')
if np is not None:
return np.dtype(number_type.name).newbyteorder("<")
else:
raise NumpyRequiredForThisFeature("Numpy was not found.")

View File

@@ -12,8 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Provide pre-compiled struct packers for encoding and decoding.
"""Provide pre-compiled struct packers for encoding and decoding.
See: https://docs.python.org/2/library/struct.html#format-characters
"""

View File

@@ -17,122 +17,132 @@ from . import number_types as N
class Table(object):
"""Table wraps a byte slice and provides read access to its data.
"""Table wraps a byte slice and provides read access to its data.
The variable `Pos` indicates the root of the FlatBuffers object therein."""
The variable `Pos` indicates the root of the FlatBuffers object therein.
"""
__slots__ = ("Bytes", "Pos")
__slots__ = ("Bytes", "Pos")
def __init__(self, buf, pos):
N.enforce_number(pos, N.UOffsetTFlags)
def __init__(self, buf, pos):
N.enforce_number(pos, N.UOffsetTFlags)
self.Bytes = buf
self.Pos = pos
self.Bytes = buf
self.Pos = pos
def Offset(self, vtableOffset):
"""Offset provides access into the Table's vtable.
def Offset(self, vtableOffset):
"""Offset provides access into the Table's vtable.
Deprecated fields are ignored by checking the vtable's length."""
Deprecated fields are ignored by checking the vtable's length.
"""
vtable = self.Pos - self.Get(N.SOffsetTFlags, self.Pos)
vtableEnd = self.Get(N.VOffsetTFlags, vtable)
if vtableOffset < vtableEnd:
return self.Get(N.VOffsetTFlags, vtable + vtableOffset)
return 0
vtable = self.Pos - self.Get(N.SOffsetTFlags, self.Pos)
vtableEnd = self.Get(N.VOffsetTFlags, vtable)
if vtableOffset < vtableEnd:
return self.Get(N.VOffsetTFlags, vtable + vtableOffset)
return 0
def Indirect(self, off):
"""Indirect retrieves the relative offset stored at `offset`."""
N.enforce_number(off, N.UOffsetTFlags)
return off + encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
def Indirect(self, off):
"""Indirect retrieves the relative offset stored at `offset`."""
N.enforce_number(off, N.UOffsetTFlags)
return off + encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
def String(self, off):
"""String gets a string from data stored inside the flatbuffer."""
N.enforce_number(off, N.UOffsetTFlags)
off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
start = off + N.UOffsetTFlags.bytewidth
length = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
return bytes(self.Bytes[start:start+length])
def String(self, off):
"""String gets a string from data stored inside the flatbuffer."""
N.enforce_number(off, N.UOffsetTFlags)
off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
start = off + N.UOffsetTFlags.bytewidth
length = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
return bytes(self.Bytes[start : start + length])
def VectorLen(self, off):
"""VectorLen retrieves the length of the vector whose offset is stored
at "off" in this object."""
N.enforce_number(off, N.UOffsetTFlags)
def VectorLen(self, off):
"""VectorLen retrieves the length of the vector whose offset is stored
off += self.Pos
off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
ret = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
return ret
at "off" in this object.
"""
N.enforce_number(off, N.UOffsetTFlags)
def Vector(self, off):
"""Vector retrieves the start of data of the vector whose offset is
stored at "off" in this object."""
N.enforce_number(off, N.UOffsetTFlags)
off += self.Pos
off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
ret = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
return ret
off += self.Pos
x = off + self.Get(N.UOffsetTFlags, off)
# data starts after metadata containing the vector length
x += N.UOffsetTFlags.bytewidth
return x
def Vector(self, off):
"""Vector retrieves the start of data of the vector whose offset is
def Union(self, t2, off):
"""Union initializes any Table-derived type to point to the union at
the given offset."""
assert type(t2) is Table
N.enforce_number(off, N.UOffsetTFlags)
stored at "off" in this object.
"""
N.enforce_number(off, N.UOffsetTFlags)
off += self.Pos
t2.Pos = off + self.Get(N.UOffsetTFlags, off)
t2.Bytes = self.Bytes
off += self.Pos
x = off + self.Get(N.UOffsetTFlags, off)
# data starts after metadata containing the vector length
x += N.UOffsetTFlags.bytewidth
return x
def Get(self, flags, off):
"""
Get retrieves a value of the type specified by `flags` at the
given offset.
"""
N.enforce_number(off, N.UOffsetTFlags)
return flags.py_type(encode.Get(flags.packer_type, self.Bytes, off))
def Union(self, t2, off):
"""Union initializes any Table-derived type to point to the union at
def GetSlot(self, slot, d, validator_flags):
N.enforce_number(slot, N.VOffsetTFlags)
if validator_flags is not None:
N.enforce_number(d, validator_flags)
off = self.Offset(slot)
if off == 0:
return d
return self.Get(validator_flags, self.Pos + off)
the given offset.
"""
assert type(t2) is Table
N.enforce_number(off, N.UOffsetTFlags)
def GetVectorAsNumpy(self, flags, off):
"""
GetVectorAsNumpy returns the vector that starts at `Vector(off)`
as a numpy array with the type specified by `flags`. The array is
a `view` into Bytes, so modifying the returned array will
modify Bytes in place.
"""
offset = self.Vector(off)
length = self.VectorLen(off) # TODO: length accounts for bytewidth, right?
numpy_dtype = N.to_numpy_type(flags)
return encode.GetVectorAsNumpy(numpy_dtype, self.Bytes, length, offset)
off += self.Pos
t2.Pos = off + self.Get(N.UOffsetTFlags, off)
t2.Bytes = self.Bytes
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 Get(self, flags, off):
"""Get retrieves a value of the type specified by `flags` at the
def GetVOffsetTSlot(self, slot, d):
"""
GetVOffsetTSlot retrieves the VOffsetT that the given vtable location
points to. If the vtable value is zero, the default value `d`
will be returned.
"""
given offset.
"""
N.enforce_number(off, N.UOffsetTFlags)
return flags.py_type(encode.Get(flags.packer_type, self.Bytes, off))
N.enforce_number(slot, N.VOffsetTFlags)
N.enforce_number(d, N.VOffsetTFlags)
def GetSlot(self, slot, d, validator_flags):
N.enforce_number(slot, N.VOffsetTFlags)
if validator_flags is not None:
N.enforce_number(d, validator_flags)
off = self.Offset(slot)
if off == 0:
return d
return self.Get(validator_flags, self.Pos + off)
off = self.Offset(slot)
if off == 0:
return d
return off
def GetVectorAsNumpy(self, flags, off):
"""GetVectorAsNumpy returns the vector that starts at `Vector(off)`
as a numpy array with the type specified by `flags`. The array is
a `view` into Bytes, so modifying the returned array will
modify Bytes in place.
"""
offset = self.Vector(off)
length = self.VectorLen(off) # TODO: length accounts for bytewidth, right?
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
points to. If the vtable value is zero, the default value `d`
will be returned.
"""
N.enforce_number(slot, N.VOffsetTFlags)
N.enforce_number(d, N.VOffsetTFlags)
off = self.Offset(slot)
if off == 0:
return d
return off

View File

@@ -16,28 +16,32 @@ from . import encode
from . import number_types
from . import packer
def GetSizePrefix(buf, offset):
"""Extract the size prefix from a buffer."""
return encode.Get(packer.int32, buf, offset)
"""Extract the size prefix from a buffer."""
return encode.Get(packer.int32, buf, offset)
def GetBufferIdentifier(buf, offset, size_prefixed=False):
"""Extract the file_identifier from a buffer"""
if size_prefixed:
# increase offset by size of UOffsetTFlags
offset += number_types.UOffsetTFlags.bytewidth
# increase offset by size of root table pointer
offset += number_types.UOffsetTFlags.bytewidth
# end of FILE_IDENTIFIER
end = offset + encode.FILE_IDENTIFIER_LENGTH
return buf[offset:end]
"""Extract the file_identifier from a buffer"""
if size_prefixed:
# increase offset by size of UOffsetTFlags
offset += number_types.UOffsetTFlags.bytewidth
# increase offset by size of root table pointer
offset += number_types.UOffsetTFlags.bytewidth
# end of FILE_IDENTIFIER
end = offset + encode.FILE_IDENTIFIER_LENGTH
return buf[offset:end]
def BufferHasIdentifier(buf, offset, file_identifier, size_prefixed=False):
got = GetBufferIdentifier(buf, offset, size_prefixed=size_prefixed)
return got == file_identifier
got = GetBufferIdentifier(buf, offset, size_prefixed=size_prefixed)
return got == file_identifier
def RemoveSizePrefix(buf, offset):
"""
Create a slice of a size-prefixed buffer that has
its position advanced just past the size prefix.
"""
return buf, offset + number_types.Int32Flags.bytewidth
"""Create a slice of a size-prefixed buffer that has
its position advanced just past the size prefix.
"""
return buf, offset + number_types.Int32Flags.bytewidth

View File

@@ -21,9 +21,11 @@ setup(
author='Derek Bailey',
author_email='derekbailey@google.com',
url='https://google.github.io/flatbuffers/',
long_description=('Python runtime library for use with the '
'`Flatbuffers <https://google.github.io/flatbuffers/>`_ '
'serialization format.'),
long_description=(
'Python runtime library for use with the '
'`Flatbuffers <https://google.github.io/flatbuffers/>`_ '
'serialization format.'
),
packages=['flatbuffers'],
include_package_data=True,
requires=[],