<commit_before>"""
Unit or integration tests for gpustat
"""
# flake8: ignore=E501

from __future__ import print_function
from __future__ import absolute_import

import unittest
import sys
from collections import namedtuple

import psutil
import pynvml
from six.moves import cStringIO as StringIO

import gpustat

try:
    import unittest.mock as mock
except ImportError:
    import mock

MagicMock = mock.MagicMock


def _configure_mock(N, Process,
                    scenario_nonexistent_pid=False):
    """
    Define mock behaviour for N: the pynvml module, and psutil.Process,
    which should be MagicMock objects from unittest.mock.
    """

    # Restore some non-mock objects (such as exceptions)
    for attr in dir(pynvml):
        if attr.startswith('NVML'):
            setattr(N, attr, getattr(pynvml, attr))
    assert issubclass(N.NVMLError, BaseException)

    # without following patch, unhashable NVMLError distrubs unit test
    N.NVMLError.__hash__ = lambda _: 0

    # mock-patch every nvml**** functions used in gpustat.
    N.nvmlInit = MagicMock()
    N.nvmlShutdown = MagicMock()
    N.nvmlDeviceGetCount.return_value = 3
    N.nvmlSystemGetDriverVersion.return_value = '415.27.mock'

    mock_handles = ['mock-handle-%d' % i for i in range(3)]

    def _raise_ex(fn):
        """Decorator to let exceptions returned from the callable re-throwed.""" # noqa:E501
        def _decorated(*args, **kwargs):
            v = fn(*args, **kwargs)<commit_msg>Fix flake8 error<commit_after>"""
Unit or integration tests for gpustat
"""
# flake8: ignore=E501

from __future__ import print_function
from __future__ import absolute_import

import unittest
import sys
from collections import namedtuple

import psutil
import pynvml
from six.moves import cStringIO as StringIO

import gpustat

try:
    import unittest.mock as mock
except ImportError:
    import mock

MagicMock = mock.MagicMock


def _configure_mock(N, Process,
                    scenario_nonexistent_pid=False):
    """
    Define mock behaviour for N: the pynvml module, and psutil.Process,
    which should be MagicMock objects from unittest.mock.
    """

    # Restore some non-mock objects (such as exceptions)
    for attr in dir(pynvml):
        if attr.startswith('NVML'):
            setattr(N, attr, getattr(pynvml, attr))
    assert issubclass(N.NVMLError, BaseException)

    # without following patch, unhashable NVMLError distrubs unit test
    N.NVMLError.__hash__ = lambda _: 0

    # mock-patch every nvml**** functions used in gpustat.
    N.nvmlInit = MagicMock()
    N.nvmlShutdown = MagicMock()
    N.nvmlDeviceGetCount.return_value = 3
    N.nvmlSystemGetDriverVersion.return_value = '415.27.mock'

    mock_handles = ['mock-handle-%d' % i for i in range(3)]

    def _raise_ex(fn):
        """Decorator to let exceptions returned from the callable re-throwed."""  # noqa:E501
        def _decorated(*args, **kwargs):
            v = fn(*args, **kwargs)
{'fraction_non_alphanumeric': 0.0872210953346856, 'fraction_numerical': 0.010480054090601758, 'mean_word_length': 3.3902077151335313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4590145', 'n_tokens_mistral': 968, 'n_tokens_neox': 920, 'n_words': 281}