mirror of
https://github.com/progval/Limnoria.git
synced 2025-04-29 06:51:08 -05:00
Modified base command to allow conversion between non-decimal bases
This commit is contained in:
parent
6a2f9010b8
commit
4d296473e5
@ -64,22 +64,52 @@ class Math(callbacks.Privmsg):
|
|||||||
# one can do list comprehensions or call __...__ functions.
|
# one can do list comprehensions or call __...__ functions.
|
||||||
###
|
###
|
||||||
def base(self, irc, msg, args):
|
def base(self, irc, msg, args):
|
||||||
"""<base> <number>
|
"""<fromBase> [<toBase>] <number>
|
||||||
|
|
||||||
Converts from base <base> the number <number>
|
Converts from base <fromBase> to base <toBase>. If <toBase> is left
|
||||||
|
out, it converts to decimal.
|
||||||
"""
|
"""
|
||||||
(base, number) = privmsgs.getArgs(args, required=2)
|
(frm, to, number) = privmsgs.getArgs(args, required=2, optional=1)
|
||||||
|
if number == '':
|
||||||
|
number = to
|
||||||
|
to = '10'
|
||||||
try:
|
try:
|
||||||
base = int(base)
|
frm = int(frm)
|
||||||
if not (2 <= base <= 36):
|
to = int(to)
|
||||||
|
if not ((2 <= frm <= 36) and (2 <= to <= 36)):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error('<base> must be a number between 2 and 36.')
|
irc.error('Bases must be numbers between 2 and 36.')
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
irc.reply(str(long(number, int(base))))
|
irc.reply(self._convertBaseToBase(number, to, frm))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error('Invalid <number> for base %s: %s' % (base, number))
|
irc.error('Invalid <number> for base %s: %s' % (frm, number))
|
||||||
|
|
||||||
|
def _convertDecimalToBase(self, number, base):
|
||||||
|
"""
|
||||||
|
Convert a decimal number to another base; returns a string.
|
||||||
|
"""
|
||||||
|
valStr = ''
|
||||||
|
if number == 0:
|
||||||
|
return '0'
|
||||||
|
while number != 0:
|
||||||
|
digit = number % base
|
||||||
|
if digit >= 10:
|
||||||
|
digit = string.uppercase[digit - 10]
|
||||||
|
else:
|
||||||
|
digit = str(digit)
|
||||||
|
valStr = digit + valStr
|
||||||
|
number = int(math.floor(number / base))
|
||||||
|
return valStr
|
||||||
|
|
||||||
|
def _convertBaseToBase(self, number, toBase, fromBase):
|
||||||
|
"""
|
||||||
|
Convert a number from any base, 2 through 36, to any other
|
||||||
|
base, 2 through 36. Returns a string.
|
||||||
|
"""
|
||||||
|
number = long(str(number), fromBase)
|
||||||
|
return self._convertDecimalToBase(number, toBase)
|
||||||
|
|
||||||
_mathEnv = {'__builtins__': types.ModuleType('__builtins__'), 'i': 1j}
|
_mathEnv = {'__builtins__': types.ModuleType('__builtins__'), 'i': 1j}
|
||||||
_mathEnv.update(math.__dict__)
|
_mathEnv.update(math.__dict__)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user