ó
Þ™xTc           @   s  d  d l  m Z d  d l Z d  d l Z d  d l Z d d d d d e e d d d „  d	 „
 Z	 d
 e
 f d „  ƒ  YZ d „  Z d  d l Z d „  Z d  d l Z d „  Z d d d „  ƒ  YZ d d d „  ƒ  YZ d d d „  ƒ  YZ d „  Z d „  Z e d k re ƒ  e ƒ  n  d S(   iÿÿÿÿ(   t   divisionNi    t   -s    | t   leftt    c         C   s   |  S(   N(    (   t   x(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   <lambda>   s    c      	      sŒ  | t  k r d } n  | t k r* d } n  ‡  f d †  } g  |  D] } | | ƒ ^ q@ } t d	 t t j | ƒ Œ } g  | D]1 } t g  | D] } t t	 | ƒ ƒ ^ qŠ ƒ ^ qz } | j
 ƒ  } t | ƒ t | ƒ } d | | t | ƒ t |	 ƒ t | ƒ t | ƒ t | ƒ d } i t	 j d 6t	 j d 6t	 j d 6| j ƒ  } t j ƒ  } d } d	 } | rp| | IJn  x£ | D]› } x` | D]X } | | j g  t | | ƒ D]! \ } } | t	 | ƒ | ƒ ^ q£ƒ |	 } | | IJq„W| sò| d k rÿ| | IJn  | rw| d 8} qwqwW| s2| r2| | IJq2n  | d	 k	 rZ| t | t | ƒ ƒ IJn  d j g  | j ƒ  j ƒ  D] } | j ƒ  ^ qsƒ S(
   s  Indents a table by column.
       - rows: A sequence of sequences of items, one sequence per row.
       - hasHeader: True if the first row consists of the columns' names.
       - headerChar: Character to be used for the row separator line
         (if hasHeader==True or separateRows==True).
       - delim: The column delimiter.
       - justify: Determines how are data justified in their column.
         Valid values are 'left','right' and 'center'.
       - separateRows: True if rows are to be separated by a line
         of 'headerChar's.
       - prefix: A string prepended to each printed row.
       - postfix: A string appended to each printed row.
       - wrapfunc: A function f(text) for wrapping text; each element in
         the table is first wrapped by this function.i   i    c            sa   g  |  D] } ˆ  | ƒ j  d ƒ ^ q } g  t d  | Œ D]% } g  | D] } | pT d ^ qE ^ q8 S(   Ns   
R   (   t   splitt   mapt   None(   t   rowt   itemt   new_rowst   substr(   t   wrapfunc(    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   row_wrapper(   s    (t    t   centert   rightR   s   
N(   t   Truet   FalseR   R   t   reducet   operatort   addt   maxt   lent   strt   lstript   sumR   t   rjustt   ljustt   lowert	   cStringIOt   StringIOt   joint   zipt   wrap_onspacet   getvaluet
   splitlinest   rstrip(   t   rowst   commentst
   has_headert   header_chart   delimt   justifyt   separate_rowst   leading_and_terminal_separatort   prefixt   postfixR   R   R	   t   logical_rowst   columnst   columnR
   t
   max_widthst   prefix_minus_leading_spacest   indentt   row_separatort   outputt   total_row_widtht   linet   physical_rowst   width(    (   R   s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   format   sD     	 	;
:. H  t   simple_tablec           B   sM   e  Z d  Z d d d „ Z d d d „ Z d „  Z d „  Z e d „  ƒ Z	 RS(	   s  
  Container for generic table contents, used in Xtriage and elsewhere.  The
  table cells are assumed to be pre-formatted as strings (but not fixed-width).
  This is designed to be easily fed into the format() function, but it can also
  be displayed in GUIs or HTML tables.
  c         C   s   t  | ƒ |  _ | |  _ d  S(   N(   t   listt   column_headerst   _rows(   t   selft
   table_rowsR@   R(   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   __init__X   s    i    c      	   C   sI   d | } t  d |  j g |  j d t |  j ƒ d k d | d d d ƒ S(	   NR   R'   R)   i    R/   s   | R0   s    |(   R=   R@   RA   R   (   RB   R6   t   equal_widthsR/   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR=   _   s    

c         C   s1   t  |  j ƒ d k r& |  j g |  j S|  j Sd  S(   Ni    (   R   R@   RA   (   RB   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   exporth   s    c         C   s
   |  j  ƒ  S(   N(   RF   (   RB   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   export_rowsn   s    c         C   s   t  |  j ƒ S(   N(   R   RA   (   RB   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   n_rowsp   s    (    N(
   t   __name__t
   __module__t   __doc__R   RD   R=   RF   RG   t   propertyRH   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR>   Q   s   			c         C   s   t  | d „ |  j d ƒ ƒ S(   s¡   
    A word-wrap function that preserves existing line breaks
    and most spaces in the text. Expects that existing line
    breaks are posix newlines (
).
    c         S   sL   d |  d t  |  |  j d ƒ d ƒ t  | j d d ƒ d ƒ | k | f S(   Ns   %s%s%ss    
s   
i   i    (   R   t   rfindR   (   R:   t   wordR<   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR   |   s
   R   (   R   R   (   t   textR<   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR#   v   s    c            s?   t  j d t ˆ  ƒ d ƒ } t | j ‡  f d †  |  ƒ ˆ  ƒ S(   se   Similar to wrap_onspace, but enforces the width constraint:
       words longer than width are split.s   \S{s   ,}c            s   t  |  j ƒ  ˆ  ƒ S(   N(   t   wrap_alwayst   group(   t   m(   R<   (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR   Š   s    (   t   ret   compileR   R#   t   sub(   RO   R<   t	   wordRegex(    (   R<   s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   wrap_onspace_strict†   s    c         C   sV   d j  g  t t t j d t |  ƒ | ƒ ƒ ƒ D] } |  | | | | d !^ q0 ƒ S(   ss   A simple word-wrap function that wraps text on exactly width characters.
       It doesn't split the text in words.s   
g      ð?i   (   R!   t   xranget   intt   matht   ceilR   (   RO   R<   t   i(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRP   �   s    	t   Spreadsheetc           B   sJ   e  Z d  d „ Z d d „ Z d „  Z d d d „ Z d „  Z d „  Z RS(   i    c         C   s   | |  _  g  |  _ d  S(   N(   t   S_table_rowst	   summaries(   RB   R'   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRD   �   s    	c         C   s#   t  |  | t |  |  j | ƒ ƒ d  S(   N(   t   setattrt   SpreadsheetColumnR^   (   RB   t   labelt   default_value(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt	   addColumn¡   s    c         C   sQ   |  j  d 7_  x; |  j D]0 } t |  j | t ƒ r |  j | j ƒ  q q Wd  S(   Ni   (   R^   t   __dict__t
   isinstanceRa   t   append(   RB   R
   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   addRow¤   s    c         C   sŽ  | d  k r$ d d  l } | j } n  | d  k rC t g |  j } n  i  } i  } xj | D]b } t t | ƒ t |  j | j |  j | d ƒ ƒ | | <d | | | | <| | | | IqV W| Jxj t	 |  j ƒ D]Y } | | sæ qÐ n  x< | D]4 } | | | |  j | j |  j | | f Iqí W| JqÐ WxL | D]D } | |  j
 k rb| d d | | Iq4| d d | | Iq4W| Jx| D]û } | |  j
 k rÏ| | | |  j | j |  j | j ƒ  f Iq‡t |  d ƒ rl| |  j k rl|  j j ƒ  }	 d }
 x7 t	 |  j ƒ D]& } |
 |  j | |  j | | 7}
 qW|
 |	 } | | | |  j | j | f Iq‡| d d | | Iq‡W| Jd  S(   Niÿÿÿÿi    s   %%%ds R   R   i   t   wtmean(   R   t   syst   stdoutR   R^   R   R   Re   R=   RX   R_   R   t   hasattrRi   t   weights(   RB   R2   t   printed_rowst   outRj   t   mastert   paddingR3   R\   t
   normalizert	   summationR   t   mean(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt
   printTableª   sP       	,
 '($
c         C   s   |  j  S(   N(   R^   (   RB   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR'   Ô   s    c         C   s
   t  | ƒ S(   N(   t   eval(   RB   t
   expression(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRv   ×   s    N(	   RI   RJ   RD   R   Rd   Rh   Ru   R'   Rv   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR]   ›   s   	*	Ra   c           B   sG   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s)   | |  _  | |  _ |  j g | |  _ d  S(   N(   t   parentt   defaultt   C_data(   RB   Rx   R'   Rc   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRD   Ü   s    		c         C   sr   t  | ƒ t  d ƒ k r( | |  j | <n  t  | ƒ t  d ƒ k rn |  j j | d ƒ } | |  j | | d <n  d  S(   Ni    i   (   i    i   (   t   typeRz   Rx   t   pointer(   RB   t   indext   valueR|   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   __setitem__á   s
    c         C   sñ   t  | ƒ t  d ƒ k r¬ |  j | } t | t ƒ r¥ | j d k ry |  j j | d k r] d S|  j j | |  j j | S| j j d d | ƒ } |  j j	 | ƒ Sq¬ | Sn  t  | ƒ t  d ƒ k rí |  j j
 | d ƒ } |  j | | d Sd  S(   Ni    s(   self.Population[%row] / self.Fract[%row]g        s   %rows   %di   (   i    i   (   R{   Rz   Rf   t   FormulaRw   Rx   t   Fractt
   Populationt   replaceRv   R|   (   RB   R}   t   Vt   ER|   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   __getitem__ì   s     c         C   s   t  |  j ƒ S(   N(   R   Rz   (   RB   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   __len__ý   s    c         C   s   |  j  j |  j ƒ d  S(   N(   Rz   Rg   Ry   (   RB   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRg      s    c         C   s   | |  _  d  S(   N(   R=   (   RB   R=   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt	   setFormat  s    c         C   s9   d „  } t  | g  t t |  ƒ ƒ D] } |  | ^ q" ƒ S(   Nc         S   s   |  | S(   N(    (   R   t   y(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   typesum  s    (   R   RX   R   (   RB   RŠ   R\   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR     s    	(	   RI   RJ   RD   R   R†   R‡   Rg   Rˆ   R   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRa   Ú   s   						R€   c           B   s   e  Z d  „  Z RS(   c         C   s   | |  _  d  S(   N(   Rw   (   RB   Rw   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRD     s    (   RI   RJ   RD   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR€   
  s   c          C   s  d t  f d „  ƒ  Y}  t j ƒ  } |  ƒ  } | j d d d | ƒ d j g  | j ƒ  j d ƒ D] } | j ƒ  ^ q] ƒ d k s„ t ‚ d t  f d	 „  ƒ  Y} t j ƒ  } | ƒ  } | j d | ƒ d j g  | j ƒ  j d ƒ D] } | j ƒ  ^ qÛ ƒ d
 k st ‚ d  S(   Nt   typical_use_case_1c           B   s&   e  Z d  „  Z d d d g d „ Z RS(   c         S   s  t  j |  d d ƒ|  j d ƒ |  j d ƒ |  j d d ƒ x` t d |  j ƒ D]L } d d d	 d
 d d g | |  j | <d d d d d d g | |  j | <qP Wd d d d d g } x+ t t | ƒ ƒ D] } | | |  j | <qÈ Wd |  j _	 d |  j _	 d |  j _	 d  S(   NR'   i   t   Limitt   MeanIR‚   i    g¼t“)@g^ºIë#@gffffff!@gš™™™™™@g\�Âõ(\@gš™™™™™@iü  iÍ  i9  iz  ix  iž  iy   i   i   s   %.2fs   %.0fs   %7d(
   R]   RD   Rd   RX   R^   RŒ   R�   R   R‚   R=   (   RB   t   xrowt   population_datat   c(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRD     s    #'RŒ   R‚   R�   c         S   sw   d g |  _  d g |  _ |  j |  _ d } | | d IJt t t t t t g } |  j | d | d | ƒ| | IJd  S(   NR‚   R�   s(   Detailed explanation of each column heres   :
Rn   Ro   (   R_   Ri   R‚   Rm   R   Ru   (   RB   t   messageRo   t   columns_to_printt   legendt   to_print(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   show&  s    (   RI   RJ   RD   R•   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR‹     s   	R‘   s+   Analysis of signals after noise suppressionRo   s   
s2  Analysis of signals after noise suppression:

Limit  Population  MeanI
12.54         926   4348
 9.96         121    461
 8.70           8    313
 7.90           2    378
 7.34           6    376
 6.90           0      0
        ----------
             1063   3845
Detailed explanation of each column here
t   typical_use_case_2c           B   s,   e  Z d  „  Z d d d d g d d „ Z RS(   c         S   sû   d } t  j |  d | ƒ|  j d ƒ |  j d ƒ |  j d d ƒ |  j d t d ƒ ƒ xk t | ƒ D]] } d	 d
 d d g | |  j | <d d d d g | |  j | <d d d d g | |  j | <qf Wd |  j _ d |  j _ d |  j _ d |  j	 _ d  S(   Ni   R'   RŒ   R�   R‚   i    t	   adjustPops(   self.Population[%row] / self.Fract[%row]gìQ¸…«C@g¸…ëQ8?@gHáz®G;@gHáz®Ç8@g      ð?g       @g      @g      @i5   if   ik   iV   s   %.2fs   %7ds   %7.1f(
   R]   RD   Rd   R€   RX   RŒ   R�   R‚   R=   R—   (   RB   t
   Total_rowsRŽ   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyRD   B  s    	!RŒ   R‚   R�   R—   c         S   s   |  j  | d | ƒd  S(   NRo   (   Ru   (   RB   Ry   Ro   (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR•   V  s    N(   RI   RJ   RD   R   R•   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyR–   A  s   	s¶   Limit  Population  Fract  adjustPop
39.34          53   1.00       53.0
31.22         102   2.00       51.0
27.28         107   3.00       35.7
24.78          86   4.00       21.5


(	   R]   R   R    R•   R!   R$   R   R&   t   AssertionError(   R‹   t   OCt	   derived_1R\   R–   t	   derived_2(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   excercise_spreadsheet  s    	4	4c          C   sZ   t  d d d d g d d d g d d	 d
 g g d d d t ƒ }  |  d k sQ t ‚ d GHd  S(   NR'   t   alphat   betat   gammat   10t   20t   30t   40t   50t   60R(   s   comments hereR)   s…   --------------------
alpha | beta | gamma
--------------------
10    | 20   | 30
40    | 50   | 60
--------------------
comments heret   OK(   R=   R   R™   (   t   formatted_table(    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   exercise_flat_tableh  s    	t   __main__(    (    (    (   t
   __future__R    t   libtbx.forward_compatibilityt   libtbxR   R   R   R   R   R=   t   objectR>   R#   RS   RW   RZ   RP   R]   Ra   R€   R�   R©   RI   (    (    (    s@   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/table_utils.pyt   <module>   s4   ;%			?0	W	