text:
"""A module which defines events related operations.

This file defines an 'Event'. One or more 'Event's are generated as a result of
a subscription or a request. 'Event's contain 'Message' objects which can be
accessed using a 'MessageIterator'. This file also defines a 'EventQueue' for
handling replies synchronously.

Usage
-----
The following code snippet shows how a 'EventQueue' is used with a
'generateToken' request. For any established session 'session' pass an
'EventQueue' object 'tokenEventQueue' when calling 'generateToken'. All
'Event's in responses to 'generateToken' request will be returned in
'tokenEventQueue'.

    tokenEventQueue = EventQueue()
    session.generateToken(eventQueue=tokenEventQueue)

Synchronously read the response 'event' and parse over messages using 'token'

    ev = tokenEventStatus.nextEvent()
    token = None
    if ev.eventType() == blpapi.Event.TOKEN_STATUS:
        for msg in ev:
           if msg.messageType() == TOKEN_SUCCESS:
               token = msg.getElementAsString(TOKEN)
           elif msg.messageType() == TOKEN_FAILURE:
               break
    if not token:
        raise Exception("Failed to get token")

"""

from __future__ import absolute_import

from .message import Message
from . import internals
from . import utils

import sys

#sys.modules[__name__] = utils._Constants()

class MessageIterator(object):
    """An iterator over the 'Message' objects within an Event.

    Few clients will ever make direct use of 'MessageIterator' objects; Python
    'for' loops allow clients to operate directly in terms of 'Event' and
    'Message' objects'. (See the usage example above.)
    """

    def __init__(self, event):
        self.__handle = \
            internals.blpapi_MessageIterator_create(event._handle())
        self.__event = event

    def __del__(self):
        internals.blpapi_MessageIterator_destroy(self.__handle)

    def __iter__(self):
        return self

    def next(self):
        retCode, message = internals.blpapi_MessageIterator_next(self.__handle)
        if retCode:
            raise StopIteration()
        else:
            return Message(message, self.__event)

class Event(object):
    """A single event resulting from a subscription or a request.

    'Event' objects are created by the API and passed to the application either
    through a registered 'EventHandler' or 'EventQueue' or returned either from
    the 'Session.nextEvent()' or 'Session.tryNextEvent()' methods. 'Event'
    objects contain 'Message' objects which can be accessed using an iteration
    over 'Event':

        for message in event:
            ...

    The 'Event' object is a handle to an event. The event is the basic unit of
    work provided to applications. Each 'Event' object consists of an
    'EventType' attribute and zero or more 'Message' objects.

    Event objects are always created by the API, never directly by the
    application.

    Class attributes:
        The possible types of event:
        ADMIN                 Admin event
        SESSION_STATUS        Status updates for a session
        SUBSCRIPTION_STATUS   Status updates for a subscription
        REQUEST_STATUS        Status updates for a request
        RESPONSE              The final (possibly only) response to a request
        PARTIAL_RESPONSE      A partial response to a request
        SUBSCRIPTION_DATA     Data updates resulting from a subscription
        SERVICE_STATUS        Status updates for a service
        TIMEOUT               An Event returned from nextEvent() if it
                              timed out
        AUTHORIZATION_STATUS  Status updates for user authorization
        RESOLUTION_STATUS     Status updates for a resolution operation
        TOPIC_STATUS          Status updates about topics for service providers
        ROKEN_STATUS          Status updates for a generate token request
        REQUEST               Request event
        UNKNOWN               Unknown event
    """

    ADMIN = internals.EVENTTYPE_ADMIN
    """Admin event"""
    SESSION_STATUS = internals.EVENTTYPE_SESSION_STATUS
    """Status updates for a session"""
    SUBSCRIPTION_STATUS = internals.EVENTTYPE_SUBSCRIPTION_STATUS
    """Status updates for a subscription"""
    REQUEST_STATUS = internals.EVENTTYPE_REQUEST_STATUS
    """Status updates for a request"""
    RESPONSE = internals.EVENTTYPE_RESPONSE
    """The final (possibly only) response to a request"""
    PARTIAL_RESPONSE = internals.EVENTTYPE_PARTIAL_RESPONSE
    """A partial response to a request"""
    SUBSCRIPTION_DATA = internals.EVENTTYPE_SUBSCRIPTION_DATA
    """Data updates resulting from a subscription"""
    SERVICE_STATUS = internals.EVENTTYPE_SERVICE_STATUS
    """Status updates for a service"""
    TIMEOUT = internals.EVENTTYPE_TIMEOUT
    """An Event returned from nextEvent() if it timed out"""
    AUTHORIZATION_STATUS = internals.EVENTTYPE_AUTHORIZATION_STATUS
    """Status updates for user authorization"""
    RESOLUTION_STATUS = internals.EVENTTYPE_RESOLUTION_STATUS
    """Status updates for a resolution operation"""
    TOPIC_STATUS = internals.EVENTTYPE_TOPIC_STATUS
    """Status updates about topics for service providers"""
    TOKEN_STATUS = internals.EVENTTYPE_TOKEN_STATUS
    """Status updates for a generate token request"""
    REQUEST = internals.EVENTTYPE_REQUEST
    """Request event"""
    UNKNOWN = -1
    """Unknown event"""

    def __init__(self, handle, sessions):
        self.__handle = handle
        self.__sessions = sessions

    def __del__(self):
        internals.blpapi_Event_release(self.__handle)

    def eventType(self):
        """Return the type of messages contained by this 'Event'."""
        return internals.blpapi_Event_eventType(self.__handle)

    def __iter__(self):
        """Return the iterator over messages contained in this 'Event'."""
        return MessageIterator(self)

    def _handle(self):
        """Return the internal implementation."""
        return self.__handle

    def _sessions(self):
        """Return session(s) that this 'Event' is related to.

        For internal use."""
        return self.__sessions

    # Protect enumeration constant(s) defined in this class and in classes
    # derived from this class from changes:
    __metaclass__ = utils.MetaClassForClassesWithEnums

class EventQueue(object):
    """A construct used to handle replies to request synchronously.

    'EventQueue()' construct an empty 'EventQueue' which can be passed to
    'Session.sendRequest()' and 'Session.sendAuthorizationRequest()' methods.

    When a request is submitted an application can either handle the responses
    asynchronously as they arrive or use an 'EventQueue' to handle all
    responses for a given request or requests synchronously. The 'EventQueue'
    will only deliver responses to the request(s) it is associated with.
    """
    def __init__(self):
        self.__handle = internals.blpapi_EventQueue_create()
        self.__sessions = set()

    def __del__(self):
        internals.blpapi_EventQueue_destroy(self.__handle)

    def nextEvent(self, timeout=0):
        """Return the next Event available from the 'EventQueue'.

        If the specified 'timeout' is zero this method will wait forever for
        the next event. If the specified 'timeout' is non zero then if no
        'Event' is available within the specified 'timeout' an 'Event' with
        type of 'TIMEOUT' will be returned.

        The 'timeout' is specified in milliseconds.
        """
        res = internals.blpapi_EventQueue_nextEvent(self.__handle, timeout)
        return Event(res, self._getSessions())

    def tryNextEvent(self):
        """If the 'EventQueue' is non-empty, return the next Event available.

        If the 'EventQueue' is non-empty, return the next 'Event' available. If
        the 'EventQueue' is empty, return None with no effect the state of
        'EventQueue'.
        """
        res = internals.blpapi_EventQueue_tryNextEvent(self.__handle)
        if res[0]:
            return None
        else:
            return Event(res[1], self._getSessions())

    def purge(self):
        """Purge any 'Event' objects in this 'EventQueue'.

        Purges any 'Event' objects in this 'EventQueue' which have not been
        processed and cancel any pending requests linked to this 'EventQueue'.
        The 'EventQueue' can subsequently be re-used for a subsequent request.
        """
        internals.blpapi_EventQueue_purge(self.__handle)
        self.__sessions.clear()

    def _handle(self):
        """Return the internal implementation. For internal use."""
        return self.__handle

    def _registerSession(self, session):
        """Add a new session to this 'EventQueue'. For internal use."""
        self.__sessions.add(session)

    def _getSessions(self):
        """Get a list of sessions this EventQueue related to.

        For internal use.
        """
        sessions = list(self.__sessions)
        return sessions

__copyright__ = """
Copyright 2012. Bloomberg Finance L.P.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:  The above
copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
"""


text:
<?xml version='1.0' encoding='UTF-8'?>
<hudson.plugins.scm__sync__configuration.ScmSyncConfigurationPlugin version="1">
  <scm class="hudson.plugins.scm_sync_configuration.scms.ScmSyncGitSCM"/>
  <scmRepositoryUrl>scm:git:git@github.com:clyde0516/jenkins-config.git</scmRepositoryUrl>
  <noUserCommitMessage>true</noUserCommitMessage>
  <displayStatus>true</displayStatus>
  <commitMessagePattern>[message] </commitMessagePattern>
  <manualSynchronizationIncludes/>
</hudson.plugins.scm__sync__configuration.ScmSyncConfigurationPlugin>

redpajama_set_name: RedPajamaGithub


text:
import importlib
import logging
import pytest
import sys
import warnings
from concurrent.futures import ThreadPoolExecutor
from io import StringIO
from itertools import permutations
from unittest import mock

import mlflow
from mlflow import MlflowClient
from mlflow.utils import gorilla
from mlflow.utils.autologging_utils import (
    safe_patch,
    get_autologging_config,
    autologging_is_disabled,
)

from tests.autologging.fixtures import test_mode_off
from tests.autologging.fixtures import reset_stderr  # pylint: disable=unused-import

AUTOLOGGING_INTEGRATIONS_TO_TEST = {
    mlflow.sklearn: "sklearn",
    mlflow.xgboost: "xgboost",
    mlflow.lightgbm: "lightgbm",
    mlflow.pytorch: "torch",
    mlflow.gluon: "mxnet.gluon",
    mlflow.fastai: "fastai",
    mlflow.statsmodels: "statsmodels",
    mlflow.spark: "pyspark",
    mlflow.pyspark.ml: "pyspark",
    mlflow.tensorflow: "tensorflow",
}

@pytest.fixture(autouse=True, scope="module")
def import_integration_libraries():
    for library_module in AUTOLOGGING_INTEGRATIONS_TO_TEST.values():
        importlib.import_module(library_module)

@pytest.fixture(autouse=True)
def disable_autologging_at_test_end():
    # The yeild statement is to insure that code below is executed as teardown code.
    # This will avoid bleeding of an active autologging session from test suite.
    yield
    for integration in AUTOLOGGING_INTEGRATIONS_TO_TEST:
        integration.autolog(disable=True)

@pytest.fixture()
def setup_sklearn_model():
    from sklearn.datasets import load_iris
    from sklearn.linear_model import LogisticRegression

    X, y = load_iris(return_X_y=True)
    model = LogisticRegression()

    return X, y, model

@pytest.mark.parametrize("integration", AUTOLOGGING_INTEGRATIONS_TO_TEST.keys())
def test_autologging_integrations_expose_configs_and_support_disablement(integration):
    for integration in AUTOLOGGING_INTEGRATIONS_TO_TEST:
        integration.autolog(disable=False)

    integration_name = integration.autolog.integration_name

    assert not autologging_is_disabled(integration_name)
    assert not get_autologging_config(integration_name, "disable", True)

    integration.autolog(disable=True)

    assert autologging_is_disabled(integration_name)
    assert get_autologging_config(integration_name, "disable", False)

@pytest.mark.parametrize("integration", AUTOLOGGING_INTEGRATIONS_TO_TEST.keys())
def test_autologging_integrations_use_safe_patch_for_monkey_patching(integration):
    with mock.patch("mlflow.utils.gorilla.apply", wraps=gorilla.apply) as gorilla_mock, mock.patch(
        integration.__name__ + ".safe_patch", wraps=safe_patch
    ) as safe_patch_mock:
        # In `mlflow.xgboost.autolog()` and `mlflow.lightgbm.autolog()`,
        # we enable autologging for XGBoost and LightGBM sklearn models
        # using `mlflow.sklearn._autolog()`. So besides `safe_patch` calls in
        # `autolog()`, we need to count additional `safe_patch` calls
        # in sklearn autologging routine as well.
        if integration.__name__ in ["mlflow.xgboost", "mlflow.lightgbm"]:
            with mock.patch(
                "mlflow.sklearn.safe_patch", wraps=safe_patch
            ) as sklearn_safe_patch_mock:
                integration.autolog(disable=False)
                safe_patch_call_count = (
                    safe_patch_mock.call_count + sklearn_safe_patch_mock.call_count
                )

                # Assert autolog integrations use the fluent API for run management. This is to
                # ensure certain fluent API methods like mlflow.last_active_run behaves as expected.
                assert any(
                    kwargs["manage_run"]
                    for _, kwargs in sklearn_safe_patch_mock.call_args_list
                    if "manage_run" in kwargs
                )
        else:
            integration.autolog(disable=False)
            safe_patch_call_count = safe_patch_mock.call_count

        if integration.__name__ != "mlflow.spark":
            # Assert autolog integrations use the fluent API for run management. This is to
            # ensure certain fluent API methods like mlflow.last_active_run behaves as expected.
            assert any(
                kwargs["manage_run"]
                for _, kwargs in safe_patch_mock.call_args_list
                if "manage_run" in kwargs
            )
        assert safe_patch_call_count > 0
        # `safe_patch` leverages `gorilla.apply` in its implementation. Accordingly, we expect
        # that the total number of `gorilla.apply` calls to be equivalent to the number of
        # `safe_patch` calls. This verifies that autologging integrations are leveraging
        # `safe_patch`, rather than calling `gorilla.apply` directly (which does not provide
        # exception safety properties)
        assert safe_patch_call_count == gorilla_mock.call_count

def test_autolog_respects_exclusive_flag(setup_sklearn_model):
    x, y, model = setup_sklearn_model

    mlflow.sklearn.autolog(exclusive=True)
    run = mlflow.start_run()
    model.fit(x, y)
    mlflow.end_run()
    run_data = MlflowClient().get_run(run.info.run_id).data
    metrics, params, tags = run_data.metrics, run_data.params, run_data.tags
    assert not metrics
    assert not params
    assert all("mlflow." in key for key in tags)

    mlflow.sklearn.autolog(exclusive=False)
    run = mlflow.start_run()
    model.fit(x, y)
    mlflow.end_run()
    run_data = MlflowClient().get_run(run.info.run_id).data
    metrics, params = run_data.metrics, run_data.params
    assert metrics
    assert params

def test_autolog_respects_disable_flag(setup_sklearn_model):
    x, y, model = setup_sklearn_model

    mlflow.sklearn.autolog(disable=True, exclusive=False)
    run = mlflow.start_run()
    model.fit(x, y)
    mlflow.end_run()
    run_data = MlflowClient().get_run(run.info.run_id).data
    metrics, params, tags = run_data.metrics, run_data.params, run_data.tags
    assert not metrics
    assert not params
    assert all("mlflow." in key for key in tags)

    mlflow.sklearn.autolog(disable=False, exclusive=False)
    run = mlflow.start_run()
    model.fit(x, y)
    mlflow.end_run()
    run_data = MlflowClient().get_run(run.info.run_id).data
    metrics, params = run_data.metrics, run_data.params
    assert metrics
    assert params

def test_autolog_reverts_patched_code_when_disabled():
    # use `KMeans` because it implements `fit`, `fit_transform`, and `fit_predict`.
    from sklearn.cluster import KMeans

    # Before any patching.
    model = KMeans()
    original_fit = model.fit
    original_fit_transform = model.fit_transform
    original_fit_predict = model.fit_predict

    # After patching.
    mlflow.sklearn.autolog(disable=False)
    patched_fit = model.fit
    patched_fit_transform = model.fit_transform
    patched_fit_predict = model.fit_predict
    assert patched_fit != original_fit
    assert patched_fit_transform != original_fit_transform
    assert patched_fit_predict != original_fit_predict

    # After revert of patching.
    mlflow.sklearn.autolog(disable=True)
    reverted_fit = model.fit
    reverted_fit_transform = model.fit_transform
    reverted_fit_predict = model.fit_predict

    assert reverted_fit == original_fit
    assert reverted_fit_transform == original_fit_transform
    assert reverted_fit_predict == original_fit_predict
    assert reverted_fit != patched_fit
    assert reverted_fit_transform != patched_fit_transform
    assert reverted_fit_predict != patched_fit_predict

def test_autolog_respects_disable_flag_across_import_orders():
    def test():
        from sklearn import svm, datasets

        iris = datasets.load_iris()
        svc = svm.SVC(C=2.0, degree=5, kernel="rbf")
        run = mlflow.start_run()
        svc.fit(iris.data, iris.target)
        mlflow.end_run()
        run_data = MlflowClient().get_run(run.info.run_id).data
        metrics, params, tags = run_data.metrics, run_data.params, run_data.tags
        assert not metrics
        assert not params
        assert all("mlflow." in key for key in tags)

    def import_sklearn():
        import sklearn  # pylint: disable=unused-import

    def disable_autolog():
        mlflow.sklearn.autolog(disable=True)

    def mlflow_autolog():
        mlflow.autolog()

    import_list = [import_sklearn, disable_autolog, mlflow_autolog]

    for func_order_list in permutations(import_list):
        for fun in func_order_list:
            fun()
        test()

@pytest.mark.usefixtures(test_mode_off.__name__)
def test_autolog_respects_silent_mode(tmpdir):
    # Use file-based experiment storage for this test. Otherwise, concurrent experiment creation in
    # multithreaded contexts may fail for other storage backends (e.g. SQLAlchemy)
    mlflow.set_tracking_uri(str(tmpdir))
    mlflow.set_experiment("test_experiment")

    og_showwarning = warnings.showwarning
    stream = StringIO()
    sys.stderr = stream
    logger = logging.getLogger(mlflow.__name__)

    from sklearn import datasets

    iris = datasets.load_iris()

    def train_model():
        import sklearn.utils
        from sklearn import svm
        from sklearn.model_selection import GridSearchCV

        parameters = {"kernel": ("linear", "rbf"), "C": [1, 10]}
        svc = svm.SVC()
        with sklearn.utils.parallel_backend(backend="threading"):
            clf = GridSearchCV(svc, parameters)
            clf.fit(iris.data, iris.target)

        return True

    # Call general and framework-specific autologging APIs to cover a
    # larger surface area for testing purposes
    mlflow.autolog(silent=True)
    mlflow.sklearn.autolog(silent=True, log_input_examples=True)

    executions = []
    with ThreadPoolExecutor(max_workers=50) as executor:
        for _ in range(2):
            e = executor.submit(train_model)
            executions.append(e)

    assert all(e.result() is True for e in executions)
    assert not stream.getvalue()
    # Verify that `warnings.showwarning` was restored to its original value after training
    # and that MLflow event logs are enabled
    assert warnings.showwarning == og_showwarning
    logger.info("verify that event logs are enabled")
    assert "verify that event logs are enabled" in stream.getvalue()

    stream.truncate(0)

    mlflow.sklearn.autolog(silent=False, log_input_examples=True)

    executions = []
    with ThreadPoolExecutor(max_workers=50) as executor:
        for _ in range(100):
            e = executor.submit(train_model)
            executions.append(e)

    assert all(e.result() is True for e in executions)
    assert stream.getvalue()
    # Verify that `warnings.showwarning` was restored to its original value after training
    # and that MLflow event logs are enabled
    assert warnings.showwarning == og_showwarning
    logger.info("verify that event logs are enabled")
    assert "verify that event logs are enabled" in stream.getvalue()

    # TODO: Investigate why this test occasionally leaks a run, which causes the
    # `clean_up_leaked_runs` fixture in `tests/conftest.py` to fail.
    while mlflow.active_run():
        mlflow.end_run()

def test_autolog_globally_configured_flag_set_correctly():
    from mlflow.utils.autologging_utils import AUTOLOGGING_INTEGRATIONS

    AUTOLOGGING_INTEGRATIONS.clear()
    import sklearn  # pylint: disable=unused-import
    import pyspark  # pylint: disable=unused-import
    import pyspark.ml  # pylint: disable=unused-import

    integrations_to_test = ["sklearn", "spark", "pyspark.ml"]
    mlflow.autolog()
    for integration_name in integrations_to_test:
        assert AUTOLOGGING_INTEGRATIONS[integration_name]["globally_configured"]

    mlflow.sklearn.autolog()
    mlflow.spark.autolog()
    mlflow.pyspark.ml.autolog()

    for integration_name in integrations_to_test:
        assert "globally_configured" not in AUTOLOGGING_INTEGRATIONS[integration_name]


text:
<?xml version="1.0" encoding="utf-8" ?>
<ruleset name="DependencyCheck Rules"
         xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0
    http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

    <rule name="LoggerChaining"
          message="Logger should be a field, don't chain it"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          language="java">
        <description><![CDATA[
Loggers should be created as fields within the class for general
use, rather than chaining the call to getLogger() followed by
the work on the Logger.
    ]]></description>
        <properties>
            <property name="xpath">
                <value><![CDATA[
//PrimaryPrefix[Name/@Image = 'Logger.getLogger' and count(following-sibling::PrimarySuffix) > 2]
        ]]></value>
            </property>
        </properties>
        <example><![CDATA[
Logger.getLogger(Foo.class).log(Level.FINEST, "Don't do this");
    ]]></example>
    </rule>
    <rule name="LoggingWrongClass"
          message="The logger is obtained for the wrong class"
          class="net.sourceforge.pmd.lang.rule.XPathRule"
          language="java">
        <description><![CDATA[
Loggers should be created as fields within the class for general
use, rather than chaining the call to getLogger() followed by
the work on the Logger.
    ]]></description>
        <properties>
            <property name="xpath">
                <value><![CDATA[
//PrimaryExpression[
  PrimaryPrefix/Name/@Image = 'Logger.getLogger'
  and
  not(PrimarySuffix/
    Arguments/
    ArgumentList/
    Expression/
    PrimaryExpression/
    PrimaryPrefix/
    ResultType/
    Type/
    ReferenceType/
    ClassOrInterfaceType/
    @Image = ancestor::ClassOrInterfaceDeclaration/@Image)
]               ]]></value>
            </property>
        </properties>
        <example><![CDATA[
public class Foo {
   Logger LOGGER = Logger.getLogger(Bar.class.getName()); //should be Foo.class
}
    ]]></example>
    </rule>
</ruleset>