Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions challenges/05-text-hearts/pythonic/README.md
Original file line number Diff line number Diff line change
@@ -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?
```
50 changes: 50 additions & 0 deletions challenges/05-text-hearts/pythonic/lovely_printer/__init__.py
Original file line number Diff line number Diff line change
@@ -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) // TEMPLATE_HEART.count('#')) + 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.')