Coverage for src/km3pipe/sys.py: 83%

29 statements  

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

1# Filename: sys.py 

2# pylint: disable=C0103 

3""" 

4Some unsorted, frequently used logic. 

5 

6""" 

7try: 

8 import resource # linux/macos 

9except ImportError: 

10 import psutil # windows 

11 

12import sys 

13from contextlib import contextmanager 

14 

15__author__ = "Tamas Gal" 

16__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration." 

17__credits__ = [] 

18__license__ = "MIT" 

19__maintainer__ = "Tamas Gal and Moritz Lotze" 

20__email__ = "tgal@km3net.de" 

21__status__ = "Development" 

22 

23 

24@contextmanager 

25def ignored(*exceptions): 

26 """Ignore-context for a given list of exceptions. 

27 

28 Example: 

29 with ignored(AttributeError): 

30 foo.a = 1 

31 

32 """ 

33 try: 

34 yield 

35 except exceptions: 

36 pass 

37 

38 

39def peak_memory_usage(): 

40 """Return peak memory usage in MB""" 

41 if sys.platform.startswith("win"): 

42 p = psutil.Process() 

43 return p.memory_info().peak_wset / 1024 / 1024 

44 

45 mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss 

46 factor_mb = 1 / 1024 

47 if sys.platform == "darwin": 

48 factor_mb = 1 / (1024 * 1024) 

49 return mem * factor_mb