Context managers · 9 / 15 Exercise Medium

Decorative frame

Objective

Create a Python program with a context manager written as a generator function and the @contextmanager decorator from the contextlib module.

Instructions

  • Define frame(title): a function with @contextmanager that, before the yield, shows a line of =, another with | title | and another of =. The number of signs is the length of the title plus 4.
  • The yield returns the length of the title. After the yield (inside finally, so that it always runs) show a line of - with the same length.
  • Read an integer n and then n texts. For each one use with frame(text) as length, and inside show Characters: length and the text reversed.
Input
2
hola
ab
Output
========
| hola |
========
Characters: 4
aloh
--------
======
| ab |
======
Characters: 2
ba
------

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise