Source code for km3pipe.sys

# Filename: sys.py
# pylint: disable=C0103
"""
Some unsorted, frequently used logic.

"""
from __future__ import absolute_import, print_function, division

try:
    import resource    # linux/macos
except ImportError:
    import psutil    # windows

import sys
from contextlib import contextmanager

__author__ = "Tamas Gal"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = []
__license__ = "MIT"
__maintainer__ = "Tamas Gal and Moritz Lotze"
__email__ = "tgal@km3net.de"
__status__ = "Development"


@contextmanager
[docs]def ignored(*exceptions): """Ignore-context for a given list of exceptions. Example: with ignored(AttributeError): foo.a = 1 """ try: yield except exceptions: pass
[docs]def peak_memory_usage(): """Return peak memory usage in MB""" if sys.platform.startswith('win'): p = psutil.Process() return p.memory_info().peak_wset / 1024 / 1024 mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss factor_mb = 1 / 1024 if sys.platform == 'darwin': factor_mb = 1 / (1024 * 1024) return mem * factor_mb