Created
June 15, 2022 21:59
-
-
Save GenevieveBuckley/6adb2b134df3c9b17f814448a8acffc5 to your computer and use it in GitHub Desktop.
Cupy context manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CupyContextManager: | |
def __init__(self): | |
self.previous_np = None | |
def __enter__(self): | |
self.previous_np = globals().get("np") | |
import cupy as np | |
globals()["np"] = np | |
return np | |
def __exit__(self, exc_type, exc_value, exc_tb): | |
if self.previous_np is not None: | |
globals()["np"] = self.previous_np | |
else: | |
globals().pop('np', None) | |
with CupyContextManager() as np: | |
print(np) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment