From d25a139b3dc4a9d5eacc13503f5c25a6b2991225 Mon Sep 17 00:00:00 2001 From: Spencer Phillip Young Date: Wed, 6 Feb 2019 17:41:54 -0800 Subject: [PATCH 1/3] create lovely_printer package --- .../pythonic/lovely_printer/__init__.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenges/05-text-hearts/pythonic/lovely_printer/__init__.py diff --git a/challenges/05-text-hearts/pythonic/lovely_printer/__init__.py b/challenges/05-text-hearts/pythonic/lovely_printer/__init__.py new file mode 100644 index 0000000..285d908 --- /dev/null +++ b/challenges/05-text-hearts/pythonic/lovely_printer/__init__.py @@ -0,0 +1,50 @@ +import builtins +from textwrap import dedent +from unittest import mock +from contextlib import contextmanager +from itertools import cycle +_print = builtins.print + +TEMPLATE_HEART = dedent(''' + #### #### +# # # # +# # # + # # + # # + # # + # # + # # + # +''') + + +def to_hearts(text): + chars = cycle(list(text)) + num_hearts = (len(text) // len(TEMPLATE_HEART)) + 1 + for heart in range(num_hearts): + lines = [] + for line in TEMPLATE_HEART.split('\n'): + while '#' in line: + line = line.replace('#', next(chars), 1) + lines.append(line) + yield '\n'.join(lines) + + +def _lovely_print(*things_to_print, **kwargs): + things_to_print = list(things_to_print) + for thing in things_to_print: + for heart in to_hearts(thing): + _print(heart, **kwargs) + + +@contextmanager +def lovely_print(*args, **kwargs): + with mock.patch('builtins.print', new=_lovely_print) as m: + yield m + + +if __name__ == '__main__': + with lovely_print(): + print('Hello World!') + print('I do not like', 'green eggs and ham') + print('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.') From 7b420bfab0703b75d843da6ab547a57b3ede477a Mon Sep 17 00:00:00 2001 From: Spencer Phillip Young Date: Wed, 6 Feb 2019 17:47:31 -0800 Subject: [PATCH 2/3] Create README.md --- challenges/05-text-hearts/pythonic/README.md | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenges/05-text-hearts/pythonic/README.md diff --git a/challenges/05-text-hearts/pythonic/README.md b/challenges/05-text-hearts/pythonic/README.md new file mode 100644 index 0000000..ef0155e --- /dev/null +++ b/challenges/05-text-hearts/pythonic/README.md @@ -0,0 +1,43 @@ +# Lovely Printer + +The idea here is simple: You have a program that prints things. Now you can have a program that prints things in the shape of a heart! + +## Usage + +The main usage is via context manager that temporarily overrides the builtin `print`. +That is, you should be able to use this with any existing program that uses `print`! How lovely! + +Suppose you have your normal boring program + +```python +# my_program.py +def main(): + print('This is just an example') +``` +Make it lovely like so + +```python +from lovely_printer import lovely_print +from my_program import main + +with lovely_print(): + main() +# then print returns to normal +print("Isn't that lovely?") +``` + +Then you should see the output something like + +``` + This is +j u s t + a n + e + x a + m p + l e + T h + i + +Isn't that lovely? +``` From 4893fac18f34a097b31ab4da96d4ab6f2d9ad162 Mon Sep 17 00:00:00 2001 From: Spencer Phillip Young Date: Thu, 7 Feb 2019 15:30:10 -0800 Subject: [PATCH 3/3] length based on # count not total length --- challenges/05-text-hearts/pythonic/lovely_printer/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/05-text-hearts/pythonic/lovely_printer/__init__.py b/challenges/05-text-hearts/pythonic/lovely_printer/__init__.py index 285d908..08bbb3e 100644 --- a/challenges/05-text-hearts/pythonic/lovely_printer/__init__.py +++ b/challenges/05-text-hearts/pythonic/lovely_printer/__init__.py @@ -20,7 +20,7 @@ def to_hearts(text): chars = cycle(list(text)) - num_hearts = (len(text) // len(TEMPLATE_HEART)) + 1 + num_hearts = (len(text) // TEMPLATE_HEART.count('#')) + 1 for heart in range(num_hearts): lines = [] for line in TEMPLATE_HEART.split('\n'):