Source code for jacinle.utils.context
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# File : context.py
# Author : Jiayuan Mao
# Email : maojiayuan@gmail.com
# Date : 12/31/16
#
# This file is part of Jacinle.
# Distributed under terms of the MIT license.
__all__ = ['EmptyContext', 'KeyboardInterruptContext', 'SaverContext']
[docs]
class EmptyContext(object):
"""An empty context manager that does nothing."""
def __enter__(self):
return
def __exit__(self, exc_type, exc_val, exc_tb):
return
[docs]
class KeyboardInterruptContext(object):
"""A context manager that catches KeyboardInterrupt and does nothing."""
def __enter__(self):
return
def __exit__(self, exc_type, exc_val, exc_tb):
if isinstance(exc_val, KeyboardInterrupt):
return True
[docs]
class SaverContext(object):
"""Save some information before entering the context. Restore the information after.
Inspired by: https://github.com/caelan/pybullet-planning/blob/master/pybullet_tools/utils.py
"""
def __enter__(self):
self.save()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.restore()
[docs]
def save(self):
pass
[docs]
def restore(self):
raise NotImplementedError()