You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
668 B
Python
25 lines
668 B
Python
2 years ago
|
from contextlib import contextmanager
|
||
|
|
||
|
|
||
|
@contextmanager
|
||
|
def monkeypatch(obj, attribute_name, new_value):
|
||
|
"""
|
||
|
Like pytest's monkeypatch, but as a value manager.
|
||
|
"""
|
||
|
old_value = getattr(obj, attribute_name)
|
||
|
try:
|
||
|
setattr(obj, attribute_name, new_value)
|
||
|
yield
|
||
|
finally:
|
||
|
setattr(obj, attribute_name, old_value)
|
||
|
|
||
|
|
||
|
def indent_block(text, indention=' '):
|
||
|
"""This function indents a text block with a default of four spaces."""
|
||
|
temp = ''
|
||
|
while text and text[-1] == '\n':
|
||
|
temp += text[-1]
|
||
|
text = text[:-1]
|
||
|
lines = text.split('\n')
|
||
|
return '\n'.join(map(lambda s: indention + s, lines)) + temp
|