Current File : //usr/lib64/python2.7/site-packages/numpy/core/records.pyo
�
E�`Qc@s�dZdddgZddlZddlmZddlZddlZddl	Z	ddl
Z
ddlmZm
Z
ejZidd	6d
d6dd
6dd6d
d6dd6dd6dd6dd6d
d
6dd6dd6dd6dd6ZejZejZd�Zdd!d��YZdejfd��YZdefd��YZdddddedd�Zdddddedd�Zddddddedd�Zd�Zddddddedd�Zdddddddeded �
Z dS("ss
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,
such as::

  >>> a = np.array([(1, 2.0), (1, 2.0)], dtype=[('x', int), ('y', float)])
  >>> a
  array([(1, 2.0), (1, 2.0)],
        dtype=[('x', '<i4'), ('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 = a.view(np.recarray)

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

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

trecordtrecarrayt
format_parseri����N(t	chararray(t	isfileobjtbytest>tbt<tlt=tntBtLtNtstSt|tIticCsfg}xYtt|��D]E}||||dkr|||kr^|j||�q^qqW|S(s@Find duplication in a list, return a list of duplicated elementsi(trangetlentappend(tlisttdupR((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pytfind_duplicateJscBs;eZdZedd�Zdd�Zd�Zd�ZRS(s�
    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')])

    cCs=|j||�|j||�|j|�|j|_dS(N(t
_parseFormatst_setfieldnamest_createdescrt_descrtdtype(tselftformatstnamesttitlestalignedt	byteorder((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyt__init__�s
icCs|dkrtd��nt|t�r^t|�dkrL|jd�ndj|�}ntj||�}|j	}|dkr�tjd|fg|�}|j	}n|j
}g|D]}||d^q�|_g|D]}||d^q�|_t|�|_
dS(	s Parse the field formats sNeed formats argumentitt,tf1iiN(tNonet
ValueErrort
isinstanceRRRtjointsbRtfieldsR!t
_f_formatst_offsetst_nfields(RR R#RR.tkeystkey((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyR�s		$$cCst|r�t|�tjtjgkr'n5t|�tkrK|jd�}ntd|
��g||j D]}|j�^qj|_	n	g|_	|j	gt
t|j	�|j�D]}d|^q�7_	t|j	�}|r�t
d|��n|r&g||j D]}|j�^q|_ng|_g}|jt|�krp|jdg|jt|�7_ndS(sQconvert input field names into a list and assign to the _names
        attribute R'sillegal input names %ssf%dsDuplicate field names: %sN(ttypettypestListTypet	TupleTypetstrtsplitt	NameErrorR1tstript_namesRRRR*t_titlesR)(RR!R"RRt_dup((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyR�s&,	",	cCsptji|jd6|jd6|jd6|jd6�}|dk	rct|d}|j|�}n||_	dS(NR!R toffsetsR"i(
R-RR<R/R0R=R)t_byteorderconvtnewbyteorderR(RR$tdescr((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyR�s

N(	t__name__t
__module__t__doc__tFalseR)R%RRR(((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRSs
:	$cBs;eZdZd�Zd�Zd�Zd�Zd�ZRS(sEA data-type scalar that allows field access as attribute lookup.
    cCs
|j�S(N(t__str__(R((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyt__repr__�scCst|j��S(N(R8titem(R((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRG�scCs�|dkrtjj||�Sytjj||�SWntk
rInXtjj|d�j}|j|d�}|r�|j|d �}y
|j}Wntk
r�|SX|jr�|j	|j
�S|jdkr�|j	t�S|Std|��dS(NtsetfieldtgetfieldRitSUs%'record' object has no attribute '%s'(ssetfieldsgetfieldsdtype(
tnttvoidt__getattribute__tAttributeErrorR.tgetR)RKRtviewt	__class__tcharR(Rtattrt	fielddicttrestobjtdt((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRO�s*


	
cCs�|dkrtd|��ntjj|d�j}|j|d�}|rc|j||d �St||d�r�tjj	|||�Std|��dS(NRJRKRsCannot set '%s' attributeis%'record' object has no attribute '%s'(ssetfieldsgetfieldsdtype(
RPRMRNROR.RQR)RJtgetattrt__setattr__(RRUtvalRVRW((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyR[�scCs|jj}tg|D]}t|�^q�}g}d|}x.|D]&}|j||t||�f�qHWdj|�S(sPretty-print all fields.s%% %ds: %%ss
(RR!tmaxRRRZR,(RR!tnametmaxlentrowstfmt((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pytpprint	s%

$(RCRDRERHRGROR[Rb(((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyR�s				cBsteZdZd
d
dd
d
d
d
d
edd�
Zd�Zd�Zd�Zd�Z	d
d�Z
d
d
d	�ZRS(s
    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 or column-major order.

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

    See Also
    --------
    rec.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', float), ('y', int)])
    >>> x
    array([(1.0, 2), (3.0, 4)],
          dtype=[('x', '<f8'), ('y', '<i4')])

    >>> 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')])

    itCcCs�|dk	rtj|�}nt||||
|	�j}|dkritj||t|fd|�}
n3tj||t|fd|d|d|d|�}
|
S(Ntordertbuffertoffsettstrides(R)R-RRRtndarrayt__new__R(tsubtypetshapeRtbufRfRgR R!R"R$R#RdRBR((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRi�s$cCs�ytj||�SWntk
r'nXtj|d�j}y||d }Wn'ttfk
rxtd|��nX|j|�}|jjr�|S|jj	dkr�|j
t�S|j
t�S(NRis record array has no attribute %sRL(tobjectRORPRhR.t	TypeErrortKeyErrorRKRRTRRR(RRURVRWRX((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRO�s

c	Cs||jk}ytj|||�}WnPtj|d�jpFi}||kr�tj�d \}}||�q�nTXtj|d�jp�i}||kr�|S|r�ytj||�Wq�|SXny||d }Wn't	t
fk
r	td|��nX|j||�S(NRis record array has no attribute %s(
t__dict__RmR[RhROR.tsystexc_infot__delattr__RnRoRPRJ(	RRUR\tnewattrtretRVtexctypetvalueRW((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyR[�s*cCs>tj||�}t|t�r:|jjr:|jt�S|S(N(Rht__getitem__R+Rt	isbuiltinRR(RtindxRX((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRx�s
cCs"tj|�}|jddd�S(NRs	rec.arrayi(RhRHtreplace(RRu((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRH�scCs�t|t�r1tj|d�j}||}ntj|d�j}||d }|dkr�|j|�}|jjr|S|jj	dkr�|j
t�S|j
t�S|j||�SdS(NRiRL(
R+tintRhROR!R.R)RKRRTRRRRJ(RRUR\R!RVRWRX((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pytfield�s


cCs�|dkrtj||�S|dkr�y#t|t�rJtj||�SWntk
r^nXtj|�}|jdkr�|j�j|�Stj||�Stj|||�SdS(N(	R)RhRRt
issubclassRnR-RR.t	__array__(RRR4((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRR�s
N(RCRDRER)RFRiROR[RxRHR}RR(((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyRsi					cCs\g|D]}tj|�^q}|dks:|dkrJ|dj}nt|t�re|f}n|dkr|dkrd}xs|D]k}	t|	t�s�td��n|t|	j	j
7}t|	j	j
tj
�r�||	j
7}n|d7}q�W|d }n|dk	r-tj	|�}
|
j}n*t|||||�}|j}|j}
t|
�t|�kr~td��n|
dj}
t|
�}|dkr�|| }nxft|�D]X\}}	t|
|j�}|	jt|	j�| }||kr�td|��q�q�Wt||
�}x,tt|��D]}|||||<q<W|S(	s@ create a record array from a (flat) list of arrays

    >>> 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)
    >>> x1[1]=34
    >>> r.a
    array([1, 2, 3, 4])
    iR&s*item in the array list must be an ndarray.R'i����s>mismatch between the number of fields and the number of arrayss array-shape mismatch in array %dN(R-tasarrayR)RkR+R|RhR*t_typestrRR4R~RMtflexibletitemsizeR!RR<RRt	enumerateRR(t	arrayListRRkR R!R"R#R$txRXRBR<tparsedtd0tnntkt	testshapet_arrayR((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyt
fromarrays�sH"

		
cCs�t|d�}|dkr�|dkr�tj|dt�}	gt|�D]%}
tj|	d|
fj��^qJ}t|d|d|d|d|d|d	|�S|dk	r�tjt	|f�}nt
|||||�j}ytj|d|�}
Wn�tk
r�|dks#|dkr2t|�}nt
|ttf�rS|f}nt|�d
krttd��nt||�}x+t|j�D]}t||�||<q�W|SX|dk	r�|
j|kr�||
_n|
jt�}|S(
si create a recarray from a list of records in text form

        The data in the same field can be heterogeneous, they will be promoted
        to the highest data type.  This method is intended for creating
        smaller record arrays.  If used to create large array without formats
        defined

        r=fromrecords([(2,3.,'abc')]*100000)

        it can be slow.

        If formats is None, then this will auto-detect formats. Use list of
        tuples rather than list of lists for faster processing.

    >>> 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
    chararray(['dbe', 'de'],
          dtype='|S3')
    >>> import cPickle
    >>> print cPickle.loads(cPickle.dumps(r))
    [(456, 'dbe', 1.2) (2, 'de', 1.3)]
    iR.R RkR!R"R#R$isCan only deal with 1-d array.N(RR)R-tarrayRmtxrangettolistR�RRRRRnR+R|tlongR*RtsizettupleRkRR(trecListRRkR R!R"R#R$tnfieldsRXRtarrlistRBtretvalR�R�RW((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pytfromrecords;s48
ic	
Cs�|dkr'|dkr'td��n|dk	rEtj|�}	nt|||||�j}	|	j}
|dks�|dks�|dkr�t|�||
}nt||	d|d|�}|S(sM create a (read-only) record array from binary data contained in
    a stringsMust have dtype= or formats=ii����RlRfN(	R)R*R-RRRR�RR(t
datastringRRkRfR R!R"R#R$RBR�R�((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyt
fromstring{s	$cCsdy|j�}Wn+tk
r=tjj|j�|j�SXtj|�}|j|j�}|S(N(	tfilenoRPtostpathtgetsizeR^ttelltfstattst_size(tfdtfntstR�((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pytget_remaining_size�s
c	Cs�|dks|dkr!d}n!t|ttf�rB|f}nd}	t|t�rod}	t|d�}n|dkr�|j|d�nt|�}
|dk	r�tj	|�}nt
|||||�j}|j}tj
|�j�}
|
|}|dkrOt|�}|
|||jd�<t|�}tj
|�j�}
n|
|}||
krttd��nt||�}|j|j�}||kr�td��n|	r�|j�n|S(	sCreate an array from binary file data

    If file is a string then that file is opened, else it is assumed
    to be a file object.

    >>> 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,)
    ii����itrbs:Not enough bytes left in file for specified shape and types%Didn't read as many bytes as expectedN(i����(R)R+R|R�R8topentseekR�R-RRRR�R�tprodRtindexR�R*RtreadintotdatatIOErrortclose(R�RRkRfR R!R"R#R$R^R�RBR�t	shapeprodt	shapesizetnbytesR�t
nbytesread((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pytfromfile�sB		


cCs�t|td�tf�s't|�rN|dkrN|dkrNtd��ni}|dk	rrtj|�}nS|dk	r�t|||||	�j	}n)i|d6|d6|d6|d6|	d6}|dkr|dkr�td��nt
||d|d	|d
|�St|t�r6t||d|d	||�St|t
tf�r�t|dtt
f�r}t|d
|d||�St|d
|d||�Sn�t|t
�r�|dk	r�|j|kr�|j|�}n|}|
r�|j�}n|St|�rt|d
|d|d	|�St|t�r�|dk	rX|j|krX|j|�}n|}|
rs|j�}n|jt
�}
t|
jjtj�r�tjt|
jf�|
_n|
St|dd�}|dks�t|t�r�td��ntj|�}|dk	r5|j|kr5|j|�}n|jt
�}
t|
jjtj�rztjt|
jf�|
_n|
SdS(s=Construct a record array from a wide-variety of objects.
    sIMust define formats (or dtype) if object is None, string, or an open fileR R!R"R#R$s"Must define a shape if obj is NoneRlRfRgRkiRt__array_interface__sUnknown input typeN(R+R4R)R8RR*R-RRRRRR�RR�R�R�RRtcopyR�RhR~RMRNRRZtdictR�(RXRRkRfRgR R!R"R#R$R�tkwdstnewRWt	interface((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyR��sl'

((!REt__all__tnumericR-tdefchararrayRtnumerictypesRMR5R�Rqtnumpy.compatRRRhR@ttypeDicttnumfmtR�RRRNRRR)RFR�R�R�R�R�tTrueR�(((s8/usr/lib64/python2.7/site-packages/numpy/core/records.pyt<module>$sP	

				�F�	D?		C