-
Notifications
You must be signed in to change notification settings - Fork 0
/
indentprinter.py
34 lines (29 loc) · 905 Bytes
/
indentprinter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''
Indent log output in a logical way.
Try this:
```python
from indentprinter import indentPrinter
bois = ['Collie', 'Husky', 'Shoob', 'Shibe']
print('All bois:')
with indentPrinter as print:
[print(x) for x in bois]
```
'''
__all__ = ['indentPrinter', 'IndentPrinter']
class IndentPrinter:
def __init__(self, saved_print, indent_str = ' '):
self.depth = 0
self.saved_print = saved_print
self.new_line = True
self.indent_str = indent_str
def __enter__(self):
self.depth += 1
return self.print
def __exit__(self, a, b, c):
self.depth -= 1
def print(self, *args, **kw):
if self.new_line:
self.saved_print(self.indent_str * self.depth, end = '')
self.saved_print(*args, **kw)
self.new_line = {'end': '\n', **kw}['end'].endswith('\n')
indentPrinter = IndentPrinter(print)