Problem 4: Pretty Printer (100pts)
Problem
Part 1
Your first task is to define pretty printer methods for
Pet,CatandNoisyCat.We start with implementing functions named
to_strforPet,Cat, andNoisyCatclasses.For
Pet, the output should be:
您的第一个任务是为 Pet、Cat 和 NoisyCat 类定义漂亮的打印方法。
我们从为 Pet、Cat 和 NoisyCat 类实现名为 to_str 的函数开始。
对于 Pet,输出应该为:
>>> kyubey = Pet('Kyubey', 'Incubator')
>>> kyubey.to_str()
'(Kyubey, Incubator)'
For
CatandNoisyCat, the output should be:
对于 Cat 和 NoisyCat,输出应该为:
>>> vanilla = Cat('Vanilla', 'Minazuki Kashou')
>>> vanilla.to_str()
'(Vanilla, Minazuki Kashou, 9)'
>>> vanilla.lose_life()
>>> vanilla.to_str()
'(Vanilla, Minazuki Kashou, 8)'
Now, we could define a function
pretty_print, which accepts an object and print it prettily (i.e. with color).
现在,我们可以定义一个函数 pretty_print,它接受一个对象并以美观的方式(即带颜色)打印它。
To colorfully print something in terminal, this simplest way is to use ANSI escape code.
要在终端中彩色打印某些内容,最简单的方法是使用 ANSI 转义码。
class Colors: HEADER = '\033[95m' OKBLUE = '\033[34m' OKCYAN = '\033[35m' OKGREEN = '\033[96m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' >>> print(f"{Colors.OKBLUE}Hello, World{Colors.ENDC}") Hello, World # this line should be blue >>> print(f"{Colors.OKCYAN}Hello, World{Colors.ENDC}") Hello, World # this line should be red >>> print(f"{Colors.UNDERLINE}Hello, World{Colors.ENDC}") Hello, World # this line should be underlinedWindows Console may not be able to display that. Use vscode console or windows terminal instead.
For real world program, you should use a library to display colorful output. E.g. termcolor
Windows 控制台可能无法显示该内容。请改用 VS Code 控制台或 Windows 终端。
对于真实世界的程序,您应该使用库来显示彩色输出。例如 termcolor
pretty_printshould print object with formtype(to_str). Thetypeis the class name of the input object, theto_stris the return value ofobject.to_str(). For example:
pretty_print 应该以 type(to_str) 的形式打印对象。其中 type 是输入对象的类名,to_str 是 object.to_str() 的返回值。例如:
>>> kyubey = Pet('Kyubey', 'Incubator')
>>> pretty_print(kyubey)
Pet(Kyubey, Incubator)

We ignore the ascii escape code in examples, you can check the real output in doctest
我们忽略示例中的 ascii 转义码,您可以在 doctest 中查看实际输出
Note:
typecould be obtained by python builtintype(obj).__name__. E.g.type(kyubey).__name__isPet- The
typepart of the printed str should be displayed with color defined byColors.OKBLUE. For most device/terminal, this color should be blue.- The
to_strpart of the printed str should be displayed with color defined byColors.OKCYAN. For most device/terminal, this color should be like red.
注意:
type可以通过 Python 内置的type(obj).__name__获取。例如,type(kyubey).__name__是Pet- 打印字符串的
type部分应该使用Colors.OKBLUE定义的颜色显示。对于大多数设备/终端,此颜色应为蓝色。 - 打印字符串的
to_str部分应该使用Colors.OKCYAN定义的颜色显示。对于大多数设备/终端,此颜色应类似于红色。
Part 2
Your second task is to inject the defined
pretty_printmethod to allPet,CatandNoisyCatclasses. For example:
您的第二个任务是将已定义的 pretty_print 方法注入到所有的 Pet、Cat 和 NoisyCat 类中。例如:
>>> kyubey = Pet('Kyubey', 'Incubator')
>>> kyubey.pp() # the same result as `pretty_print`
Pet(Kyubey, Incubator)
To smoothly support such method call, edit the declaration of the
Petclass.
为了顺利支持这种方法调用,请编辑 Pet 类的声明。
Test your code using
python -i lab06.pywith:
使用 python -i lab06.py 测试您的代码,并输入:
>>> kyubey = Pet('Kyubey', 'Incubator')
>>> kyubey.pp()
Pet(Kyubey, Incubator)
>>> vanilla = Cat('Vanilla', 'Minazuki Kashou')
>>> vanilla.pp()
Cat(Vanilla, Minazuki Kashou, 9)
This is an ugly implementation of mixin. You can check this page for ruby style mixin, which is clear and elegant.
这是一个丑陋的 mixin 实现。您可以查看 此页面 以了解 Ruby 风格的 mixin,它清晰且优雅。
Hints
Hint1: You may not need to edit more than one line of code for this task.
Hint2: You should use the provided
PrintModuleclass.
-
对于此任务,您可能不需要编辑超过一行代码。
-
您应该使用提供的
PrintModule类。