Ńň
)ŽŐHc           @   sq   d  Z  d Z d Z e d  Z d Z e d  Z d Z e d  d Z h  Z	 d	 e d
  Z
 d	 e d  Z d   Z d S(   sÎ  

numconv
-------

:synopsys: Python library to convert strings to numbers and numbers to
           strings.
:copyright: 2008 by Gustavo Picon
:license: Apache License 2.0
:version: 1.1-svn
:url: http://code.google.com/p/numconv/
:documentation:
   `numconv-docs
   <http://numconv.googlecode.com/svn/docs/index.html>`_
:examples:
   `numconv-tests
   <http://code.google.com/p/numconv/source/browse/trunk/tests.py>`_


:mod:`numconv` converts a string into a number and a number into a string using
default or user supplied encoding alphabets.

constants
~~~~~~~~~

.. data:: BASE85

   Alphabet defined in section 4 of :rfc:`1924`. Supposed to be a joke (it is
   an April's fools RFC after all), but is quite useful because can be used as
   a base for the most common numeric conversions.

.. data:: BASE16
          BASE32
          BASE32HEX
          BASE64
          BASE64URL

   Alphabets defined in :rfc:`4648`. Not really for common numeric conversion
   use.

i   sU   0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~i   t    ABCDEFGHIJKLMNOPQRSTUVWXYZ234567i    s@   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/i>   s   -_i
   c         C   sú   | t  j o t |  n t |   |  j o t d  n |  d j  o t d  n t |  | j o t d  n d | j o t |  j n p t d t |  f  n d } x: t o2 | |  | | } |  | j  o Pn |  | }  qź W| S(   sů  Converts an integer into a string.

    :param num: A numeric value to be converted to another base as a string.
    :param radix: The base that will be used in the conversion.
       The default value is 10 for decimal conversion.
    :param alphabet: A string that will be used as a encoding alphabet.

       The length of the alphabet can be longer than the radix. In this case
       the alphabet will be internally truncated.

       The default value is :data:`numconv.BASE85`

    :rtype: string

    :raise TypeError: when *num* isn't an integer
    :raise ValueError: when *num* isn't positive
    :raise TypeError: when *radix* isn't an integer
    :raise ValueError: when *radix* is invalid
    :raise ValueError: when *alphabet* has duplicated characters

    **Examples** (taken from :file:`tests.py`):
       
       3735928559 to hexadecimal::

           >> numconv.int2str(3735928559, 16)
           'DEADBEEF'

       10284 to binary::

           >> numconv.int2str(19284, 2)
           '100101101010100'

       37 to base 4 using a custom dictionary::

           >> numconv.int2str(37, 4, 'rofl')
           'foo'

       Very large number to :data:`~numconv.BASE85`::

           >> numconv.int2str(2693233728041137L, 85)
           '~123AFz@'

    s   number must be an integeri    s   number must be positives   radix must be an integeri   s   radix must be >= 2 and <= %dt    (   t   CMAPSt   getcmapt   intt	   TypeErrort
   ValueErrort   lent   True(   t   numt   radixt   alphabett   ret(    (    sS   /afs/sipb.mit.edu/project/remit/demo-code/django-treebeard-1.1/treebeard/numconv.pyt   int2str=   s$    ,$ c         C   s  | t  j o t |  n t |  | j o t d  n d | j o t |  j n p t d t |  f  n | d j o/ | |  i   t |  i   j o t |  |  Sd } t  | } | |  } xD |  D]< } | | j o t d | |  f  n | | | | } qÖ W| S(   s.  Converts a string into an integer.

    If possible, the built-in python conversion will be used for speed
    porpuses.

    :param num: A string that will be converted to an integer.
    :param radix: The base that will be used in the conversion.
       The default value is 10 for decimal conversion.
    :param alphabet: A string that will be used as a encoding alphabet.

       The length of the alphabet can be longer than the radix. In this case
       the alphabet will be internally truncated.

       The default value is :data:`numconv.BASE85`

    :rtype: integer

    :raise TypeError: when *radix* isn't an integer
    :raise ValueError: when *radix* is invalid
    :raise ValueError: when *num* is invalid
    :raise ValueError: when *alphabet* has duplicated characters

    **Examples** (taken from :file:`tests.py`):
       
       Hexadecimal 'DEADBEEF' to integer::

          >> numconv.str2int('DEADBEEF', 16)
          3735928559L

       Binary '100101101010100' to integer::

           >> numconv.str2int('100101101010100', 2)
           19284

       Base 4 with custom encoding 'foo' to integer::

           >> numconv.str2int('foo', 4, 'rofl')
           37

       :data:`~numconv.BASE85` '~123AFz@' to integer::

           >> numconv.str2int('~123AFz@', 85)
           2693233728041137L

    s   radix must be an integeri   s   radix must be >= 2 and <= %di$   i    s3   invalid literal for radix2int() with radix %d: '%s'(   R   R   R   R   R   R   t   lowert   BASE85(   R	   R
   R   R   t   lmapt	   lalphabett   char(    (    sS   /afs/sipb.mit.edu/project/remit/demo-code/django-treebeard-1.1/treebeard/numconv.pyt   str2int}   s$    .$.

 c         C   s\   t  t |  t t |      } t |  t |   j o t d |  f  n | t |  <| S(   s?   Builds an internal alphabet lookup table, to be stored in CMAPSs"   duplicate characters found in '%s'(   t   dictt   zipt   rangeR   R   R   (   R   R   (    (    sS   /afs/sipb.mit.edu/project/remit/demo-code/django-treebeard-1.1/treebeard/numconv.pyR   ž   s
    !
N(   i   i   N(   t   __doc__t   Nonet   VERSIONR   t   BASE16t   BASE32t	   BASE32HEXt   BASE64t	   BASE64URLR   R   R   R   (    (    (    sS   /afs/sipb.mit.edu/project/remit/demo-code/django-treebeard-1.1/treebeard/numconv.pyt   <module>*   s   

@A