o
    ;Di                     @   s   d dl mZmZmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZ dd Zdd ZdddZee
_dd Zee
_dd ZG dd de
ZdddZee	_ee	_d	S )    )divisionprint_functionabsolute_import)nextstring_types)iterpeek
ValuesViewTable)columnsc                 C   sF   dd l }t| }t|}ttt|}t|}|j|j	}||_
|S )Nr   )numpyiterr   listmapstrtuplerecarraydtypenames)tablenpithdrfldsrowsr    r   F/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/numpy.pyinfer_dtype
   s   r   c           	      C   s  dd l }|d u rt|}|S t|tr)dd |dD }dd t| |D }|S t|trd|vs6d|vrt|}g g d}| D ]=}|d | ||v rat|| t	ra|d || d  qA||vru|
|| }|d |j qA|d ||  qA|}|S )	Nr   c                 S   s   g | ]}|  qS r   )strip).0sr   r   r   
<listcomp>   s    z#construct_dtype.<locals>.<listcomp>,c                 S   s   g | ]\}}||fqS r   r   )r   ftr   r   r   r!      s    r   formats)r   r%   )r   r   
isinstancer   splitzipdictr
   appendr   r   r   )	r   peekr   r   typestringscolsnewdtyper#   ar   r   r   construct_dtype   s,   


r0   N  c           
      C   sb   ddl }t| }t||\}}t|}ttt|}t|||}dd |D }|j|||d}	|	S )a  
    Load data from the given `table` into a
    `numpy <http://www.numpy.org/>`_ structured array. E.g.::

        >>> import petl as etl
        >>> table = [('foo', 'bar', 'baz'),
        ...          ('apples', 1, 2.5),
        ...          ('oranges', 3, 4.4),
        ...          ('pears', 7, .1)]
        >>> a = etl.toarray(table)
        >>> a
        array([('apples', 1, 2.5), ('oranges', 3, 4.4), ('pears', 7, 0.1)],
              dtype=(numpy.record, [('foo', '<U7'), ('bar', '<i8'), ('baz', '<f8')]))
        >>> # the dtype can be specified as a string
        ... a = etl.toarray(table, dtype='S4, i2, f4')
        >>> a
        array([(b'appl', 1, 2.5), (b'oran', 3, 4.4), (b'pear', 7, 0.1)],
              dtype=[('foo', 'S4'), ('bar', '<i2'), ('baz', '<f4')])
        >>> # the dtype can also be partially specified
        ... a = etl.toarray(table, dtype={'foo': 'S4'})
        >>> a
        array([(b'appl', 1, 2.5), (b'oran', 3, 4.4), (b'pear', 7, 0.1)],
              dtype=[('foo', 'S4'), ('bar', '<i8'), ('baz', '<f8')])

    If the dtype is not completely specified, `sample` rows will be
    examined to infer an appropriate dtype.

    r   Nc                 s   s    | ]}t |V  qd S N)r   )r   rowr   r   r   	<genexpr>]   s    ztoarray.<locals>.<genexpr>r   count)	r   r   r   r   r   r   r   r0   fromiter)
r   r   r7   sampler   r   r+   r   r   sar   r   r   toarray7   s   r;   c                  O   s   ddl }t| i ||jS )zS
    Convenient shorthand for ``toarray(*args, **kwargs).view(np.recarray)``.

    r   N)r   r;   viewrecarray)argskwargsr   r   r   r   
torecarrayf   s   r@   c                 C   s   t | S )a  
    Extract a table from a `numpy <http://www.numpy.org/>`_ structured array,
    e.g.::

        >>> import petl as etl
        >>> import numpy as np
        >>> a = np.array([('apples', 1, 2.5),
        ...               ('oranges', 3, 4.4),
        ...               ('pears', 7, 0.1)],
        ...              dtype='S8, i4, f4')
        >>> table = etl.fromarray(a)
        >>> table # doctest: +SKIP
        +-----------+----+-----+
        | f0        | f1 | f2  |
        +===========+====+=====+
        | 'apples'  | 1  | 2.5 |
        +-----------+----+-----+
        | 'oranges' | 3  | 4.4 |
        +-----------+----+-----+
        | 'pears'   | 7  | 0.1 |
        +-----------+----+-----+

    )	ArrayViewr/   r   r   r   	fromarrays   s   rC   c                   @   s   e Zd Zdd Zdd ZdS )rA   c                 C   s
   || _ d S r3   rB   )selfr/   r   r   r   __init__   s   
zArrayView.__init__c                 c   s,    t | jjjV  | jD ]}t |V  qd S r3   )r   r/   r   r   )rD   r4   r   r   r   __iter__   s
   
zArrayView.__iter__N)__name__
__module____qualname__rE   rF   r   r   r   r   rA      s    rA   c                 C   sF   ddl }t| }|du rt||\}}||j}|j|||d}|S )a  
    Load values from a table column into a `numpy <http://www.numpy.org/>`_
    array, e.g.::

        >>> import petl as etl
        >>> table = [('foo', 'bar', 'baz'),
        ...          ('apples', 1, 2.5),
        ...          ('oranges', 3, 4.4),
        ...          ('pears', 7, .1)]
        >>> table = etl.wrap(table)
        >>> table.values('bar').array()
        array([1, 3, 7])
        >>> # specify dtype
        ... table.values('bar').array(dtype='i4')
        array([1, 3, 7], dtype=int32)

    r   Nr6   )r   r   r   r   r   r8   )valsr   r7   r9   r   r   r+   r/   r   r   r   valuestoarray   s   rK   )Nr1   r2   )
__future__r   r   r   petl.compatr   r   petl.util.baser   r   r	   petl.util.materialiser
   r   r0   r;   r@   rC   rA   rK   r   r   r   r   r   <module>   s   
!,


