BaseN manual

About <module basen>

This is python module that treats notation system of base n.
You can define the numeral system by the base(radix) or digits(numerals).

This was made referring to Math::BaseCalc(Perl module).

This module contains only one class BaseN.
This can do import like this.

>>> from basen import BaseN  # or *

Define your numeral system <class BaseN>

by the base(radix) number

This can be changed/checked later by the n() method.

>>> bn = BaseN(2)   # minimum
>>> bn = BaseN(62)  # maximum

by the preset name

This can be changed/checked later by the n() method.

>>> bn = BaseN('bin')  # same as BaseN(2)
>>> bn = BaseN('oct')  # same as BaseN(8)
>>> bn = BaseN('hex')  # same as BaseN(16)
>>> bn = BaseN('HEX')  # same as BaseN(digits='0123456789ABCDEF')

by the digits(numerals)

Digits initialized from sequence's items.
This can be changed/checked later by the digits() method.

>>> bn = BaseN(digits='01')        # same as BaseN(2)
>>> bn = BaseN(digits=['0', '1'])  # same as BaseN(2)
>>> bn = BaseN(digits='0123')      # same as BaseN(4)
>>> bn = BaseN(digits='0abcdefg')  # octal notation by alphabets

by the strange digits

>>> bn = BaseN(digits=[False, True])   # binary notation by bools
>>> bn = BaseN(digits=['Zero', 'One']) # binary notation by words
>>> bn = BaseN(digits=range(10))       # decimal notation by integers
>>> bn = BaseN(digits=['XII', 'I', 'II', 'III', 'IIII',
...            'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI'])  # clock ?

options

point
Object used to express decimal(fraction) point.
negative_sign
Object used to express negative number.
after_point
Number of digits of Fraction part. This is used in str() and list() methods.
>>> bn = BaseN(4,
...            point='.',
...            negative_sign='-',
...            after_point=17)    # All are the values of default

Number to digits

Get the string on your numeral system <method str>

>>> bn = BaseN(digits='0abc')
>>> bn.str(4)
'a0'
>>> bn.str(-1)
'-a'
>>> bn.str(0.5)
'0.b'
>>> bn.str(1.0)
'a.0'
>>> bn.str(0.1)
'0.0abababababababab'

The str method will not work if your digits is not string.
(Use list() method.)

>>> bn = BaseN(digits=[False, True])  # binary notation by bools
>>> bn.str(0)                         # !! error example
Traceback (most recent call last):
    ...
TypeError: 'bool' object is unsubscriptable

The return value of the str() method can be nonsense.
(Use list() method.)

>>> bn = BaseN(digits=['O', 'I', 'II', 'III'])
>>> bn.str(27)
'IIIIII'
>>> bn.str(57)
'IIIIII'
>>> bn.str(1365)
'IIIIII'

Get the list on your numeral system <method list>

This method works even if the str() method doesn't work.

>>> bn = BaseN(digits='0abc')
>>> bn.list(4)
['a', '0']

>>> bn = BaseN(digits=[False, True])
>>> bn.list(0)
[False]
>>> bn.list(4)
[True, False, False]

>>> bn = BaseN(digits=['O', 'I', 'II', 'III'])
>>> bn.str(27)
'IIIIII'
>>> bn.list(27)
['I', 'II', 'III']

Digits to number

Get the integer <method int>

>>> bn = BaseN(digits='0abc')
>>> bn.int('a0')
4

>>> bn = BaseN(digits=['O', 'I', 'II', 'III'])
>>> bn.int(['I','II','III'])
27

Get the float <method float>

>>> bn = BaseN(digits='0abc')
>>> bn.float('a0.a')
4.25
>>> bn.float('a0')
4.0

Other methods and propertys

<method n>

n() returns the base(radix) number when there is no argument.

>>> bn = BaseN(digits='0abc')
>>> bn.n()
4

When base(radix) number or the preset name is given,
n() redefines your numeral system.
>>> bn = BaseN(2)
>>> bn.n()
2
>>> bn.n(4)
>>> bn.n()
4

<method digits>

digits() returns digits when there is no argument.

>>> bn = BaseN(4)
>>> bn.digits()
('0', '1', '2', '3')

When digits is given, digits() redefines your numeral system.
>>> bn = BaseN(2)
>>> bn.digits()
('0', '1')
>>> bn.digits('0abc')
>>> bn.digits()
('0', 'a', 'b', 'c')

<property point>

Object of decimal point.
This default value is '.'.

>>> bn = BaseN(2)
>>> bn.point
'.'
>>> bn.str(0.5)
'0.1'

>>> bn.point = '@'
>>> bn.str(0.5)
'0@1'

<property negative_sign>

Object of negative sign.
This default value is '-'.

>>> bn = BaseN(2)
>>> bn.negative_sign
'-'
>>> bn.str(-2)
'-10'

>>> bn.negative_sign = '*'
>>> bn.str(-2)
'*10'

<property after_point>

Number of digits of Fraction part.
This is used in str() and list() methods.
This default value is 17.

>>> b = BaseN(2)
>>> b.after_point
17
>>> b.str(1.1)
'1.00011001100110011'

>>> b.after_point = 3
>>> b.str(1.1)
'1.000'