a
    ³ÐOc©’  ã                   @   sD  d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z
 ddlmZ dd	lmZ dd
lmZ g d¢ZejZdddddddddddddddœZe
jZdd„ ZedƒG dd„ dƒƒZG dd„ de
jƒZG dd„ deƒZdd„ Zedƒd,dd „ƒZedƒd-d!d"„ƒZedƒd.d#d$„ƒZd%d&„ Zedƒd/d'd(„ƒZ edƒd0d*d+„ƒZ!dS )1a‚  
Record Arrays
=============
Record arrays expose the fields of structured arrays as properties.

Most commonly, ndarrays contain elements of a single type, e.g. floats,
integers, bools etc.  However, it is possible for elements to be combinations
of these using structured types, such as::

  >>> a = np.array([(1, 2.0), (1, 2.0)], dtype=[('x', np.int64), ('y', np.float64)])
  >>> a
  array([(1, 2.), (1, 2.)], dtype=[('x', '<i8'), ('y', '<f8')])

Here, each element consists of two fields: x (and int), and y (a float).
This is known as a structured array.  The different fields are analogous
to columns in a spread-sheet.  The different fields can be accessed as
one would a dictionary::

  >>> a['x']
  array([1, 1])

  >>> a['y']
  array([2., 2.])

Record arrays allow us to access fields as properties::

  >>> ar = np.rec.array(a)

  >>> ar.x
  array([1, 1])

  >>> ar.y
  array([2., 2.])

é    N)ÚCounter)Únullcontexté   )Únumeric)Únumerictypes)Ú	os_fspath)Ú
set_module)Ú_get_legacy_print_mode)ÚrecordÚrecarrayÚformat_parserÚ
fromarraysÚfromrecordsÚ
fromstringÚfromfileÚarrayú>ú<ú=Úsú|)ÚbÚlÚnÚBÚLÚNÚSr   r   r   r   r   ÚIÚic                 C   s   dd„ t | ƒ ¡ D ƒS )z@Find duplication in a list, return a list of duplicated elementsc                 S   s   g | ]\}}|d kr|‘qS ©r   © )Ú.0ÚitemÚcountsr!   r!   úI/var/www/html/gsl.tools/lib/python3.9/site-packages/numpy/core/records.pyÚ
<listcomp>P   s   þz"find_duplicate.<locals>.<listcomp>)r   Úitems)Úlistr!   r!   r%   Úfind_duplicateN   s    
þr)   Únumpyc                   @   s4   e Zd ZdZddd„Zddd„Zdd	„ Zd
d„ ZdS )r   a‡  
    Class to convert formats, names, titles description to a dtype.

    After constructing the format_parser object, the dtype attribute is
    the converted data-type:
    ``dtype = format_parser(formats, names, titles).dtype``

    Attributes
    ----------
    dtype : dtype
        The converted data-type.

    Parameters
    ----------
    formats : str or list of str
        The format description, either specified as a string with
        comma-separated format descriptions in the form ``'f8, i4, a5'``, or
        a list of format description strings  in the form
        ``['f8', 'i4', 'a5']``.
    names : str or list/tuple of str
        The field names, either specified as a comma-separated string in the
        form ``'col1, col2, col3'``, or as a list or tuple of strings in the
        form ``['col1', 'col2', 'col3']``.
        An empty list can be used, in that case default field names
        ('f0', 'f1', ...) are used.
    titles : sequence
        Sequence of title strings. An empty list can be used to leave titles
        out.
    aligned : bool, optional
        If True, align the fields by padding as the C-compiler would.
        Default is False.
    byteorder : str, optional
        If specified, all the fields will be changed to the
        provided byte-order.  Otherwise, the default byte-order is
        used. For all available string specifiers, see `dtype.newbyteorder`.

    See Also
    --------
    dtype, typename, sctype2char

    Examples
    --------
    >>> np.format_parser(['<f8', '<i4', '<a5'], ['col1', 'col2', 'col3'],
    ...                  ['T1', 'T2', 'T3']).dtype
    dtype([(('T1', 'col1'), '<f8'), (('T2', 'col2'), '<i4'), (('T3', 'col3'), 'S5')])

    `names` and/or `titles` can be empty lists. If `titles` is an empty list,
    titles will simply not appear. If `names` is empty, default field names
    will be used.

    >>> np.format_parser(['f8', 'i4', 'a5'], ['col1', 'col2', 'col3'],
    ...                  []).dtype
    dtype([('col1', '<f8'), ('col2', '<i4'), ('col3', '<S5')])
    >>> np.format_parser(['<f8', '<i4', '<a5'], [], []).dtype
    dtype([('f0', '<f8'), ('f1', '<i4'), ('f2', 'S5')])

    FNc                 C   s&   |   ||¡ |  ||¡ |  |¡ d S ©N)Ú_parseFormatsÚ_setfieldnamesÚ_createdtype)ÚselfÚformatsÚnamesÚtitlesÚalignedÚ	byteorderr!   r!   r%   Ú__init__“   s    zformat_parser.__init__c                    s¤   |du rt dƒ‚t|tƒr6t dd„ t|ƒD ƒ|¡}nt ||¡}|j‰ ˆ du rht d|fg|¡}|j‰ |j}‡ fdd„|D ƒ| _‡ fdd„|D ƒ| _	t
|ƒ| _dS )z Parse the field formats NzNeed formats argumentc                 S   s   g | ]\}}d   |¡|f‘qS )zf{})Úformat)r"   r   Zformat_r!   r!   r%   r&   Ÿ   ó    z/format_parser._parseFormats.<locals>.<listcomp>Úf1c                    s   g | ]}ˆ | d  ‘qS ©r   r!   ©r"   Úkey©Úfieldsr!   r%   r&   ©   r7   c                    s   g | ]}ˆ | d  ‘qS r    r!   r:   r<   r!   r%   r&   ª   r7   )Ú
ValueErrorÚ
isinstancer(   ÚsbÚdtypeÚ	enumerater=   r1   Ú
_f_formatsÚ_offsetsÚlenÚ_nfields)r/   r0   r3   rA   Úkeysr!   r<   r%   r,   ˜   s     
þzformat_parser._parseFormatsc                 C   sø   |rXt |ƒttfv rn&t|tƒr,| d¡}ntdt|ƒ ƒ‚dd„ |d| j… D ƒ| _	ng | _	|  j	dd„ t
t| j	ƒ| jƒD ƒ7  _	t| j	ƒ}|ržtd| ƒ‚|r¾dd„ |d| j… D ƒ| _n
g | _g }| jt|ƒkrô|  jdg| jt|ƒ  7  _dS )	zQconvert input field names into a list and assign to the _names
        attribute ú,zillegal input names %sc                 S   s   g | ]}|  ¡ ‘qS r!   ©Ústrip©r"   r   r!   r!   r%   r&   ¹   r7   z0format_parser._setfieldnames.<locals>.<listcomp>Nc                 S   s   g | ]}d | ‘qS )zf%dr!   ©r"   r   r!   r!   r%   r&   Á   r7   zDuplicate field names: %sc                 S   s   g | ]}|  ¡ ‘qS r!   rI   rK   r!   r!   r%   r&   É   r7   )Útyper(   Útupler?   ÚstrÚsplitÚ	NameErrorÚreprrF   Ú_namesÚrangerE   r)   r>   Ú_titles)r/   r1   r2   Z_dupr!   r!   r%   r-   ­   s(    
ÿ
zformat_parser._setfieldnamesc                 C   sD   t  | j| j| j| jdœ¡}|d ur:t|d  }| |¡}|| _d S )N)r1   r0   Úoffsetsr2   r   )r@   rA   rS   rC   rD   rU   Ú_byteorderconvZnewbyteorder)r/   r4   rA   r!   r!   r%   r.   Ñ   s    ü
zformat_parser._createdtype)FN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r5   r,   r-   r.   r!   r!   r!   r%   r   W   s
   :

$r   c                       sT   e Zd ZdZd Z dZ‡ fdd„Z‡ fdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )r
   zEA data-type scalar that allows field access as attribute lookup.
    r*   c                    s   t ƒ dkr|  ¡ S tƒ  ¡ S ©Néq   )r	   Ú__str__ÚsuperÚ__repr__©r/   ©Ú	__class__r!   r%   r`   è   s    
zrecord.__repr__c                    s    t ƒ dkrt|  ¡ ƒS tƒ  ¡ S r\   )r	   rO   r#   r_   r^   ra   rb   r!   r%   r^   í   s    
zrecord.__str__c                 C   s¾   |dv rt j | |¡S zt j | |¡W S  ty8   Y n0 t j | d¡j}| |d ¡}|r®| j|d d… Ž }z
|j}W n tyŒ   | Y S 0 |jd urª| 	| j
|jf¡S |S td| ƒ‚d S )N©ÚsetfieldÚgetfieldrA   rA   é   ú%'record' object has no attribute '%s')ÚntÚvoidÚ__getattribute__ÚAttributeErrorr=   Úgetrf   rA   r1   Úviewrc   )r/   ÚattrÚ	fielddictÚresÚobjÚdtr!   r!   r%   rk   ò   s(    


ÿzrecord.__getattribute__c                 C   sz   |dv rt d| ƒ‚tj | d¡j}| |d ¡}|rN| j|g|d d… ¢R Ž S t| |d ƒrjtj | ||¡S t d| ƒ‚d S )Nrd   zCannot set '%s' attributerA   rg   rh   )	rl   ri   rj   rk   r=   rm   re   ÚgetattrÚ__setattr__)r/   ro   Úvalrp   rq   r!   r!   r%   ru     s    ÿzrecord.__setattr__c                 C   s@   t j | |¡}t|t jƒr8|jjd ur8| | j|jf¡S |S d S r+   )ri   rj   Ú__getitem__r?   rA   r1   rn   rc   ©r/   Zindxrr   r!   r!   r%   rw     s    zrecord.__getitem__c                    s@   ˆj j}tdd„ |D ƒƒ}d| ‰ ‡ ‡fdd„|D ƒ}d |¡S )zPretty-print all fields.c                 s   s   | ]}t |ƒV  qd S r+   )rE   ©r"   Únamer!   r!   r%   Ú	<genexpr>'  r7   z record.pprint.<locals>.<genexpr>z%% %ds: %%sc                    s   g | ]}ˆ |t ˆ|ƒf ‘qS r!   )rt   ry   ©Úfmtr/   r!   r%   r&   )  r7   z!record.pprint.<locals>.<listcomp>Ú
)rA   r1   ÚmaxÚjoin)r/   r1   ÚmaxlenÚrowsr!   r|   r%   Úpprint#  s
    zrecord.pprint)rX   rY   rZ   r[   r`   r^   rk   ru   rw   rƒ   Ú__classcell__r!   r!   rb   r%   r
   ß   s   
r
   c                
       s\   e Zd ZdZd Z dZddd„Zd	d
„ Zdd„ Zdd„ Z‡ fdd„Zdd„ Z	ddd„Z
‡  ZS )r   a-  Construct an ndarray that allows field access using attributes.

    Arrays may have a data-types containing fields, analogous
    to columns in a spread sheet.  An example is ``[(x, int), (y, float)]``,
    where each entry in the array is a pair of ``(int, float)``.  Normally,
    these attributes are accessed using dictionary lookups such as ``arr['x']``
    and ``arr['y']``.  Record arrays allow the fields to be accessed as members
    of the array, using ``arr.x`` and ``arr.y``.

    Parameters
    ----------
    shape : tuple
        Shape of output array.
    dtype : data-type, optional
        The desired data-type.  By default, the data-type is determined
        from `formats`, `names`, `titles`, `aligned` and `byteorder`.
    formats : list of data-types, optional
        A list containing the data-types for the different columns, e.g.
        ``['i4', 'f8', 'i4']``.  `formats` does *not* support the new
        convention of using types directly, i.e. ``(int, float, int)``.
        Note that `formats` must be a list, not a tuple.
        Given that `formats` is somewhat limited, we recommend specifying
        `dtype` instead.
    names : tuple of str, optional
        The name of each column, e.g. ``('x', 'y', 'z')``.
    buf : buffer, optional
        By default, a new array is created of the given shape and data-type.
        If `buf` is specified and is an object exposing the buffer interface,
        the array will use the memory from the existing buffer.  In this case,
        the `offset` and `strides` keywords are available.

    Other Parameters
    ----------------
    titles : tuple of str, optional
        Aliases for column names.  For example, if `names` were
        ``('x', 'y', 'z')`` and `titles` is
        ``('x_coordinate', 'y_coordinate', 'z_coordinate')``, then
        ``arr['x']`` is equivalent to both ``arr.x`` and ``arr.x_coordinate``.
    byteorder : {'<', '>', '='}, optional
        Byte-order for all fields.
    aligned : bool, optional
        Align the fields in memory as the C-compiler would.
    strides : tuple of ints, optional
        Buffer (`buf`) is interpreted according to these strides (strides
        define how many bytes each array element, row, column, etc.
        occupy in memory).
    offset : int, optional
        Start reading buffer (`buf`) from this offset onwards.
    order : {'C', 'F'}, optional
        Row-major (C-style) or column-major (Fortran-style) order.

    Returns
    -------
    rec : recarray
        Empty array of the given shape and type.

    See Also
    --------
    core.records.fromrecords : Construct a record array from data.
    record : fundamental data-type for `recarray`.
    format_parser : determine a data-type from formats, names, titles.

    Notes
    -----
    This constructor can be compared to ``empty``: it creates a new record
    array but does not fill it with data.  To create a record array from data,
    use one of the following methods:

    1. Create a standard ndarray and convert it to a record array,
       using ``arr.view(np.recarray)``
    2. Use the `buf` keyword.
    3. Use `np.rec.fromrecords`.

    Examples
    --------
    Create an array with two fields, ``x`` and ``y``:

    >>> x = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', '<f8'), ('y', '<i8')])
    >>> x
    array([(1., 2), (3., 4)], dtype=[('x', '<f8'), ('y', '<i8')])

    >>> x['x']
    array([1., 3.])

    View the array as a record array:

    >>> x = x.view(np.recarray)

    >>> x.x
    array([1., 3.])

    >>> x.y
    array([2, 4])

    Create a new, empty record array:

    >>> np.recarray((2,),
    ... dtype=[('x', int), ('y', float), ('z', int)]) #doctest: +SKIP
    rec.array([(-1073741821, 1.2249118382103472e-301, 24547520),
           (3471280, 1.2134086255804012e-316, 0)],
          dtype=[('x', '<i4'), ('y', '<f8'), ('z', '<i4')])

    r*   Nr   FÚCc              	   C   sf   |d urt  |¡}nt||||
|	ƒj}|d u rFtj| |t|f|d�}ntj| |t|f||||d�}|S )N)Úorder)ÚbufferÚoffsetÚstridesr†   )r@   rA   r   ÚndarrayÚ__new__r
   )ÚsubtypeÚshaperA   Úbufrˆ   r‰   r0   r1   r2   r4   r3   r†   Údescrr/   r!   r!   r%   r‹   ¢  s    þzrecarray.__new__c                 C   s$   | j jtur | j jd ur | j | _ d S r+   )rA   rM   r
   r1   )r/   rr   r!   r!   r%   Ú__array_finalize__³  s    zrecarray.__array_finalize__c              
   C   sÊ   zt  | |¡W S  ty    Y n0 t | d¡j}z|| d d… }W n6 ttfyz } ztd| ƒ|‚W Y d }~n
d }~0 0 | j|Ž }|jj	d ur¼t
|jjtjƒr¸|j| jj|jfd�S |S | t¡S d S )NrA   rg   zrecarray has no attribute %s©rA   )Úobjectrk   rl   rŠ   r=   Ú	TypeErrorÚKeyErrorrf   rA   r1   Ú
issubclassrM   ri   rj   rn   )r/   ro   rp   rq   Úerr   r!   r!   r%   rk   ¹  s    $
zrecarray.__getattribute__c              
   C   s$  |dkr.t |jtjƒr.|jd ur.t t|f¡}|| jv}zt	 
| ||¡}W n. tyx   t | d¡jphi }||vrt‚ Y nL0 t | d¡jpŠi }||vr˜|S |rÄzt	 | |¡ W n tyÂ   | Y S 0 z|| d d… }W n8 ttf�y } ztd| ƒ|‚W Y d }~n
d }~0 0 | j|g|¢R Ž S )NrA   rg   z record array has no attribute %s)r•   rM   ri   rj   r1   r@   rA   r
   Ú__dict__r’   ru   Ú	ExceptionrŠ   rk   r=   Ú__delattr__r“   r”   rl   re   )r/   ro   rv   ZnewattrÚretrp   rq   r–   r!   r!   r%   ru   Û  s4     

ÿþzrecarray.__setattr__c                    sp   t ƒ  |¡}t|tƒrh|jjd urZ| t| ƒ¡}t|jjt	j
ƒrV|j| jj|jfd�S |S |jtd�S n|S d S )Nr‘   )rM   )r_   rw   r?   rŠ   rA   r1   rn   rM   r•   ri   rj   rx   rb   r!   r%   rw   ü  s    
zrecarray.__getitem__c                 C   s¶   | j }| j jtu s"t| j jtjƒsF|jtu r<t  tj|f¡}d}d}nd}d}| jdksb| jdkrvtj	| d|dd	�}nd
t
| jƒf }ddt|ƒ  }tƒ dkr¨d| }||||f S )Nz
rec.array(zrec.array(%s,%sdtype=%s)zarray(z)array(%s,%sdtype=%s).view(numpy.recarray)r   r9   z, rH   )Ú	separatorÚprefixÚsuffixz[], shape=%sr~   ú r]   )rA   rM   r
   r•   ri   rj   r@   Úsizer�   Zarray2stringrR   rE   r	   )r/   Z
repr_dtyperœ   r}   ÚlstÚlfr!   r!   r%   r`     s"    
ÿ
zrecarray.__repr__c                 C   s€   t |tƒr t | d¡j}|| }t | d¡j}|| d d… }|d u rj| j|Ž }|jjd ur`|S | t¡S | j	|g|¢R Ž S d S )NrA   rg   )
r?   ÚintrŠ   rk   r1   r=   rf   rA   rn   re   )r/   ro   rv   r1   rp   rq   rr   r!   r!   r%   Úfield.  s    


zrecarray.field)
NNr   NNNNNFr…   )N)rX   rY   rZ   r[   r‹   r�   rk   ru   rw   r`   r£   r„   r!   r!   rb   r%   r   4  s   j   þ
"!!r   c                 C   s$   | dkrt jdtdd� d S | S d S )Nr   z¹Passing `shape=0` to have the shape be inferred is deprecated, and in future will be equivalent to `shape=(0,)`. To infer the shape and suppress this warning, pass `shape=None` instead.é   ©Ú
stacklevel)ÚwarningsÚwarnÚFutureWarning)r�   r!   r!   r%   Ú_deprecate_shape_0_as_None@  s    ürª   z	numpy.recFc                 C   s2  dd„ | D ƒ} t |ƒ}|du r*| d j}nt|tƒr:|f}|du rX|du rXdd„ | D ƒ}|durlt |¡}nt|||||ƒj}|j}	t|ƒt| ƒkrœt	dƒ‚|d j}
t|
ƒ}|dkrÄ|d| … }t
||ƒ}t| ƒD ]V\}}|| j}|jd|j| … }|	| }||k�r$t	d|› d|› d	�ƒ‚|||< qÖ|S )
a¶  Create a record array from a (flat) list of arrays

    Parameters
    ----------
    arrayList : list or tuple
        List of array-like objects (such as lists, tuples,
        and ndarrays).
    dtype : data-type, optional
        valid dtype for all arrays
    shape : int or tuple of ints, optional
        Shape of the resulting array. If not provided, inferred from
        ``arrayList[0]``.
    formats, names, titles, aligned, byteorder :
        If `dtype` is ``None``, these arguments are passed to
        `numpy.format_parser` to construct a dtype. See that function for
        detailed documentation.

    Returns
    -------
    np.recarray
        Record array consisting of given arrayList columns.

    Examples
    --------
    >>> x1=np.array([1,2,3,4])
    >>> x2=np.array(['a','dd','xyz','12'])
    >>> x3=np.array([1.1,2,3,4])
    >>> r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c')
    >>> print(r[1])
    (2, 'dd', 2.0) # may vary
    >>> x1[1]=34
    >>> r.a
    array([1, 2, 3, 4])

    >>> x1 = np.array([1, 2, 3, 4])
    >>> x2 = np.array(['a', 'dd', 'xyz', '12'])
    >>> x3 = np.array([1.1, 2, 3,4])
    >>> r = np.core.records.fromarrays(
    ...     [x1, x2, x3],
    ...     dtype=np.dtype([('a', np.int32), ('b', 'S3'), ('c', np.float32)]))
    >>> r
    rec.array([(1, b'a', 1.1), (2, b'dd', 2. ), (3, b'xyz', 3. ),
               (4, b'12', 4. )],
              dtype=[('a', '<i4'), ('b', 'S3'), ('c', '<f4')])
    c                 S   s   g | ]}t  |¡‘qS r!   )r@   Zasarray)r"   Úxr!   r!   r%   r&   }  r7   zfromarrays.<locals>.<listcomp>Nr   c                 S   s   g | ]
}|j ‘qS r!   r‘   )r"   rr   r!   r!   r%   r&   Š  r7   z>mismatch between the number of fields and the number of arrayszarray-shape mismatch in array z ("z"))rª   r�   r?   r¢   r@   rA   r   r1   rE   r>   r   rB   Úndim)Z	arrayListrA   r�   r0   r1   r2   r3   r4   r�   rS   Zd0ÚnnÚ_arrayÚkrr   Z	testshaperz   r!   r!   r%   r   L  s6    1





r   c              	      sJ  |du rP|du rPt j| td�‰ ‡ fdd„tˆ jd ƒD ƒ}t|||||||d�S |durht  t|f¡}	nt|||||ƒj}	zt j| |	d�}
W n’ t	t
f�y   t|ƒ}|du r¸t| ƒ}t|tƒrÈ|f}t|ƒdkrÜt
dƒ‚t||	ƒ}t|jƒD ]}t| | ƒ||< qðtjd	td
d� | Y S 0 |du�r<|
j|k�r<||
_|
 t¡}|S )a  Create a recarray from a list of records in text form.

    Parameters
    ----------
    recList : sequence
        data in the same field may be heterogeneous - they will be promoted
        to the highest data type.
    dtype : data-type, optional
        valid dtype for all arrays
    shape : int or tuple of ints, optional
        shape of each array.
    formats, names, titles, aligned, byteorder :
        If `dtype` is ``None``, these arguments are passed to
        `numpy.format_parser` to construct a dtype. See that function for
        detailed documentation.

        If both `formats` and `dtype` are None, then this will auto-detect
        formats. Use list of tuples rather than list of lists for faster
        processing.

    Returns
    -------
    np.recarray
        record array consisting of given recList rows.

    Examples
    --------
    >>> r=np.core.records.fromrecords([(456,'dbe',1.2),(2,'de',1.3)],
    ... names='col1,col2,col3')
    >>> print(r[0])
    (456, 'dbe', 1.2)
    >>> r.col1
    array([456,   2])
    >>> r.col2
    array(['dbe', 'de'], dtype='<U3')
    >>> import pickle
    >>> pickle.loads(pickle.dumps(r))
    rec.array([(456, 'dbe', 1.2), (  2, 'de', 1.3)],
              dtype=[('col1', '<i8'), ('col2', '<U3'), ('col3', '<f8')])
    Nr‘   c                    s"   g | ]}t  ˆ d |f  ¡ ¡‘qS ).)r@   r   ÚtolistrL   ©rr   r!   r%   r&   Ù  r7   zfromrecords.<locals>.<listcomp>éÿÿÿÿ)r0   r�   r1   r2   r3   r4   r   zCan only deal with 1-d array.zxfromrecords expected a list of tuples, may have received a list of lists instead. In the future that will raise an errorrg   r¥   )r@   r   r’   rT   r�   r   rA   r
   r   r“   r>   rª   rE   r?   r¢   r   rŸ   rN   r§   r¨   r©   rn   )ZrecListrA   r�   r0   r1   r2   r3   r4   Zarrlistr�   Úretvalr®   r¯   rq   r!   r±   r%   r   «  s>    ,
ÿ

ý

r   c	                 C   sx   |du r|du rt dƒ‚|dur,t |¡}	nt|||||ƒj}	|	j}
t|ƒ}|dv rdt| ƒ| |
 }t||	| |d�}|S )aö  Create a record array from binary data

    Note that despite the name of this function it does not accept `str`
    instances.

    Parameters
    ----------
    datastring : bytes-like
        Buffer of binary data
    dtype : data-type, optional
        Valid dtype for all arrays
    shape : int or tuple of ints, optional
        Shape of each array.
    offset : int, optional
        Position in the buffer to start reading from.
    formats, names, titles, aligned, byteorder :
        If `dtype` is ``None``, these arguments are passed to
        `numpy.format_parser` to construct a dtype. See that function for
        detailed documentation.


    Returns
    -------
    np.recarray
        Record array view into the data in datastring. This will be readonly
        if `datastring` is readonly.

    See Also
    --------
    numpy.frombuffer

    Examples
    --------
    >>> a = b'\x01\x02\x03abc'
    >>> np.core.records.fromstring(a, dtype='u1,u1,u1,S3')
    rec.array([(1, 2, 3, b'abc')],
            dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'S3')])

    >>> grades_dtype = [('Name', (np.str_, 10)), ('Marks', np.float64),
    ...                 ('GradeLevel', np.int32)]
    >>> grades_array = np.array([('Sam', 33.3, 3), ('Mike', 44.4, 5),
    ...                         ('Aadi', 66.6, 6)], dtype=grades_dtype)
    >>> np.core.records.fromstring(grades_array.tobytes(), dtype=grades_dtype)
    rec.array([('Sam', 33.3, 3), ('Mike', 44.4, 5), ('Aadi', 66.6, 6)],
            dtype=[('Name', '<U10'), ('Marks', '<f8'), ('GradeLevel', '<i4')])

    >>> s = '\x01\x02\x03abc'
    >>> np.core.records.fromstring(s, dtype='u1,u1,u1,S3')
    Traceback (most recent call last)
       ...
    TypeError: a bytes-like object is required, not 'str'
    Nz2fromstring() needs a 'dtype' or 'formats' argument)Nr²   )rŽ   rˆ   )r“   r@   rA   r   Úitemsizerª   rE   r   )Z
datastringrA   r�   rˆ   r0   r1   r2   r3   r4   r�   r´   r®   r!   r!   r%   r      s    8r   c              
   C   sB   |   ¡ }z&|  dd¡ |   ¡ | W |  |d¡ S |  |d¡ 0 d S )Nr   rg   )ÚtellÚseek)ÚfdÚposr!   r!   r%   Úget_remaining_sizeK  s    þr¹   c	                 C   sr  |du r|du rt dƒ‚t|ƒ}|du r.d}nt|tƒr>|f}t| dƒrRt| ƒ}	ntt| ƒdƒ}	|	�ø} |dkrz|  |d¡ t	| ƒ}
|dur–t
 |¡}nt|||||ƒj}|j}t
 |¡jtjd�}|| }|dk �rt|ƒ}|
|  || d	¡< t|ƒ}t
 |¡jtjd�}|| }||
k�r&td
ƒ‚t||ƒ}|  |j¡}||k�rNtdƒ‚W d  ƒ n1 �sd0    Y  |S )aú  Create an array from binary file data

    Parameters
    ----------
    fd : str or file type
        If file is a string or a path-like object then that file is opened,
        else it is assumed to be a file object. The file object must
        support random access (i.e. it must have tell and seek methods).
    dtype : data-type, optional
        valid dtype for all arrays
    shape : int or tuple of ints, optional
        shape of each array.
    offset : int, optional
        Position in the file to start reading from.
    formats, names, titles, aligned, byteorder :
        If `dtype` is ``None``, these arguments are passed to
        `numpy.format_parser` to construct a dtype. See that function for
        detailed documentation

    Returns
    -------
    np.recarray
        record array consisting of data enclosed in file.

    Examples
    --------
    >>> from tempfile import TemporaryFile
    >>> a = np.empty(10,dtype='f8,i4,a5')
    >>> a[5] = (0.5,10,'abcde')
    >>>
    >>> fd=TemporaryFile()
    >>> a = a.newbyteorder('<')
    >>> a.tofile(fd)
    >>>
    >>> _ = fd.seek(0)
    >>> r=np.core.records.fromfile(fd, formats='f8,i4,a5', shape=10,
    ... byteorder='<')
    >>> print(r[5])
    (0.5, 10, 'abcde')
    >>> r.shape
    (10,)
    Nz0fromfile() needs a 'dtype' or 'formats' argument)r²   ÚreadintoÚrbr   r   r‘   r²   z:Not enough bytes left in file for specified shape and typez%Didn't read as many bytes as expected)r“   rª   r?   r¢   Úhasattrr   Úopenr   r¶   r¹   r@   rA   r   r´   r   Úprodri   Zintpr(   ÚindexrN   r>   r   rº   ÚdataÚOSError)r·   rA   r�   rˆ   r0   r1   r2   r3   r4   ÚctxrŸ   r�   r´   Z	shapeprodZ	shapesizeÚnbytesr®   Z
nbytesreadr!   r!   r%   r   T  sF    .




ÿ

(r   Tc                 C   s$  t | tdƒtfƒst| dƒr4|du r4|du r4tdƒ‚i }|durLt |¡}n,|durht|||||	ƒj}n|||||	dœ}| du r¢|du r�tdƒ‚t||| ||d�S t | t	ƒrÄt
| |f||dœ|¤ŽS t | ttfƒ�rt | d ttfƒ� rþt| f||d	œ|¤ŽS t| f||d	œ|¤ŽS �nt | tƒ�r\|du�rF| j|k�rF|  |¡}n| }|
�rX| ¡ }|S t| dƒ�rxt| |||d
�S t | tƒ�rÂ|du�r¦| j|k�r¦|  |¡}n| }|
�r¸| ¡ }| t¡S t| ddƒ}|du �sät |tƒ�sìtdƒ‚t | ¡} |du�r| j|k�r|  |¡} |  t¡S dS )aŸ
  
    Construct a record array from a wide-variety of objects.

    A general-purpose record array constructor that dispatches to the
    appropriate `recarray` creation function based on the inputs (see Notes).

    Parameters
    ----------
    obj : any
        Input object. See Notes for details on how various input types are
        treated.
    dtype : data-type, optional
        Valid dtype for array.
    shape : int or tuple of ints, optional
        Shape of each array.
    offset : int, optional
        Position in the file or buffer to start reading from.
    strides : tuple of ints, optional
        Buffer (`buf`) is interpreted according to these strides (strides
        define how many bytes each array element, row, column, etc.
        occupy in memory).
    formats, names, titles, aligned, byteorder :
        If `dtype` is ``None``, these arguments are passed to
        `numpy.format_parser` to construct a dtype. See that function for
        detailed documentation.
    copy : bool, optional
        Whether to copy the input object (True), or to use a reference instead.
        This option only applies when the input is an ndarray or recarray.
        Defaults to True.

    Returns
    -------
    np.recarray
        Record array created from the specified object.

    Notes
    -----
    If `obj` is ``None``, then call the `~numpy.recarray` constructor. If
    `obj` is a string, then call the `fromstring` constructor. If `obj` is a
    list or a tuple, then if the first object is an `~numpy.ndarray`, call
    `fromarrays`, otherwise call `fromrecords`. If `obj` is a
    `~numpy.recarray`, then make a copy of the data in the recarray
    (if ``copy=True``) and use the new formats, names, and titles. If `obj`
    is a file, then call `fromfile`. Finally, if obj is an `ndarray`, then
    return ``obj.view(recarray)``, making a copy of the data if ``copy=True``.

    Examples
    --------
    >>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])

    >>> np.core.records.array(a)
    rec.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]],
        dtype=int32)

    >>> b = [(1, 1), (2, 4), (3, 9)]
    >>> c = np.core.records.array(b, formats = ['i2', 'f2'], names = ('x', 'y'))
    >>> c
    rec.array([(1, 1.0), (2, 4.0), (3, 9.0)],
              dtype=[('x', '<i2'), ('y', '<f2')])

    >>> c.x
    rec.array([1, 2, 3], dtype=int16)

    >>> c.y
    rec.array([ 1.0,  4.0,  9.0], dtype=float16)

    >>> r = np.rec.array(['abc','def'], names=['col1','col2'])
    >>> print(r.col1)
    abc

    >>> r.col1
    array('abc', dtype='<U3')

    >>> r.col2
    array('def', dtype='<U3')
    Nrº   zIMust define formats (or dtype) if object is None, string, or an open file)r0   r1   r2   r3   r4   z"Must define a shape if obj is None)rŽ   rˆ   r‰   )r�   rˆ   r   )rA   r�   )rA   r�   rˆ   Z__array_interface__zUnknown input type)r?   rM   rO   r¼   r>   r@   rA   r   r   Úbytesr   r(   rN   r   r   rn   Úcopyr   rŠ   rt   Údictr   )rr   rA   r�   rˆ   r‰   r0   r1   r2   r3   r4   rÅ   ÚkwdsÚnewZ	interfacer!   r!   r%   r   ¹  sh    Uÿÿÿü



r   )NNNNNFN)NNNNNFN)NNr   NNNFN)NNr   NNNFN)
NNr   NNNNFNT)"r[   r§   Úcollectionsr   Ú
contextlibr   Ú r   r@   r   ri   Znumpy.compatr   Znumpy.core.overridesr   Z
arrayprintr	   Ú__all__rŠ   rW   Z
sctypeDictZnumfmtr)   r   rj   r
   r   rª   r   r   r   r¹   r   r   r!   r!   r!   r%   Ú<module>   sp   #ó	 U    ÿ^  ÿT  ÿJ	  ÿd  ÿ