Last active
June 15, 2019 19:19
-
-
Save ben-cohen/fd37ae2f6f02eacd963372b1b56b9562 to your computer and use it in GitHub Desktop.
GDB Python pretty printer for the int datatype to Roman Numerals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# GDB Python pretty printer for the int datatype to Roman Numerals | |
# | |
# Ben Cohen, June 2019 | |
# | |
import gdb | |
# GDB pretty printer for the "int" type | |
class IntPrinter: | |
def __init__(self, val): | |
self.val = val | |
def to_string(self): | |
ret = int_to_Roman(int(self.val)) | |
if ret == "": | |
return "exceptis" | |
else: | |
return ret | |
def lookup_type(val): | |
if str(val.type) == "int": | |
return IntPrinter(val) | |
return None | |
gdb.pretty_printers.append(lookup_type) | |
# Convert num to Roman numerals. | |
# Code from https://www.w3resource.com/python-exercises/class-exercises/python-class-exercise-1.php | |
# (Attribution-NonCommercial-ShareAlike 3.0 Unported licence) | |
def int_to_Roman(num): | |
val = [ | |
1000, 900, 500, 400, | |
100, 90, 50, 40, | |
10, 9, 5, 4, | |
1 | |
] | |
syb = [ | |
"M", "CM", "D", "CD", | |
"C", "XC", "L", "XL", | |
"X", "IX", "V", "IV", | |
"I" | |
] | |
roman_num = '' | |
i = 0 | |
while num > 0: | |
for _ in range(num // val[i]): | |
roman_num += syb[i] | |
num -= val[i] | |
i += 1 | |
return roman_num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment