1
0
mirror of https://github.com/mnaberez/py65.git synced 2024-06-06 20:29:34 +00:00

Replace if/elif/elif/else with dict lookup

This commit is contained in:
Mike Naberezny 2017-09-19 15:30:15 -07:00
parent 9d8957e32b
commit cc40261499

View File

@ -3,17 +3,10 @@ def itoa(num, base=10):
is silently passed through. Other bases raise a ValueError. Returns a
string with hex digits lowercase.
"""
if base == 2:
newnum = "{0:b}".format(num)
elif base == 16:
newnum = "{0:x}".format(num)
elif base == 10:
newnum = "{0}".format(num)
else:
msg = 'Could not convert number "{0}" to base "{1}"'.format(num, base)
raise ValueError(msg)
return newnum
fmt = _itoa_fmts.get(base)
if fmt is None:
raise ValueError("Unsupported base: %r" % base)
return fmt.format(num)
def convert_to_bin(bcd):
@ -24,6 +17,13 @@ def convert_to_bcd(bin):
return _bin2bcd[bin]
_itoa_fmts = {
2: "{0:b}",
10: "{0}",
16: "{0:x}"
}
_bcd2bin = (
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 20, 21, 22, 23, 24, 25,