o
    ;Di                     @   s   d dl mZmZmZ d dlmZmZ d dlmZ d dl	m
Z
mZ dddZG dd	 d	eZdddZee_dddZee_dd ZdddZee_G dd deZdS )    )absolute_importprint_functiondivision)picklenext)Table)read_source_from_argwrite_source_from_argNc                 C   s   t | } t| S )a&  
    Extract a table From data pickled in the given file. The rows in the
    table should have been pickled to the file one at a time. E.g.::

        >>> import petl as etl
        >>> import pickle
        >>> # set up a file to demonstrate with
        ... with open('example.p', 'wb') as f:
        ...     pickle.dump(['foo', 'bar'], f)
        ...     pickle.dump(['a', 1], f)
        ...     pickle.dump(['b', 2], f)
        ...     pickle.dump(['c', 2.5], f)
        ...
        >>> # now demonstrate the use of frompickle()
        ... table1 = etl.frompickle('example.p')
        >>> table1
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' |   1 |
        +-----+-----+
        | 'b' |   2 |
        +-----+-----+
        | 'c' | 2.5 |
        +-----+-----+


    )r   
PickleViewsource r   G/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/io/pickle.py
frompickle   s   r   c                   @   s   e Zd Zdd Zdd ZdS )r
   c                 C   s
   || _ d S Nr   )selfr   r   r   r   __init__2   s   
zPickleView.__init__c              	   c   s\    | j d}z
	 tt|V  q
 ty   Y nw W d    d S 1 s'w   Y  d S )Nrb)r   opentupler   loadEOFError)r   fr   r   r   __iter__5   s   "zPickleView.__iter__N__name__
__module____qualname__r   r   r   r   r   r   r
   0   s    r
   Tc                 C      t | |d||d dS )a  
    Write the table to a pickle file. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['c', 2]]
        >>> etl.topickle(table1, 'example.p')
        >>> # look what it did
        ... table2 = etl.frompickle('example.p')
        >>> table2
        +-----+-----+
        | foo | bar |
        +=====+=====+
        | 'a' |   1 |
        +-----+-----+
        | 'b' |   2 |
        +-----+-----+
        | 'c' |   2 |
        +-----+-----+

    Note that if a file already exists at the given location, it will be
    overwritten.

    The pickle file format preserves type information, i.e., reading and writing
    is round-trippable for tables with non-string data values.

    wbr   modeprotocolwrite_headerN_writepickletabler   r#   r$   r   r   r   topickle>   s   

r)   Fc                 C   r   )a=  
    Append data to an existing pickle file. I.e.,
    as :func:`petl.io.pickle.topickle` but the file is opened in append mode.

    Note that no attempt is made to check that the fields or row lengths are
    consistent with the existing data, the data rows from the table are simply
    appended to the file.

    abr!   Nr%   r'   r   r   r   appendpickled   s   

r+   c           	   	   C   s   t ||}||9}t| }zt|}W n ty%   Y W d    d S w |r/t||| |D ]	}t||| q1W d    d S 1 sFw   Y  d S r   )r	   r   iterr   StopIterationr   dump)	r(   r   r"   r#   r$   r   ithdrrowr   r   r   r&   v   s   
"r&   c                 C   s   t | |||dS )zZ
    Return a table that writes rows to a pickle file as they are iterated
    over.

    )r   r#   r$   )TeePickleViewr'   r   r   r   	teepickle   s   r3   c                   @   s   e Zd ZdddZdd ZdS )	r2   Nr   Tc                 C   s   || _ || _|| _|| _d S r   r'   )r   r(   r   r#   r$   r   r   r   r      s   
zTeePickleView.__init__c              	   c   s    | j }t| j}|dE}t| j}zt|}W n ty*   Y W d    d S w | jr5t	
||| t|V  |D ]}t	
||| t|V  q<W d    d S 1 sVw   Y  d S )Nr    )r#   r	   r   r   r,   r(   r   r-   r$   r   r.   r   )r   r#   r   r   r/   r0   r1   r   r   r   r      s&   


"zTeePickleView.__iter__Nr   Tr   r   r   r   r   r2      s    
r2   r   r4   )Nr   F)
__future__r   r   r   petl.compatr   r   petl.util.baser   petl.io.sourcesr   r	   r   r
   r)   r+   r&   r3   r2   r   r   r   r   <module>   s   
"
#

