
    d)                        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
ee                   Z G d de          Zej        dd            Zej        d d            Zej        	 	 	 	 d!d            Zej        	 	 	 	 d"d            Zej        d             Zd Zd Zd Zd Zd Z d Zd Z!dS )#    N)curried)Callable   )sanitize_dataframe)sanitize_geo_interface)AltairDeprecationWarning)PluginRegistryc                   L    e Zd ZddiZed             Zej        d             ZdS )DataTransformerRegistryconsolidate_datasetsTc                     | j         d         S Nr   _global_settings)selfs    1lib/python3.11/site-packages/altair/utils/data.pyr   z,DataTransformerRegistry.consolidate_datasets   s    $%;<<    c                     || j         d<   d S r   r   )r   values     r   r   z,DataTransformerRegistry.consolidate_datasets   s    8=4555r   N)__name__
__module____qualname__r   propertyr   setter r   r   r   r      sW        .5= = X=  > > ! > > >r   r   c                       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        55Dr   r     c                    t          |            t          | d          r'| j        d         dk    r| j        d         }n[| j        }nSt          | t          j                  r| }n6t          | t                    rd| v r	| d         }n| S t          | d          r| }|&t          |          |k    rt          d| 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__typeFeatureCollectionfeaturesvalues__dataframe__NzHThe number of rows in your dataset is greater than the maximum allowed (z).

See https://altair-viz.github.io/user_guide/large_datasets.html for information on how to plot large datasets, including how to install third-party data management tools and, in the right circumstance, disable the restriction)	check_data_typehasattrr!   
isinstancepd	DataFramedictlenr   )datamax_rowsr%   s      r   
limit_rowsr0   <   s     Dt()) !&)-@@@+J7FF+FF	D",	'	' 	D$		 t(^FFK		'	' Fh 6 6A)1A A A
 
 	
 Kr   c                 x   t          |            t          | t          j                  r|                     ||          S t          | t
                    rJd| v rD| d         }|r|nt          |t          |          z            }t          j        ||          }d|iS dS t          | d          rt                      }|                    |           }|r|nt          |t          |          z            }t          j        t          t          |                    |          }|                    |          S dS )zBReduce the size of the data model by sampling without replacement.)nfracr%   r&   N)r'   r)   r*   r+   sampler,   intr-   randomr(   import_pyarrow_interchangefrom_dataframerangetake)r.   r2   r3   r%   pipa_tableindicess          r   r4   r4   ]   s5    D$%% &{{QT{***	D$		 &t(^F3Cs6{{ 233A]61--Ff%%	 
 
	'	' &'))$$T**1AAD3x==011-c(mm 4 4a88}}W%%%& &r   altair-datajson{prefix}-{hash}.{extension} c                 2   t          |           }t          |          }|                    |||          }t          |d          5 }|                    |           ddd           n# 1 swxY w Y   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wNr"   r?   urlformat)_data_to_json_string_compute_data_hashrJ   openwriteospathjoin)r.   rD   rF   filenameurlpath	data_json	data_hashfs           r   to_jsonrW   r   s     %T**I"9--If9	RRH	h		 		              7<<22vv>NOOO   A))A-0A-csvc                 2   t          |           }t          |          }|                    |||          }t          |d          5 }|                    |           ddd           n# 1 swxY w Y   t
          j                            ||          ddidS )zFWrite the data model to a .csv file and return a url based data model.rC   rG   Nr"   rY   rH   )_data_to_csv_stringrL   rJ   rM   rN   rO   rP   rQ   )r.   rD   rF   rR   rS   data_csvrU   rV   s           r   to_csvr]      s     #4((H"8,,If9	RRH	h		 	              7<<22vuoNNNrX   c                 .   t          |            t          | d          rAt          | t          j                  rt          |           } t          | j                  } d| iS t          | t          j                  r't          |           } d|                     d          iS t          | t                    rd| vrt          d          | S t          | d          r9t                      }|                    |           }d|                                iS dS )z0Replace a DataFrame by a data model with values.r!   r%   records)orient.values expected in data dict, but not present.r&   N)r'   r(   r)   r*   r+   r   r   r!   to_dictr,   KeyErrorr7   r8   	to_pylistr.   r;   r<   s      r   	to_valuesrf      s    Dt()) 0dBL)) 	,%d++D%d&<==$	D",	'	' 0!$''$,,i,8899	D$		 04KLLL		'	' 0'))$$T**(,,..//	0 0r   c                      t           t          t          j        f          sJt	           fddD                       s1t          d                    t                                         dS dS )z-Raise if the data is not a dict or DataFrame.c              3   8   K   | ]}t          |          V  d S N)r(   ).0attrr.   s     r   	<genexpr>z"check_data_type.<locals>.<genexpr>   s>       > > $d> > > > > >r   )r!   r&   zBExpected dict, DataFrame or a __geo_interface__ attribute, got: {}N)r)   r,   r*   r+   any	TypeErrorrJ   r"   )r.   s   `r   r'   r'      s    dT2<011 
# > > > >(N> > > ; ; 
 PWWT

 
 
 	

 
 
 
r   c                 r    t          j        |                                                                           S ri   )hashlibmd5encode	hexdigest)data_strs    r   rL   rL      s(    ;x(())33555r   c                    t          |            t          | d          rQt          | t          j                  rt          |           } t          | j                  } t          j	        |           S t          | t          j                  r&t          |           } | 
                    dd          S t          | t                    r/d| vrt          d          t          j	        | d         d          S t          | d	          rIt                      }|                    |           }t          j	        |                                          S t!          d
          )z5Return a JSON string representation of the input datar!   r_      )r`   double_precisionr%   ra   T)	sort_keysr&   zBto_json only works with data expressed as a DataFrame or as a dict)r'   r(   r)   r*   r+   r   r   r!   r?   dumpsrW   r,   rc   r7   r8   rd   NotImplementedErrorre   s      r   rK   rK      sA   Dt()) 
dBL)) 	,%d++D%d&<==z$	D",	'	' 
!$''||9r|BBB	D$		 
4KLLLz$x.D9999		'	' 
'))$$T**z(,,..///!S
 
 	
r   c                    t          |            t          | d          rt          d          t          | t          j                  r%t          |           } |                     d          S t          | t                    rLd| vrt          d          t          j        
                    | d                                       d          S t          | d          rt                      }dd	l}dd	lm} |                    |           }|                                }|                    ||           |                                                                                                S t          d
          )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 presentr&   r   NzAto_csv only works with data expressed as a DataFrame or as a dict)r'   r(   rz   r)   r*   r+   r   r]   r,   rc   	from_dictr7   pyarrowpyarrow.csvrY   r8   BufferOutputStream	write_csvgetvalue
to_pybytesdecode)r.   r;   papa_csvr<   
csv_buffers         r   r[   r[      s|   Dt()) 
!7
 
 	
 
D",	'	' 
!$''{{{'''	D$		 
4JKKK|%%d8n55<<5<III		'	' 
'))$$$$$$$$T****,,
:...""$$//1188:::!R
 
 	
r   c                 ^    t          j        dt          d           t          j        | g|R  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.r   
stacklevel)warningswarnr   r   pipe)r.   funcss     r   r   r      sB     M	, 	    <%u%%%%r   c                  \    t          j        dt          d           t          j        | i |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.r   r   )r   r   r   r   curry)argskwargss     r   r   r   
  s?    
 M	- 	    =$)&)))r   c                      dd l } 	 |                     d           dd lm} |S # | j        $ r}t          d          |d }~w| j        $ r}t          d          |d }~ww xY w)Nr   zpyarrow>=11.0.0zdUsage of the DataFrame Interchange Protocol requires the package 'pyarrow', but it is not installed.zThe installed version of 'pyarrow' does not meet the minimum requirement of version 11.0.0. Please update 'pyarrow' to use the DataFrame Interchange Protocol.)pkg_resourcesrequirepyarrow.interchangeinterchangeDistributionNotFoundImportErrorVersionConflict)r   r;   errs      r   r7   r7     s    /000((((((	-   r
 
	 (   Q
 
 	s   # 
A=A
AA)r   )NN)r>   r?   r@   rA   )r>   rY   r@   rA   )"r?   rO   r6   rp   r   pandasr*   toolzr   typingr   corer   r   deprecationr   plugin_registryr	   DataTransformerTyper   	Exceptionr   r   r0   r4   rW   r]   rf   r'   rL   rK   r[   r   r7   r   r   r   <module>r      sI    				                    $ $ $ $ $ $ ( ( ( ( ( ( 1 1 1 1 1 1 + + + + + +  	> 	> 	> 	> 	>n-@A 	> 	> 	>>	 	 	 	 	9 	 	 	 	   @ 	& & & &( 	 *P P P P$ 	 *O O O O  	0 0 0,	
 	
 	
"6 6 6
 
 
4
 
 
>& & &* * *    r   