MCPcopy Create free account
hub / github.com/numpy/numpy / _get_fields_and_offsets

Function _get_fields_and_offsets

numpy/lib/recfunctions.py:851–886  ·  view source on GitHub ↗

Returns a flat list of (dtype, count, offset) tuples of all the scalar fields in the dtype "dt", including nested fields, in left to right order.

(dt, offset=0)

Source from the content-addressed store, hash-verified

849 return np.dtype((a.type, dt))
850
851def _get_fields_and_offsets(dt, offset=0):
852 """
853 Returns a flat list of (dtype, count, offset) tuples of all the
854 scalar fields in the dtype "dt", including nested fields, in left
855 to right order.
856 """
857
858 # counts up elements in subarrays, including nested subarrays, and returns
859 # base dtype and count
860 def count_elem(dt):
861 count = 1
862 while dt.shape != ():
863 for size in dt.shape:
864 count *= size
865 dt = dt.base
866 return dt, count
867
868 fields = []
869 for name in dt.names:
870 field = dt.fields[name]
871 f_dt, f_offset = field[0], field[1]
872 f_dt, n = count_elem(f_dt)
873
874 if f_dt.names is None:
875 fields.append((np.dtype((f_dt, (n,))), n, f_offset + offset))
876 else:
877 subfields = _get_fields_and_offsets(f_dt, f_offset + offset)
878 size = f_dt.itemsize
879
880 for i in range(n):
881 if i == 0:
882 # optimization: avoid list comprehension if no subarray
883 fields.extend(subfields)
884 else:
885 fields.extend([(d, c, o + i*size) for d, c, o in subfields])
886 return fields
887
888def _common_stride(offsets, counts, itemsize):
889 """

Callers 2

Calls 2

count_elemFunction · 0.85
dtypeMethod · 0.45

Tested by

no test coverage detected