Format: text/x-python
⬇ Download File (668 B)
   1  from contextlib import contextmanager
   2  
   3  
   4  @contextmanager
   5  def monkeypatch(obj, attribute_name, new_value):
   6      """
   7      Like pytest's monkeypatch, but as a value manager.
   8      """
   9      old_value = getattr(obj, attribute_name)
  10      try:
  11          setattr(obj, attribute_name, new_value)
  12          yield
  13      finally:
  14          setattr(obj, attribute_name, old_value)
  15  
  16  
  17  def indent_block(text, indention='    '):
  18      """This function indents a text block with a default of four spaces."""
  19      temp = ''
  20      while text and text[-1] == '\n':
  21          temp += text[-1]
  22          text = text[:-1]
  23      lines = text.split('\n')
  24      return '\n'.join(map(lambda s: indention + s, lines)) + temp
  25