Skip to content

Commit 2c6ac77

Browse files
committed
Simplify buffer_size by using ensure_ndarray
Rewrite `buffer_size` to just use Numcodecs' `ensure_ndarray` to get an `ndarray` that views the data. Once the `ndarray` is gotten, all that is needed is to access its `nbytes` member, which returns the number of bytes that it takes up.
1 parent 7bd8a2a commit 2c6ac77

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

zarr/util.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, print_function, division
3-
import operator
43
from textwrap import TextWrapper, dedent
54
import numbers
65
import uuid
@@ -10,10 +9,11 @@
109
from asciitree import BoxStyle, LeftAligned
1110
from asciitree.traversal import Traversal
1211
import numpy as np
12+
from numcodecs.compat import ensure_ndarray
1313
from numcodecs.registry import codec_registry
1414

1515

16-
from zarr.compat import PY2, reduce, text_type, binary_type
16+
from zarr.compat import PY2, text_type, binary_type
1717

1818

1919
# codecs to use for object dtype convenience API
@@ -314,17 +314,7 @@ def normalize_storage_path(path):
314314

315315

316316
def buffer_size(v):
317-
from array import array as _stdlib_array
318-
if PY2 and isinstance(v, _stdlib_array): # pragma: py3 no cover
319-
# special case array.array because does not support buffer
320-
# interface in PY2
321-
return v.buffer_info()[1] * v.itemsize
322-
else: # pragma: py2 no cover
323-
v = memoryview(v)
324-
if v.shape:
325-
return reduce(operator.mul, v.shape) * v.itemsize
326-
else:
327-
return v.itemsize
317+
return ensure_ndarray(v).nbytes
328318

329319

330320
def info_text_report(items):

0 commit comments

Comments
 (0)