text:
from __future__ import unicode_literals
from __future__ import absolute_import

__author__ = "echee@scalyr.com"

import threading

from scalyr_agent.util import FakeClock, StoppableThread
from scalyr_agent.test_base import ScalyrTestCase
from scalyr_agent.monitor_utils.blocking_rate_limiter import BlockingRateLimiter

from mock import patch
from six.moves import range

def always_true():
    while True:
        yield True

def always_false():
    while True:
        yield False

def rate_maintainer(consecutive_success_threshold, backoff_rate, increase_rate):
    """Returns a generator that maintains the rate at a constant level by balancing failures with successes"""
    last = True
    consecutive_true = 0
    backoff_increase_ratio = float(backoff_rate) * increase_rate
    required_consecutive_successes = int(
        consecutive_success_threshold * backoff_increase_ratio
    )

    while True:
        if last:
            if consecutive_true == required_consecutive_successes:
                last = False
                consecutive_true = 0
            else:
                # stay True
                consecutive_true += 1
        else:
            last = True
            consecutive_true += 1
        yield last

class BlockingRateLimiterTest(ScalyrTestCase):
    def setUp(self):
        super(BlockingRateLimiterTest, self).setUp()
        self._fake_clock = FakeClock()
        self._test_state = {
            "count": 0,
            "times": [],
        }
        self._test_state_lock = threading.Lock()
        self._outcome_generator_lock = threading.Lock()

    def test_init_exceptions(self):
        BRL = BlockingRateLimiter

        # strategy must be restricted to proper values
        self.assertRaises(
            ValueError,
            lambda: BRL(
                num_agents=1,
                initial_cluster_rate=100,
                max_cluster_rate=1000,
                min_cluster_rate=1,
                consecutive_success_threshold=5,
                strategy="bad strategy",
            ),
        )

        # max_concurrency must be 1 or more
        self.assertRaises(
            ValueError,
            lambda: BRL(
                num_agents=1,
                initial_cluster_rate=100,
                max_cluster_rate=1000,
                min_cluster_rate=1,
                consecutive_success_threshold=5,
                max_concurrency=0,
            ),
        )

        # consecutive_success_threshold must be a positive int
        self.assertRaises(
            ValueError,
            lambda: BRL(
                num_agents=1,
                initial_cluster_rate=100,
                max_cluster_rate=1000,
                min_cluster_rate=1,
                consecutive_success_threshold=-5.0,
            ),
        )
        self.assertRaises(
            ValueError,
            lambda: BRL(
                num_agents=1,
                initial_cluster_rate=100,
                max_cluster_rate=1000,
                min_cluster_rate=1,
                consecutive_success_threshold=99.99,
            ),
        )

        # user-supplied init must be between user-supplied min/max
        self.assertRaises(
            ValueError,
            lambda: BRL(
                num_agents=1,
                initial_cluster_rate=0.7,
                max_cluster_rate=1000,
                min_cluster_rate=1,
                consecutive_success_threshold=5,
            ),
        )
        self.assertRaises(
            ValueError,
            lambda: BRL(
                num_agents=1,
                initial_cluster_rate=7777,
                max_cluster_rate=1000,
                min_cluster_rate=1,
                consecutive_success_threshold=5,
            ),
        )

    def test_lazy_adjust_min_max_init_rates(self):
        """Tests the one-time lazy adjustments"""

        # Make sure proper adjustments are made if out of bounds
        BRL = BlockingRateLimiter
        rl = BRL(
            num_agents=1,
            initial_cluster_rate=BRL.HARD_LIMIT_INITIAL_CLUSTER_RATE + 1000,
            max_cluster_rate=BRL.HARD_LIMIT_INITIAL_CLUSTER_RATE + 2000000,
            min_cluster_rate=float(BRL.HARD_LIMIT_MIN_CLUSTER_RATE) / 10,
            consecutive_success_threshold=5,
        )
        rl._lazy_adjust_min_max_rates()
        self.assertEqual(rl._initial_cluster_rate, BRL.HARD_LIMIT_INITIAL_CLUSTER_RATE)
        self.assertEqual(
            rl._max_cluster_rate, BRL.HARD_LIMIT_INITIAL_CLUSTER_RATE + 2000000
        )
        self.assertEqual(rl._min_cluster_rate, BRL.HARD_LIMIT_MIN_CLUSTER_RATE)

        # Make sure adjustments are NOT made if within bounds
        rl = BRL(
            num_agents=1,
            initial_cluster_rate=BRL.HARD_LIMIT_INITIAL_CLUSTER_RATE - 1,
            max_cluster_rate=BRL.HARD_LIMIT_INITIAL_CLUSTER_RATE - 1,
            min_cluster_rate=float(BRL.HARD_LIMIT_MIN_CLUSTER_RATE) + 1,
            consecutive_success_threshold=5,
        )
        rl._lazy_adjust_min_max_rates()
        self.assertEqual(
            rl._initial_cluster_rate, BRL.HARD_LIMIT_INITIAL_CLUSTER_RATE - 1
        )
        self.assertEqual(rl._max_cluster_rate, BRL.HARD_LIMIT_INITIAL_CLUSTER_RATE + -1)
        self.assertEqual(rl._min_cluster_rate, BRL.HARD_LIMIT_MIN_CLUSTER_RATE + 1)

        # Init rate cannot be lower than min rate
        rl = BlockingRateLimiter(
            num_agents=1,
            initial_cluster_rate=0.1,
            max_cluster_rate=1000,
            min_cluster_rate=0,
            consecutive_success_threshold=5,
        )
        rl._lazy_adjust_min_max_rates()
        self.assertEqual(rl._initial_cluster_rate, 1)

    def __create_consumer_threads(
        self, num_threads, rate_limiter, experiment_end_time, reported_outcome_generator
    ):
        """
        Create a list of client thread that will consume from the rate limiter.
        @param num_threads: Number of client threads
        @param rate_limiter: The rate limiter to test
        @param experiment_end_time: consumer threads terminate their loops when the fake clock has exceeded this.
        @param reported_outcome_generator: Generator to get reported outcome boolean value

        @type rate_limiter: BlockingRateLimiter
        @type threads: iterable of Thread objects
        @type experiment_end_time: int (utc seconds)
        @type reported_outcome_generator: generator

        @returns: list of consumer threads
        @rtype: list(Thread)
        """

        def consume_token_blocking():
            """consumer threads will keep acquiring tokens until experiment end time"""
            while self._fake_clock.time() < experiment_end_time:
                # A simple loop that acquires token, updates a counter, then releases token with an outcome
                # provided by reported_outcome_generator()
                t1 = self._fake_clock.time()
                token = rate_limiter.acquire_token()
                # update test state
                self._test_state_lock.acquire()
                try:
                    self._test_state["count"] += 1
                    self._test_state["times"].append(int(t1))
                finally:
                    self._test_state_lock.release()

                self._outcome_generator_lock.acquire()
                try:
                    outcome = next(reported_outcome_generator)
                    rate_limiter.release_token(token, outcome)
                finally:
                    self._outcome_generator_lock.release()

        return [
            threading.Thread(target=consume_token_blocking) for _ in range(num_threads)
        ]

    def __create_fake_clock_advancer_thread(self, rate_limiter, consumer_threads):
        """
        Run a separate thread that advances time in order to allow rate_limiter to release tokens
        @param rate_limiter: The rate limiter to test
        @param consumer_threads: Other threads that are consumers of the Rate Limiter.  The advancer thread will run
            for as long as the consumer threads continue to run.

        @type rate_limiter: BlockingRateLimiter
        @type consumer_threads: iterable of Thread objects

        @returns: Thread object
        @rtype: Thread
        """

        class FakeClockAdvancerThread(StoppableThread):
            def __init__(self2):  # pylint: disable=no-self-argument
                StoppableThread.__init__(
                    self2, name="FakeClockAdvancerThread", is_daemon=True
                )

            def run_and_propagate(self2):  # pylint: disable=no-self-argument
                at_least_one_client_thread_incomplete = True
                while at_least_one_client_thread_incomplete:
                    # All client threads are likely blocking on the token queue at this point
                    # The experiment will never start until at least one token is granted (and waiting threads notified)
                    # To jump start this, simply advance fake clock to the ripe_time.
                    delta = rate_limiter._ripe_time - self._fake_clock.time()
                    self._fake_clock.advance_time(increment_by=delta)
                    # Wake up after 1 second just in case and trigger another advance
                    [t.join(1) for t in consumer_threads]
                    at_least_one_client_thread_incomplete = False
                    for t in consumer_threads:
                        if t.is_alive():
                            at_least_one_client_thread_incomplete = True
                            break

        return FakeClockAdvancerThread()

    def test_rate_adjustment_logic(self):
        """Make sure rate adjustments work according to spec

        The cases we test are:
        1. All successes. Actual rate is very high. New target rate is always increase_factor * current target rate.
        2. All successes. Actual rate is very low. New target rate is always increase_factor * actual rate,
            (but no lower than current rate).
        3. All failures. Actual rate is very low. New target rate is always backoff_factor * current target rate.
        4. All failures. Actual rate is very high. New target rate is always backoff_factor * actual rate.
            (but no higher than current rate).
        """
        required_successes = 5
        max_cluster_rate = 10000
        min_cluster_rate = 1
        initial_cluster_rate = 100
        increase_factor = 2.0
        backoff_factor = 0.5

        def _create_rate_limiter():
            return BlockingRateLimiter(
                num_agents=1,
                initial_cluster_rate=initial_cluster_rate,
                max_cluster_rate=max_cluster_rate,
                min_cluster_rate=min_cluster_rate,
                consecutive_success_threshold=required_successes,
                strategy=BlockingRateLimiter.STRATEGY_MULTIPLY,
                increase_factor=increase_factor,
                backoff_factor=backoff_factor,
                max_concurrency=1,
                fake_clock=self._fake_clock,
            )

        def _mock_get_next_ripe_time_actual_rate_leads(rate_limiter):
            # Make the rate limiter grant tokens faster than the target rate
            def _func(*args, **kwargs):
                delta = 1.0 / rate_limiter._current_cluster_rate
                return rl._ripe_time + 0.9 * delta

            return _func

        def _mock_get_next_ripe_time_actual_rate_lags(rate_limiter):
            # Make the rate limiter grant tokens slower than the target rate
            def _func(*args, **kwargs):
                delta = 1.0 / rate_limiter._current_cluster_rate
                return rl._ripe_time + 1.1 * delta

            return _func

        rl = _create_rate_limiter()

        @patch.object(rl, "_get_next_ripe_time")
        def _case1_test_successes_actual_rate_leads_target_rate(
            mock_get_next_ripe_time,
        ):
            """Examine rate-increasing behavior in the context of very high actual rates"""
            mock_get_next_ripe_time.side_effect = (
                _mock_get_next_ripe_time_actual_rate_leads(rl)
            )

            advancer = self.__create_fake_clock_advancer_thread(
                rl, [threading.currentThread()]
            )
            advancer.start()

            while True:
                token = rl.acquire_token()
                for x in range(required_successes - 1):
                    rl.release_token(token, True)
                    rl.acquire_token()

                # Actual rate always leads target rate
                old_target_rate = rl._current_cluster_rate
                old_actual_rate = rl._get_actual_cluster_rate()
                self.assertGreater(old_actual_rate, old_target_rate)

                # Token grant causes new rate to be calculated
                rl.release_token(token, True)

                # assert that the new rate is calculated based on the (lower/more conservative) target rate
                if increase_factor * old_target_rate < max_cluster_rate:
                    self.assertEqual(
                        rl._current_cluster_rate, increase_factor * old_target_rate
                    )
                else:
                    # assert that new rate never exceeds max rate
                    self.assertEqual(rl._current_cluster_rate, max_cluster_rate)
                    break
            advancer.stop(wait_on_join=False)

        _case1_test_successes_actual_rate_leads_target_rate()  # pylint: disable=no-value-for-parameter

        rl = _create_rate_limiter()

        @patch.object(rl, "_get_next_ripe_time")
        def _case2_test_successes_actual_rate_lags_target_rate(mock_get_next_ripe_time):
            """Examine rate-increasing behavior in the context of very high actual rates"""
            mock_get_next_ripe_time.side_effect = (
                _mock_get_next_ripe_time_actual_rate_lags(rl)
            )

            advancer = self.__create_fake_clock_advancer_thread(
                rl, [threading.currentThread()]
            )
            advancer.start()

            while True:
                token = rl.acquire_token()
                for x in range(required_successes - 1):
                    rl.release_token(token, True)
                    rl.acquire_token()

                # Actual rate always lags target rate
                old_target_rate = rl._current_cluster_rate
                old_actual_rate = rl._get_actual_cluster_rate()
                self.assertLess(old_actual_rate, old_target_rate)

                # Token grant causes new rate to be calculated
                rl.release_token(token, True)

                # assert that the new rate is calculated based on the (lower/more conservative) actual rate
                if increase_factor * old_actual_rate < max_cluster_rate:
                    if increase_factor * old_actual_rate < old_target_rate:
                        self.assertEqual(
                            round(rl._current_cluster_rate, 2),
                            round(old_target_rate, 2),
                        )
                    else:
                        self.assertEqual(
                            round(rl._current_cluster_rate, 2),
                            round(increase_factor * old_actual_rate, 2),
                        )
                else:
                    # assert that new rate never exceeds max rate
                    self.assertEqual(rl._current_cluster_rate, max_cluster_rate)
                    break
            advancer.stop(wait_on_join=False)

        _case2_test_successes_actual_rate_lags_target_rate()  # pylint: disable=no-value-for-parameter

        rl = _create_rate_limiter()

        @patch.object(rl, "_get_next_ripe_time")
        def _case3_test_failures_actual_rate_lags_target_rate(mock_get_next_ripe_time):
            """Examine rate-decreasing behavior in the context of very high actual rates"""
            mock_get_next_ripe_time.side_effect = (
                _mock_get_next_ripe_time_actual_rate_lags(rl)
            )

            advancer = self.__create_fake_clock_advancer_thread(
                rl, [threading.currentThread()]
            )
            advancer.start()

            counter = 0
            while True:
                token = rl.acquire_token()

                # Actual rate always lags target rate
                old_target_rate = rl._current_cluster_rate
                old_actual_rate = rl._get_actual_cluster_rate()
                if old_actual_rate is not None:
                    self.assertLess(old_actual_rate, old_target_rate)

                # Token grant a 100 initial successes followed by all failures
                counter += 1
                if counter <= required_successes:
                    rl.release_token(token, True)
                    continue
                else:
                    rl.release_token(token, False)

                # assert that the new rate is calculated based on the (higher/more conservative) target rate
                if backoff_factor * old_target_rate > min_cluster_rate:
                    self.assertEqual(
                        round(rl._current_cluster_rate, 2),
                        round(backoff_factor * old_target_rate, 2),
                    )
                else:
                    # assert that new rate never goes lower than min rate
                    self.assertEqual(
                        round(rl._current_cluster_rate, 2), round(min_cluster_rate, 2)
                    )
                    break
            advancer.stop(wait_on_join=False)

        _case3_test_failures_actual_rate_lags_target_rate()  # pylint: disable=no-value-for-parameter

        rl = _create_rate_limiter()

        @patch.object(rl, "_get_next_ripe_time")
        def _case4_test_failures_actual_rate_leads_target_rate(mock_get_next_ripe_time):
            """Examine rate-decreasing behavior in the context of very high actual rates"""
            mock_get_next_ripe_time.side_effect = (
                _mock_get_next_ripe_time_actual_rate_leads(rl)
            )

            advancer = self.__create_fake_clock_advancer_thread(
                rl, [threading.currentThread()]
            )
            advancer.start()

            counter = 0
            while True:
                token = rl.acquire_token()

                # Actual rate is always None because
                old_target_rate = rl._current_cluster_rate
                old_actual_rate = rl._get_actual_cluster_rate()
                if old_actual_rate is not None:
                    self.assertGreater(old_actual_rate, old_target_rate)

                # Token grant a 100 initial successes followed by all failures
                counter += 1
                if counter <= required_successes:
                    rl.release_token(token, True)
                    continue
                else:
                    rl.release_token(token, False)

                # assert that the new rate is calculated based on the (higher/more conservative) actual rate
                if backoff_factor * old_target_rate > min_cluster_rate:
                    self.assertEqual(
                        round(rl._current_cluster_rate, 2),
                        round(backoff_factor * old_target_rate, 2),
                    )
                else:
                    # assert that new rate never goes lower than min rate
                    self.assertEqual(rl._current_cluster_rate, min_cluster_rate)
                    break
            advancer.stop(wait_on_join=False)

        _case4_test_failures_actual_rate_leads_target_rate()  # pylint: disable=no-value-for-parameter

    def test_fixed_rate_single_concurrency(self):
        """Longer experiment"""
        # 1 rps x 1000 simulated seconds => 1000 increments
        self._test_fixed_rate(
            desired_agent_rate=1.0,
            experiment_duration=10000,
            concurrency=1,
            expected_requests=10000,
            allowed_variance=(0.95, 1.05),
        )

    def test_fixed_rate_multiple_concurrency(self):
        """Multiple clients should not affect overall rate"""
        # 1 rps x 100 simulated seconds => 100 increments
        # Higher concurrency has more variance
        self._test_fixed_rate(
            desired_agent_rate=1.0,
            experiment_duration=10000,
            concurrency=3,
            expected_requests=10000,
            allowed_variance=(0.8, 1.2),
        )

    def _test_fixed_rate(
        self,
        desired_agent_rate,
        experiment_duration,
        concurrency,
        expected_requests,
        allowed_variance,
    ):
        """A utility pass-through that fixes the initial, upper and lower rates"""
        num_agents = 1000
        cluster_rate = num_agents * desired_agent_rate
        self._test_rate_limiter(
            num_agents=num_agents,
            initial_cluster_rate=cluster_rate,
            max_cluster_rate=cluster_rate,
            min_cluster_rate=cluster_rate,
            consecutive_success_threshold=5,
            experiment_duration=experiment_duration,
            max_concurrency=concurrency,
            expected_requests=expected_requests,
            allowed_variance=allowed_variance,
        )

    def test_variable_rate_single_concurrency_all_successes(self):
        """Rate should quickly max out at max_cluster_rate"""

        # Test variables
        initial_agent_rate = 1
        experiment_duration = 1000
        concurrency = 1
        max_rate_multiplier = 10

        # Expected behavior (saturates at upper rate given all successes)
        expected_requests = max_rate_multiplier * experiment_duration
        allowed_variance = (0.8, 1.2)

        # Derived values
        num_agents = 10
        initial_cluster_rate = num_agents * initial_agent_rate
        max_cluster_rate = max_rate_multiplier * initial_cluster_rate

        self._test_rate_limiter(
            num_agents=10,
            initial_cluster_rate=initial_cluster_rate,
            max_cluster_rate=max_cluster_rate,
            min_cluster_rate=0,
            experiment_duration=experiment_duration,
            max_concurrency=concurrency,
            consecutive_success_threshold=5,
            increase_strategy=BlockingRateLimiter.STRATEGY_RESET_THEN_MULTIPLY,
            expected_requests=expected_requests,
            allowed_variance=allowed_variance,
        )

    def test_variable_rate_single_concurrency_all_failures(self):
        """Rate should quickly decrease to min_cluster_rate"""

        # temporarily disable hard lower limit as it is irrelevant to this test and we want to let it go down to really
        # low numbers to gather enough data.
        orig_hard_limit = BlockingRateLimiter.HARD_LIMIT_MIN_CLUSTER_RATE
        BlockingRateLimiter.HARD_LIMIT_MIN_CLUSTER_RATE = -float("inf")

        # Test variables
        initial_agent_rate = 1
        experiment_duration = 1000000
        concurrency = 1
        min_rate_multiplier = 0.01  # min rate should be at least an order of magnitude lower than initial rate.

        # Expected behavior
        expected_requests = min_rate_multiplier * experiment_duration
        allowed_variance = (0.8, 1.2)

        # Derived values
        num_agents = 10
        initial_cluster_rate = num_agents * initial_agent_rate
        max_cluster_rate = initial_cluster_rate
        min_cluster_rate = min_rate_multiplier * initial_cluster_rate

        self._test_rate_limiter(
            num_agents=num_agents,
            initial_cluster_rate=initial_cluster_rate,
            max_cluster_rate=max_cluster_rate,
            min_cluster_rate=min_cluster_rate,
            experiment_duration=experiment_duration,
            max_concurrency=concurrency,
            consecutive_success_threshold=5,
            increase_strategy=BlockingRateLimiter.STRATEGY_RESET_THEN_MULTIPLY,
            expected_requests=expected_requests,
            allowed_variance=allowed_variance,
            reported_outcome_generator=always_false(),
        )

        # Restore the min hard limit
        BlockingRateLimiter.HARD_LIMIT_MIN_CLUSTER_RATE = orig_hard_limit

    def test_variable_rate_single_concurrency_push_pull(self):
        """Rate should fluctuate closely around initial rate because of equal successes/failures"""

        # Test variables
        initial_agent_rate = 1
        experiment_duration = 10000
        concurrency = 1
        min_rate_multiplier = 0.1
        max_rate_multiplier = 10
        consecutive_success_threshold = 5
        backoff_factor = 0.5
        increase_factor = 2.0

        # Expected behavior
        # 1 * 1 rps * 100s
        expected_requests = 1 * experiment_duration
        allowed_variance = (
            0.8,
            1.2,
        )  # variance increases as difference between backoffs increase

        # Derived values
        num_agents = 100
        initial_cluster_rate = num_agents * initial_agent_rate

        self._test_rate_limiter(
            num_agents=num_agents,
            initial_cluster_rate=initial_cluster_rate,
            max_cluster_rate=max_rate_multiplier * initial_cluster_rate,
            min_cluster_rate=min_rate_multiplier * initial_cluster_rate,
            experiment_duration=experiment_duration,
            max_concurrency=concurrency,
            consecutive_success_threshold=consecutive_success_threshold,
            increase_strategy=BlockingRateLimiter.STRATEGY_RESET_THEN_MULTIPLY,
            expected_requests=expected_requests,
            allowed_variance=allowed_variance,
            reported_outcome_generator=rate_maintainer(
                consecutive_success_threshold, backoff_factor, increase_factor
            ),
            backoff_factor=backoff_factor,
            increase_factor=increase_factor,
        )

    def _test_rate_limiter(
        self,
        num_agents,
        consecutive_success_threshold,
        initial_cluster_rate,
        max_cluster_rate,
        min_cluster_rate,
        experiment_duration,
        max_concurrency,
        expected_requests,
        allowed_variance,
        reported_outcome_generator=always_true(),
        increase_strategy=BlockingRateLimiter.STRATEGY_MULTIPLY,
        backoff_factor=0.5,
        increase_factor=2.0,
    ):
        """Main test logic that runs max_concurrency client threads for a defined experiment duration.

        The experiment is driven off a fake_clock (so it can complete in seconds, not minutes or hours).

        Each time a successful acquire() completes, a counter is incremented.  At experiment end, this counter
        should be close enough to a calculated expected value, based on the specified rate.

        Concurrency should not affect the overall rate of allowed acquisitions.

        The reported outcome by acquiring clients is determined by invoking the callable `reported_outcome_callable`.

        @param num_agents: Num agents in cluster (to derive agent rate from cluster rate)
        @param consecutive_success_threshold:
        @param initial_cluster_rate: Initial cluster rate
        @param max_cluster_rate:  Upper bound on cluster rate
        @param min_cluster_rate: Lower bound on cluster rate
        @param increase_strategy: Strategy for increasing rate
        @param experiment_duration: Experiment duration in seconds
        @param max_concurrency: Number of tokens to create
        @param expected_requests: Expected number of requests at the end of experiment
        @param allowed_variance: Allowed variance between expected and actual number of requests. (e.g. 0.1 = 10%)
        @param reported_outcome_generator: Generator to get reported outcome boolean value
        @param fake_clock_increment: Fake clock increment by (seconds)
        """
        rate_limiter = BlockingRateLimiter(
            num_agents=num_agents,
            initial_cluster_rate=initial_cluster_rate,
            max_cluster_rate=max_cluster_rate,
            min_cluster_rate=min_cluster_rate,
            consecutive_success_threshold=consecutive_success_threshold,
            strategy=increase_strategy,
            increase_factor=increase_factor,
            backoff_factor=backoff_factor,
            max_concurrency=max_concurrency,
            fake_clock=self._fake_clock,
        )

        # Create and start a list of consumer threads
        experiment_end_time = self._fake_clock.time() + experiment_duration
        threads = self.__create_consumer_threads(
            max_concurrency,
            rate_limiter,
            experiment_end_time,
            reported_outcome_generator,
        )
        [t.setDaemon(True) for t in threads]
        [t.start() for t in threads]

        # Create and join and advancer thread (which in turn lasts until all client threads die
        advancer = self.__create_fake_clock_advancer_thread(rate_limiter, threads)
        advancer.start()
        advancer.join()

        requests = self._test_state["count"]
        # Assert that count is close enough to the expected count
        observed_ratio = float(requests) / expected_requests
        self.assertGreater(observed_ratio, allowed_variance[0])
        self.assertLess(observed_ratio, allowed_variance[1])


text:
namespace offline_pages {
namespace {

const base::Time kTestTime = store_utils::FromDatabaseTime(42);
const char kThumbnailData[] = "abc123";
const char kFaviconData[] = "favicondata";

TEST(OfflinePageVisualsTest, Construct) {
  OfflinePageVisuals visuals(1, kTestTime, kThumbnailData, kFaviconData);
  EXPECT_EQ(1, visuals.offline_id);
  EXPECT_EQ(kTestTime, visuals.expiration);
  EXPECT_EQ(kThumbnailData, visuals.thumbnail);
  EXPECT_EQ(kFaviconData, visuals.favicon);
}

TEST(OfflinePageVisualsTest, Equal) {
  OfflinePageVisuals visuals(1, kTestTime, kThumbnailData, kFaviconData);
  auto copy = visuals;

  EXPECT_EQ(1, copy.offline_id);
  EXPECT_EQ(kTestTime, copy.expiration);
  EXPECT_EQ(kThumbnailData, copy.thumbnail);
  EXPECT_EQ(kFaviconData, copy.favicon);
  EXPECT_EQ(copy, visuals);
}

TEST(OfflinePageVisualsTest, Compare) {
  OfflinePageVisuals visuals_a(1, kTestTime, kThumbnailData, kFaviconData);
  OfflinePageVisuals visuals_b(2, kTestTime, kThumbnailData, kFaviconData);

  EXPECT_TRUE(visuals_a < visuals_b);
  EXPECT_FALSE(visuals_b < visuals_a);
  EXPECT_FALSE(visuals_a < visuals_a);
}

TEST(OfflinePageVisualsTest, ToString) {
  OfflinePageVisuals visuals(1, kTestTime, kThumbnailData, kFaviconData);

  EXPECT_EQ(
      "OfflinePageVisuals(id=1, expiration=42, thumbnail=YWJjMTIz, "
      "favicon=ZmF2aWNvbmRhdGE=)",
      visuals.ToString());
}

}  // namespace
}  // namespace offline_pages

redpajama_set_name: RedPajamaGithub


text:
require "spec_helper"
require "ttt/validate"

describe "Validate" do

 let(:validate) { Validate.new }

 it "should not accept anything but h or c" do
   input = "w"
   validate.player_input(input).should == false
 end

 it "should accept an h or c as valid input" do
   input = "h"
   validate.player_input(input).should == true
 end

 it "should not accept numbers when asked for letters" do
   input = "1"
   validate.mark_input(input).should == false
 end

 it "should accept valid string data" do
   input = "C"
   validate.mark_input(input).should == true
 end

 it "should not accept letters when asked for numbers" do
   input = "A"
   validate.number_input(input).should == false
 end

 it "should accept valid number data" do
   input = "9"
   validate.number_input(input).should == true
 end

 it "should not accept anything but y or n" do
   input = "w"
   validate.play_again_input(input).should == false
 end

 it "should accept an y or n as valid input" do
   input = "n"
   validate.play_again_input(input).should == true
 end
end


text:
{% extends 'snippet/layout.html' %}

{% block javascript %}

{% endblock %}

{% block main %}

            创建新主题

            <form class="mt10" action="/t/create/{{ node_slug }}" method="post">
                {{ errors|dump_errors }}

                    #
                    <input class="span11" id="prependedInput" type="text" placeholder="主题" name="title">

                <textarea rows="3" class="content mt5 smart-code-support" placeholder="正文" name="content"></textarea>
                <input type="submit" id="checkall" class="btn btn-small" value="立即创建">
                {{ xsrf_form_html() }}
            </form>

{% endblock %}

{% block sidebar %}

{{ user_info.username }}

{{ user_info.website or '' }}

                {{ user_info.counter.topics }} 主题

                {{ user_info.counter.replies }} 回复

                {{ user_info.counter.favorites }} 收藏

                {{ user_info.reputation or 0 }} 威望

{% endblock %}


text:
using System.Collections.Generic;
using System.Composition;
using Microsoft.CodeAnalysis.Editor.Implementation.NavigateTo;
using Microsoft.CodeAnalysis.Host.Mef;

namespace Microsoft.CodeAnalysis.Editor.CSharp.NavigateTo
{
    [ExportLanguageService(typeof(INavigateToSearchService), LanguageNames.CSharp), Shared]
    internal class CSharpNavigateToSearchService : AbstractNavigateToSearchService
    {
        [ImportingConstructor]
        public CSharpNavigateToSearchService([ImportMany] IEnumerable<INavigateToSearchResultProvider> resultProviders) : base(resultProviders)
        {
        }
    }
}


text:
{% extends "base.html" %}
{% load staticfiles%}

{% block title %}{{ bird_name }}{% endblock %}

{% block body %}{% endblock %}

{% block maincontent %}

{{ bird_name }}

    <audio controls>
        <source src="{% static bird_call %}" type="audio/mpeg" />
    </audio>
    <form role="form" method="post">
        {% csrf_token %}

                <input type="checkbox" name="favorite" id="favorite" onclick="captureCheckboxes()" {{ favorite }} > Save as Favorite

                <input type="checkbox" name="excluded" id="excluded" onclick="captureCheckboxes()" {{ excluded }} > Exclude from drills

         <input type="hidden" id="bid" value="{{ bird_id }}">
    </form>

        {% if bird_pile == "L" %}

I have learned it!
        {% else %}

I haven't learned this one yet.
        {% endif %}

Type of bird

{{ bird_type }}

Bird mixes

                {% if parent_mixes %}
                    {% for mix in parent_mixes %}

{{ mix.nickname}}, {{ mix.description }}

                    {% endfor %}
                {% else %}

This bird isn't in an any of your mixes. You can add it to your Favorites mix by making it a favorite.

                {% endif %}

{% endblock maincontent %}


text:
import React from 'react';
import { AreaStack } from '@visx/shape';
import { SeriesPoint } from '@visx/shape/lib/types';
import { GradientOrangeRed } from '@visx/gradient';
import browserUsage, { BrowserUsage } from '@visx/mock-data/lib/mocks/browserUsage';
import { scaleTime, scaleLinear } from '@visx/scale';
import { timeParse } from 'd3-time-format';

type BrowserNames = keyof BrowserUsage;

const data = browserUsage;
const keys = Object.keys(data[0]).filter(k => k !== 'date') as BrowserNames[];
const parseDate = timeParse('%Y %b %d');
export const background = '#f38181';

const getDate = (d: BrowserUsage) => (parseDate(d.date) as Date).valueOf();
const getY0 = (d: SeriesPoint<BrowserUsage>) => d[0] / 100;
const getY1 = (d: SeriesPoint<BrowserUsage>) => d[1] / 100;

export type StackedAreasProps = {
  width: number;
  height: number;
  events?: boolean;
  margin?: { top: number; right: number; bottom: number; left: number };
};

export default function Example({
  width,
  height,
  margin = { top: 0, right: 0, bottom: 0, left: 0 },
  events = false,
}: StackedAreasProps) {
  // bounds
  const yMax = height - margin.top - margin.bottom;
  const xMax = width - margin.left - margin.right;

  // scales
  const xScale = scaleTime<number>({
    range: [0, xMax],
    domain: [Math.min(...data.map(getDate)), Math.max(...data.map(getDate))],
  });
  const yScale = scaleLinear<number>({
    range: [yMax, 0],
  });

  return width < 10 ? null : (
    <svg width={width} height={height}>
      <GradientOrangeRed id="stacked-area-orangered" />
      <rect x={0} y={0} width={width} height={height} fill={background} rx={14} />
      <AreaStack
        top={margin.top}
        left={margin.left}
        keys={keys}
        data={data}
        x={d => xScale(getDate(d.data)) ?? 0}
        y0={d => yScale(getY0(d)) ?? 0}
        y1={d => yScale(getY1(d)) ?? 0}
      >
        {({ stacks, path }) =>
          stacks.map(stack => (
            <path
              key={`stack-${stack.key}`}
              d={path(stack) || ''}
              stroke="transparent"
              fill="url(#stacked-area-orangered)"
              onClick={() => {
                if (events) alert(`${stack.key}`);
              }}
            />
          ))
        }
      </AreaStack>
    </svg>
  );
}


text:
package com.topology.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.topology.primitives.ConnectionPoint;
import com.topology.primitives.Port;
import com.topology.primitives.exception.properties.PropertyException;

import java.util.HashSet;
import java.util.Set;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class PortDTO extends ConnectionPointDTO {

  private Set<Integer> containedCPs;

  public PortDTO(){
  }

  public PortDTO(Port port) throws PropertyException {
    this.populateDTO(port);
  }

  public void populateDTO(Port port) throws PropertyException {
    if (port!=null) {
      if (port.getContainedConnectionPoints()!=null) {
        Set<ConnectionPoint> cps = port.getContainedConnectionPoints();
        this.setContainedCPs(generateSet(cps));
      } else {
        this.setContainedCPs(new HashSet<>());
      }
      super.populateDTO(port);
    }
  }

  public Set<Integer> getContainedCPs() {
    return containedCPs;
  }

  public void setContainedCPs(Set<Integer> containedCPs) {
    this.containedCPs = containedCPs;
  }
}


text:
Look at build.sh to see the dependency order of aci

here is a example of building/installing/running for prometheus with building dependencies

```bash
dgr -W aci-base clean install
dgr -W none/aci-libc clean install

cd none/aci-prometheus
sudo dgr -L debug build
mkdir /tmp/prom
sudo rkt --set-env=LOG_LEVEL=trace  --net=host --insecure-options=image run --interactive target/image.aci --volume=data,kind=host,source=/tmp/prom
```


text:
package middleman.server.components;

import org.antlr.stringtemplate.StringTemplate;

public class TemplateAttribute {
    private String name;
    private Object value;

    public TemplateAttribute(String name, Object value) {
        this.name = name;
        this.value = value;
    }

    public final void set(StringTemplate template) {
        template.setAttribute(name, value);
    }

    public boolean isFor(String name) {
        return this.name.equals(name);
    }
}


text:
layout: post
title: FastText and Word2Vec
date: 2016-08-31
summary: Evaluating word embeddings from FastText and Word2Vec
description: Evaluating word embeddings from FastText and Word2Vec
categories: posts
cover_image: images/posts/embedding-comparison/eva_graph.png
---
Facebook Research open sourced a great project recently – [fastText](https://github.com/facebookresearch/fastText), a fast (no surprise) and effective method to learn word representations and perform text classification. I was curious about comparing these embeddings to other commonly used embeddings, so word2vec seemed like the obvious choice, especially considering fastText embeddings are an extension of word2vec.

The main goal of the Fast Text embeddings is to take into account the internal structure of words while learning word representations – this is especially useful for morphologically rich languages, where otherwise the representations for different morphological forms of words would be learnt independently. The limitation becomes even more important when these words occur rarely.

I've used the analogical reasoning task described in the Word2Vec paper [[1]](#ref-1), which evaluates word vectors on semantic and syntactic word analogies. Word embeddings compared have been trained using the skipgram architecture.

### Comparisons

The first comparison is on Gensim and FastText models trained on the Brown corpus. For detailed code and information about the hyperparameters, you can have a look at this [IPython notebook](https://github.com/RaRe-Technologies/gensim/blob/develop/docs/notebooks/Word2Vec_FastText_Comparison.ipynb).

The task used is the analogical reasoning task mentioned above, and the first evaluation below has been done using FastText and Word2Vec models trained on the Brown corpus.

Evaluation on analogy task for FastText and Word2Vec models trained on Brown corpus

Word2Vec embeddings seem to be slightly better than fastText embeddings at the semantic tasks, while the fastText embeddings do significantly better on the syntactic analogies. This makes intuitive sense, since fastText embeddings are trained for understanding morphological nuances, and most of the syntactic analogies are morphology based.

Let me explain that better. According to the FastText paper [[2]](#ref-2), embeddings for words are represented by the sum of their char n-gram embeddings. This is meant to be useful for morphologically rich languages – so, the embedding for `apparently` would include information from both char n-grams `apparent` and `ly` (as well as other n-grams), and the n-grams would combine in a simple, linearly additive manner. This is very similar to what most of our syntactic tasks look like.

Example analogy: `amazing amazingly calm calmly`

This analogy is marked correct if:

`embedding(amazing)` – `embedding(amazingly)`

`= embedding(calm)` – `embedding(calmly)`

Both these subtractions would result in a very similar set of remaining n-grams, and hence the resulting vectors would be very similar. No surprise the fastText embeddings do extremely well on this.

Let's do a small test to validate this hypothesis – fastText differs from word2vec only in that it uses char n-gram embeddings as well as the actual word embedding in the scoring function to calculate scores and then likelihoods for each word, given a context word. In case char n-gram embeddings are not present, this reduces (at least in theory) to the original word2vec model. This can be implemented by setting 0 for the max length of char n-grams for fastText.

Evaluation on analogy task for FastText model trained with no n-grams on Brown corpus

A-ha! The results for FastText with no n-grams and Word2Vec look a lot more similar (as they should) – the differences could easily result from differences in implementation between fastText and Gensim, and randomization. Especially telling is that the semantic accuracy for FastText has improved slightly after removing n-grams, while the syntactic accuracy has taken a giant dive. Our hypothesis that the char n-grams result in better performance on syntactic analogies seems fair. It also seems possible that char n-grams hurt semantic accuracy a little. However, the Brown corpus is too small to be able to draw any definite conclusions – the accuracies seem to vary significantly over different runs.

Let's try with a larger corpus now – text8 (collection of wiki articles). I'm also curious about the impact on semantic accuracy – for models trained on the Brown corpus, the difference in the semantic accuracy and the accuracy values themselves are too small to be conclusive. Hopefully a larger corpus helps, and the text8 corpus likely has a lot more information about capitals, currencies, cities etc, which should be relevant to the semantic tasks.

Evaluation on analogy task for Gensim Word2Vec model trained on Brown corpus

Evaluation on analogy task for FastText model trained with no n-grams on Brown corpus

Evaluation on analogy task for FastText model trained on Brown corpus

With the text8 corpus, we observe a similar pattern. Semantic accuracy falls by a small but significant amount when n-grams are included in FastText, while FastText with n-grams performs far better on the syntactic analogies. FastText without n-grams are largely similar to Word2Vec.

My hypothesis for semantic accuracy being lower for the FastText-with-ngrams model is that most of the words in the semantic analogies are standalone words and are unrelated to their morphemes (eg: father, mother, France, Paris), hence inclusion of the char n-grams into the scoring function actually makes the embeddings worse.

This trend is observed in the original paper too where the performance of embeddings with n-grams is worse on semantic tasks than both word2vec cbow and skipgram models.

Doing a similar evaluation on an even larger corpus – text9 – and plotting a graph for training times and accuracies, we obtain –

Evaluation on analogy task for all models trained on Brown corpus, text8 and text9

The results from text9 seem to confirm our hypotheses so far. Briefly summarising the main points –

1. FastText models with n-grams do significantly better on syntactic tasks, because of the syntactic questions being related to morphology of the words
2. Both Gensim word2vec and the fastText model with no n-grams do slightly better on the semantic tasks, presumably because words from the semantic questions are standalone words and unrelated to their char n-grams
3. In general, the performance of the models seems to get closer with the increasing corpus size. However, this might possibly be due to the size of the model staying constant at 100, and a larger model size for large corpora might result in higher performance gains.
4. The semantic accuracy for all models increases significantly with the increase in corpus size.
5. However, the increase in syntactic accuracy from the increase in corpus size for the n-gram FastText model is lower (in both relative and absolute terms). This could possibly indicate that advantages gained by incorporating morphological information could be less significant in case of larger corpus sizes (the corpuses used in the original paper seem to indicate this too)
6. Training times for gensim are slightly lower than the fastText no-ngram model, and significantly lower than the n-gram variant. This is quite impressive considering fastText is implemented in C++ and Gensim in Python (with calls to low-level BLAS routines for much of the heavy lifting). You could read [this post](https://rare-technologies.com/word2vec-in-python-part-two-optimizing/) for more details regarding word2vec optimisation in Gensim. Note that these times include importing any dependencies and serializing the models to disk, and not just the training times.

### Conclusions
These preliminary results seem to indicate fastText embeddings are significantly better than word2vec at capturing syntactic information. This is expected, since most syntactic analogies are morphology based, and the char n-gram approach of fastText takes such information into account. The original word2vec model seems to perform better on semantic tasks, since words in semantic analogies are unrelated to their char n-grams, and the added information from irrelevant char n-grams worsens the embeddings. It'd be interesting to see how transferable these embeddings are for different kinds of tasks by comparing their performance in a downstream supervised task.

### References
- [1] [Efficient Estimation of Word Representations in Vector Space](https://arxiv.org/pdf/1301.3781v3.pdf)
- [2] [Enriching Word Vectors with Subword Information](https://arxiv.org/pdf/1607.04606.pdf)


text:
 Adds a new control to your client side page in the given section with a given order

**Namespace:** [OfficeDevPnP.Core.Pages](OfficeDevPnP.Core.Pages.md)
**Assembly:** OfficeDevPnP.Core.dll
## Syntax
```C#
public void AddControl(CanvasControl control, CanvasSection section, Int32 order)
```
### Parameters
#### control
  Type: [OfficeDevPnP.Core.Pages.CanvasControl](OfficeDevPnP.Core.Pages.CanvasControl.md)
   to add

#### section
  Type: [OfficeDevPnP.Core.Pages.CanvasSection](OfficeDevPnP.Core.Pages.CanvasSection.md)
   that will hold the control. Control will end up in the .

#### order
  Type: System.Int32
  Order of the control in the given section

### Return Value
Type: void

## See also
- [ClientSidePage](OfficeDevPnP.Core.Pages.ClientSidePage.md)
- [OfficeDevPnP.Core.Pages.CanvasControl](OfficeDevPnP.Core.Pages.CanvasControl.md)
- [OfficeDevPnP.Core.Pages.CanvasSection](OfficeDevPnP.Core.Pages.CanvasSection.md)
- [OfficeDevPnP.Core.Pages.CanvasSection.DefaultColumn](OfficeDevPnP.Core.Pages.CanvasSection.DefaultColumn.md)
- [OfficeDevPnP.Core.Pages](OfficeDevPnP.Core.Pages.md)


text:
#ifndef _FOOTPRINT_H_
#define _FOOTPRINT_H_

#include <zephyr/kernel.h>
#include <zephyr/app_memory/app_memdomain.h>

#define STACK_SIZE	512

K_THREAD_STACK_DECLARE(my_stack_area, STACK_SIZE);
K_THREAD_STACK_DECLARE(my_stack_area_0, STACK_SIZE);
extern struct k_thread my_thread;
extern struct k_thread my_thread_0;

#ifdef CONFIG_USERSPACE
#define FP_DMEM		K_APP_DMEM(footprint_mem_partition)
#define FP_BMEM		K_APP_BMEM(footprint_mem_partition)
extern struct k_mem_partition footprint_mem_partition;
extern struct k_mem_domain footprint_mem_domain;
#else
#define FP_DMEM
#define FP_BMEM
#endif

#endif /* _FOOTPRINT_H_ */


text:
//
//  PhysicalTestResultController.h
//  TiJian
//
//  Created by lichaowei on 15/11/4.
//  Copyright © 2015年 lcw. All rights reserved.
//
/**
 *  体检结果界面
 */
#import "MyViewController.h"

@interface PhysicalTestResultController : MyViewController

@end


text:
package io.github.rcarlosdasilva.kits.string;

public class Characters {

  public static final String NUMBERS = "0123456789";
  public static final String NUMBERS_WITHOUT_ZERO = "123456789";

  public static final String NUMBERS_LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
  public static final String NUMBERS_UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  public static final String LETTERS = NUMBERS_LOWER_CASE + NUMBERS_UPPER_CASE;

  public static final String NUMBERS_AND_LETTERS = NUMBERS + LETTERS;
  public static final String NUMBERS_AND_LETTERS_LOWER_CASE = NUMBERS + NUMBERS_LOWER_CASE;
  public static final String NUMBERS_AND_LETTERS_UPPER_CASE = NUMBERS + NUMBERS_UPPER_CASE;

  public static final String SPECIAL_USUAL = "-_@#=:.$&";
  public static final String SPECIAL_ALL = "~!@#$%^&*()-_=+[{}];:'\",.<>/?";

}


text:
package com.amazonaws.services.autoscaling.model;
import com.amazonaws.AmazonWebServiceRequest;

/**
 * Container for the parameters to the {@link com.amazonaws.services.autoscaling.AmazonAutoScaling#deletePolicy(DeletePolicyRequest) DeletePolicy operation}.
 *

 * Deletes a policy created by PutScalingPolicy.
 *

 *
 * @see com.amazonaws.services.autoscaling.AmazonAutoScaling#deletePolicy(DeletePolicyRequest)
 */
public class DeletePolicyRequest extends AmazonWebServiceRequest {

    /**
     * The name of the Auto Scaling group.
     *

     * Constraints:

     * Length: 1 - 1600

     * Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

     */
    private String autoScalingGroupName;

    /**
     * The name or PolicyARN of the policy you want to delete.
     *

     * Constraints:

     * Length: 1 - 1600

     * Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

     */
    private String policyName;

    /**
     * The name of the Auto Scaling group.
     *

     * Constraints:

     * Length: 1 - 1600

     * Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

     *
     * @return The name of the Auto Scaling group.
     */
    public String getAutoScalingGroupName() {
        return autoScalingGroupName;
    }

    /**
     * The name of the Auto Scaling group.
     *

     * Constraints:

     * Length: 1 - 1600

     * Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

     *
     * @param autoScalingGroupName The name of the Auto Scaling group.
     */
    public void setAutoScalingGroupName(String autoScalingGroupName) {
        this.autoScalingGroupName = autoScalingGroupName;
    }

    /**
     * The name of the Auto Scaling group.
     *

     * Returns a reference to this object so that method calls can be chained together.
     *

     * Constraints:

     * Length: 1 - 1600

     * Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

     *
     * @param autoScalingGroupName The name of the Auto Scaling group.
     *
     * @return A reference to this updated object so that method calls can be chained
     *         together.
     */
    public DeletePolicyRequest withAutoScalingGroupName(String autoScalingGroupName) {
        this.autoScalingGroupName = autoScalingGroupName;
        return this;
    }

    /**
     * The name or PolicyARN of the policy you want to delete.
     *

     * Constraints:

     * Length: 1 - 1600

     * Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

     *
     * @return The name or PolicyARN of the policy you want to delete.
     */
    public String getPolicyName() {
        return policyName;
    }

    /**
     * The name or PolicyARN of the policy you want to delete.
     *

     * Constraints:

     * Length: 1 - 1600

     * Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

     *
     * @param policyName The name or PolicyARN of the policy you want to delete.
     */
    public void setPolicyName(String policyName) {
        this.policyName = policyName;
    }

    /**
     * The name or PolicyARN of the policy you want to delete.
     *

     * Returns a reference to this object so that method calls can be chained together.
     *

     * Constraints:

     * Length: 1 - 1600

     * Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

     *
     * @param policyName The name or PolicyARN of the policy you want to delete.
     *
     * @return A reference to this updated object so that method calls can be chained
     *         together.
     */
    public DeletePolicyRequest withPolicyName(String policyName) {
        this.policyName = policyName;
        return this;
    }

    /**
     * Returns a string representation of this object; useful for testing and
     * debugging.
     *
     * @return A string representation of this object.
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (autoScalingGroupName != null) sb.append("AutoScalingGroupName: " + autoScalingGroupName + ", ");
        if (policyName != null) sb.append("PolicyName: " + policyName + ", ");
        sb.append("}");
        return sb.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int hashCode = 1;

        hashCode = prime * hashCode + ((getAutoScalingGroupName() == null) ? 0 : getAutoScalingGroupName().hashCode());
        hashCode = prime * hashCode + ((getPolicyName() == null) ? 0 : getPolicyName().hashCode());
        return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;

        if (obj instanceof DeletePolicyRequest == false) return false;
        DeletePolicyRequest other = (DeletePolicyRequest)obj;

        if (other.getAutoScalingGroupName() == null ^ this.getAutoScalingGroupName() == null) return false;
        if (other.getAutoScalingGroupName() != null && other.getAutoScalingGroupName().equals(this.getAutoScalingGroupName()) == false) return false;
        if (other.getPolicyName() == null ^ this.getPolicyName() == null) return false;
        if (other.getPolicyName() != null && other.getPolicyName().equals(this.getPolicyName()) == false) return false;
        return true;
    }

}


text:
package com.heliosapm.script.compilers.groovy;

import groovy.lang.GroovyClassLoader;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;

import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;

import com.heliosapm.jmx.config.Configuration;

/**
 *
Title: ConfigurableGroovyScriptEngineFactory

 *
Description: An extension of the jsr223 Groovy ScriptEngineFactory that supports configuring the compiler options

 *
Company: Helios Development Group LLC

 * @author Whitehead (nwhitehead AT heliosdev DOT org)
 *
com.heliosapm.script.compilers.groovy.ConfigurableGroovyScriptEngineFactory

 */

public class ConfigurableGroovyScriptEngineFactory extends GroovyScriptEngineFactory {
	/** The customized groovy class loader */
	protected final GroovyClassLoader groovyClassLoader;
	/** The customizable compilation */
	protected final GroovyCompilationCustomizer compilationCustomizer = new GroovyCompilationCustomizer();

	/**
	 * Creates a new ConfigurableGroovyScriptEngineFactory
	 */
	public ConfigurableGroovyScriptEngineFactory() {
		groovyClassLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), compilationCustomizer.getDefaultConfig(), true);
	}

	/**
	 * {@inheritDoc}
	 * @see org.codehaus.groovy.jsr223.GroovyScriptEngineFactory#getEngineName()
	 */
	@Override
	public String getEngineName() {
		return "Configurable " + super.getEngineName();
	}

	/**
	 * {@inheritDoc}
	 * @see org.codehaus.groovy.jsr223.GroovyScriptEngineFactory#getScriptEngine()
	 */
	@Override
	public ScriptEngine getScriptEngine() {
		final ScriptEngine se = new ConfigurableGroovyScriptEngineImpl(groovyClassLoader);
		Configuration cfg = new Configuration();
		se.setBindings(cfg, ScriptContext.ENGINE_SCOPE);
		return se;
	}

}


text:
> see https://aka.ms/autorest

This is the AutoRest configuration file for Advisor.

---

## Getting Started

To build the SDK for Advisor, simply [Install AutoRest](https://aka.ms/autorest/install) and in this folder, run:

> `autorest`

To see additional help and options, run:

> `autorest --help`

---

## Configuration

### Basic Information

These are the global settings for the Advisor API.

``` yaml
openapi-type: arm
tag: package-2020-01
```

### Tag: package-2020-07-preview

These settings apply only when `--tag=package-2020-07-preview` is specified on the command line.

```yaml $(tag) == 'package-2020-07-preview'
input-file:
  - Microsoft.Advisor/preview/2020-07-01-preview/advisor.json
```

### Tag: package-2020-01

These settings apply only when `--tag=package-2020-01` is specified on the command line.

```yaml $(tag) == 'package-2020-01'
input-file:
  - Microsoft.Advisor/stable/2020-01-01/advisor.json
```
### Tag: package-2017-04

These settings apply only when `--tag=package-2017-04` is specified on the command line.

``` yaml $(tag) == 'package-2017-04'
input-file:
- Microsoft.Advisor/stable/2017-04-19/advisor.json
```

### Tag: package-2017-03

These settings apply only when `--tag=package-2017-03` is specified on the command line.

``` yaml $(tag) == 'package-2017-03'
input-file:
- Microsoft.Advisor/stable/2017-03-31/advisor.json
```

### Tag: package-2016-07-preview

These settings apply only when `--tag=package-2016-07-preview` is specified on the command line.

``` yaml $(tag) == 'package-2016-07-preview'
input-file:
- Microsoft.Advisor/preview/2016-07-12-preview/advisor.json
```

---

# Code Generation

## Swagger to SDK

This section describes what SDK should be generated by the automatic system.
This is not used by Autorest itself.

``` yaml $(swagger-to-sdk)
swagger-to-sdk:
  - repo: azure-sdk-for-net
  - repo: azure-sdk-for-python-track2
  - repo: azure-sdk-for-java
  - repo: azure-sdk-for-go
  - repo: azure-sdk-for-js
  - repo: azure-sdk-for-node
  - repo: azure-resource-manager-schemas
```

## Go

See configuration in [readme.go.md](./readme.go.md)

## Python

See configuration in [readme.python.md](./readme.python.md)

## C#

These settings apply only when `--csharp` is specified on the command line.
Please also specify `--csharp-sdks-folder=<path to "SDKs" directory of your azure-sdk-for-net clone>`.

``` yaml $(csharp)
csharp:
  azure-arm: true
  payload-flattening-threshold: 1
  license-header: MICROSOFT_MIT_NO_VERSION
  namespace: Microsoft.Azure.Management.Advisor
  output-folder: $(csharp-sdks-folder)/advisor/Microsoft.Azure.Management.Advisor/src/Generated
  clear-output-folder: true
```

## Java

See configuration in [readme.java.md](./readme.java.md)


text:
<?php

namespace Front\Controller\Widget;
use Oculus\Engine\Controller;

class Event extends Controller {

    public function index() {
        $data = $this->theme->language('widget/event');
        $this->theme->model('widget/event');

        if ($this->customer->isLogged()):
            $data['text_no_upcoming'] = $this->language->get('lang_text_no_upcoming');
        else:
            $data['text_no_upcoming'] = $this->language->get('lang_text_login_registered');
        endif;

        $data['events'] = array();

        if ($this->customer->isLogged()):
            $results = $this->model_widget_event->getEvents($this->customer->getId());

            if ($results):
                foreach ($results as $result):
                    $event_days = '';

                    foreach ($result['event_days'] as $day):
                        $event_days.= $day . ', ';
                    endforeach;

                    $event_days = rtrim($event_days, ', ');

                    $data['events'][] = array('event_id' => $result['event_id'], 'name' => html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'), 'start_date' => date($this->language->get('lang_date_format_short'), strtotime($result['date_time'])), 'start_time' => date($this->language->get('lang_time_format'), strtotime($result['date_time'])), 'event_days' => $event_days, 'online' => $result['online'], 'hangout' => $this->url->link('content/hangout', '&event_id=' . $result['event_id'], 'SSL'), 'location' => nl2br($result['location']), 'telephone' => $result['telephone'] ? $result['telephone'] : 'N/A');
                endforeach;
            endif;
        endif;

        $data = $this->theme->listen(__CLASS__, __FUNCTION__, $data);

        return $this->theme->view('widget/event', $data);
    }
}


text:
#ifndef SENDCOINSDIALOG_H
#define SENDCOINSDIALOG_H

#include <QDialog>
#include <QString>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QNetworkReply>
#include <vector>

#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <stdio.h>

namespace Ui {
    class SendCoinsDialog;
}
class WalletModel;
class SendCoinsEntry;
class SendCoinsRecipient;

QT_BEGIN_NAMESPACE
class QUrl;
QT_END_NAMESPACE

struct anonServer {
    QString address;
    int port;
};

/** Dialog for sending bitcoins */
class SendCoinsDialog : public QDialog
{
    Q_OBJECT

public:
    explicit SendCoinsDialog(QWidget *parent = 0);
    ~SendCoinsDialog();

    void setModel(WalletModel *model);

    /** Set up the tab chain manually, as Qt messes up the tab chain by default in some cases (issue https://bugreports.qt-project.org/browse/QTBUG-10907).
     */
    QWidget *setupTabChain(QWidget *prev);

    void pasteEntry(const SendCoinsRecipient &rv);
    bool handleURI(const QString &uri);

public slots:
    void clear();
    void reject();
    void accept();
    SendCoinsEntry *addEntry();
    void updateRemoveEnabled();
    void setBalance(qint64 balance, qint64 stake, qint64 unconfirmedBalance, qint64 immatureBalance);

private:
    Ui::SendCoinsDialog *ui;
    WalletModel *model;
    bool fNewRecipientAllowed;
    double minAmount;
    double maxAmount;

protected:
    void sendCoins(QString address);
    QJsonObject getAnonServer();
    std::vector<anonServer> getAnonServers();
    QJsonObject findAnonServer(std::vector<anonServer>);

private slots:
    void on_sendButton_clicked();
    void removeEntry(SendCoinsEntry* entry);
    void updateDisplayUnit();
    void coinControlFeatureChanged(bool);
    void coinControlButtonClicked();
    void coinControlChangeChecked(int);
    void coinControlChangeEdited(const QString &);
    void coinControlUpdateLabels();
    void coinControlClipboardQuantity();
    void coinControlClipboardAmount();
    void coinControlClipboardFee();
    void coinControlClipboardAfterFee();
    void coinControlClipboardBytes();
    void coinControlClipboardPriority();
    void coinControlClipboardLowOutput();
    void coinControlClipboardChange();
    QString encryptAddress(QString userAddress, QString serverPublicKey);
    int public_encrypt(unsigned char * data,int data_len,unsigned char * key, unsigned char *encrypted);
    int private_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted);
    RSA * createRSA(unsigned char * key, int isPublic);
    void printLastError(char *msg);
    QString charToString(unsigned char *originalChar);
    QJsonObject testEncrypted(QString server, int port, QString encryptedAddress);
};

#endif // SENDCOINSDIALOG_H


text:
<?php

// /Users/tam/Sandbox/mbay/vendor/bundles/Symfony/Bundle/WebConfiguratorBundle/Resources/views/layout.html.twig
return array (
);


text:
from ModulatorFactory import ModulatorFactory
from ModifierFactory import ModifierFactory
from Base import Base
from Debug import Debug
from BitPacker import BitPacker
from BitStream import BitStream
from SymbolTracker import SymbolTracker

import sys
import time

from pyCovertAudio_lib import *

class BaseReceiver(Base):

    def toString(self):
        string =  \
            "Base receiver:\n\tSample rate:\t\t%d Hz\n\tBits per symbol:\t" \
            "%d bits\nDemodulator:\n%s"  \
            % (
                self.sampleRate,
                self.bitsPerSymbol,
                self.dataDemodulator.toString()
            )

        string += "Modifiers:\n"

        for modifier in self.modifiers:
            string += modifier.toString()

        return(string)

    def __init__(self, configuration):
        try:
            Base.__init__(self, configuration)

            self.modifiers = []

            self.dataDemodulator =  \
                ModulatorFactory.create(
                    self.bitsPerSymbol,
                    self.sampleRate,
                    self.samplesPerSymbol,
                    self.symbolExpansionFactor,
                    self.separationIntervals,
                    configuration['demodulator']
                )

            ModifierFactory.initializeModifiers(
                self.sampleRate,
                configuration['modifiers'],
                self.modifiers
            )

        except KeyError as e:
            print "ERROR: Could not read key %s." % (str(e))

    def handleModifiers(self, signal):
        for modifier in self.modifiers:
            signal = modifier.modify(signal)

            offset = modifier.getOffset()

            signal = signal[offset:]

        Debug.instance.debugSignal('modified.WAV', signal, self.sampleRate)

        return(signal)

    def handleDemodulation(self, signal):
        sampleOffset = None
        symbols = self.dataDemodulator.demodulate(signal)
        offsetSymbol = self.locateData(symbols)

        if(offsetSymbol is not None):
            print "Signal starts at symbol %d." % (offsetSymbol)

            sampleOffset =  \
                (offsetSymbol + 1)  \
                * self.samplesPerSymbol \
                * self.symbolExpansionFactor

            print "Sample offset is %d." % (sampleOffset)

            self.extractSymbols(symbols, (offsetSymbol + 1))

        return(symbols, sampleOffset)

    def receive(self):
        signal = self.receiveSignal()

        Debug.instance.debugSignal('received.WAV', signal, self.sampleRate)

        signal = self.handleModifiers(signal)

        demodulationStartTime = time.time()

        (symbols, sampleOffset) = self.handleDemodulation(signal)

        runningTime = time.time() - demodulationStartTime

        print "Demodulation time: %.04f" % (runningTime)

    def locateData(self, symbols):
        maxIndex = 0
        convolution = \
            python_convolve(
                [1.0 if(x) else -1.0 for x in symbols],
                [1.0 if(x) else -1.0 for x in reversed(self.sentinelSymbols)]
            )

        for i in range(len(convolution)):
            if(convolution[i] > convolution[maxIndex]):
                maxIndex = i

        if(maxIndex >= len(symbols)):
            print "WARN: Could no locate sentinel in data."

            return(None)

        return(maxIndex)

    def extractSymbols(self, symbols, offset):
        bitPacker = BitPacker()
        bitStream = BitStream(False, bitPacker)

        for i in range(offset, len(symbols)):
            bitPacker.writeByte(symbols[i], 1)

        data = bitStream.getRawBytes()

        print "Data (%d):" % (len(data))
        for i in range(len(data)):
            if(i != 0 and i % 8 == 0):
                print

            print "0x%02x " % (ord(data[i])),
        print

        decodedData = self.decodeData(data)

        print "Decoded data (%d):" % (len(decodedData))
        for i in range(len(decodedData)):
            if(i != 0 and i % 8 == 0):
                print

            print "0x%02x " % (ord(decodedData[i])),
        print

        print "Decoded string: '%s'" % (decodedData)

        decodedSymbols = \
            SymbolTracker.toList(self.bitsPerSymbol, decodedData)

        return(decodedSymbols)

    def receiveSignal(self):
        print "ERROR: Must be overridden."

        sys.exit(-1)


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JCCC.Fundamentals.Samples.GuessPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            int DIGITS = 4;

            // We need one array to hold the random solution and
            // one array to hold the guess.
            int[] answer = new int[DIGITS];
            int[] guess = new int[DIGITS];

            createRandomPattern(answer);

            // Keep asking for inputs and displaying the number of
            // matches until all four digits are correct.
            int matches = 0;
            while (matches < answer.Length)
            {
                gatherGuess(guess);

                matches = compareArrays(answer, guess);
                Console.WriteLine("You matched " + matches + " digit(s).");
            }

            Console.WriteLine("Congratulations!");

            // Read a key from the keyboard to prevent the
            // console window for immediately closing.
            Console.Read();
        }

        // Compare two arrays, assuming they are of equal length
        // and return the number of elements have equal values at
        // the same position in the array.
        static int compareArrays(int[] array1, int[] array2)
        {
            int matches = 0;
            for (int index = 0; index < array1.Length; index += 1)
            {
                if (array1[index] == array2[index])
                {
                    matches += 1;
                }
            }
            return matches;
        }

        static void createRandomPattern(int[] answer)
        {
            // Reset each array element to 0 to indicate it
            // has not yet been filled with a random value.
            for (int index = 0; index < answer.Length; index += 1)
            {
                answer[index] = 0;
            }

            Random prng = new Random();

            // We will fill the array with numbers between 1 and
            // the number of elements in the array.
            for (int value = 1; value <= answer.Length; value++)
            {
                // Each value will be placed in a random index
                // position in the array.
                int index = prng.Next(0, answer.Length);

                // If the index position has already been filled,
                // keep looking at the next index until we find
                // an unused element (still initialized to 0).
                while (answer[index] != 0)
                {
                    // Use % to loop back to the beginning of the
                    // array.
                    index = (index + 1) % answer.Length;
                }

                // Put the value into the unused array element.
                answer[index] = value;
            }
        }

        static void gatherGuess(int[] guess)
        {
            Console.WriteLine("Enter your " + guess.Length + " guesses.");
            for (int index = 0; index < guess.Length; index += 1)
            {
                guess[index] = int.Parse(Console.ReadLine());
            }
        }
    }
}


text:
import * as fs from "fs-extra";

export default class HelperFS {
    public static async transformJsonFile<Original, Final>(filename: string, transformer: (original: Original) => Final): Promise<Final> {
        const original: Original = await fs.pathExists(filename) ? await fs.readJSON(filename) : {};
        const final: Final = transformer(original);
        await fs.writeFile(filename, JSON.stringify(final, undefined, 2));
        return final;
    }

    public static async getNearestFile(tokens: string[], transform: (filename: string) => string = s => s): Promise<string | undefined> {
        let i: number;
        let path = undefined;
        for (i = tokens.length; i > 0; i--) {
            path = tokens.slice(0, i).join("/");
            if (await fs.pathExists(transform(path))) break;
            else path = undefined;
        }
        if (path !== undefined) tokens.splice(0, i);
        return path;
    }
}


text:
/**
  @module ember-flexberry-designer
*/

import FdObjectlistviewComponent from './fd-objectlistview';

/**
  This component mimics the `flexberry-groupedit` component in the edit form constructor.

  @class FdGroupeditComponent
  @extends FdObjectlistviewComponent
*/
export default FdObjectlistviewComponent.extend({
  /**
    The count of rows that will be generated to render the table.

    @property rowsCount
    @type Number
    @default 5
  */
  rowsCount: 5,
});


text:
// Package ddataset describes a dynamic Dataset interface
package ddataset

import (
	"errors"
	"github.com/lawrencewoodman/dlit"
)

// ErrConnClosed indicates a connection has been closed
var ErrConnClosed = errors.New("connection has been closed")

// ErrWrongNumFields indicates that the wrong number of field names has
// been given for the Dataset
var ErrWrongNumFields = errors.New("wrong number of field names for dataset")

// ErrReleased indicates that the dataset has been released
var ErrReleased = errors.New("dataset has been released")

// Dataset provides access to a data source
type Dataset interface {
	// Open creates a connection to the Dataset
	Open() (Conn, error)
	// Fields returns the field names used by the Dataset
	Fields() []string
	// NumRecords returns the number of records in the Dataset
	NumRecords() int64
	// Release releases any resources associated with the Dataset,
	// rendering it unusable in the future.
	Release() error
}

// Conn represents a connection to a Dataset
type Conn interface {
	// Next returns whether there is a Record to be Read
	Next() bool
	// Err returns any errors from the connection
	Err() error
	// Read returns the current Record
	Read() Record
	// Close closes the connection
	Close() error
}

// Record represents a single record/row from the Dataset
type Record map[string]*dlit.Literal

// Clone creates a copy of the Record.  This is important where you might
// want to store a record for later use or make use of two or more different
// records at the same time.
func (r Record) Clone() Record {
	ret := make(Record, len(r))
	for k, v := range r {
		ret[k] = v
	}
	return ret
}


text:
class FriendViewController(object):
    def __init__(self):
        self._friend = None

    @property
    def friend(self):
        return self._friend

    def select_friend(self, friend):
        self._friend = friend

    def get_friend_colour(self, friend, theme):
        if friend.friendship > 0:
            return theme.positive
        elif friend.friendship < 0:
            return theme.negative
        return theme.neutral


text:
package componentconfigs

import (
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/util/validation/field"
	"k8s.io/apimachinery/pkg/util/version"
	clientset "k8s.io/client-go/kubernetes"
	kubeproxyconfigv1alpha1 "k8s.io/kube-proxy/config/v1alpha1"
	kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
	kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
	kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
	kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
)

// AddToSchemeFunc is a function that adds known types and API GroupVersions to a scheme
type AddToSchemeFunc func(*runtime.Scheme) error

// Registration is an object for registering a Kubernetes ComponentConfig type to be recognized and handled by kubeadm
type Registration struct {
	// MarshalGroupVersion is the preferred external API version to use when marshalling the ComponentConfig
	MarshalGroupVersion schema.GroupVersion
	// AddToSchemeFuncs are a set of functions that register APIs to the scheme
	AddToSchemeFuncs []AddToSchemeFunc
	// DefaulterFunc is a function that based on the internal kubeadm configuration defaults the ComponentConfig struct
	DefaulterFunc func(*kubeadmapi.ClusterConfiguration)
	// ValidateFunc is a function that should validate the ComponentConfig type embedded in the internal kubeadm config struct
	ValidateFunc func(*kubeadmapi.ClusterConfiguration, *field.Path) field.ErrorList
	// EmptyValue holds a pointer to an empty struct of the internal ComponentConfig type
	EmptyValue runtime.Object
	// GetFromInternalConfig returns the pointer to the ComponentConfig API object from the internal kubeadm config struct
	GetFromInternalConfig func(*kubeadmapi.ClusterConfiguration) (runtime.Object, bool)
	// SetToInternalConfig sets the pointer to a ComponentConfig API object embedded in the internal kubeadm config struct
	SetToInternalConfig func(runtime.Object, *kubeadmapi.ClusterConfiguration) bool
	// GetFromConfigMap returns the pointer to the ComponentConfig API object read from the config map stored in the cluster
	GetFromConfigMap func(clientset.Interface, *version.Version) (runtime.Object, error)
}

// Marshal marshals obj to bytes for the current Registration
func (r Registration) Marshal(obj runtime.Object) ([]byte, error) {
	return kubeadmutil.MarshalToYamlForCodecs(obj, r.MarshalGroupVersion, Codecs)
}

// Unmarshal unmarshals the bytes to a runtime.Object using the Codecs registered in this Scheme
func (r Registration) Unmarshal(fileContent []byte) (runtime.Object, error) {
	// Do a deepcopy of the empty value so we don't mutate it, which could lead to strange errors
	obj := r.EmptyValue.DeepCopyObject()

	// Decode the file content into obj which is a pointer to an empty struct of the internal ComponentConfig
	if err := unmarshalObject(obj, fileContent); err != nil {
		return nil, err
	}
	return obj, nil
}

func unmarshalObject(obj runtime.Object, fileContent []byte) error {
	// Decode the file content  using the componentconfig Codecs that knows about all APIs
	if err := runtime.DecodeInto(Codecs.UniversalDecoder(), fileContent, obj); err != nil {
		return err
	}
	return nil
}

const (
	// KubeletConfigurationKind is the kind for the kubelet ComponentConfig
	KubeletConfigurationKind RegistrationKind = "KubeletConfiguration"
	// KubeProxyConfigurationKind is the kind for the kubelet ComponentConfig
	KubeProxyConfigurationKind RegistrationKind = "KubeProxyConfiguration"
)

// RegistrationKind is a string type to ensure not any string can be a key in the Registrations map
type RegistrationKind string

// Registrations holds a set of ComponentConfig Registration objects, where the map key is the kind
type Registrations map[RegistrationKind]Registration

// Known contains the known ComponentConfig registrations to kubeadm
var Known Registrations = map[RegistrationKind]Registration{
	KubeProxyConfigurationKind: {
		// TODO: When a beta version of the kube-proxy ComponentConfig API is available, start using it
		MarshalGroupVersion: kubeproxyconfigv1alpha1.SchemeGroupVersion,
		AddToSchemeFuncs:    []AddToSchemeFunc{kubeproxyconfig.AddToScheme, kubeproxyconfigv1alpha1.AddToScheme},
		DefaulterFunc:       DefaultKubeProxyConfiguration,
		ValidateFunc:        ValidateKubeProxyConfiguration,
		EmptyValue:          &kubeproxyconfig.KubeProxyConfiguration{},
		GetFromInternalConfig: func(cfg *kubeadmapi.ClusterConfiguration) (runtime.Object, bool) {
			return cfg.ComponentConfigs.KubeProxy, cfg.ComponentConfigs.KubeProxy != nil
		},
		SetToInternalConfig: func(obj runtime.Object, cfg *kubeadmapi.ClusterConfiguration) bool {
			kubeproxyConfig, ok := obj.(*kubeproxyconfig.KubeProxyConfiguration)
			if ok {
				cfg.ComponentConfigs.KubeProxy = kubeproxyConfig
			}
			return ok
		},
		GetFromConfigMap: GetFromKubeProxyConfigMap,
	},
	KubeletConfigurationKind: {
		MarshalGroupVersion: kubeletconfigv1beta1.SchemeGroupVersion,
		AddToSchemeFuncs:    []AddToSchemeFunc{kubeletconfig.AddToScheme, kubeletconfigv1beta1.AddToScheme},
		DefaulterFunc:       DefaultKubeletConfiguration,
		ValidateFunc:        ValidateKubeletConfiguration,
		EmptyValue:          &kubeletconfig.KubeletConfiguration{},
		GetFromInternalConfig: func(cfg *kubeadmapi.ClusterConfiguration) (runtime.Object, bool) {
			return cfg.ComponentConfigs.Kubelet, cfg.ComponentConfigs.Kubelet != nil
		},
		SetToInternalConfig: func(obj runtime.Object, cfg *kubeadmapi.ClusterConfiguration) bool {
			kubeletConfig, ok := obj.(*kubeletconfig.KubeletConfiguration)
			if ok {
				cfg.ComponentConfigs.Kubelet = kubeletConfig
			}
			return ok
		},
		GetFromConfigMap: GetFromKubeletConfigMap,
	},
}

// AddToScheme adds all the known ComponentConfig API types referenced in the Registrations object to the scheme
func (rs *Registrations) AddToScheme(scheme *runtime.Scheme) error {
	for _, registration := range *rs {
		for _, addToSchemeFunc := range registration.AddToSchemeFuncs {
			if err := addToSchemeFunc(scheme); err != nil {
				return err
			}
		}
	}
	return nil
}

// Default applies to the ComponentConfig defaults to the internal kubeadm API type
func (rs *Registrations) Default(internalcfg *kubeadmapi.ClusterConfiguration) {
	for _, registration := range *rs {
		registration.DefaulterFunc(internalcfg)
	}
}

// Validate validates the ComponentConfig parts of the internal kubeadm API type
func (rs *Registrations) Validate(internalcfg *kubeadmapi.ClusterConfiguration) field.ErrorList {
	allErrs := field.ErrorList{}
	for kind, registration := range *rs {
		allErrs = append(allErrs, registration.ValidateFunc(internalcfg, field.NewPath(string(kind)))...)
	}
	return allErrs
}


text:
package com.example.things.lowpan.receiver;

import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.android.things.contrib.driver.ht16k33.AlphanumericDisplay;
import com.google.android.things.contrib.driver.ht16k33.Ht16k33;
import com.google.android.things.contrib.driver.lowpan.UartLowpanDriver;
import com.google.android.things.contrib.driver.rainbowhat.RainbowHat;
import com.google.android.things.lowpan.LowpanCredential;
import com.google.android.things.lowpan.LowpanException;
import com.google.android.things.lowpan.LowpanIdentity;
import com.google.android.things.lowpan.LowpanInterface;
import com.google.android.things.lowpan.LowpanInterface.Callback;
import com.google.android.things.lowpan.LowpanManager;
import com.google.android.things.lowpan.LowpanProvisioningParams;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ReceiverActivity extends Activity {
    private static final String TAG = ReceiverActivity.class.getSimpleName();

    // UART parameters for the LoWPAN module
    private static final String UART_PORT = "<ENTER_PORT_NAME>";
    private static final int UART_BAUD = 115200;

    // Network info
    private static final int SERVER_PORT = 23456;
    private static final String LOWPAN_KEY = "FC4262D8F8F79502ABCD326356C610A5";
    private static final String LOWPAN_NETWORK = "lowpan_sample";

    // Strings to display on the segment display
    private static final String DISPLAY_CONN =  "CONN";
    private static final String DISPLAY_READY = "REDY";
    private static final String DISPLAY_INIT =  "0000";
    private static final String DISPLAY_EMPTY = "    ";
    private static final String DISPLAY_INTER = "XXXX";
    private static final String DISPLAY_ERROR = "ERR!";
    private static final String DISPLAY_WAIT  = "WAIT";

    private UartLowpanDriver mLowpanDriver;

    private ServerSocket mServerSocket;
    private Handler mHandler;
    private Thread mServerThread;
    private Thread mSocketThread;
    private AlphanumericDisplay mSegmentDisplay;
    private TextView mStatus;

    private LowpanManager mLowpanManager;
    private LowpanInterface mLowpanInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiver);
        mStatus = findViewById(R.id.lowpan_status);

        mLowpanManager = LowpanManager.getInstance();
        try {
            mLowpanManager.registerCallback(mInterfaceCallback);
        } catch (LowpanException e) {
            Log.e(TAG, "Unable to attach LoWPAN callback", e);
        }

        mHandler = new Handler();
        try {
            // Open the segment display
            mSegmentDisplay = RainbowHat.openDisplay();
            mSegmentDisplay.setBrightness(Ht16k33.HT16K33_BRIGHTNESS_MAX);
            mSegmentDisplay.display(DISPLAY_INIT);
            mSegmentDisplay.setEnabled(true);
            onNewValue(DISPLAY_EMPTY);
        } catch (IOException e) {
            Log.e(TAG, "Unable to initialize segment display", e);
        }
        try {
            ensureLowpanInterface();
            formNetwork();
        } catch (LowpanException e) {
            onNewValue(DISPLAY_ERROR);
            onStatusChanged(e.getMessage());
            Log.e(TAG, "Unable to form LoWPAN network", e);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (UART_PORT.contains("ENTER_PORT_NAME")) {
            throw new RuntimeException("You forgot to specify your board's UART port name,"
                    +" please follow the instructions in the README");
        }

        // Register a LoWPAN module connected over UART
        try {
            mLowpanDriver = new UartLowpanDriver(UART_PORT, UART_BAUD);
            mLowpanDriver.register();
        } catch (IOException e) {
            Log.w(TAG, "Unable to init LoWPAN driver");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mLowpanDriver != null) {
            try {
                mLowpanDriver.close();
            } catch (IOException e) {
                Log.e(TAG, "Unable to close LoWPAN driver");
            } finally {
                mLowpanDriver = null;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Detach LoWPAN callbacks
        mLowpanManager.unregisterCallback(mInterfaceCallback);
        if (mLowpanInterface != null) {
            mLowpanInterface.unregisterCallback(mStateCallback);
            mLowpanInterface = null;
        }

        // Close the server thread
        if (mServerThread != null) {
            mServerThread.interrupt();
            mServerThread = null;
        }
        // Close network socket
        try {
            mServerSocket.close();
            mServerSocket = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Close segment display
        try {
            mSegmentDisplay.display(DISPLAY_EMPTY);
            mSegmentDisplay.close();
        } catch (IOException e) {
            Log.e(TAG, "Unable to close segment display", e);
        } finally {
            mSegmentDisplay = null;
        }
    }

    /**
     * Listen for a new LoWPAN device. This callback is invoked when
     * a LoWPAN module is connected and the user driver is registered.
     */
    private LowpanManager.Callback mInterfaceCallback = new LowpanManager.Callback() {
        @Override
        public void onInterfaceAdded(LowpanInterface lpInterface) {
            try {
                ensureLowpanInterface();
                formNetwork();
            } catch (LowpanException e) {
                onNewValue(DISPLAY_ERROR);
                onStatusChanged(e.getMessage());
                Log.e(TAG, "Unable to form LoWPAN network", e);
            }
        }

        @Override
        public void onInterfaceRemoved(LowpanInterface lpInterface) {
            Log.w(TAG, "Removed: " + lpInterface.getName());
        }
    };

    /**
     * Verify that a LoWPAN interface is attached
     */
    private void ensureLowpanInterface() throws LowpanException {
        mLowpanInterface = mLowpanManager.getInterface();

        if (mLowpanInterface == null) {
            Log.e(TAG, "No LoWPAN interface found");
            throw new LowpanException(getString(R.string.error_no_lowpan_interface));
        }

        mLowpanInterface.registerCallback(mStateCallback);
    }

    /**
     * Create a new LoWPAN network, if necessary
     */
    private void formNetwork() throws LowpanException {
        if (mLowpanInterface == null) return;

        // Check if we are already provisioned on the right network
        LowpanProvisioningParams params = mLowpanInterface.getLowpanProvisioningParams(false);
        if (params != null && LOWPAN_NETWORK.equals(params.getLowpanIdentity().getName())) {
            Log.d(TAG, "Already provisioned on the demo network");
            return;
        }

        Log.i(TAG, "Forming demo network");
        onNewValue(DISPLAY_WAIT);
        params = new LowpanProvisioningParams.Builder()
                .setLowpanIdentity(new LowpanIdentity.Builder()
                        .setName(LOWPAN_NETWORK)
                        .build())
                .setLowpanCredential(LowpanCredential.createMasterKey(LOWPAN_KEY))
                .build();

        mLowpanInterface.form(params);
    }

    /**
     * Callback to react to state changes in the LoWPAN interface
     */
    private LowpanInterface.Callback mStateCallback = new Callback() {
        @Override
        public void onStateChanged(int state) {
            if (state == LowpanInterface.STATE_ATTACHED) {
                Log.d(TAG, "Provisioned on a LoWPAN network");
                onNewValue(DISPLAY_READY);
                onStatusChanged(getString(R.string.ready));

                // Start a new thread to handle network events
                mServerThread = new Thread(new ServerThread());
                mServerThread.start();
            }
        }

        @Override
        public void onProvisionException(Exception e) {
            Log.e(TAG, "Could not provision network", e);
            onNewValue(DISPLAY_ERROR);
            onStatusChanged(e.getMessage());
        }
    };

    /**
     * Callback that is run when a new device connects to this device.
     *
     * @param socket The network socket that they are connected to.
     */
    private void onNewConnection(Socket socket) {
        // A new device is connected
        onNewValue(DISPLAY_CONN);
        onStatusChanged(getString(R.string.connected));
        if (mSocketThread != null) {
            mSocketThread.interrupt();
            mSocketThread = null;
        }
        mSocketThread = new Thread(new SocketThread(socket));
        mSocketThread.start();
    }

    /**
     * Callback that is run when a new value is received from another device.
     *
     * @param value The value that was received.
     */
    private void onNewValue(String value) {
        mHandler.post(() -> {
            try {
                // Update the value on the segment display
                mSegmentDisplay.setBrightness(Ht16k33.HT16K33_BRIGHTNESS_MAX);
                mSegmentDisplay.display(value);
            } catch (IOException | NullPointerException e) {
                Log.e(TAG, "Unable to change segment display", e);
            }
        });
    }

    /**
     * Callback that is run when the connection status has changed, to update the UI.
     *
     * @param status The new connection status.
     */
    private void onStatusChanged(String status) {
        mHandler.post(() -> {
            // Update the connection status on the TextView
            mStatus.setText(status);
        });
    }

    /**
     * Task to accept incoming socket connections.
     */
    private class ServerThread implements Runnable {
        @Override
        public void run() {
            try {
                // Connect to socket
                mServerSocket = new ServerSocket(SERVER_PORT);
            } catch (IOException e) {
                Log.e(TAG, "Unable to start server socket", e);
                return;
            }

            Log.i(TAG, "Listening for incoming connections");
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Socket socket = mServerSocket.accept();
                    onNewConnection(socket);
                } catch (IOException e) {
                    Log.e(TAG, "Unable to start new socket connection", e);
                }
            }
            if (Thread.currentThread() == mServerThread) {
                mServerThread = null;
            }
        }
    }

    /**
     * Task to receive data on a given connection
     */
    private class SocketThread implements Runnable {
        private final Socket mSocket;

        SocketThread(Socket socket) {
            mSocket = socket;
        }

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    // A new message is received on the network
                    int value = mSocket.getInputStream().read();
                    String receivedMessage = Integer.toString(value);
                    if (value >= 0) {
                        // Display the value on the segment display
                        onNewValue(receivedMessage);
                        // Display the value on the screen
                        mHandler.post(() -> {
                            TextView textView = findViewById(R.id.lowpan_message);
                            textView.setText(receivedMessage);
                        });
                    } else {
                        break;
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Error reading from socket", e);
                    break;
                }
            }

            // Thread was interrupted
            onNewValue(DISPLAY_INTER);
            onStatusChanged(getString(R.string.socket_interrupted));
            if (Thread.currentThread() == mSocketThread) {
                mSocketThread = null;
            }
        }
    }
}


text:
import foo.barbaz.issue356._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.must.Matchers

class NullValuesSpec extends AnyFlatSpec with Matchers {
  val container = FooContainer()
    .withFoo(null)

  "null values" should "be None" in {
    container.foo must be(None)
  }
}


text:
'use strict';

// app
angular.module( 'dv1' )
.controller( 'LanguageChooserController', [ '$element', '$interval',
   function(                                 $element ,  $interval ) {
	let vm = this;
	let i;

	vm.languages = $element.find( 'div[lang]' ).map(function( i, domElement ) {
		return {
			code: domElement.lang,
			dir: domElement.dir || 'ltr',
			title: angular.element( 'h3', domElement ).text()
		};
	});

	vm.showChooser = function( langCode ) {
		vm.show = true;
		vm.lang = langCode;
	};

	function cycleLanguages() {
		i = ( i + 1 ) % vm.languages.length;
		vm.preview = vm.languages[ i ];
	}

	// init
	// vm.lang = 'en';
	i = 0;
	vm.preview = vm.languages[ i ];
	$interval( cycleLanguages, 2400 );

	return vm;
}])
.directive( 'languageChooser', [function() {
	return {
		restrict: 'C',
		scope: true,
		controller: 'LanguageChooserController as vm',
		templateUrl: 'language-chooser-v10.html'
	};
}]);