U
    ꥡco                     @   s"  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ e
ZG d	d
 d
ee ZG dd deZejd)ddZejd*ddZejd+ddZejd,ddZejdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Z d'd( ZdS )-    N)curried)Callable   )sanitize_dataframe)sanitize_geo_interface)AltairDeprecationWarning)PluginRegistryc                   @   s.   e Zd ZddiZedd Zejdd ZdS )DataTransformerRegistryconsolidate_datasetsTc                 C   s
   | j d S Nr
   _global_settings)self r   5/tmp/pip-unpacked-wheel-y9_o96ar/altair/utils/data.pyr
      s    z,DataTransformerRegistry.consolidate_datasetsc                 C   s   || j d< d S r   r   )r   valuer   r   r   r
      s    N)__name__
__module____qualname__r   propertyr
   setterr   r   r   r   r	      s
   
r	   c                   @   s   e Zd ZdZdS )MaxRowsErrorz+Raised when a data model has too many rows.N)r   r   r   __doc__r   r   r   r   r   6   s   r     c                 C   s   t |  t| dr4| jd dkr,| jd }qf| j}n2t| tjrF| }n t| trfd| krb| d }n| S |dk	rt||krtd	|| S )zzRaise MaxRowsError if the data model has more than max_rows.

    If max_rows is None, then do not perform any check.
    __geo_interface__typeZFeatureCollectionfeaturesvaluesNzThe number of rows in your dataset is greater than the maximum allowed ({}). For information on how to plot larger datasets in Altair, see the documentation)
check_data_typehasattrr   
isinstancepd	DataFramedictlenr   format)dataZmax_rowsr   r   r   r   
limit_rows<   s$    


r'   c                 C   sl   t |  t| tjr"| j||dS t| trhd| krh| d }|rD|nt|t| }t||}d|iS dS )zBReduce the size of the data model by sampling without replacement.)nfracr   N)	r   r    r!   r"   sampler#   intr$   random)r&   r(   r)   r   r   r   r   r*   Y   s    
r*   altair-datajson{prefix}-{hash}.{extension} c              	   C   sX   t | }t|}|j|||d}t|d}|| W 5 Q R X tj||ddidS )zQ
    Write the data model to a .json file and return a url based data model.
    prefixhash	extensionwr   r.   urlr%   )_data_to_json_string_compute_data_hashr%   openwriteospathjoin)r&   r2   r4   filenameurlpathZ	data_json	data_hashfr   r   r   to_jsong   s    rC   csvc              	   C   sX   t | }t|}|j|||d}t|d}|| W 5 Q R X tj||ddidS )zFWrite the data model to a .csv file and return a url based data model.r1   r5   r   rD   r6   )_data_to_csv_stringr9   r%   r:   r;   r<   r=   r>   )r&   r2   r4   r?   r@   Zdata_csvrA   rB   r   r   r   to_csvz   s    	rF   c                 C   s~   t |  t| dr8t| tjr&t| } t| j} d| iS t| tjr\t| } d| jddiS t| t	rzd| krvt
d| S dS )z0Replace a DataFrame by a data model with values.r   r   records)orient.values expected in data dict, but not present.N)r   r   r    r!   r"   r   r   r   Zto_dictr#   KeyErrorr&   r   r   r   	to_values   s    


rL   c                 C   s0   t | ttjfs,t| ds,tdt| dS )z-Raise if the data is not a dict or DataFrame.r   zBExpected dict, DataFrame or a __geo_interface__ attribute, got: {}N)r    r#   r!   r"   r   	TypeErrorr%   r   rK   r   r   r   r      s     r   c                 C   s   t |   S )N)hashlibmd5encode	hexdigest)Zdata_strr   r   r   r9      s    r9   c                 C   s   t |  t| dr:t| tjr&t| } t| j} t	| S t| tjr\t| } | j
dddS t| trd| krvtdtj	| d ddS td	d
S )z5Return a JSON string representation of the input datar   rG      )rH   Zdouble_precisionr   rI   T)	sort_keyszBto_json only works with data expressed as a DataFrame or as a dictN)r   r   r    r!   r"   r   r   r   r.   dumpsrC   r#   rJ   NotImplementedErrorrK   r   r   r   r8      s     



r8   c                 C   sz   t |  t| drtdnZt| tjr<t| } | jddS t| trnd| krVt	dtj
| d jddS tddS )	z4return a CSV string representation of the input datar   zLto_csv does not work with data that contains the __geo_interface__ attributeF)indexr   z-values expected in data dict, but not presentzAto_csv only works with data expressed as a DataFrame or as a dictN)r   r   rU   r    r!   r"   r   rF   r#   rJ   	from_dictrK   r   r   r   rE      s    

rE   c                 G   s   t dt tj| f| S )zi
    Pipe a value through a sequence of functions

    Deprecated: use toolz.curried.pipe() instead.
    zdalt.pipe() is deprecated, and will be removed in a future release. Use toolz.curried.pipe() instead.)warningswarnr   r   pipe)r&   funcsr   r   r   rZ      s
    rZ   c                  O   s   t dt tj| |S )zRCurry a callable function

    Deprecated: use toolz.curried.curry() instead.
    zfalt.curry() is deprecated, and will be removed in a future release. Use toolz.curried.curry() instead.)rX   rY   r   r   curry)argskwargsr   r   r   r\      s
    r\   )r   )NN)r-   r.   r/   r0   )r-   rD   r/   r0   )!r.   r<   r,   rN   rX   Zpandasr!   Ztoolzr   typingr   corer   r   deprecationr   Zplugin_registryr   ZDataTransformerTyper	   	Exceptionr   r\   r'   r*   rC   rF   rL   r   r9   r8   rE   rZ   r   r   r   r   <module>   sL           
