Coverage for C:\src\imod-python\imod\util\context.py: 82%

17 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-08 14:15 +0200

1import contextlib 

2import os 

3import pathlib 

4import warnings 

5from typing import Union 

6 

7 

8@contextlib.contextmanager 

9def ignore_warnings(): 

10 """ 

11 Contextmanager to ignore RuntimeWarnings as they are frequently 

12 raised by the Dask delayed scheduler. 

13 

14 Examples 

15 -------- 

16 >>> with imod.util.context.ignore_warnings(): 

17 function_that_throws_warnings() 

18 

19 """ 

20 with warnings.catch_warnings(): 

21 warnings.simplefilter("ignore", RuntimeWarning) 

22 yield 

23 

24 

25@contextlib.contextmanager 

26def cd(path: Union[str, pathlib.Path]): 

27 """ 

28 Change directory, and change it back after the with block. 

29 

30 Examples 

31 -------- 

32 >>> with imod.util.context.cd("docs"): 

33 do_something_in_docs() 

34 

35 """ 

36 curdir = os.getcwd() 

37 os.chdir(path) 

38 try: 

39 yield 

40 finally: 

41 os.chdir(curdir)