tabo / numconv

Python library to convert strings to numbers and numbers to strings. Docs: http://docs.tabo.pe/numconv/tip/

Clone this repository (size: 158.5 KB): HTTPS / SSH
$ hg clone http://code.tabo.pe/numconv

Changed (Δ3.8 KB):

raw changeset »

docs/index.rst (93 lines added, 12 lines removed)

numconv.py (3 lines added, 90 lines removed)

Up to file-list docs/index.rst:

1
:mod:`numconv` --- flexible numeric conversion for python
2
=========================================================
1
numconv
2
=======
3
3
4
.. automodule:: numconv
4
:synopsys: Convert strings to numbers and numbers to strings.
5
:copyright: 2008-2009 by Gustavo Picon
6
:license: Apache License 2.0
7
:version: 2.1a
8
:url: http://code.tabo.pe/numconv/
9
:documentation:
10
   `numconv-docs
11
   <http://docs.tabo.pe/numconv/2.0/>`_
12
:examples:
13
   `numconv-tests
14
   <http://code.tabo.pe/numconv/src/2.0/tests.py>`_
5
15
6
   .. autoclass:: NumConv
7
      :show-inheritance:
16
:mod:`numconv` converts a string into a number and a number into a string
17
using default or user supplied encoding alphabets.
8
18
9
      .. automethod:: int2str
10
      .. automethod:: str2int
11
19
20
.. module:: numconv
12
21
13
   functions
14
   ~~~~~~~~~
15
   
16
   .. autofunction:: int2str
22
.. autoclass:: NumConv
23
   :show-inheritance:
17
24
18
   .. autofunction:: str2int
25
   .. automethod:: int2str
26
27
      **Examples** (taken from :file:`tests.py`):
28
29
        3735928559 to hexadecimal::
30
31
            >> NumConv(16).int2str(3735928559)
32
            'DEADBEEF'
33
34
        19284 to binary::
35
36
            >> NumConv(2).int2str(19284)
37
            '100101101010100'
38
39
        37 to base 4 using a custom dictionary::
40
41
            >> NumConv(4, 'rofl').int2str(37)
42
            'foo'
43
44
        Very large number to :data:`~numconv.BASE85`::
45
46
            >> NumConv(85).int2str(2693233728041137)
47
            '~123AFz@'
48
49
   .. automethod:: str2int
50
51
      **Examples** (taken from :file:`tests.py`):
52
53
        Hexadecimal 'DEADBEEF' to integer::
54
55
           >> NumConv(16).str2int('DEADBEEF')
56
           3735928559
57
58
        Binary '100101101010100' to integer::
59
60
            >> NumConv(2).str2int('100101101010100')
61
            19284
62
63
        Base 4 with custom encoding 'foo' to integer::
64
65
            >> NumConv(4, 'rofl').str2int('foo')
66
            37
67
68
        :data:`~numconv.BASE85` '~123AFz@' to integer::
69
70
            >> NumConv(85).str2int('~123AFz@')
71
            2693233728041137
72
73
74
.. data:: BASE85
75
76
   Alphabet defined in section 4 of :rfc:`1924`. Supposed to be a joke
77
   (it is an April's fools RFC after all), but is quite useful because
78
   it can be used as a base for the most common numeric conversions.
79
80
.. data:: BASE16
81
          BASE32
82
          BASE32HEX
83
          BASE64
84
          BASE64URL
85
86
   Alphabets defined in :rfc:`4648`. Not really for common numeric
87
   conversion use.
88
89
.. data:: BASE62
90
91
   Useful for URL shorteners.
92
93
94
functions
95
---------
96
97
.. autofunction:: int2str
98
99
.. autofunction:: str2int
19
100
20
101
21
102

Up to file-list numconv.py:

1
# -*- coding: utf-8 -*-
2
"""
1
"""Convert strings to numbers and numbers to strings.
3
2
4
numconv
5
-------
6
7
:synopsys: Python library to convert strings to numbers and numbers to
8
           strings.
9
:copyright: 2008-2009 by Gustavo Picon
10
:license: Apache License 2.0
11
:version: 2.1a
12
:url: http://code.tabo.pe/numconv/
13
:documentation:
14
   `numconv-docs
15
   <http://docs.tabo.pe/numconv/2.0/>`_
16
:examples:
17
   `numconv-tests
18
   <http://code.tabo.pe/numconv/src/2.0/tests.py>`_
19
20
21
:mod:`numconv` converts a string into a number and a number into a string
22
using default or user supplied encoding alphabets.
23
24
constants
25
~~~~~~~~~
26
27
.. data:: BASE85
28
29
   Alphabet defined in section 4 of :rfc:`1924`. Supposed to be a joke (it is
30
   an April's fools RFC after all), but is quite useful because it can be used
31
   as a base for the most common numeric conversions.
32
33
.. data:: BASE16
34
          BASE32
35
          BASE32HEX
36
          BASE64
37
          BASE64URL
38
39
   Alphabets defined in :rfc:`4648`. Not really for common numeric conversion
40
   use.
41
42
.. data:: BASE62
43
44
   Useful for URL shorteners.
3
Gustavo Picon
4
http://code.tabo.pe/numconv/
45
5
46
6
"""
47
7
@@ -99,34 +59,10 @@ class NumConv(object):
99
59
        :param num: A numeric value to be converted to another base as a
100
60
                    string.
101
61
102
103
62
        :rtype: string
104
63
105
64
        :raise TypeError: when *num* isn't an integer
106
65
        :raise ValueError: when *num* isn't positive
107
108
        **Examples** (taken from :file:`tests.py`):
109
110
           3735928559 to hexadecimal::
111
112
               >> NumConv(16).int2str(3735928559)
113
               'DEADBEEF'
114
115
           19284 to binary::
116
117
               >> NumConv(2).int2str(19284)
118
               '100101101010100'
119
120
           37 to base 4 using a custom dictionary::
121
122
               >> NumConv(4, 'rofl').int2str(37)
123
               'foo'
124
125
           Very large number to :data:`~numconv.BASE85`::
126
127
               >> NumConv(85).int2str(2693233728041137)
128
               '~123AFz@'
129
130
66
        """
131
67
        if int(num) != num:
132
68
            raise TypeError('number must be an integer')
@@ -155,29 +91,6 @@ class NumConv(object):
155
91
        :rtype: integer
156
92
157
93
        :raise ValueError: when *num* is invalid
158
159
        **Examples** (taken from :file:`tests.py`):
160
161
           Hexadecimal 'DEADBEEF' to integer::
162
163
              >> NumConv(16).str2int('DEADBEEF')
164
              3735928559
165
166
           Binary '100101101010100' to integer::
167
168
               >> NumConv(2).str2int('100101101010100')
169
               19284
170
171
           Base 4 with custom encoding 'foo' to integer::
172
173
               >> NumConv(4, 'rofl').str2int('foo')
174
               37
175
176
           :data:`~numconv.BASE85` '~123AFz@' to integer::
177
178
               >> NumConv(85).str2int('~123AFz@')
179
               2693233728041137
180
181
94
        """
182
95
        radix, alphabet = self.radix, self.alphabet
183
96
        if radix <= 36 and alphabet[:radix].lower() == BASE85[:radix].lower():