Refs #32712 -- Removed django.utils.baseconv module per deprecation timeline.

This commit is contained in:
Mariusz Felisiak 2023-01-06 09:33:02 +01:00
parent 6c0539ed7c
commit ef46f3778a
3 changed files with 2 additions and 175 deletions

View File

@ -1,115 +0,0 @@
# RemovedInDjango50Warning
# Copyright (c) 2010 Guilherme Gondim. All rights reserved.
# Copyright (c) 2009 Simon Willison. All rights reserved.
# Copyright (c) 2002 Drew Perttula. All rights reserved.
#
# License:
# Python Software Foundation License version 2
#
# See the file "LICENSE" for terms & conditions for usage, and a DISCLAIMER OF
# ALL WARRANTIES.
#
# This Baseconv distribution contains no GNU General Public Licensed (GPLed)
# code so it may be used in proprietary projects just like prior ``baseconv``
# distributions.
#
# All trademarks referenced herein are property of their respective holders.
#
"""
Convert numbers from base 10 integers to base X strings and back again.
Sample usage::
>>> base20 = BaseConverter('0123456789abcdefghij')
>>> base20.encode(1234)
'31e'
>>> base20.decode('31e')
1234
>>> base20.encode(-1234)
'-31e'
>>> base20.decode('-31e')
-1234
>>> base11 = BaseConverter('0123456789-', sign='$')
>>> base11.encode(-1234)
'$-22'
>>> base11.decode('$-22')
-1234
"""
import warnings
from django.utils.deprecation import RemovedInDjango50Warning
warnings.warn(
"The django.utils.baseconv module is deprecated.",
category=RemovedInDjango50Warning,
stacklevel=2,
)
BASE2_ALPHABET = "01"
BASE16_ALPHABET = "0123456789ABCDEF"
BASE56_ALPHABET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz"
BASE36_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"
BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
BASE64_ALPHABET = BASE62_ALPHABET + "-_"
class BaseConverter:
decimal_digits = "0123456789"
def __init__(self, digits, sign="-"):
self.sign = sign
self.digits = digits
if sign in self.digits:
raise ValueError("Sign character found in converter base digits.")
def __repr__(self):
return "<%s: base%s (%s)>" % (
self.__class__.__name__,
len(self.digits),
self.digits,
)
def encode(self, i):
neg, value = self.convert(i, self.decimal_digits, self.digits, "-")
if neg:
return self.sign + value
return value
def decode(self, s):
neg, value = self.convert(s, self.digits, self.decimal_digits, self.sign)
if neg:
value = "-" + value
return int(value)
def convert(self, number, from_digits, to_digits, sign):
if str(number)[0] == sign:
number = str(number)[1:]
neg = 1
else:
neg = 0
# make an integer out of the number
x = 0
for digit in str(number):
x = x * len(from_digits) + from_digits.index(digit)
# create the result in base 'len(to_digits)'
if x == 0:
res = to_digits[0]
else:
res = ""
while x > 0:
digit = x % len(to_digits)
res = to_digits[digit] + res
x = int(x // len(to_digits))
return neg, res
base2 = BaseConverter(BASE2_ALPHABET)
base16 = BaseConverter(BASE16_ALPHABET)
base36 = BaseConverter(BASE36_ALPHABET)
base56 = BaseConverter(BASE56_ALPHABET)
base62 = BaseConverter(BASE62_ALPHABET)
base64 = BaseConverter(BASE64_ALPHABET, sign="$")

View File

@ -257,6 +257,8 @@ to remove usage of these features.
* The ``SERIALIZE`` test setting is removed.
* The undocumented ``django.utils.baseconv`` module is removed.
See :ref:`deprecated-features-4.1` for details on these changes, including how
to remove usage of these features.

View File

@ -1,60 +0,0 @@
from unittest import TestCase
from django.test import ignore_warnings
from django.utils.deprecation import RemovedInDjango50Warning
with ignore_warnings(category=RemovedInDjango50Warning):
from django.utils.baseconv import (
BaseConverter,
base2,
base16,
base36,
base56,
base62,
base64,
)
# RemovedInDjango50Warning
class TestBaseConv(TestCase):
def test_baseconv(self):
nums = [-(10**10), 10**10, *range(-100, 100)]
for converter in [base2, base16, base36, base56, base62, base64]:
for i in nums:
self.assertEqual(i, converter.decode(converter.encode(i)))
def test_base11(self):
base11 = BaseConverter("0123456789-", sign="$")
self.assertEqual(base11.encode(1234), "-22")
self.assertEqual(base11.decode("-22"), 1234)
self.assertEqual(base11.encode(-1234), "$-22")
self.assertEqual(base11.decode("$-22"), -1234)
def test_base20(self):
base20 = BaseConverter("0123456789abcdefghij")
self.assertEqual(base20.encode(1234), "31e")
self.assertEqual(base20.decode("31e"), 1234)
self.assertEqual(base20.encode(-1234), "-31e")
self.assertEqual(base20.decode("-31e"), -1234)
def test_base64(self):
self.assertEqual(base64.encode(1234), "JI")
self.assertEqual(base64.decode("JI"), 1234)
self.assertEqual(base64.encode(-1234), "$JI")
self.assertEqual(base64.decode("$JI"), -1234)
def test_base7(self):
base7 = BaseConverter("cjdhel3", sign="g")
self.assertEqual(base7.encode(1234), "hejd")
self.assertEqual(base7.decode("hejd"), 1234)
self.assertEqual(base7.encode(-1234), "ghejd")
self.assertEqual(base7.decode("ghejd"), -1234)
def test_exception(self):
with self.assertRaises(ValueError):
BaseConverter("abc", sign="a")
self.assertIsInstance(BaseConverter("abc", sign="d"), BaseConverter)
def test_repr(self):
base7 = BaseConverter("cjdhel3", sign="g")
self.assertEqual(repr(base7), "<BaseConverter: base7 (cjdhel3)>")