@contextmanager
def substate_context(substate_name):
    original_getitem = State.__getitem__
    original_setitem = State.__setitem__
    original_delitem = State.__delitem__
    original_iter = State.__iter__

    def getsubitem(self, key):
        """Get an item
        """
        if not key.startswith('__'):
            key = substate_name + State.SECTION_SEPARATOR + key
        State.__setitem__ = original_setitem
        try:
            value = original_getitem(self, key)
        except Exception as err:
            raise err
        finally:
            State.__setitem__ = setsubitem
        return value

    def setsubitem(self, key, value):
        """Set an item
        """
        if not key.startswith('__'):
            key = substate_name + State.SECTION_SEPARATOR + key
        original_setitem(self, key, value)

    def delsubitem(self, key):
        """Delete an item
        """
        if not key.startswith('__'):
            key = substate_name + State.SECTION_SEPARATOR + key
        original_delitem(self, key)

    def subiter(self):
        """Iterate
        """
        return original_getitem(self, substate_name).__iter__()

    State.__getitem__ = getsubitem
    State.__setitem__ = setsubitem
    State.__delitem__ = delsubitem
    State.__iter__ = subiter

    try:
        yield
    except Exception as err:
        raise err
    finally:
        State.__getitem__ = original_getitem
        State.__setitem__ = original_setitem
        State.__delitem__ = original_delitem
        State.__iter__ = original_iter
Register a handler for a block
dataset/ETHPy150Open duerrp/pyexperiment/pyexperiment/state_context.py/substate_context--crossover with--dataset/ETHPy150Open geky/pyDAPLink/pyDAPLink/daplink/core.py/DAPLinkCore._readBlock