mò
­fIc           @   sŽ  d  Z  d k Z d k l Z l Z l Z l Z l Z l Z l	 Z	 l
 Z
 l Z l Z l Z l Z l  Z  y d k l Z Wn  e j
 o d k l Z n Xd d d d d d	 d
 d d d d d d d d d d d g Z d	 f  d „  ƒ  YZ d
 e f d „  ƒ  YZ e d
 e ƒ d e f d „  ƒ  YZ e d e ƒ d f  d „  ƒ  YZ d f  d „  ƒ  YZ y e Wn e j
 o e Z n Xd f  d „  ƒ  YZ d S(   s+   
csv.py - read/write/investigate CSV files
N(   s   Errors   __version__s   writers   readers   register_dialects   unregister_dialects   get_dialects   list_dialectss   QUOTE_MINIMALs	   QUOTE_ALLs   QUOTE_NONNUMERICs
   QUOTE_NONEs   __doc__(   s   StringIOt   QUOTE_MINIMALt	   QUOTE_ALLt   QUOTE_NONNUMERICt
   QUOTE_NONEt   Errort   Dialectt   excelt	   excel_tabt   readert   writert   register_dialectt   get_dialectt   list_dialectst   Sniffert   unregister_dialectt   __version__t
   DictReadert
   DictWriterc           B   sP   t  Z d Z e Z d  Z d  Z d  Z d  Z	 d  Z
 d  Z d  Z d „  Z d „  Z RS(   Nt    c         C   sT   |  i t j o t |  _ n |  i ƒ  } | g  j o t d d i | ƒ ‚ n d  S(   Ns   Dialect did not validate: %ss   , (	   t   selft	   __class__R   t   Truet   _validt	   _validatet   errorsR   t   join(   R   R   (    (    t    /mit/python/lib/python2.4/csv.pyt   __init__"   s
    c         C   sú  g  } |  i p | i d ƒ n |  i d  j o | i d ƒ n< t |  i t ƒ p t |  i ƒ d j o | i d ƒ n |  i	 d  j o% |  i
 t j o | i d ƒ qí n< t |  i	 t ƒ p t |  i	 ƒ d j o | i d ƒ n |  i d  j o | i d ƒ n% t |  i t ƒ p | i d ƒ n |  i t t f j o | i d	 ƒ n |  i t t f j o | i d
 ƒ n |  i
 d  j o | i d ƒ n |  i
 t j oE t |  i t t f ƒ p t |  i ƒ d j o | i d ƒ qön | S(   Ns(   can't directly instantiate Dialect classs   delimiter character not seti   s&   delimiter must be one-character strings   quotechar not sets&   quotechar must be one-character strings   lineterminator not sets   lineterminator must be a strings+   doublequote parameter must be True or Falses0   skipinitialspace parameter must be True or Falses   quoting parameter not sets;   escapechar must be a one-character string or unicode object(   R   R   R   t   appendt	   delimitert   Nonet
   isinstancet   strt   lent	   quotechart   quotingR   t   lineterminatort   doublequoteR   t   Falset   skipinitialspacet
   escapechart   unicode(   R   R   (    (    R   R   )   s4    
**0(   t   __name__t
   __module__t   _nameR&   R   R   R   R"   R(   R%   R'   R$   R#   R   R   (    (    (    R   R      s   	c           B   s,   t  Z d Z d Z e Z e Z d Z e	 Z
 RS(   Nt   ,t   "s   
(   R*   R+   R   R"   R   R%   R&   R'   R$   R    R#   (    (    (    R   R   P   s   c           B   s   t  Z d Z RS(   Ns   	(   R*   R+   R   (    (    (    R   R   Y   s   s	   excel-tabc           B   s/   t  Z d  d  d  d d „ Z d „  Z d „  Z RS(   NR   c         O   s7   | |  _  | |  _ | |  _ t | | | | Ž |  _ d  S(   N(	   t
   fieldnamesR   t   restkeyt   restvalR   t   ft   dialectt   argst   kwds(   R   R2   R/   R0   R1   R3   R4   R5   (    (    R   R   _   s    			c         C   s   |  S(   N(   R   (   R   (    (    R   t   __iter__f   s    c         C   sî   |  i i ƒ  } |  i d  j o | |  _ |  i i ƒ  } n x! | g  j o |  i i ƒ  } q> Wt t |  i | ƒ ƒ } t	 |  i ƒ } t	 | ƒ } | | j  o | | | |  i <n7 | | j o) x& |  i | D] } |  i | | <qÏ Wn | S(   N(   R   R   t   nextt   rowR/   R   t   dictt   zipt   dR!   t   lft   lrR0   t   keyR1   (   R   R<   R;   R>   R=   R8   (    (    R   R7   i   s"    	  (   R*   R+   R   R   R6   R7   (    (    (    R   R   ^   s   	c           B   s5   t  Z d d d d „ Z d „  Z d „  Z d „  Z RS(   NR   t   raiseR   c         O   s[   | |  _  | |  _ | i ƒ  d j o t d | ‚ n | |  _ t | | | | Ž |  _ d  S(   NR?   t   ignores-   extrasaction (%s) must be 'raise' or 'ignore'(   R?   s   ignore(   R/   R   R1   t   extrasactiont   lowert
   ValueErrorR	   R2   R3   R4   R5   (   R   R2   R/   R1   RA   R3   R4   R5   (    (    R   R   €   s    			c         C   sy   |  i d j o8 x5 | i ƒ  D]# } | |  i j o t d ‚ q q Wn g  } |  i D] } | | i	 | |  i
 ƒ qV ~ S(   NR?   s&   dict contains fields not in fieldnames(   R   RA   t   rowdictt   keyst   kR/   RC   t   _[1]R>   t   getR1   (   R   RD   RG   R>   RF   (    (    R   t   _dict_to_list‹   s     c         C   s   |  i i |  i | ƒ ƒ S(   N(   R   R	   t   writerowRI   RD   (   R   RD   (    (    R   RJ   ’   s    c         C   s=   g  } x$ | D] } | i |  i | ƒ ƒ q W|  i i | ƒ S(   N(   t   rowst   rowdictsRD   R   R   RI   R	   t	   writerows(   R   RL   RK   RD   (    (    R   RM   •   s
     (   R*   R+   R   RI   RJ   RM   (    (    (    R   R      s   		c           B   s>   t  Z d  Z d „  Z e d „ Z d „  Z d „  Z d „  Z RS(   se   
    "Sniffs" the format of a CSV file (i.e. delimiter, quotechar)
    Returns a Dialect object.
    c         C   s   d d d d d g |  _ d  S(   NR-   s   	t   ;t    t   :(   R   t	   preferred(   R   (    (    R   R   ¦   s    c         C   s€   |  i | | ƒ \ } } } | d j o |  i | | ƒ \ } } n d t	 f d „  ƒ  Y} | | _ | p d | _ | | _ | S(   sI   
        Returns a dialect (or None) corresponding to the sample
        R3   c           B   s    t  Z d Z d Z e Z e Z RS(   Nt   sniffeds   
(   R*   R+   R,   R$   R    R#   R&   R%   (    (    (    R   R3   ¶   s   R.   N(   R   t   _guess_quote_and_delimitert   samplet
   delimitersR"   R   R'   R   t   _guess_delimiterR   R3   (   R   RT   RU   R3   R'   R   R"   (    (    R   t   sniff«   s     			c         C   sÿ  g  } xE d D]= } t i | t i t i Bƒ } | i | ƒ } | o Pq q W| p d Sn h  }
 h  } d }	 x| D]} | i d d } | | } | o |
 i | d ƒ d |
 | <n y | i d	 d } | | } Wn t j
 o
 qv n X| o8 | d j p | | j o | i | d ƒ d | | <n y | i d
 d } Wn t j
 o
 qv n X| | o |	 d 7}	 qv qv Wt |
 d „ |
 i ƒ  ƒ } | oF t | d „ | i ƒ  ƒ } | | |	 j } | d j o
 d } qòn d } d } | | | f S(   s  
        Looks for text enclosed between two identical quotes
        (the probable quotechar) which are preceded and followed
        by the same character (the probable delimiter).
        For example:
                         ,'some text',
        The quote with the most wins, same with the delimiter.
        If there is no quotechar the delimiter can't be determined
        this way.
        sF   (?P<delim>[^\w
"'])(?P<space> ?)(?P<quote>["']).*?(?P=quote)(?P=delim)sC   (?:^|
)(?P<quote>["']).*?(?P=quote)(?P<delim>[^\w
"'])(?P<space> ?)sD   (?P<delim>>[^\w
"'])(?P<space> ?)(?P<quote>["']).*?(?P=quote)(?:$|
)s*   (?:^|
)(?P<quote>["']).*?(?P=quote)(?:$|
)R   i    t   quotei   t   delimt   spacec         C   s    | |  | | j o |  p | S(   N(   t   quotest   at   b(   R\   R]   R[   (    (    R   t   <lambda>ô   s    c         C   s    | |  | | j o |  p | S(   N(   t   delimsR\   R]   (   R\   R]   R_   (    (    R   R^   ø   s    s   
N(   sF   (?P<delim>[^\w
"'])(?P<space> ?)(?P<quote>["']).*?(?P=quote)(?P=delim)sC   (?:^|
)(?P<quote>["']).*?(?P=quote)(?P<delim>[^\w
"'])(?P<space> ?)sD   (?P<delim>>[^\w
"'])(?P<space> ?)(?P<quote>["']).*?(?P=quote)(?:$|
)s*   (?:^|
)(?P<quote>["']).*?(?P=quote)(?:$|
)(   R   Ni    (   t   matchest   restrt   ret   compilet   DOTALLt	   MULTILINEt   regexpt   findallt   dataR   R[   R_   t   spacest   mt
   groupindext   nR>   RH   t   KeyErrorRU   t   reduceRE   R"   RY   R'   (   R   Rh   RU   Ra   R'   Rj   R_   Rl   R`   Ri   R[   R>   R"   Rf   RY   (    (    R   RS   Å   sV    
  	 
!c         C   sÿ  t  d | i d ƒ ƒ } g  } t d ƒ D] } | t | ƒ q) ~ } t	 d t
 | ƒ ƒ } d } h  } h  } h  }
 d t	 | t
 | ƒ ƒ } }	 x¥| t
 | ƒ j  o‘| d 7} xk | | |	 !D]\ } xS | D]K } | i | h  ƒ } | i | ƒ } | i | d ƒ d | | <| | | <qÉ Wq¼ WxÓ | i ƒ  D]Å } | | i ƒ  } t
 | ƒ d j o | d d d j o q)n t
 | ƒ d j o_ t d „  | ƒ | | <| i | | ƒ | | d | | d t d „  | ƒ d f | | <q)| d | | <q)W| i ƒ  } t | | ƒ } d } d	 } x© t
 |
 ƒ d j o• | | j oˆ xw | D]o \ } } | d d j oR | d d j oA | d | | j o( | d j p | | j o | |
 | <q³qDqDW| d
 8} qWt
 |
 ƒ d j oH |
 i ƒ  d } | d i | ƒ | d i d | ƒ j } | | f Sn |	 } |	 | 7}	 q‘ W|
 p d Sn t
 |
 ƒ d j oc x` |  i' D]Q } | |
 i ƒ  j o8 | d i | ƒ | d i d | ƒ j } | | f SqbqbWn |
 i ƒ  d } | d i | ƒ | d i d | ƒ j } | | f S(   s¼  
        The delimiter /should/ occur the same number of times on
        each row. However, due to malformed data, it may not. We don't want
        an all or nothing approach, so we allow for small variations in this
        number.
          1) build a table of the frequency of each character on every line.
          2) build a table of freqencies of this frequency (meta-frequency?),
             e.g.  'x occurred 5 times in 10 rows, 6 times in 1000 rows,
             7 times in 2 rows'
          3) use the mode of the meta-frequency to determine the /expected/
             frequency for that character
          4) find out how often the character actually meets that goal
          5) the character that best meets its goal is the delimiter
        For performance reasons, the data is evaluated in chunks, so it can
        try and evaluate the smallest portion of the data possible, evaluating
        additional chunks as necessary.
        s   
i   i
   i    i   c         C   s    |  d | d j o |  p | S(   Ni   (   R\   R]   (   R\   R]   (    (    R   R^   4  s    c         C   s   d |  d | d f S(   Ni    i   (   R\   R]   (   R\   R]   (    (    R   R^   :  s    f1.0f0.90000000000000002f0.01s   %c R   N(   R   i    ()   t   filterR   Rh   t   splitRG   t   ranget   ct   chrt   asciit   minR!   t   chunkLengtht	   iterationt   charFrequencyt   modesR_   t   startt   endt   linet   charRH   t   metaFrequencyt   countt   freqRE   t   itemsRn   t   removet   modeListt   floatt   totalt   consistencyt	   thresholdRF   t   vRU   RY   R'   R   RQ   R;   (   R   Rh   RU   R~   R}   R‡   R…   Rt   R€   R{   R_   Rz   Rv   RY   Rˆ   Rx   R|   R;   Rr   Ry   R'   R   RF   Rw   RG   Rƒ   R†   (    (    R   RV     sz     - 
   (	   "/*
 **c      	   C   s*  t  t | ƒ |  i | ƒ ƒ }	 |	 i ƒ  } t | ƒ } h  } x t | ƒ D] } d  | | <qI Wd } x	|	 D]} | d j o Pn | d 7} t | ƒ | j o qj n xÂ | i ƒ  D]´ } xY t t t t g D]5 }
 y |
 | | ƒ PWqÌ t t f j
 o qÌ XqÌ Wt | | ƒ }
 |
 t j o
 t }
 n |
 | | j o* | | d  j o |
 | | <qg| | =q³ q³ Wqj Wd } x¨ | i ƒ  D]š \ } } t | ƒ t d ƒ j o3 t | | ƒ | j o | d 7} q| d 8} q‚y | | | ƒ Wn# t t f j
 o | d 7} q‚X| d 8} q‚W| d j S(   Ni    i   i   (   R   t   StringIORT   R   RW   t   rdrR7   t   headerR!   t   columnst   columnTypesRq   t   iR   t   checkedR8   RE   t   colt   intt   longR„   t   complext   thisTypeRC   t   OverflowErrort	   hasHeaderR   t   colTypet   typet	   TypeError(   R   RT   R‹   R—   R8   R   R   RŒ   RŽ   RŠ   R”   R   R–   (    (    R   t
   has_headerj  sX    
   
  	
 (	   R*   R+   t   __doc__R   R   RW   RS   RV   Rš   (    (    (    R   R   ¡   s    		@	e(   R›   Rb   t   _csvR   R   R	   R   R
   R   R   R   R    R   R   R   t	   cStringIOR‰   t   ImportErrort   __all__R   R   R   R   R   R“   t	   NameErrorR„   R   (   R   R   R   R‰   R   RŸ   R   R	   R   R   R   R“   R
   R   R   R›   R   R   Rb   R   R    R   R   (    (    R   t   ?   s&   	U<:!