# HG changeset patch # User Gustavo Picon # Date 1271229206 18000 # Node ID a507bcdecc113c4ef26460db48479284c13ed6be # Parent 6b6d6c1b9e5ccace509af9444a4182e6cb486719 docs diff -r 6b6d6c1b9e5ccace509af9444a4182e6cb486719 -r a507bcdecc113c4ef26460db48479284c13ed6be docs/index.rst --- a/docs/index.rst Tue Apr 13 23:29:56 2010 -0500 +++ b/docs/index.rst Wed Apr 14 02:13:26 2010 -0500 @@ -1,21 +1,102 @@ -:mod:`numconv` --- flexible numeric conversion for python -========================================================= +numconv +======= -.. automodule:: numconv +:synopsys: Convert strings to numbers and numbers to strings. +:copyright: 2008-2009 by Gustavo Picon +:license: Apache License 2.0 +:version: 2.1a +:url: http://code.tabo.pe/numconv/ +:documentation: + `numconv-docs + `_ +:examples: + `numconv-tests + `_ - .. autoclass:: NumConv - :show-inheritance: +:mod:`numconv` converts a string into a number and a number into a string +using default or user supplied encoding alphabets. - .. automethod:: int2str - .. automethod:: str2int +.. module:: numconv - functions - ~~~~~~~~~ - - .. autofunction:: int2str +.. autoclass:: NumConv + :show-inheritance: - .. autofunction:: str2int + .. automethod:: int2str + + **Examples** (taken from :file:`tests.py`): + + 3735928559 to hexadecimal:: + + >> NumConv(16).int2str(3735928559) + 'DEADBEEF' + + 19284 to binary:: + + >> NumConv(2).int2str(19284) + '100101101010100' + + 37 to base 4 using a custom dictionary:: + + >> NumConv(4, 'rofl').int2str(37) + 'foo' + + Very large number to :data:`~numconv.BASE85`:: + + >> NumConv(85).int2str(2693233728041137) + '~123AFz@' + + .. automethod:: str2int + + **Examples** (taken from :file:`tests.py`): + + Hexadecimal 'DEADBEEF' to integer:: + + >> NumConv(16).str2int('DEADBEEF') + 3735928559 + + Binary '100101101010100' to integer:: + + >> NumConv(2).str2int('100101101010100') + 19284 + + Base 4 with custom encoding 'foo' to integer:: + + >> NumConv(4, 'rofl').str2int('foo') + 37 + + :data:`~numconv.BASE85` '~123AFz@' to integer:: + + >> NumConv(85).str2int('~123AFz@') + 2693233728041137 + + +.. 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 + it 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. + +.. data:: BASE62 + + Useful for URL shorteners. + + +functions +--------- + +.. autofunction:: int2str + +.. autofunction:: str2int diff -r 6b6d6c1b9e5ccace509af9444a4182e6cb486719 -r a507bcdecc113c4ef26460db48479284c13ed6be numconv.py --- a/numconv.py Tue Apr 13 23:29:56 2010 -0500 +++ b/numconv.py Wed Apr 14 02:13:26 2010 -0500 @@ -1,47 +1,7 @@ -# -*- coding: utf-8 -*- -""" +"""Convert strings to numbers and numbers to strings. -numconv -------- - -:synopsys: Python library to convert strings to numbers and numbers to - strings. -:copyright: 2008-2009 by Gustavo Picon -:license: Apache License 2.0 -:version: 2.1a -:url: http://code.tabo.pe/numconv/ -:documentation: - `numconv-docs - `_ -:examples: - `numconv-tests - `_ - - -: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 it 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. - -.. data:: BASE62 - - Useful for URL shorteners. +Gustavo Picon +http://code.tabo.pe/numconv/ """ @@ -99,34 +59,10 @@ :param num: A numeric value to be converted to another base as a string. - :rtype: string :raise TypeError: when *num* isn't an integer :raise ValueError: when *num* isn't positive - - **Examples** (taken from :file:`tests.py`): - - 3735928559 to hexadecimal:: - - >> NumConv(16).int2str(3735928559) - 'DEADBEEF' - - 19284 to binary:: - - >> NumConv(2).int2str(19284) - '100101101010100' - - 37 to base 4 using a custom dictionary:: - - >> NumConv(4, 'rofl').int2str(37) - 'foo' - - Very large number to :data:`~numconv.BASE85`:: - - >> NumConv(85).int2str(2693233728041137) - '~123AFz@' - """ if int(num) != num: raise TypeError('number must be an integer') @@ -155,29 +91,6 @@ :rtype: integer :raise ValueError: when *num* is invalid - - **Examples** (taken from :file:`tests.py`): - - Hexadecimal 'DEADBEEF' to integer:: - - >> NumConv(16).str2int('DEADBEEF') - 3735928559 - - Binary '100101101010100' to integer:: - - >> NumConv(2).str2int('100101101010100') - 19284 - - Base 4 with custom encoding 'foo' to integer:: - - >> NumConv(4, 'rofl').str2int('foo') - 37 - - :data:`~numconv.BASE85` '~123AFz@' to integer:: - - >> NumConv(85).str2int('~123AFz@') - 2693233728041137 - """ radix, alphabet = self.radix, self.alphabet if radix <= 36 and alphabet[:radix].lower() == BASE85[:radix].lower():