
                         x    S r SSKrSSKrSSKr " S S\5      r " S S\5      r " S S\5      r " S	 S
\5      r	g)zDataflow-related utilities.
    Nc                       \ rS rSrSrSrg)DecodeError   zBase decode error. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       1lib/third_party/ml_sdk/cloud/ml/util/_decoders.pyr   r      s    r   r   c                       \ rS rSrS rSrg)PassthroughDecoder   c                     U$ Nr   selfxs     r   decodePassthroughDecoder.decode   s    Hr   r   N)r   r   r	   r
   r   r   r   r   r   r   r      s    r   r   c                       \ rS rSrSrS rSrg)JsonDecoder!   z"A decoder for JSON formatted data.c                 .    [         R                  " U5      $ r   )jsonloadsr   s     r   r   JsonDecoder.decode$   s    ::a=r   r   N)r   r   r	   r
   r   r   r   r   r   r   r   r   !   s
    *r   r   c                   \    \ rS rSrSr " S S\5      r " S S\5      rS rS r	S	 r
S
 rSrg)
CsvDecoder(   z$A decoder for CSV formatted data.
  c                   0    \ rS rSrSrS rS rS rS rSr	g)	CsvDecoder._LineGenerator.   zCA csv line generator that allows feeding lines to a csv.DictReader.c                     / U l         g r   _linesr   s    r   __init__"CsvDecoder._LineGenerator.__init__1   s	    dkr   c                 `    U R                   (       a   eU R                   R                  U5        g r   )r(   append)r   lines     r   	push_line#CsvDecoder._LineGenerator.push_line4   s     _
kkr   c                     U $ r   r   r)   s    r   __iter__"CsvDecoder._LineGenerator.__iter__9   s    kr   c                     [        U R                  5      nUS:X  a  [        S5      eUS:X  d
   SU-  5       eU R                  R                  5       $ )Nr   z@Columns do not match specified csv headers: empty line was found   zUnexpected number of lines %s)lenr(   r   pop)r   line_lengths     r   nextCsvDecoder._LineGenerator.next<   sX     $k		NP 	PAL>LL [[__r   r'   N)
r   r   r	   r
   r   r*   r/   r2   r9   r   r   r   r   _LineGeneratorr$   .   s    M
r   r;   c                   0    \ rS rSrSrS rS rS rS rSr	g)	CsvDecoder._ReaderWrapperJ   z?A wrapper for csv.reader / csv.DictReader to make it picklable.c                     XX4U4U l         Xl        U(       a%  [        R                  " X[	        U5      US9U l        g [        R                  " U[	        U5      US9U l        g )N)	delimiterskipinitialspace)_state_line_generatorcsv
DictReaderstr_readerreader)r   line_generatorcolumn_namesr@   decode_to_dictskip_initial_spaces         r   r*   "CsvDecoder._ReaderWrapper.__init__M   sV    #9')dk+	~~C	N/1 zz.C	N3EGr   c                 l    U R                   R                  U5        U R                  R                  5       $ r   )rC   r/   rG   r9   r   s     r   read_record%CsvDecoder._ReaderWrapper.read_recordZ   s)    
$$Q'\\  r   c                     U R                   $ r   )rB   r)   s    r   __getstate__&CsvDecoder._ReaderWrapper.__getstate__^   s    [[r   c                 "    U R                   " U6   g r   )r*   )r   states     r   __setstate__&CsvDecoder._ReaderWrapper.__setstate__a   s    
mmUr   )rC   rG   rB   N)
r   r   r	   r
   r   r*   rO   rR   rV   r   r   r   r   _ReaderWrapperr=   J   s    IG!r   rX   c                     Xl         [        U5      U l        U R                  U R	                  5       XUU5      U l        X@l        XPl        g)aQ  Initializer.

Args:
  column_names: Tuple of strings. Order must match the order in the file.
  numeric_column_names: Tuple of strings. Contains column names that are
      numeric. Every name in numeric_column_names must also be in
      column_names.
  delimiter:  String used to separate fields.
  decode_to_dict: Boolean indicating whether the docoder should generate a
      dictionary instead of a raw sequence. True by default.
  fail_on_error: Whether to fail if a corrupt row is found.
  skip_initial_space: When True, whitespace immediately following the
      delimiter is ignored.
N)_column_namesset_numeric_column_namesrX   r;   rG   _decode_to_dict_fail_on_error)r   rJ   numeric_column_namesr@   rK   fail_on_errorrL   s          r   r*   CsvDecoder.__init__d   sK    " &!$%9!:D&&|DL *'r   c                 j    U R                   (       a  [        U5      e[        R                  " SU5        g)aF  Handle corrupt rows.

Depending on whether the decoder is configured to fail on error it will
raise a DecodeError or return None.

Args:
  message: String, the error message to raise.
Returns:
  None, when the decoder is not configured to fail on error.
Raises:
  DecodeError: when the decoder is configured to fail on error.
zDiscarding invalid row: %sN)r^   r   loggingwarning)r   messages     r   _handle_corrupt_rowCsvDecoder._handle_corrupt_row}   s,        oo2G<r   c                 t    U(       a  UR                  5       (       d  g XR                  ;   a  [        U5      $ U$ r   )stripr\   float)r   column_namevalues      r   
_get_valueCsvDecoder._get_value   s/     0005\Lr   c                 x    U R                   R                  U5      n[	        U5      [	        U R
                  5      :w  a#  U R                  SU R
                  < SU< 35      $ U R                  (       ac  XR
                  S      c#  U R                  SU R
                  < SU< 35      $ UR                  5        H  u  p4U R                  X45      X'   M     U$ [        U R
                  5       H  u  pSX   nU R                  X45      X'   M     U$ ! [         a"  nU R                  U< SU< 35      s SnA$ SnAff = f)aT  Decodes the given string.

Args:
  record: String to be decoded.

Returns:
  Serialized object corresponding to decoded string. Or None if there's an
  error and the decoder is configured not to fail on error.

Raises:
  DecodeError: If columns do not match specified csv headers.
  ValueError: If some numeric column has non-numeric data.
z: Nz,Columns do not match specified csv headers: z -> )
rG   rO   	Exceptionrf   r6   rZ   r]   	iteritemsrm   	enumerate)r   recordenamerl   indexs         r   r   CsvDecoder.decode   s(   >||''/f 6{c$,,--%%  &*+ +  
""2&	'	/''""F,- 	-  ))++$t3 , M #4#5#56+%4 7 M-  >%%!V&<==>s   D 
D9D4.D94D9)rZ   r]   r^   r\   rG   N)r   r   r	   r
   r   objectr;   rX   r*   rf   rm   r   r   r   r   r   r!   r!   (   s3    
v 8v 4(2(&r   r!   )
r   rD   r   rc   rq   r   ry   r   r   r!   r   r   r   <module>rz      sG      ) 
 & [ [r   