votes up 0

Bases greater than 36 not handled in base_repr.

Package:
numpy
github stars 17141
Exception Class:
ValueError

Raise code

""" 
    'A'
    >>> np.base_repr(32, base=16)
    '20'

    """
    digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    if base > len(digits):
        raise ValueError("Bases greater than 36 not handled in base_repr.")
    elif base < 2:
        raise ValueError("Bases less than 2 not handled in base_repr.")

    num = abs(number)
    res = []
    while num:
        res.append(digits[num % base])

Ways to fix

votes up 2 votes down

numpy.base_repr is used to get a string representation of a number in the given base system.

usage:

base_string = numpy.base_repr(number, base=2, padding=0)

The The valid range of the base is 2-36. Anything outside this range cause an error:

If it is greater than 36, this particular error is raised.

How to reproduce the error:

import numpy as np
base_str = np.base_repr(7, base=37, padding=3)
print(base_str)

Fix: Make sure base is in the range of 2-36.

import numpy as np
base_str = np.base_repr(7, base=32, padding=3)
print(base_str)

Jun 10, 2021 kellemnegasi answer
kellemnegasi 2.7k

Add a possible fix

Please authorize to post fix