Skip to content

Problem 4: Pretty Printer (100pts)

Problem

Part 1

Your first task is to define pretty printer methods for Pet, Cat and NoisyCat.

We start with implementing functions named to_str for Pet, Cat, and NoisyCat classes.

For Pet, the output should be:

您的第一个任务是为 PetCatNoisyCat 类定义漂亮的打印方法。

我们从为 PetCatNoisyCat 类实现名为 to_str 的函数开始。

对于 Pet,输出应该为:

>>> kyubey = Pet('Kyubey', 'Incubator')
>>> kyubey.to_str()
'(Kyubey, Incubator)'

For Cat and NoisyCat, the output should be:

对于 CatNoisyCat,输出应该为:

>>> 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 underlined

Windows 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_print should print object with form type(to_str). The type is the class name of the input object, the to_str is the return value of object.to_str(). For example:

pretty_print 应该以 type(to_str) 的形式打印对象。其中 type 是输入对象的类名,to_strobject.to_str() 的返回值。例如:

>>> kyubey = Pet('Kyubey', 'Incubator')
>>> pretty_print(kyubey)
Pet(Kyubey, Incubator)

example

We ignore the ascii escape code in examples, you can check the real output in doctest

我们忽略示例中的 ascii 转义码,您可以在 doctest 中查看实际输出

Note:

  • type could be obtained by python builtin type(obj).__name__. E.g. type(kyubey).__name__ is Pet
  • The type part of the printed str should be displayed with color defined by Colors.OKBLUE. For most device/terminal, this color should be blue.
  • The to_str part of the printed str should be displayed with color defined by Colors.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_print method to all Pet, Cat and NoisyCat classes. For example:

您的第二个任务是将已定义的 pretty_print 方法注入到所有的 PetCatNoisyCat 类中。例如:

>>> 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 Pet class.

为了顺利支持这种方法调用,请编辑 Pet 类的声明。

Test your code using python -i lab06.py with:

使用 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 PrintModule class.

  • 对于此任务,您可能不需要编辑超过一行代码。

  • 您应该使用提供的 PrintModule 类。