Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

baseconv.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # RemovedInDjango50Warning
  2. # Copyright (c) 2010 Guilherme Gondim. All rights reserved.
  3. # Copyright (c) 2009 Simon Willison. All rights reserved.
  4. # Copyright (c) 2002 Drew Perttula. All rights reserved.
  5. #
  6. # License:
  7. # Python Software Foundation License version 2
  8. #
  9. # See the file "LICENSE" for terms & conditions for usage, and a DISCLAIMER OF
  10. # ALL WARRANTIES.
  11. #
  12. # This Baseconv distribution contains no GNU General Public Licensed (GPLed)
  13. # code so it may be used in proprietary projects just like prior ``baseconv``
  14. # distributions.
  15. #
  16. # All trademarks referenced herein are property of their respective holders.
  17. #
  18. """
  19. Convert numbers from base 10 integers to base X strings and back again.
  20. Sample usage::
  21. >>> base20 = BaseConverter('0123456789abcdefghij')
  22. >>> base20.encode(1234)
  23. '31e'
  24. >>> base20.decode('31e')
  25. 1234
  26. >>> base20.encode(-1234)
  27. '-31e'
  28. >>> base20.decode('-31e')
  29. -1234
  30. >>> base11 = BaseConverter('0123456789-', sign='$')
  31. >>> base11.encode(-1234)
  32. '$-22'
  33. >>> base11.decode('$-22')
  34. -1234
  35. """
  36. import warnings
  37. from django.utils.deprecation import RemovedInDjango50Warning
  38. warnings.warn(
  39. "The django.utils.baseconv module is deprecated.",
  40. category=RemovedInDjango50Warning,
  41. stacklevel=2,
  42. )
  43. BASE2_ALPHABET = "01"
  44. BASE16_ALPHABET = "0123456789ABCDEF"
  45. BASE56_ALPHABET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz"
  46. BASE36_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"
  47. BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  48. BASE64_ALPHABET = BASE62_ALPHABET + "-_"
  49. class BaseConverter:
  50. decimal_digits = "0123456789"
  51. def __init__(self, digits, sign="-"):
  52. self.sign = sign
  53. self.digits = digits
  54. if sign in self.digits:
  55. raise ValueError("Sign character found in converter base digits.")
  56. def __repr__(self):
  57. return "<%s: base%s (%s)>" % (
  58. self.__class__.__name__,
  59. len(self.digits),
  60. self.digits,
  61. )
  62. def encode(self, i):
  63. neg, value = self.convert(i, self.decimal_digits, self.digits, "-")
  64. if neg:
  65. return self.sign + value
  66. return value
  67. def decode(self, s):
  68. neg, value = self.convert(s, self.digits, self.decimal_digits, self.sign)
  69. if neg:
  70. value = "-" + value
  71. return int(value)
  72. def convert(self, number, from_digits, to_digits, sign):
  73. if str(number)[0] == sign:
  74. number = str(number)[1:]
  75. neg = 1
  76. else:
  77. neg = 0
  78. # make an integer out of the number
  79. x = 0
  80. for digit in str(number):
  81. x = x * len(from_digits) + from_digits.index(digit)
  82. # create the result in base 'len(to_digits)'
  83. if x == 0:
  84. res = to_digits[0]
  85. else:
  86. res = ""
  87. while x > 0:
  88. digit = x % len(to_digits)
  89. res = to_digits[digit] + res
  90. x = int(x // len(to_digits))
  91. return neg, res
  92. base2 = BaseConverter(BASE2_ALPHABET)
  93. base16 = BaseConverter(BASE16_ALPHABET)
  94. base36 = BaseConverter(BASE36_ALPHABET)
  95. base56 = BaseConverter(BASE56_ALPHABET)
  96. base62 = BaseConverter(BASE62_ALPHABET)
  97. base64 = BaseConverter(BASE64_ALPHABET, sign="$")