mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 04:21:13 +00:00
[Python] (scalar) vector reading speedup via numpy (#4390)
* Add numpy accessor to python flatbuffers scalar vectors * Update python tests to test numpy vector accessor * Update appveyor CI to run Python tests, save generated code as artifact * Update example generated python code * Add numpy info to python usage docs * Update test schema and python tests w/ multi-byte vector * did not mean to push profiling code * adding float64 numpy tests
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
89a68942ac
commit
3282a84e30
@@ -12,9 +12,11 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
""" A tiny version of `six` to help with backwards compability. """
|
||||
""" A tiny version of `six` to help with backwards compability. Also includes
|
||||
compatibility helpers for numpy. """
|
||||
|
||||
import sys
|
||||
import imp
|
||||
|
||||
PY2 = sys.version_info[0] == 2
|
||||
PY26 = sys.version_info[0:2] == (2, 6)
|
||||
@@ -43,4 +45,37 @@ 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.
|
||||
"""
|
||||
try:
|
||||
imp.find_module('numpy')
|
||||
numpy_exists = True
|
||||
except ImportError:
|
||||
numpy_exists = False
|
||||
|
||||
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
|
||||
|
||||
|
||||
# NOTE: Future Jython support may require code here (look at `six`).
|
||||
|
||||
Reference in New Issue
Block a user