
    LMh                         U d dl Z d dlZd dlmZmZmZmZ d dlZd dl	m
Z
mZ dededej        fdZi dej        d	ej        d
ej        dej        dej        dej        dededededededej        dej        dej        dej        dej        Zeeef         ed<   ej        ej        ej        ej        ej        dZeeef         ed<   d	ddiiZeeeeeeef         f         f         ed<   dZe
j         dd d!eded"e
d#edee         deej        eee!ef         ej        f         f         fd$Z"d%edee         d"e
de#ee
f         fd&Z$d'ededee         de%fd(Z&d%ed#edefd)Z'dS )*    N)AnyCallableOptionalUnion)PolarsFrameTypedataset_download	sql_querypathreturnc                     t          j        |          5 }t          j        | |          }|cd d d            S # 1 swxY w Y   d S N)sqlite3connectplread_database)r	   r
   conndfs       Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/kagglehub/polars_datasets.pywrapped_read_databaser      s    			 $i..                 s   9= =.csv.tsvz.json.jsonl.parquet.featherz.sqlitez.sqlite3z.dbz.db3z.s3dbz.dl3z.xlsz.xlsxz.xlsbz.xlsmz.ods%SUPPORTED_READ_FUNCTIONS_BY_EXTENSION)r   r   r   r   r   %SUPPORTED_SCAN_FUNCTIONS_BY_EXTENSION	separator	STATIC_KWARGS_BY_EXTENSIONz/Loading from a SQLite file requires a SQL query)polars_frame_typepolars_kwargshandler    r!   c                b   |i n|}t           j                            |          d         }t          |||          \  }}t	          | |          }	  |t          |||          i t          ||          }	n'# t          $ r}
d|
 }t          |          |
d}
~
ww xY w|t          j
        u r|t          j        u rut          |	t                    rL|	                                D ]6\  }}t          |t          j                  r|                                |	|<   7n|	                                }	|	S )a  Creates polars LazyFrame(s) or DataFrame(s) from a file in the dataset

    Args:
        handle: (string) The dataset handle
        path: (string) Path to a file within the dataset
        polars_frame_type:
            (PolarsFrameType) Optional control for which Frame to return: LazyFrame or DataFrame. The default is
            PolarsFrameType.LAZY_FRAME.

            PolarsFrameType.LAZY_FRAME: We attempt to use a scan_* method if it's available for the provided file
            extension. Otherwise, we use a read_* method to produce a DataFrame and return the result after calling
            .lazy() on it. This satisfies the requested polars_frame_type as a LazyFrame, but does require loading the
            file in memory.

            PolarsFrameType.DATA_FRAME: We use whatever read_* method corresponds to the provided file extension and
            return the resulting DatFrame.
        polars_kwargs:
            (dict) Optional set of kwargs to pass to the polars `read_*` method while constructing the DataFrame(s)
        sql_query:
            (string) Argument to be used for SQLite files. Required when reading a SQLite file. See polars documentation
            for details: https://docs.pola.rs/api/python/stable/reference/api/polars.read_database.html

    Returns:
        - dict[int | str, LazyFrame] or dict[int | str, DataFrame] for Excel-like files with multiple sheets
        - A polars LazyFrame or DataFrame for all others

    Raises:
        ValueError: If the file extension is not supported or the file fails to read
    N   zError reading file: )osr
   splitext_validate_io_functionr   _build_args_build_kwargs	Exception
ValueErrorr   
DATA_FRAME
LAZY_FRAME
isinstancedictitemsr   	DataFramelazy)r"   r
   r    r!   r	   file_extensionio_functionio_frame_typefilepathresulteread_error_messagekeyvalues                 r   load_polars_datasetr<   H   sZ   J (/BB]MW%%d++A.N!6~yRc!d!dK  --H4h	::
NM::
 
  4 4 47A77+,,!34 2227HOLf7f7ffd## 	#$llnn / /
U eR\22 /"'**,,F3K	/ [[]]FMs   %A7 7
BBBr3   c                 l   | t           vrBd|  dd                    t                                                      }t          |          d t           |          }|t          u r|st          t
                    |t          j        u s	| t          vr|t          j        fS t          |          t          j	        fS )NzUnsupported file extension: 'z"'. Supported file extensions are: z, )
r   joinkeysr+   r   MISSING_SQL_QUERY_ERROR_MESSAGEr   r,   r   r-   )r3   r	   r    extension_error_messageread_functions        r   r'   r'      s     BBBhN h h.2ii8]8b8b8d8d.e.eh h 	  011t;9.IM---i-8999 O666.Pu:u:u9::1.A?C]^^    rB   c                 &    | t           k    r|gn||gS r   )r   )rB   r
   r	   s      r   r(   r(      s    "&;;;D66)TARRrC   c                 >    | t           vri nt           |          }i ||S r   )r   )r3   r!   static_kwargss      r   r)   r)      s2    $>>>D^_mDn 
 .m-}--rC   )(r%   r   typingr   r   r   r   polarsr   kagglehub.datasetsr   r   strr1   r   read_csv	read_jsonread_ndjsonread_parquetread_ipc
read_excelr   r/   __annotations__scan_csvscan_ndjsonscan_parquetscan_ipcr   r   boolr@   r-   intr<   tupler'   listr(   r)    rC   r   <module>r[      sf   					  1 1 1 1 1 1 1 1 1 1 1 1     @ @ @ @ @ @ @ @
S      >
BK>
BK> R\> bn	>
 > > $> %> 
 > !> "> !>$ BM%>& R]'>( R])>. R]/>2 BM3> %tCM':   B KKn> > %tCM':    GM{\`NaEb Dd3c4i0@+@&A!AB b b b"S  *9)C@ @ @@
@ '	@
 @ }@ 2<eCHor|;<<=@ @ @ @F__$,SM_FU_
8_$%_ _ _ _*Sx Ss Sx} SQU S S S S
.# .c .d . . . . . .rC   