o
    ;Di                     @   sz   d dl mZmZmZ d dlZd dlmZmZ d dlm	Z	m
Z
 d dlmZ dddZG dd	 d	e	ZdddZdddZdS )    )absolute_importprint_functiondivisionN)string_types	text_type)Tableiterpeek)construct_dtypec                 C   s   t | ||||dS )a'  Extract a table from a bcolz ctable, e.g.::

        >>> import petl as etl
        >>>
        >>> def example_from_bcolz():
        ...     import bcolz
        ...     cols = [
        ...         ['apples', 'oranges', 'pears'],
        ...         [1, 3, 7],
        ...         [2.5, 4.4, .1]
        ...     ]
        ...     names = ('foo', 'bar', 'baz')
        ...     ctbl = bcolz.ctable(cols, names=names)
        ...     return etl.frombcolz(ctbl)
        >>>
        >>> example_from_bcolz() # doctest: +SKIP
        +-----------+-----+-----+
        | foo       | bar | baz |
        +===========+=====+=====+
        | 'apples'  |   1 | 2.5 |
        +-----------+-----+-----+
        | 'oranges' |   3 | 4.4 |
        +-----------+-----+-----+
        | 'pears'   |   7 | 0.1 |
        +-----------+-----+-----+

    If `expression` is provided it will be executed by bcolz and only
    matching rows returned, e.g.::

        >>> tbl2 = etl.frombcolz(ctbl, expression='bar > 1') # doctest: +SKIP
        >>> tbl2 # doctest: +SKIP
        +-----------+-----+-----+
        | foo       | bar | baz |
        +===========+=====+=====+
        | 'oranges' |   3 | 4.4 |
        +-----------+-----+-----+
        | 'pears'   |   7 | 0.1 |
        +-----------+-----+-----+

    .. versionadded:: 1.1.0

    )
expressionoutcolslimitskip)	BcolzViewsourcer
   r   r   r    r   F/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/bcolz.py	frombcolz   s   ,r   c                   @   s"   e Zd Z		dddZdd ZdS )r   Nr   c                 C   s"   || _ || _|| _|| _|| _d S Nr   )selfr   r
   r   r   r   r   r   r   __init__=   s
   
zBcolzView.__init__c                 #   s    t | jtrdd l}|j| jdd n| j | jd u r"t j}nt| j}t fdd|D s6J d|V  | j	d u rJ j
| j| j| jd}n j| j	| j| j| jd}|D ]}|V  qYd S )Nr   rmodec                 3   s    | ]}| j v V  qd S r   )names).0hctblr   r   	<genexpr>T   s    z%BcolzView.__iter__.<locals>.<genexpr>zinvalid outcols)r   r   r   )
isinstancer   r   bcolzopenr   tupler   allr
   iterr   r   where)r   r!   headeritrowr   r   r   __iter__E   s(   


zBcolzView.__iter__NNNr   )__name__
__module____qualname__r   r*   r   r   r   r   r   ;   s
    
r     c                    s   ddl }ddl}t| }t||\}}t|}dd |D }ttt|}	t|	||}|	dd |	dd |j
|jg |d	fi | t fd
d jD t j }
	 tt||
}|j||d	} | t||
k rrnqW    S )a  Load data into a bcolz ctable, e.g.::

        >>> import petl as etl
        >>>
        >>> def example_to_bcolz():
        ...     table = [('foo', 'bar', 'baz'),
        ...              ('apples', 1, 2.5),
        ...              ('oranges', 3, 4.4),
        ...              ('pears', 7, .1)]
        ...     return etl.tobcolz(table)
        >>> 
        >>> ctbl = example_to_bcolz() # doctest: +SKIP
        >>> ctbl # doctest: +SKIP
        ctable((3,), [('foo', '<U7'), ('bar', '<i8'), ('baz', '<f8')])
          nbytes: 132; cbytes: 1023.98 KB; ratio: 0.00
          cparams := cparams(clevel=5, shuffle=1, cname='lz4', quantize=0)
        [('apples', 1, 2.5) ('oranges', 3, 4.4) ('pears', 7, 0.1)]
        >>> ctbl.names # doctest: +SKIP
        ['foo', 'bar', 'baz']
        >>> ctbl['foo'] # doctest: +SKIP
        carray((3,), <U7)
          nbytes := 84; cbytes := 511.98 KB; ratio: 0.00
          cparams := cparams(clevel=5, shuffle=1, cname='lz4', quantize=0)
          chunklen := 18724; chunksize: 524272; blocksize: 0
        ['apples' 'oranges' 'pears']

    Other keyword arguments are passed through to the ctable constructor.

    .. versionadded:: 1.1.0

    r   Nc                 s   s    | ]}t |V  qd S r   )r#   )r   r)   r   r   r   r      s    ztobcolz.<locals>.<genexpr>expectedleni@B r   wdtypec                 3       | ]	} j | jV  qd S r   colschunklenr   namer   r   r   r          )r!   numpyr%   r   nextlistmapr   r	   
setdefaultctablearraysumr   len	itertoolsisliceappendflush)tabler3   samplekwargsr!   npr(   peekhdrfldsr7   datar   r   r   tobcolzc   s2   !
rP   Tc                    s   ddl }ddl}t|tr|j|dd nt|drt|ds%J d| |  j}t| }t|}t	t
t|}|rHt|t jksHJ dt fd	d
 jD t j }		 t	t||	}
|j|
|d}
 |
 t|
|	k runqZ    S )zAppend data into a bcolz ctable. The `obj` argument can be either an
    existing ctable or the name of a directory were an on-disk ctable is
    stored.

    .. versionadded:: 1.1.0

    r   Nar   rF   r   z$expected rootdir or ctable, found %rzcolumn names do not matchc                 3   r4   r   r5   r8   r   r   r   r      r:   zappendbcolz.<locals>.<genexpr>Tr2   )r!   r;   r    r   r"   hasattrr3   r%   r<   r=   r>   r   r#   r   rB   rC   rD   rE   rA   rF   rG   )rH   objcheck_namesr!   rK   r3   r(   rM   rN   r7   rO   r   r   r   appendbcolz   s8   	

rU   r+   )Nr/   )T)
__future__r   r   r   rD   petl.compatr   r   petl.util.baser   r   petl.io.numpyr	   r   r   rP   rU   r   r   r   r   <module>   s   
0
(?