ó
č¾bc           @   sĮ   d  Z  d d l m Z e d d d [ d d l Z d d d	 g Z e j d
  Z y d	 e f d     YZ Wn e	 k
 r d Z n Xd   Z
 d   Z d   Z d   Z d   Z d   Z d S(   s8  General floating point formatting functions.

Functions:
fix(x, digits_behind)
sci(x, digits_behind)

Each takes a number or a string and a number of digits as arguments.

Parameters:
x:             number to be formatted; or a string resembling a number
digits_behind: number of digits behind the decimal point
i’’’’(   t   warnpy3ks2   the fpformat module has been removed in Python 3.0t
   stackleveli   Nt   fixt   scit
   NotANumbers+   ^([-+]?)(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$c           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s   /usr/lib/python2.7/fpformat.pyR      s   s   fpformat.NotANumberc         C   s±   t  j |   } | d	 k r' t |   n  | j d d d d  \ } } } } | j d  } | d k ro d } n  | r | d } n  | r t | d  } n d } | | | | f S(
   sÅ   Return (sign, intpart, fraction, expo) or raise an exception:
    sign is '+' or '-'
    intpart is 0 or more digits beginning with a nonzero
    fraction is 0 or more digits
    expo is an integeri   i   i   i   t   0t   +t    i    N(   t   decodert   matcht   NoneR   t   groupt   lstript   int(   t   st   rest   signt   intpartt   fractiont   exppartt   expo(    (    s   /usr/lib/python2.7/fpformat.pyt   extract#   s     $ 	  c         C   s¶   | d k rU t  |  } |  | |  | | }  } | | k r¬ |  d | | }  q¬ nW | d k  r¬ t  |   } |  |  |  | | }  } | | k  r¬ d | | | } q¬ n  |  | f S(   s5   Remove the exponent by changing intpart and fraction.i    R   (   t   len(   R   R   R   t   ft   i(    (    s   /usr/lib/python2.7/fpformat.pyt   unexpo3   s    c         C   s[  t  |  } | | k r. |  | d | | f St  |   } | | d k  rY d | d f S|  | } | | | } | d k r&| | d } xH | d k r» | | d k r® Pn  | d } q Wd | } | d } d } | |  t t | |  d  d t  |  | d } | |  | | }  } n  | d k r@|  | |  f S|  |  d | d f Sd S(   s*   Round or extend the fraction to size digs.R   i    R	   t   5i   t   9N(   R   t   chrt   ord(   R   R   t   digsR   R   t   totalt	   nextdigitt   n(    (    s   /usr/lib/python2.7/fpformat.pyt	   roundfracA   s,    
 

8c         C   sģ   t  |   t  d  k r' t |   }  n  y t |   \ } } } } Wn t k
 rW |  SXt | | |  \ } } t | | |  \ } } x$ | r® | d d k r® | d } q W| d k rÄ d } n  | d k rą | | d | S| | Sd S(   s   Format x as [-]ddd.ddd with 'digs' digits after the point
    and at least one digit before.
    If digs <= 0, the point is suppressed.R	   i    R   i   t   .N(   t   typet   reprR   R   R   R$   (   t   xR    R   R   R   R   (    (    s   /usr/lib/python2.7/fpformat.pyR   [   s       	 c         C   sĀ  t  |   t  d  k r' t |   }  n  t |   \ } } } } | s§ x. | ru | d d k ru | d } | d } qH W| r | d | d } } | d } qŌ d } n- | t |  d } | d | d | } } t d |  } t | | |  \ } } t |  d k rB| d | d | d  | t |  d } } } n  | | } | d k ri| d | } n  t t |   } d d t |  | } | d k  r¬d | } n
 d	 | } | d
 | S(   s„   Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point
    and exactly one digit before.
    If digs is <= 0, one digit is kept and the point is suppressed.R	   i    R   i   i’’’’R%   i   t   -R   t   e(   R&   R'   R   R   t   maxR$   t   abs(   R(   R    R   R   R   R   R   R*   (    (    s   /usr/lib/python2.7/fpformat.pyR   k   s6     
	
  
c          C   sZ   y< x5 t  d  \ }  } |  Gt |  |  Gt |  |  GHq WWn t t f k
 rU n Xd S(   s   Interactive test run.s   Enter (x, digs): N(   t   inputR   R   t   EOFErrort   KeyboardInterrupt(   R(   R    (    (    s   /usr/lib/python2.7/fpformat.pyt   test   s    '(   t   __doc__t   warningsR    t   ret   __all__t   compileR
   t
   ValueErrorR   t	   TypeErrorR   R   R$   R   R   R0   (    (    (    s   /usr/lib/python2.7/fpformat.pyt   <module>   s    
					 