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
commit 33: b27cf772ba32
parent 32: d6e806ea0e33
branch: default
making the latest version of pep8.py happy
Gustavo Picon / tabo
5 months ago

Changed (Δ449 bytes):

raw changeset »

docs/.buildinfo (1 lines added, 1 lines removed)

docs/_static/basic.css (13 lines added, 1 lines removed)

docs/_static/default.css (8 lines added, 0 lines removed)

docs/conf.py (2 lines added, 2 lines removed)

docs/genindex.html (10 lines added, 10 lines removed)

docs/index.html (13 lines added, 13 lines removed)

docs/modindex.html (10 lines added, 10 lines removed)

docs/objects.inv (1 lines added, 1 lines removed)

docs/search.html (8 lines added, 8 lines removed)

numconv.py (38 lines added, 39 lines removed)

setup.py (2 lines added, 2 lines removed)

tests.py (1 lines added, 1 lines removed)

Up to file-list docs/.buildinfo:

1
1
# Sphinx build info version 1
2
2
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3
config: 4702369698cd000d34fde9c8155e9cf5
3
config: 667b97e8fd7a1c34d350e43d2f86f81d
4
4
tags: fbb0d17656682115ca4d033fb2f83ba1

Up to file-list docs/_static/basic.css:

@@ -332,6 +332,18 @@ dl.glossary dt {
332
332
    background-color: #ffa
333
333
}
334
334
335
.line-block {
336
    display: block;
337
    margin-top: 1em;
338
    margin-bottom: 1em;
339
}
340
341
.line-block .line-block {
342
    margin-top: 0;
343
    margin-bottom: 0;
344
    margin-left: 1.5em;
345
}
346
335
347
/* -- code displays --------------------------------------------------------- */
336
348
337
349
pre {
@@ -392,7 +404,7 @@ span.eqno {
392
404
    div.document,
393
405
    div.documentwrapper,
394
406
    div.bodywrapper {
395
        margin: 0;
407
        margin: 0 !important;
396
408
        width: 100%;
397
409
    }
398
410

Up to file-list docs/_static/default.css:

@@ -208,3 +208,11 @@ tt {
208
208
    padding: 0 1px 0 1px;
209
209
    font-size: 0.95em;
210
210
}
211
212
.warning tt {
213
    background: #efc2c2;
214
}
215
216
.note tt {
217
    background: #d6d6d6;
218
}

Up to file-list docs/conf.py:

@@ -42,9 +42,9 @@ copyright = '2008-2009, Gustavo Picon'
42
42
# other places throughout the built documents.
43
43
#
44
44
# The short X.Y version.
45
version = '2.0'
45
version = '2.1a'
46
46
# The full version, including alpha/beta/rc tags.
47
release = '2.0'
47
release = '2.1a'
48
48
49
49
# There are two options for replacing |today|: either, you set today to some
50
50
# non-false value, then it is used:

Up to file-list docs/genindex.html:

5
5
  <head>
6
6
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
7
    
8
    <title>Index — numconv v2.0 documentation</title>
8
    <title>Index — numconv v2.1a documentation</title>
9
9
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
10
10
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
11
11
    <script type="text/javascript">
12
12
      var DOCUMENTATION_OPTIONS = {
13
        URL_ROOT:    '',
14
        VERSION:     '2.0',
13
        URL_ROOT:    '#',
14
        VERSION:     '2.1a',
15
15
        COLLAPSE_MODINDEX: false,
16
16
        FILE_SUFFIX: '.html',
17
17
        HAS_SOURCE:  true
19
19
    </script>
20
20
    <script type="text/javascript" src="_static/jquery.js"></script>
21
21
    <script type="text/javascript" src="_static/doctools.js"></script>
22
    <link rel="top" title="numconv v2.0 documentation" href="index.html" /> 
22
    <link rel="top" title="numconv v2.1a documentation" href="index.html" /> 
23
23
  </head>
24
24
  <body>
25
25
    <div class="related">
26
26
      <h3>Navigation</h3>
27
27
      <ul>
28
28
        <li class="right" style="margin-right: 10px">
29
          <a href="" title="General Index"
29
          <a href="#" title="General Index"
30
30
             accesskey="I">index</a></li>
31
31
        <li class="right" >
32
32
          <a href="modindex.html" title="Global Module Index"
33
33
             accesskey="M">modules</a> |</li>
34
        <li><a href="index.html">numconv v2.0 documentation</a> »</li> 
34
        <li><a href="index.html">numconv v2.1a documentation</a> »</li> 
35
35
      </ul>
36
36
    </div>  
37
37
131
131
      <h3>Navigation</h3>
132
132
      <ul>
133
133
        <li class="right" style="margin-right: 10px">
134
          <a href="" title="General Index"
134
          <a href="#" title="General Index"
135
135
             >index</a></li>
136
136
        <li class="right" >
137
137
          <a href="modindex.html" title="Global Module Index"
138
138
             >modules</a> |</li>
139
        <li><a href="index.html">numconv v2.0 documentation</a> »</li> 
139
        <li><a href="index.html">numconv v2.1a documentation</a> »</li> 
140
140
      </ul>
141
141
    </div>
142
142
    <div class="footer">
143
143
      © Copyright 2008-2009, Gustavo Picon.
144
      Last updated on Dec 14, 2009.
145
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
144
      Last updated on Apr 05, 2010.
145
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
146
146
    </div>
147
147
  </body>
148
148
</html>

Up to file-list docs/index.html:

5
5
  <head>
6
6
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
7
    
8
    <title>numconv — flexible numeric conversion for python — numconv v2.0 documentation</title>
8
    <title>numconv — flexible numeric conversion for python — numconv v2.1a documentation</title>
9
9
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
10
10
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
11
11
    <script type="text/javascript">
12
12
      var DOCUMENTATION_OPTIONS = {
13
        URL_ROOT:    '',
14
        VERSION:     '2.0',
13
        URL_ROOT:    '#',
14
        VERSION:     '2.1a',
15
15
        COLLAPSE_MODINDEX: false,
16
16
        FILE_SUFFIX: '.html',
17
17
        HAS_SOURCE:  true
19
19
    </script>
20
20
    <script type="text/javascript" src="_static/jquery.js"></script>
21
21
    <script type="text/javascript" src="_static/doctools.js"></script>
22
    <link rel="top" title="numconv v2.0 documentation" href="" /> 
22
    <link rel="top" title="numconv v2.1a documentation" href="#" /> 
23
23
  </head>
24
24
  <body>
25
25
    <div class="related">
31
31
        <li class="right" >
32
32
          <a href="modindex.html" title="Global Module Index"
33
33
             accesskey="M">modules</a> |</li>
34
        <li><a href="">numconv v2.0 documentation</a> »</li> 
34
        <li><a href="#">numconv v2.1a documentation</a> »</li> 
35
35
      </ul>
36
36
    </div>  
37
37
@@ -55,7 +55,7 @@ strings.</td>
55
55
</tr>
56
56
<tr class="field"><th class="field-name">license:</th><td class="field-body">Apache License 2.0</td>
57
57
</tr>
58
<tr class="field"><th class="field-name">version:</th><td class="field-body">2.0</td>
58
<tr class="field"><th class="field-name">version:</th><td class="field-body">2.1a</td>
59
59
</tr>
60
60
<tr class="field"><th class="field-name">url:</th><td class="field-body"><a class="reference external" href="http://code.tabo.pe/numconv/">http://code.tabo.pe/numconv/</a></td>
61
61
</tr>
@@ -108,8 +108,8 @@ use.</dd></dl>
108
108
<li><em>radix</em> – The base that will be used in the conversions.
109
109
The default value is 10 for decimal conversions.</li>
110
110
<li><em>alphabet</em> – <p>A string that will be used as a encoding alphabet.
111
The length of the alphabet can be longer than the radix. In this case
112
the alphabet will be internally truncated.</p>
111
The length of the alphabet can be longer than the radix. In this
112
case the alphabet will be internally truncated.</p>
113
113
<p>The default value is <a title="numconv.BASE85" class="reference internal" href="#numconv.BASE85"><tt class="xref docutils literal"><span class="pre">numconv.BASE85</span></tt></a></p>
114
114
</li>
115
115
</ul>
@@ -244,9 +244,9 @@ 2693233728041137L</pre>
244
244
      </div>
245
245
      <div class="sphinxsidebar">
246
246
        <div class="sphinxsidebarwrapper">
247
            <h3><a href="">Table Of Contents</a></h3>
247
            <h3><a href="#">Table Of Contents</a></h3>
248
248
            <ul>
249
<li><a class="reference external" href=""><tt class="docutils literal"><span class="pre">numconv</span></tt> — flexible numeric conversion for python</a><ul>
249
<li><a class="reference external" href="#"><tt class="docutils literal"><span class="pre">numconv</span></tt> — flexible numeric conversion for python</a><ul>
250
250
<li><a class="reference external" href="#numconv">numconv</a><ul>
251
251
<li><a class="reference external" href="#constants">constants</a></li>
252
252
<li><a class="reference external" href="#functions">functions</a></li>
@@ -288,13 +288,13 @@ 2693233728041137L</pre>
288
288
        <li class="right" >
289
289
          <a href="modindex.html" title="Global Module Index"
290
290
             >modules</a> |</li>
291
        <li><a href="">numconv v2.0 documentation</a> »</li> 
291
        <li><a href="#">numconv v2.1a documentation</a> »</li> 
292
292
      </ul>
293
293
    </div>
294
294
    <div class="footer">
295
295
      © Copyright 2008-2009, Gustavo Picon.
296
      Last updated on Dec 14, 2009.
297
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
296
      Last updated on Apr 05, 2010.
297
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
298
298
    </div>
299
299
  </body>
300
300
</html>

Up to file-list docs/modindex.html:

5
5
  <head>
6
6
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
7
    
8
    <title>Global Module Index — numconv v2.0 documentation</title>
8
    <title>Global Module Index — numconv v2.1a documentation</title>
9
9
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
10
10
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
11
11
    <script type="text/javascript">
12
12
      var DOCUMENTATION_OPTIONS = {
13
        URL_ROOT:    '',
14
        VERSION:     '2.0',
13
        URL_ROOT:    '#',
14
        VERSION:     '2.1a',
15
15
        COLLAPSE_MODINDEX: false,
16
16
        FILE_SUFFIX: '.html',
17
17
        HAS_SOURCE:  true
19
19
    </script>
20
20
    <script type="text/javascript" src="_static/jquery.js"></script>
21
21
    <script type="text/javascript" src="_static/doctools.js"></script>
22
    <link rel="top" title="numconv v2.0 documentation" href="index.html" />
22
    <link rel="top" title="numconv v2.1a documentation" href="index.html" />
23
23
 
24
24
25
25
    <script type="text/javascript">
36
36
          <a href="genindex.html" title="General Index"
37
37
             accesskey="I">index</a></li>
38
38
        <li class="right" >
39
          <a href="" title="Global Module Index"
39
          <a href="#" title="Global Module Index"
40
40
             accesskey="M">modules</a> |</li>
41
        <li><a href="index.html">numconv v2.0 documentation</a> »</li> 
41
        <li><a href="index.html">numconv v2.1a documentation</a> »</li> 
42
42
      </ul>
43
43
    </div>  
44
44
90
90
          <a href="genindex.html" title="General Index"
91
91
             >index</a></li>
92
92
        <li class="right" >
93
          <a href="" title="Global Module Index"
93
          <a href="#" title="Global Module Index"
94
94
             >modules</a> |</li>
95
        <li><a href="index.html">numconv v2.0 documentation</a> »</li> 
95
        <li><a href="index.html">numconv v2.1a documentation</a> »</li> 
96
96
      </ul>
97
97
    </div>
98
98
    <div class="footer">
99
99
      © Copyright 2008-2009, Gustavo Picon.
100
      Last updated on Dec 14, 2009.
101
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
100
      Last updated on Apr 05, 2010.
101
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
102
102
    </div>
103
103
  </body>
104
104
</html>

Up to file-list docs/objects.inv:

1
1
# Sphinx inventory version 1
2
2
# Project: numconv
3
# Version: 2.0
3
# Version: 2.1a
4
4
numconv mod index.html
5
5
numconv.int2str function index.html
6
6
numconv.BASE85 data index.html

Up to file-list docs/search.html:

5
5
  <head>
6
6
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
7
    
8
    <title>Search — numconv v2.0 documentation</title>
8
    <title>Search — numconv v2.1a documentation</title>
9
9
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
10
10
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
11
11
    <script type="text/javascript">
12
12
      var DOCUMENTATION_OPTIONS = {
13
        URL_ROOT:    '',
14
        VERSION:     '2.0',
13
        URL_ROOT:    '#',
14
        VERSION:     '2.1a',
15
15
        COLLAPSE_MODINDEX: false,
16
16
        FILE_SUFFIX: '.html',
17
17
        HAS_SOURCE:  true
20
20
    <script type="text/javascript" src="_static/jquery.js"></script>
21
21
    <script type="text/javascript" src="_static/doctools.js"></script>
22
22
    <script type="text/javascript" src="_static/searchtools.js"></script>
23
    <link rel="top" title="numconv v2.0 documentation" href="index.html" /> 
23
    <link rel="top" title="numconv v2.1a documentation" href="index.html" /> 
24
24
  </head>
25
25
  <body>
26
26
    <div class="related">
32
32
        <li class="right" >
33
33
          <a href="modindex.html" title="Global Module Index"
34
34
             accesskey="M">modules</a> |</li>
35
        <li><a href="index.html">numconv v2.0 documentation</a> »</li> 
35
        <li><a href="index.html">numconv v2.1a documentation</a> »</li> 
36
36
      </ul>
37
37
    </div>  
38
38
83
83
        <li class="right" >
84
84
          <a href="modindex.html" title="Global Module Index"
85
85
             >modules</a> |</li>
86
        <li><a href="index.html">numconv v2.0 documentation</a> »</li> 
86
        <li><a href="index.html">numconv v2.1a documentation</a> »</li> 
87
87
      </ul>
88
88
    </div>
89
89
  
90
90
    <div class="footer">
91
91
      © Copyright 2008-2009, Gustavo Picon.
92
      Last updated on Dec 14, 2009.
93
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
92
      Last updated on Apr 05, 2010.
93
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
94
94
    </div>
95
95
  <script type="text/javascript" src="searchindex.js"></script>
96
96

Up to file-list numconv.py:

@@ -8,7 +8,7 @@ numconv
8
8
           strings.
9
9
:copyright: 2008-2009 by Gustavo Picon
10
10
:license: Apache License 2.0
11
:version: 2.0
11
:version: 2.1a
12
12
:url: http://code.tabo.pe/numconv/
13
13
:documentation:
14
14
   `numconv-docs
@@ -46,7 +46,7 @@ constants
46
46
"""
47
47
48
48
49
__version__ = '2.0.0'
49
__version__ = '2.1.0a'
50
50
51
51
# from april fool's rfc 1924
52
52
BASE85 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' \
@@ -65,20 +65,20 @@ BASE62 = BASE85[:62]
65
65
66
66
class NumConv(object):
67
67
    """Class to create converter objects.
68
    
68
69
69
        :param radix: The base that will be used in the conversions.
70
70
           The default value is 10 for decimal conversions.
71
71
        :param alphabet: A string that will be used as a encoding alphabet.
72
           The length of the alphabet can be longer than the radix. In this case
73
           the alphabet will be internally truncated.
74
    
72
           The length of the alphabet can be longer than the radix. In this
73
           case the alphabet will be internally truncated.
74
75
75
           The default value is :data:`numconv.BASE85`
76
        
76
77
77
        :raise TypeError: when *radix* isn't an integer
78
78
        :raise ValueError: when *radix* is invalid
79
79
        :raise ValueError: when *alphabet* has duplicated characters
80
80
    """
81
    
81
82
82
    def __init__(self, radix=10, alphabet=BASE85):
83
83
        "basic validation and cached_map storage"
84
84
        if int(radix) != radix:
@@ -95,38 +95,38 @@ class NumConv(object):
95
95
96
96
    def int2str(self, num):
97
97
        """Converts an integer into a string.
98
    
98
99
99
        :param num: A numeric value to be converted to another base as a
100
100
                    string.
101
    
102
    
101
102
103
103
        :rtype: string
104
    
104
105
105
        :raise TypeError: when *num* isn't an integer
106
106
        :raise ValueError: when *num* isn't positive
107
    
107
108
108
        **Examples** (taken from :file:`tests.py`):
109
    
109
110
110
           3735928559 to hexadecimal::
111
    
111
112
112
               >> NumConv(16).int2str(3735928559)
113
113
               'DEADBEEF'
114
    
114
115
115
           19284 to binary::
116
    
116
117
117
               >> NumConv(2).int2str(19284)
118
118
               '100101101010100'
119
    
119
120
120
           37 to base 4 using a custom dictionary::
121
    
121
122
122
               >> NumConv(4, 'rofl').int2str(37)
123
123
               'foo'
124
    
124
125
125
           Very large number to :data:`~numconv.BASE85`::
126
    
126
127
127
               >> NumConv(85).int2str(2693233728041137L)
128
128
               '~123AFz@'
129
    
129
130
130
        """
131
131
        if int(num) != num:
132
132
            raise TypeError('number must be an integer')
@@ -143,42 +143,41 @@ class NumConv(object):
143
143
                break
144
144
            num //= radix
145
145
        return ret
146
    
147
    
146
148
147
    def str2int(self, num):
149
148
        """Converts a string into an integer.
150
    
149
151
150
        If possible, the built-in python conversion will be used for speed
152
151
        purposes.
153
    
152
154
153
        :param num: A string that will be converted to an integer.
155
    
154
156
155
        :rtype: integer
157
    
156
158
157
        :raise ValueError: when *num* is invalid
159
    
158
160
159
        **Examples** (taken from :file:`tests.py`):
161
    
160
162
161
           Hexadecimal 'DEADBEEF' to integer::
163
    
162
164
163
              >> NumConv(16).str2int('DEADBEEF')
165
164
              3735928559L
166
    
165
167
166
           Binary '100101101010100' to integer::
168
    
167
169
168
               >> NumConv(2).str2int('100101101010100')
170
169
               19284
171
    
170
172
171
           Base 4 with custom encoding 'foo' to integer::
173
    
172
174
173
               >> NumConv(4, 'rofl').str2int('foo')
175
174
               37
176
    
175
177
176
           :data:`~numconv.BASE85` '~123AFz@' to integer::
178
    
177
179
178
               >> NumConv(85).str2int('~123AFz@')
180
179
               2693233728041137L
181
    
180
182
181
        """
183
182
        radix, alphabet = self.radix, self.alphabet
184
183
        if radix <= 36 and alphabet[:radix].lower() == BASE85[:radix].lower():
@@ -197,7 +196,7 @@ def int2str(num, radix=10, alphabet=BASE
197
196
    "helper for quick base conversions from integers to strings"
198
197
    return NumConv(radix, alphabet).int2str(num)
199
198
199
200
200
def str2int(num, radix=10, alphabet=BASE85):
201
201
    "helper for quick base conversions from strings to integers"
202
202
    return NumConv(radix, alphabet).str2int(num)
203

Up to file-list setup.py:

3
3
import os
4
4
from distutils.core import setup
5
5
6
version = '2.0'
6
version = '2.1a'
7
7
8
8
classifiers = [
9
9
    "Development Status :: 5 - Production/Stable",
@@ -15,7 +15,7 @@ classifiers = [
15
15
]
16
16
17
17
root_dir = os.path.dirname(__file__)
18
long_desc = open((root_dir if root_dir else '.')+'/README').read()
18
long_desc = open((root_dir if root_dir else '.') + '/README').read()
19
19
20
20
setup(
21
21
    name='numconv',

Up to file-list tests.py:

@@ -97,7 +97,7 @@ class BaseconvSanity(unittest.TestCase):
97
97
        """sanity check: testing a large interval and lots of radixes"""
98
98
        for radix in range(2, len(BASE85)):
99
99
            ncobj = NumConv(radix)
100
            for num in range(1000) + [10**x for x in range(5, 15)]:
100
            for num in range(1000) + [10 ** x for x in range(5, 15)]:
101
101
                self.assertEqual(num, ncobj.str2int(ncobj.int2str(num)))
102
102
103
103
if __name__ == "__main__":