pytorch/torch/utils/hooks.py
Luke Yeager e7c1e6a8e3 [pep8] Fix most lint automatically with autopep8
Here's the command I used to invoke autopep8 (in parallel!):

    git ls-files | grep '\.py$' | xargs -n1 -P`nproc` autopep8 -i

Several rules are ignored in setup.cfg. The goal is to let autopep8
handle everything which it can handle safely, and to disable any rules
which are tricky or controversial to address. We may want to come back
and re-enable some of these rules later, but I'm trying to make this
patch as safe as possible.

Also configures flake8 to match pep8's behavior.

Also configures TravisCI to check the whole project for lint.
2017-01-28 01:15:51 +01:00

21 lines
494 B
Python

import weakref
class RemovableHandle(object):
"""A handle which provides the capability to remove a hook."""
def __init__(self, hooks_dict):
self.hooks_dict_ref = weakref.ref(hooks_dict)
def remove(self):
hooks_dict = self.hooks_dict_ref()
key = id(self)
if hooks_dict is not None and key in hooks_dict:
del hooks_dict[key]
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self.remove()