o
    Ø¶©Yã„  ã                   @   sô   d Z ddlmZ ddlmZ ddlmZ ddlmZmZ dZ	dZ
dZd	Zd
ZdZdZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zd$dd„Zd$dd„ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d d!„ d!eƒZG d"d#„ d#eƒZdS )%z¶
    tablib.core
    ~~~~~~~~~~~

    This module implements the central Tablib objects.

    :copyright: (c) 2016 by Kenneth Reitz.
    :license: MIT, see LICENSE for more details.
é    )Úcopy)Ú
itemgetter)Úformats)ÚOrderedDictÚunicodeÚtablibz0.12.1i  zKenneth ReitzÚMITzCopyright 2017 Kenneth ReitzÚrestructuredtextc                   @   sº   e Zd ZdZddgZeƒ eƒ fdd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zed"d#„ ƒZed$d%„ ƒZd&d'„ Zd(S ))ÚRowz/Internal Row object. Mainly used for filtering.Ú_rowÚtagsc                 C   s   t |ƒ| _t |ƒ| _d S ©N)Úlistr   r   ©ÚselfÚrowr   © r   ú=/var/www/html/env/lib/python3.10/site-packages/tablib/core.pyÚ__init__"   s   
zRow.__init__c                 C   s   dd„ | j D ƒS )Nc                 s   s   � | ]}|V  qd S r   r   )Ú.0Úcolr   r   r   Ú	<genexpr>'   s   € zRow.__iter__.<locals>.<genexpr>©r   ©r   r   r   r   Ú__iter__&   ó   zRow.__iter__c                 C   ó
   t | jƒS r   )Úlenr   r   r   r   r   Ú__len__)   ó   
zRow.__len__c                 C   r   r   )Úreprr   r   r   r   r   Ú__repr__,   r   zRow.__repr__c                 C   s   | j ||… S r   r   )r   ÚiÚjr   r   r   Ú__getslice__/   ó   zRow.__getslice__c                 C   s
   | j | S r   r   ©r   r"   r   r   r   Ú__getitem__2   r   zRow.__getitem__c                 C   s   || j |< d S r   r   )r   r"   Úvaluer   r   r   Ú__setitem__5   r%   zRow.__setitem__c                 C   s   | j |= d S r   r   r&   r   r   r   Ú__delitem__8   s   zRow.__delitem__c                 C   s(   t ƒ }| jD ]}t| |ƒ}|||< q|S r   )ÚdictÚ	__slots__Úgetattr)r   ÚslotsÚslotÚ	attributer   r   r   Ú__getstate__;   s
   


zRow.__getstate__c                 C   s&   t | ¡ ƒD ]
\}}t| ||ƒ qd S r   )r   ÚitemsÚsetattr)r   ÚstateÚkÚvr   r   r   Ú__setstate__E   s   &zRow.__setstate__c                 C   s   |   d|¡ d S )Nr   ©Úinsert©r   r(   r   r   r   ÚrpushH   r   z	Row.rpushc                 C   s   |   t|ƒ|¡ d S r   )r9   r   r:   r   r   r   ÚlpushK   s   z	Row.lpushc                 C   s   |   |¡ d S r   ©r;   r:   r   r   r   ÚappendN   r%   z
Row.appendc                 C   s   | j  ||¡ d S r   )r   r9   )r   Úindexr(   r   r   r   r9   Q   s   z
Row.insertc                 C   s
   || j v S r   r   )r   Úitemr   r   r   Ú__contains__T   r   zRow.__contains__c                 C   r   )z%Tuple representation of :class:`Row`.)Útupler   r   r   r   r   rB   W   ó   
z	Row.tuplec                 C   r   )z$List representation of :class:`Row`.)r   r   r   r   r   r   r   \   rC   zRow.listc                 C   s:   |dkrdS t |tƒr|| jv S ttt|ƒt| jƒ@ ƒƒS )z)Returns true if current row contains tag.NF)Ú
isinstanceÚstrr   Úboolr   Úset)r   Útagr   r   r   Úhas_taga   s
   

zRow.has_tagN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r   r   r   r   r!   r$   r'   r)   r*   r1   r7   r;   r<   r>   r9   rA   ÚpropertyrB   rI   r   r   r   r   r
      s.    


r
   c                   @   sL  e Zd ZdZi Zdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zedd„ ƒZdtdd„Zdudd„Zdd„ Zdd„ ZeeeƒZdd „ Zd!d"„ ZeeeƒZd#d$„ Zed%d&„ ƒZed'd(„ ƒZdvd)d*„Zd+d,„ Zed-d.„ ƒZed/d0„ ƒZed1d2„ ƒZed3d4„ ƒZ ed5d6„ ƒZ!ed7d8„ ƒZ"ed9d:„ ƒZ#ed;d<„ ƒZ$ed=d>„ ƒZ%ed?d@„ ƒZ&edAdB„ ƒZ'e(ƒ fdCdD„Z)e(ƒ fdEdF„Z*e(ƒ fdGdH„Z+e(ƒ fdIdJ„Z,e(ƒ fdKdL„Z-dMdN„ Z.dOdP„ Z/dQdR„ Z0dwdSdT„Z1dvdUdV„Z2dvdWdX„Z3dxdZd[„Z4dxd\d]„Z5dvd^d_„Z6d`da„ Z7dbdc„ Z8ddde„ Z9dydfdg„Z:dhdi„ Z;djdk„ Z<dldm„ Z=dndo„ Z>dpdq„ Z?dwdrds„Z@dS )zÚDatasetaÈ  The :class:`Dataset` object is the heart of Tablib. It provides all core
    functionality.

    Usually you create a :class:`Dataset` instance in your main module, and append
    rows as you collect data. ::

        data = tablib.Dataset()
        data.headers = ('name', 'age')

        for (name, age) in some_collector():
            data.append((name, age))


    Setting columns is similar. The column data length must equal the
    current height of the data and headers must be set ::

        data = tablib.Dataset()
        data.headers = ('first_name', 'last_name')

        data.append(('John', 'Adams'))
        data.append(('George', 'Washington'))

        data.append_col((90, 67), header='age')


    You can also set rows and headers upon instantiation. This is useful if
    dealing with dozens or hundreds of :class:`Dataset` objects. ::

        headers = ('first_name', 'last_name')
        data = [('John', 'Adams'), ('George', 'Washington')]

        data = tablib.Dataset(*data, headers=headers)

    :param \*args: (optional) list of rows to populate Dataset
    :param headers: (optional) list strings for Dataset header row
    :param title: (optional) string to use as title of the Dataset


    .. admonition:: Format Attributes Definition

     If you look at the code, the various output/import formats are not
     defined within the :class:`Dataset` object. To add support for a new format, see
     :ref:`Adding New Formats <newformats>`.

    c                 O   sJ   t dd„ |D ƒƒ| _d | _g | _g | _| d¡| _| d¡| _|  ¡  d S )Nc                 s   s   � | ]}t |ƒV  qd S r   ©r
   )r   Úargr   r   r   r       ó   € z#Dataset.__init__.<locals>.<genexpr>ÚheadersÚtitle)	r   Ú_dataÚ_Dataset__headersÚ_separatorsÚ_formattersÚgetrS   rT   Ú_register_formats)r   ÚargsÚkwargsr   r   r   r   Ÿ   s   zDataset.__init__c                 C   ó   | j S r   )Úheightr   r   r   r   r   °   ó   zDataset.__len__c                    sj   t |tƒs
t |tƒr!|| jv r| j |¡‰ ‡ fdd„| jD ƒS t‚| j| }t |tƒr.|jS dd„ |D ƒS )Nc                    ó   g | ]}|ˆ  ‘qS r   r   ©r   r   ©Úposr   r   Ú
<listcomp>¸   ó    z'Dataset.__getitem__.<locals>.<listcomp>c                 S   s   g | ]}|j ‘qS r   )rB   )r   Úresultr   r   r   rd   À   s    )	rD   rE   r   rS   r?   rU   ÚKeyErrorr
   rB   )r   ÚkeyÚ_resultsr   rb   r   r'   ´   s   


zDataset.__getitem__c                 C   s   |   |¡ t|ƒ| j|< d S r   )Ú	_validater
   rU   )r   rh   r(   r   r   r   r)   Â   s   
zDataset.__setitem__c                 C   sj   t |tƒs
t |tƒr/|| jv r-| j |¡}| j|= t| jƒD ]\}}||= || j|< qd S t‚| j|= d S r   )rD   rE   r   rS   r?   Ú	enumeraterU   rg   )r   rh   rc   r"   r   r   r   r   r*   Ç   s   
ýzDataset.__delitem__c                 C   ó&   zd| j  ¡  W S  ty   Y dS w )Nz<%s dataset>z<dataset object>©rT   ÚlowerÚAttributeErrorr   r   r   r   r!   Ù   ó
   ÿzDataset.__repr__c                    s¢   g }| j r| dd„ | j D ƒ¡ | dd„ | jD ƒ¡ dd„ |D ƒ}tttt|Ž ƒƒ}| j r9| ddd„ |D ƒ¡ d 	d	d„ t
|ƒD ƒ¡‰ d
 	‡ fdd„|D ƒ¡S )Nc                 S   ó   g | ]}t |ƒ‘qS r   )r   )r   Úhr   r   r   rd   ä   re   z'Dataset.__unicode__.<locals>.<listcomp>c                 s   s   � | ]
}t tt|ƒƒV  qd S r   )r   Úmapr   ra   r   r   r   r   ç   ó   € z&Dataset.__unicode__.<locals>.<genexpr>c                 S   s   g | ]	}t tt|ƒƒ‘qS r   )r   rs   r   ra   r   r   r   rd   é   ó    é   c                 S   s   g | ]}d | ‘qS ©ú-r   )r   Úlengthr   r   r   rd   î   re   ú|c                 s   s   � | ]}d | V  qdS )z{%s:%s}Nr   )r   r@   r   r   r   r   ð   rR   Ú
c                 3   s   � | ]}ˆ j |Ž V  qd S r   )Úformatra   ©Úformat_stringr   r   r   ò   s   € )rV   r>   ÚextendrU   r   rs   ÚmaxÚzipr9   Újoinrk   )r   rf   ÚlensÚ
field_lensr   r}   r   Ú__unicode__ß   s   zDataset.__unicode__c                 C   ó   |   ¡ S r   )r…   r   r   r   r   Ú__str__ô   s   zDataset.__str__c              	   C   sØ   t jD ]f}zUz,t| |jt|j|jƒƒ t| d|j |jƒ t| d|j |jƒ |j|jf| j|j< W n& tyX   t| |jt|jƒƒ t| d|j |jƒ |jdf| j|j< Y nw W q tyi   d| j|j< Y qw dS )úAdds format properties.zget_%szset_%sN©NN)	r   Ú	availabler3   rT   rN   Ú
export_setÚ
import_setÚ_formatsro   ©ÚclsÚfmtr   r   r   rZ   û   s"   
ý€ÿôzDataset._register_formatsNFc                    sz   |rˆ j rt|ƒˆ j knd}n$|r't|ƒdk rd}nˆ jr$t|ƒˆ jknd}nt‡ fdd„ˆ jD ƒƒ}|r7dS |s;t‚dS )z>Assures size of every row in dataset is of proper proportions.Trv   c                 3   s   � | ]
}t |ƒˆ jkV  qd S r   )r   Úwidth)r   Úxr   r   r   r     rt   z$Dataset._validate.<locals>.<genexpr>F)r‘   r   r^   ÚallrU   ÚInvalidDimensions)r   r   r   ÚsafetyÚis_validr   r   r   rj     s   zDataset._validateTc              
      sâ   t ˆjƒ}|r
t‰ nt‰ ˆjrKt|ƒD ]7\}}ˆjD ]/\}}z"|du r5t|ƒD ]\}}	||	ƒ|| |< q'n
||| ƒ|| |< W q tyI   t‚w qˆjrh|r\‡ ‡fdd„|D ƒ}
|
S t ˆjƒgt |ƒ }
|
S dd„ |D ƒ}
|
S )z=Packages Dataset into lists of dictionaries for transmission.Nc                    s    g | ]}ˆ t tˆj|ƒƒƒ‘qS r   )r   r�   rS   )r   Údata_row©Ú	dict_packr   r   r   rd   =  s     z$Dataset._package.<locals>.<listcomp>c                 S   rq   r   )r   ra   r   r   r   rd   A  re   )	r   rU   r   r+   rX   rk   Ú
IndexErrorÚInvalidDatasetIndexrS   )r   ÚdictsÚorderedrU   Úrow_ir   r   Úcallbackr#   ÚcÚdatar   r˜   r   Ú_package"  s4   
ÿ€ÿùüþzDataset._packagec                 C   r]   )z´An *optional* list of strings to be used for header rows and attribute names.

        This must be set manually. The given list length must equal :class:`Dataset.width`.

        )rV   r   r   r   r   Ú_get_headersG  s   zDataset._get_headersc                 C   s<   |   |¡ |rzt|ƒ| _W dS  ty   t‚w d| _dS )zValidating headers setter.N)rj   r   rV   Ú	TypeError)r   Ú
collectionr   r   r   Ú_set_headersP  s   
ÿ
zDataset._set_headersc                 C   r†   )aÆ  A native Python representation of the :class:`Dataset` object. If headers have
        been set, a list of Python dictionaries will be returned. If no headers have been set,
        a list of tuples (rows) will be returned instead.

        A dataset object can also be imported by setting the `Dataset.dict` attribute: ::

            data = tablib.Dataset()
            data.dict = [{'age': 90, 'first_name': 'Kenneth', 'last_name': 'Reitz'}]

        )r¢   r   r   r   r   Ú	_get_dict^  s   zDataset._get_dictc                 C   sŽ   t |ƒsdS t|d tƒr|  ¡  |D ]	}|  t|ƒ¡ qdS t|d tƒrE|  ¡  t|d  ¡ ƒ| _|D ]}|  tt| 	¡ ƒƒ¡ q5dS t
‚)aÄ  A native Python representation of the Dataset object. If headers have been
        set, a list of Python dictionaries will be returned. If no headers have been
        set, a list of tuples (rows) will be returned instead.

        A dataset object can also be imported by setting the :class:`Dataset.dict` attribute. ::

            data = tablib.Dataset()
            data.dict = [{'age': 90, 'first_name': 'Kenneth', 'last_name': 'Reitz'}]

        Nr   )r   rD   r   Úwiper>   r
   r+   ÚkeysrS   ÚvaluesÚUnsupportedFormat)r   Úpickler   r   r   r   Ú	_set_dictl  s   ÿÿzDataset._set_dictc                 C   s^   t |ƒ}| jr| d¡g}ng }t|ƒdkr't|d dƒr't t|d | jƒƒ}t|| ƒ}|S )z,Prepares the given column for insert/append.r   rv   Ú__call__)r   rS   Úpopr   Úhasattrrs   rU   rB   ©r   r   Úheaderr   r   r   Ú
_clean_col�  s   zDataset._clean_colc                 C   r   )zfThe number of rows currently in the :class:`Dataset`.
           Cannot be directly modified.
        )r   rU   r   r   r   r   r^   Ÿ  s   
zDataset.heightc                 C   sH   zt | jd ƒW S  ty#   zt | jƒW  Y S  ty"   Y Y dS w w )ziThe number of columns currently in the :class:`Dataset`.
           Cannot be directly modified.
        r   )r   rU   rš   rS   r¤   r   r   r   r   r‘   §  s   ÿýzDataset.widthc                 K   sF   |st |ƒ}| j |d¡\}}|std |¡ƒ‚|| |fi |¤Ž | S )z¯
        Import `in_stream` to the :class:`Dataset` object using the `format`.

        :param \*\*kwargs: (optional) custom configuration to the format `import_set`.
        r‰   zFormat {0} cannot be imported.©Údetect_formatr�   rY   r«   r|   )r   Ú	in_streamr|   r\   r‹   rŒ   r   r   r   Úload¶  ó   zDataset.loadc                 K   ó4   | j  |d¡\}}|std |¡ƒ‚|| fi |¤ŽS )z•
        Export :class:`Dataset` object to `format`.

        :param \*\*kwargs: (optional) custom configuration to the format `export_set`.
        r‰   úFormat {0} cannot be exported.©r�   rY   r«   r|   )r   r|   r\   r‹   rŒ   r   r   r   ÚexportÉ  ó   zDataset.exportc                   C   ó   dS )aä  A Legacy Excel Spreadsheet representation of the :class:`Dataset` object, with :ref:`separators`. Cannot be set.

        .. note::

            XLS files are limited to a maximum of 65,000 rows. Use :class:`Dataset.xlsx` to avoid this limitation.

         .. admonition:: Binary Warning

             :class:`Dataset.xls` contains binary data, so make sure to write in binary mode::

                with open('output.xls', 'wb') as f:
                    f.write(data.xls)
        Nr   r   r   r   r   ÚxlsÙ  s   zDataset.xlsc                   C   r¾   )a_  An Excel '07+ Spreadsheet representation of the :class:`Dataset` object, with :ref:`separators`. Cannot be set.

         .. admonition:: Binary Warning

             :class:`Dataset.xlsx` contains binary data, so make sure to write in binary mode::

                with open('output.xlsx', 'wb') as f:
                    f.write(data.xlsx)
        Nr   r   r   r   r   Úxlsxê  ó   zDataset.xlsxc                   C   r¾   )a^  An OpenDocument Spreadsheet representation of the :class:`Dataset` object, with :ref:`separators`. Cannot be set.

         .. admonition:: Binary Warning

             :class:`Dataset.ods` contains binary data, so make sure to write in binary mode::

                with open('output.ods', 'wb') as f:
                    f.write(data.ods)
        Nr   r   r   r   r   Úods÷  rÁ   zDataset.odsc                   C   r¾   )ac  A CSV representation of the :class:`Dataset` object. The top row will contain
        headers, if they have been set. Otherwise, the top row will contain
        the first row of the dataset.

        A dataset object can also be imported by setting the :class:`Dataset.csv` attribute. ::

            data = tablib.Dataset()
            data.csv = 'age, first_name, last_name\n90, John, Adams'

        Import assumes (for now) that headers exist.

        .. admonition:: Binary Warning

             :class:`Dataset.csv` uses \r\n line endings by default, so make
             sure to write in binary mode::

                 with open('output.csv', 'wb') as f:
                     f.write(data.csv)

             If you do not do this, and you export the file on Windows, your
             CSV file will open in Excel with a blank line between each row.
        Nr   r   r   r   r   Úcsv  s   zDataset.csvc                   C   r¾   )aÅ  A TSV representation of the :class:`Dataset` object. The top row will contain
        headers, if they have been set. Otherwise, the top row will contain
        the first row of the dataset.

        A dataset object can also be imported by setting the :class:`Dataset.tsv` attribute. ::

            data = tablib.Dataset()
            data.tsv = 'age	first_name	last_name\n90	John	Adams'

        Import assumes (for now) that headers exist.
        Nr   r   r   r   r   Útsv  ó   zDataset.tsvc                   C   r¾   )aë  A YAML representation of the :class:`Dataset` object. If headers have been
        set, a YAML list of objects will be returned. If no headers have
        been set, a YAML list of lists (rows) will be returned instead.

        A dataset object can also be imported by setting the :class:`Dataset.yaml` attribute: ::

            data = tablib.Dataset()
            data.yaml = '- {age: 90, first_name: John, last_name: Adams}'

        Import assumes (for now) that headers exist.
        Nr   r   r   r   r   Úyaml.  rÅ   zDataset.yamlc                   C   r¾   )a4  A DataFrame representation of the :class:`Dataset` object.

        A dataset object can also be imported by setting the :class:`Dataset.df` attribute: ::

            data = tablib.Dataset()
            data.df = DataFrame(np.random.randn(6,4))

        Import assumes (for now) that headers exist.
        Nr   r   r   r   r   Údf=  rÁ   z
Dataset.dfc                   C   r¾   )aõ  A JSON representation of the :class:`Dataset` object. If headers have been
        set, a JSON list of objects will be returned. If no headers have
        been set, a JSON list of lists (rows) will be returned instead.

        A dataset object can also be imported by setting the :class:`Dataset.json` attribute: ::

            data = tablib.Dataset()
            data.json = '[{"age": 90, "first_name": "John", "last_name": "Adams"}]'

        Import assumes (for now) that headers exist.
        Nr   r   r   r   r   ÚjsonJ  rÅ   zDataset.jsonc                   C   r¾   )zÇA HTML table representation of the :class:`Dataset` object. If
        headers have been set, they will be used as table headers.

        ..notice:: This method can be used for export only.
        Nr   r   r   r   r   ÚhtmlY  ó   zDataset.htmlc                   C   r¾   )aº  A dBASE representation of the :class:`Dataset` object.

        A dataset object can also be imported by setting the
        :class:`Dataset.dbf` attribute. ::

            # To import data from an existing DBF file:
            data = tablib.Dataset()
            data.dbf = open('existing_table.dbf').read()

            # to import data from an ASCII-encoded bytestring:
            data = tablib.Dataset()
            data.dbf = '<bytestring of tabular data>'

        .. admonition:: Binary Warning

            :class:`Dataset.dbf` contains binary data, so make sure to write in binary mode::

                with open('output.dbf', 'wb') as f:
                    f.write(data.dbf)
        Nr   r   r   r   r   Údbfb  s   zDataset.dbfc                   C   r¾   )zÏA LaTeX booktabs representation of the :class:`Dataset` object. If a
        title has been set, it will be exported as the table caption.

        .. note:: This method can be used for export only.
        Nr   r   r   r   r   Úlatex{  rÊ   zDataset.latexc                 C   s$   |   |¡ | j |t||d�¡ dS )zùInserts a row to the :class:`Dataset` at the given index.

        Rows inserted must be the correct size (height or width).

        The default behaviour is to insert the given row to the :class:`Dataset`
        object at the given index.
       )r   N)rj   rU   r9   r
   )r   r?   r   r   r   r   r   r9   ‰  s   
	zDataset.insertc                 C   ó   | j | j||d� dS )zyAdds a row to the end of the :class:`Dataset`.
        See :class:`Dataset.insert` for additional documentation.
        ©r   r   N)r9   r^   r   r   r   r   r;   –  ó   zDataset.rpushc                 C   ó   | j d||d� dS )zyAdds a row to the top of the :class:`Dataset`.
        See :class:`Dataset.insert` for additional documentation.
        r   rÎ   Nr8   r   r   r   r   r<   ž  ó   zDataset.lpushc                 C   ó   |   ||¡ dS )znAdds a row to the :class:`Dataset`.
        See :class:`Dataset.insert` for additional documentation.
        Nr=   r   r   r   r   r>   ¦  ó   zDataset.appendc                 C   s   |D ]}|   ||¡ qdS )zZAdds a list of rows to the :class:`Dataset` using
        :class:`Dataset.append`
        N)r>   )r   Úrowsr   r   r   r   r   r   ­  s   ÿzDataset.extendc                 C   ó   | d }| d= |S )z:Removes and returns the first row of the :class:`Dataset`.r   r   ©r   Úcacher   r   r   Úlpop¶  ó   zDataset.lpopc                 C   rÕ   )ú9Removes and returns the last row of the :class:`Dataset`.éÿÿÿÿr   rÖ   r   r   r   Úrpop¿  rÙ   zDataset.rpopc                 C   r†   )rÚ   )rÜ   r   r   r   r   r¯   È  s   zDataset.popc                 C   sÆ   |du rg }t |dƒrtt|| jƒƒ}|  |¡}| j|d� | jr:|s&tƒ ‚|r3| jdkr3t	|ƒr3t
‚| j ||¡ | jrY| jrYt| jƒD ]\}}| ||| ¡ || j|< qEdS dd„ |D ƒ| _dS )a  Inserts a column to the :class:`Dataset` at the given index.

        Columns inserted must be the correct height.

        You can also insert a column of a single callable object, which will
        add a new column with the return values of the callable each as an
        item in the column. ::

            data.append_col(col=random.randint)

        If inserting a column, and :class:`Dataset.headers` is set, the
        header attribute must be set, and will be considered the header for
        that row.

        See :ref:`dyncols` for an in-depth example.

        .. versionchanged:: 0.9.0
           If inserting a column, and :class:`Dataset.headers` is set, the
           header attribute must be set, and will be considered the header for
           that row.

        .. versionadded:: 0.9.0
           If inserting a row, you can add :ref:`tags <tags>` to the row you are inserting.
           This gives you the ability to :class:`filter <Dataset.filter>` your
           :class:`Dataset` later.

        Nr®   ©r   r   c                 S   s   g | ]}t |gƒ‘qS r   rP   ra   r   r   r   rd     s    z&Dataset.insert_col.<locals>.<listcomp>)r°   r   rs   rU   r³   rj   rS   ÚHeadersNeededr^   r   r”   r9   r‘   rk   )r   r?   r   r²   r"   r   r   r   r   Ú
insert_colÒ  s$   

ýzDataset.insert_colc                 C   rÍ   )z|Adds a column to the end of the :class:`Dataset`.
        See :class:`Dataset.insert` for additional documentation.
        ©r²   N)rß   r‘   r±   r   r   r   Ú	rpush_col  rÏ   zDataset.rpush_colc                 C   rÐ   )z|Adds a column to the top of the :class:`Dataset`.
        See :class:`Dataset.insert` for additional documentation.
        r   rà   N)rß   r±   r   r   r   Ú	lpush_col  rÑ   zDataset.lpush_colrx   c                 C   s   ||f}| j  |¡ dS )z4Adds a separator to :class:`Dataset` at given index.N)rW   r>   )r   r?   ÚtextÚsepr   r   r   Úinsert_separator   s   zDataset.insert_separatorc                 C   s<   | j s| jr	| jnd}n
| jr| jd nd}|  ||¡ dS )z=Adds a :ref:`separator <separators>` to the :class:`Dataset`.r   rv   N)rS   r^   rå   )r   rã   r?   r   r   r   Úappend_separator'  s   zDataset.append_separatorc                 C   rÒ   )zuAdds a column to the :class:`Dataset`.
        See :class:`Dataset.insert_col` for additional documentation.
        N)rá   r±   r   r   r   Ú
append_col3  rÓ   zDataset.append_colc                    s   ‡ fdd„| j D ƒS )z@Returns the column from the :class:`Dataset` at the given index.c                    r`   r   r   ra   ©r?   r   r   rd   >  re   z#Dataset.get_col.<locals>.<listcomp>)rU   )r   r?   r   rè   r   Úget_col;  s   zDataset.get_colc                 C   sH   t |tƒr|| jv r| j |¡}nt‚|| jks"| j ||f¡ dS t‚)a  Adds a :ref:`formatter` to the :class:`Dataset`.

        .. versionadded:: 0.9.5
           :param col: column to. Accepts index int or header str.
           :param handler: reference to callback function to execute
           against each cell value.
        T)	rD   rE   rS   r?   rg   r‘   rX   r>   r›   )r   r   Úhandlerr   r   r   Úadd_formatterE  s   
	

þzDataset.add_formatterc                    s"   t | ƒ}‡ fdd„|jD ƒ|_|S )z…Returns a new instance of the :class:`Dataset`, excluding any rows
        that do not contain the given :ref:`tags <tags>`.
        c                    s   g | ]	}|  ˆ ¡r|‘qS r   )rI   ra   ©rH   r   r   rd   a  ru   z"Dataset.filter.<locals>.<listcomp>)r   rU   )r   rH   Ú_dsetr   rì   r   Úfilter\  s   zDataset.filterc                    sÞ   t |tƒs
t |tƒr8| jst‚t| jt|ƒ|d�}t| j| j	d�}|D ]‰ ‡ fdd„| jD ƒ}|j
|d� q#|S | jr@| j| }t| jt|ƒ|d�}t| j| j	d�}|D ]‰ | jrd‡ fdd„| jD ƒ}nˆ }|j
|d� qT|S )a  Sort a :class:`Dataset` by a specific column, given string (for
        header) or integer (for column index). The order can be reversed by
        setting ``reverse`` to ``True``.

        Returns a new :class:`Dataset` instance where columns have been
        sorted.
        )rh   Úreverse)rS   rT   c                    ó   g | ]}ˆ | ‘qS r   r   ©r   rh   ©r@   r   r   rd   x  re   z Dataset.sort.<locals>.<listcomp>©r   c                    rð   r   r   rñ   rò   r   r   rd   „  re   )rD   rE   r   rS   rÞ   Úsortedr+   r   rO   rT   r>   )r   r   rï   Ú_sortedrí   r   r   rò   r   Úsortf  s&   	ò
zDataset.sortc                 C   sz   | sdS t ƒ }| jd g| | jd   }||_t| jƒD ]\}}|| jd kr(q|g|  |¡ }t|ƒ}|j|d� q|S )zÀTranspose a :class:`Dataset`, turning rows into columns and vice
        versa, returning a new ``Dataset`` instance. The first row of the
        original instance becomes the new header row.Nr   ró   )rO   rS   rk   ré   r
   r>   )r   rí   Únew_headersr?   ÚcolumnÚrow_datar   r   r   Ú	transpose�  s   zDataset.transposec                 C   sZ   t |tƒsdS | j|jkrt‚t| ƒ}dd„ |jD ƒ}dd„ |jD ƒ}| |¡ ||_|S )zˆStack two :class:`Dataset` instances together by
        joining at the row level, and return new combined
        ``Dataset`` instance.Nc                 S   ó   g | ]}|‘qS r   r   ra   r   r   r   rd   ¸  ó    z!Dataset.stack.<locals>.<listcomp>c                 S   rû   r   r   ra   r   r   r   rd   ¹  rü   )rD   rO   r‘   r”   r   rU   r   )r   Úotherrí   Úrows_to_stackÚ
other_rowsr   r   r   Ústackª  s   

zDataset.stackc                 C   sª   t |tƒsdS | js|jr| jr|jst‚| j|jkrt‚z| j|j }W n ty0   d}Y nw tƒ }| jD ]
}|j| | d� q7|jD ]
}|j|| d� qE||_|S )zÙStack two :class:`Dataset` instances together by
        joining at the column level, and return a new
        combined ``Dataset`` instance. If either ``Dataset``
        has headers set, than the other must as well.NrÝ   )rD   rO   rS   rÞ   r^   r”   r¤   rç   )r   rý   r÷   rí   rø   r   r   r   Ú
stack_colsÁ  s&   
ÿ

zDataset.stack_colsc                    s(   t ƒ ‰ ‡ fdd„| jD ƒ| jdd…< dS )ziRemoves all duplicate rows from the :class:`Dataset` object
        while maintaining the original order.c                    s*   g | ]}t |ƒˆ v sˆ  t |ƒ¡s|‘qS r   )rB   Úaddra   ©Úseenr   r   rd   ç  s   * z-Dataset.remove_duplicates.<locals>.<listcomp>N)rG   rU   r   r   r  r   Úremove_duplicatesã  s   "zDataset.remove_duplicatesc                 C   s   t ƒ | _d| _dS )zARemoves all content and headers from the :class:`Dataset` object.N)r   rU   rV   r   r   r   r   r¨   ê  s   
zDataset.wipec           	         sÒ   ˆ sdS |du rt tˆ jƒƒ}|du rt ˆ jƒ}‡ fdd„|D ƒ}‡ fdd„|D ƒ}tƒ }t |ƒ|_g |_tˆ jƒD ],\}}g }|jD ]}|ˆ jv rXˆ j |¡}| || ¡ qCt	‚||v rf|jt
|ƒd� q:|S )zkReturns a new instance of the :class:`Dataset`,
        including only specified rows and columns.
        Nc                    s   g | ]}|t ˆ jƒv r|‘qS r   )Úranger^   ra   r   r   r   rd      s    z"Dataset.subset.<locals>.<listcomp>c                    s   g | ]	}|ˆ j v r|‘qS r   )rS   )r   r²   r   r   r   rd     ru   ró   )r   r  r^   rS   rO   rU   rk   r?   r>   rg   r
   )	r   rÔ   Úcolsrí   Úrow_nor   r—   rh   rc   r   r   r   Úsubsetð  s,   



€zDataset.subset)NNF)TTr   r‰   rw   )F)ArJ   rK   rL   rM   r�   r   r   r'   r)   r*   r!   r…   r‡   ÚclassmethodrZ   rj   r¢   r£   r¦   rN   rS   r§   r­   r+   r³   r^   r‘   r·   r¼   r¿   rÀ   rÂ   rÃ   rÄ   rÆ   rÇ   rÈ   rÉ   rË   rÌ   r   r9   r;   r<   r>   r   rØ   rÜ   r¯   rß   rá   râ   rå   ræ   rç   ré   rë   rî   rö   rú   r   r  r  r¨   r	  r   r   r   r   rO   n   s”    .


%	















			


>






'"rO   c                   @   sp   e Zd ZdZi Zddd„Zdd„ Zdd„ Zed	d
„ ƒZ	dd„ Z
dd„ Zddd„Zedd„ ƒZdd„ Zdd„ ZdS )ÚDatabookz(A book of :class:`Dataset` objects.
    Nc                 C   s$   |d u r	t ƒ | _n|| _|  ¡  d S r   )r   Ú	_datasetsrZ   )r   Úsetsr   r   r   r     s   
zDatabook.__init__c                 C   rl   )Nz<%s databook>z<databook object>rm   r   r   r   r   r!   (  rp   zDatabook.__repr__c                 C   s
   g | _ dS )z@Removes all :class:`Dataset` objects from the :class:`Databook`.N©r  r   r   r   r   r¨   .  s   
zDatabook.wipec              	   C   sœ   t jD ]H}z7zt| |jt|j|jƒƒ |j|jf| j|j< W n ty:   t| |jt|jƒƒ |jdf| j|j< Y nw W q tyK   d| j|j< Y qw dS )rˆ   Nr‰   )	r   rŠ   r3   rT   rN   Úexport_bookÚimport_bookr�   ro   rŽ   r   r   r   rZ   3  s   
þ€ÿ÷zDatabook._register_formatsc                 C   r]   r   r  r   r   r   r   ÚsheetsB  r_   zDatabook.sheetsc                 C   s   t |tƒr| j |¡ dS t‚)z5Adds given :class:`Dataset` to the :class:`Databook`.N)rD   rO   r  r>   ÚInvalidDatasetType)r   Údatasetr   r   r   Ú	add_sheetE  s   
zDatabook.add_sheetTc                 C   s>   g }|rt }nt}| jD ]}| ||j|j|d�d�¡ q|S )z(Packages :class:`Databook` for delivery.)r�   )rT   r¡   )r   r+   r  r>   rT   r¢   )r   r�   Ú	collectorr™   Údsetr   r   r   r¢   M  s   


þzDatabook._packagec                 C   r   )zDThe number of the :class:`Dataset` objects within :class:`Databook`.)r   r  r   r   r   r   Úsize^  rC   zDatabook.sizec                 K   sF   |st |ƒ}| j |d¡\}}|std |¡ƒ‚|| |fi |¤Ž | S )z±
        Import `in_stream` to the :class:`Databook` object using the `format`.

        :param \*\*kwargs: (optional) custom configuration to the format `import_book`.
        r‰   zFormat {0} cannot be loaded.r´   )r   r|   r¶   r\   r  r  r   r   r   r·   c  r¸   zDatabook.loadc                 K   r¹   )z—
        Export :class:`Databook` object to `format`.

        :param \*\*kwargs: (optional) custom configuration to the format `export_book`.
        r‰   rº   r»   )r   r|   r\   r  r  r   r   r   r¼   t  r½   zDatabook.exportr   )T)rJ   rK   rL   rM   r�   r   r!   r¨   r
  rZ   r  r  r¢   rN   r  r·   r¼   r   r   r   r   r    s    
	


r  c              	   C   s<   t jD ]}z| | ¡r|jW   S W q ty   Y qw dS )z#Return format name of given stream.N)r   rŠ   ÚdetectrT   ro   )Ústreamr�   r   r   r   rµ   �  s   

ÿÿürµ   Nc                 K   ó   t ƒ j| |fi |¤ŽS ©zReturn dataset of given stream.)rO   r·   ©r  r|   r\   r   r   r   rŒ   Š  ó   rŒ   c                 K   r  r  )r  r·   r  r   r   r   r  �  r  r  c                   @   ó   e Zd ZdZdS )r  z(Only Datasets can be added to a DataBookN©rJ   rK   rL   rM   r   r   r   r   r  –  ó    r  c                   @   r  )r”   zInvalid sizeNr  r   r   r   r   r”   š  r   r”   c                   @   r  )r›   zOutside of Dataset sizeNr  r   r   r   r   r›   �  r   r›   c                   @   r  )rÞ   zGHeader parameter must be given when appending a column in this Dataset.Nr  r   r   r   r   rÞ      r   rÞ   c                   @   r  )r«   zFormat is not supportedNr  r   r   r   r   r«   £  r   r«   r   )rM   r   Úoperatorr   r   r   Útablib.compatr   r   Ú	__title__Ú__version__Ú	__build__Ú
__author__Ú__license__Ú__copyright__Ú__docformat__Úobjectr
   rO   r  rµ   rŒ   r  Ú	Exceptionr  r”   r›   rÞ   ÚNotImplementedErrorr«   r   r   r   r   Ú<module>   s<   
Q       2h
	
