text:
from collections import Iterable
from numbers import Real, Integral
import warnings
from xml.etree import ElementTree as ET
import sys

import numpy as np

from openmc.clean_xml import *
from openmc.checkvalue import (check_type, check_length, check_value,
                               check_greater_than, check_less_than)
from openmc.source import Source

if sys.version_info[0] >= 3:
    basestring = str

class SettingsFile(object):
    """Settings file used for an OpenMC simulation. Corresponds directly to the
    settings.xml input file.

    Attributes
    ----------
    run_mode : {'eigenvalue' or 'fixed source'}
        The type of calculation to perform (default is 'eigenvalue')
    batches : int
        Number of batches to simulate
    generations_per_batch : int
        Number of generations per batch
    inactive : int
        Number of inactive batches
    particles : int
        Number of particles per generation
    keff_trigger : dict
        Dictionary defining a trigger on eigenvalue. The dictionary must have
        two keys, 'type' and 'threshold'. Acceptable values corresponding to
        type are 'variance', 'std_dev', and 'rel_err'. The threshold value
        should be a float indicating the variance, standard deviation, or
        relative error used.
    source : Iterable of openmc.source.Source
        Distribution of source sites in space, angle, and energy
    output : dict
        Dictionary indicating what files to output. Valid keys are 'summary',
        'cross_sections', 'tallies', and 'distribmats'. Values corresponding to
        each key should be given as a boolean value.
    output_path : str
        Path to write output to
    verbosity : int
        Verbosity during simulation between 1 and 10
    statepoint_batches : Iterable of int
        List of batches at which to write statepoint files
    statepoint_interval : int
        Number of batches after which a new statepoint file should be written
    sourcepoint_batches : Iterable of int
        List of batches at which to write source files
    sourcepoint_interval : int
        Number of batches after which a new source file should be written
    sourcepoint_separate : bool
        Indicate whether the souce should be written as part of the statepoint
        file or on its own
    sourcepoint_write : bool
        Indicate whether the source should be written at all
    sourcepoint_overwrite : bool
        Indicate whether to
    confidence_intervals : bool
        If True, uncertainties on tally results will be reported as the
        half-width of the 95% two-sided confidence interval. If False,
        uncertainties on tally results will be reported as the sample standard
        deviation.
    cross_sections : str
        Indicates the path to an XML cross section listing file (usually named
        cross_sections.xml). If it is not set, the :envvar:`CROSS_SECTIONS`
        environment variable will be used to find the path to the XML cross
        section listing.
    energy_grid : str
        Set the method used to search energy grids. Acceptable values are
        'nuclide', 'logarithm', and 'material-union'.
    ptables : bool
        Determine whether probability tables are used.
    run_cmfd : bool
        Indicate if coarse mesh finite difference acceleration is to be used
    seed : int
        Seed for the linear congruential pseudorandom number generator
    survival_biasing : bool
        Indicate whether survival biasing is to be used
    weight : float
        Weight cutoff below which particle undergo Russian roulette
    weight_avg : float
        Weight assigned to particles that are not killed after Russian roulette
    entropy_dimension : tuple or list
        Number of Shannon entropy mesh cells in the x, y, and z directions,
        respectively
    entropy_lower_left : tuple or list
        Coordinates of the lower-left point of the Shannon entropy mesh
    entropy_upper_right : tuple or list
        Coordinates of the upper-right point of the Shannon entropy mesh
    trigger_active : bool
        Indicate whether tally triggers are used
    trigger_max_batches : int
        Maximum number of batches simulated. If this is set, the number of
        batches specified via ``batches`` is interpreted as the minimum number
        of batches
    trigger_batch_interval : int
        Number of batches in between convergence checks
    no_reduce : bool
        Indicate that all user-defined and global tallies should not be reduced
        across processes in a parallel calculation.
    threads : int
        Number of OpenMP threads
    trace : tuple or list
        Show detailed information about a single particle, indicated by three
        integers: the batch number, generation number, and particle number
    track : tuple or list
        Specify particles for which track files should be written. Each particle
        is identified by a triplet with the batch number, generation number, and
        particle number.
    ufs_dimension : tuple or list
        Number of uniform fission site (UFS) mesh cells in the x, y, and z
        directions, respectively
    ufs_lower_left : tuple or list
        Coordinates of the lower-left point of the UFS mesh
    ufs_upper_right : tuple or list
        Coordinates of the upper-right point of the UFS mesh

    """

    def __init__(self):

        # Run mode subelement (default is 'eigenvalue')
        self._run_mode = 'eigenvalue'
        self._batches = None
        self._generations_per_batch = None
        self._inactive = None
        self._particles = None
        self._keff_trigger = None

        # Source subelement
        self._source = None

        self._confidence_intervals = None
        self._cross_sections = None
        self._energy_grid = None
        self._ptables = None
        self._run_cmfd = None
        self._seed = None
        self._survival_biasing = None

        # Entropy subelement
        self._entropy_dimension = None
        self._entropy_lower_left = None
        self._entropy_upper_right = None

        # Trigger subelement
        self._trigger_subelement = None
        self._trigger_active = None
        self._trigger_max_batches = None
        self._trigger_batch_interval = None

        self._output = None
        self._output_path = None

        # Statepoint subelement
        self._statepoint_batches = None
        self._statepoint_interval = None
        self._sourcepoint_batches = None
        self._sourcepoint_interval = None
        self._sourcepoint_separate = None
        self._sourcepoint_write = None
        self._sourcepoint_overwrite = None

        self._threads = None
        self._no_reduce = None

        self._verbosity = None

        self._trace = None
        self._track = None

        # Cutoff subelement
        self._weight = None
        self._weight_avg = None

        # Uniform fission source subelement
        self._ufs_dimension = 1
        self._ufs_lower_left = None
        self._ufs_upper_right = None

        # Domain decomposition subelement
        self._dd_mesh_dimension = None
        self._dd_mesh_lower_left = None
        self._dd_mesh_upper_right = None
        self._dd_nodemap = None
        self._dd_allow_leakage = False
        self._dd_count_interactions = False

        self._settings_file = ET.Element("settings")
        self._run_mode_subelement = None
        self._source_element = None

    @property
    def run_mode(self):
        return self._run_mode

    @property
    def batches(self):
        return self._batches

    @property
    def generations_per_batch(self):
        return self._generations_per_batch

    @property
    def inactive(self):
        return self._inactive

    @property
    def particles(self):
        return self._particles

    @property
    def keff_trigger(self):
        return self._keff_trigger

    @property
    def source(self):
        return self._source

    @property
    def confidence_intervals(self):
        return self._confidence_intervals

    @property
    def cross_sections(self):
        return self._cross_sections

    @property
    def energy_grid(self):
        return self._energy_grid

    @property
    def ptables(self):
        return self._ptables

    @property
    def run_cmfd(self):
        return self._run_cmfd

    @property
    def seed(self):
        return self._seed

    @property
    def survival_biasing(self):
        return self._survival_biasing

    @property
    def entropy_dimension(self):
        return self._entropy_dimension

    @property
    def entropy_lower_left(self):
        return self._entropy_lower_left

    @property
    def entropy_upper_right(self):
        return self._entropy_upper_right

    @property
    def trigger_active(self):
        return self._trigger_active

    @property
    def trigger_max_batches(self):
        return self._trigger_max_batches

    @property
    def trigger_batch_interval(self):
        return self._batch_interval

    @property
    def output(self):
        return self._output

    @property
    def output_path(self):
        return self._output_path

    @property
    def statepoint_batches(self):
        return self._statepoint_batches

    @property
    def statepoint_interval(self):
        return self._statepoint_interval

    @property
    def sourcepoint_batches(self):
        return self._sourcepoint_interval

    @property
    def sourcepoint_interval(self):
        return self._sourcepoint_interval

    @property
    def sourcepoint_separate(self):
        return self._sourcepoint_separate

    @property
    def sourcepoint_write(self):
        return self._sourcepoint_write

    @property
    def sourcepoint_overwrite(self):
        return self._sourcepoint_overwrite

    @property
    def threads(self):
        return self._threads

    @property
    def no_reduce(self):
        return self._no_reduce

    @property
    def verbosity(self):
        return self._verbosity

    @property
    def trace(self):
        return self._trace

    @property
    def track(self):
        return self._track

    @property
    def weight(self):
        return self._weight

    @property
    def weight_avg(self):
        return self._weight_avg

    @property
    def ufs_dimension(self):
        return self._ufs_dimension

    @property
    def ufs_lower_left(self):
        return self._ufs_lower_left

    @property
    def ufs_upper_right(self):
        return self._ufs_upper_right

    @property
    def dd_mesh_dimension(self):
        return self._dd_mesh_dimension

    @property
    def dd_mesh_lower_left(self):
        return self._dd_mesh_lower_left

    @property
    def dd_mesh_upper_right(self):
        return self._dd_mesh_upper_right

    @property
    def dd_nodemap(self):
        return self._dd_nodemap

    @property
    def dd_allow_leakage(self):
        return self._dd_allow_leakage

    @property
    def dd_count_interactions(self):
        return self._dd_count_interactions

    @run_mode.setter
    def run_mode(self, run_mode):
        if 'run_mode' not in ['eigenvalue', 'fixed source']:
            msg = 'Unable to set run mode to "{0}". Only "eigenvalue" ' \
                  'and "fixed source" are supported."'.format(run_mode)
            raise ValueError(msg)
        self._run_mode = run_mode

    @batches.setter
    def batches(self, batches):
        check_type('batches', batches, Integral)
        check_greater_than('batches', batches, 0)
        self._batches = batches

    @generations_per_batch.setter
    def generations_per_batch(self, generations_per_batch):
        check_type('generations per patch', generations_per_batch, Integral)
        check_greater_than('generations per batch', generations_per_batch, 0)
        self._generations_per_batch = generations_per_batch

    @inactive.setter
    def inactive(self, inactive):
        check_type('inactive batches', inactive, Integral)
        check_greater_than('inactive batches', inactive, 0, True)
        self._inactive = inactive

    @particles.setter
    def particles(self, particles):
        check_type('particles', particles, Integral)
        check_greater_than('particles', particles, 0)
        self._particles = particles

    @keff_trigger.setter
    def keff_trigger(self, keff_trigger):
        if not isinstance(keff_trigger, dict):
            msg = 'Unable to set a trigger on keff from "{0}" which ' \
                  'is not a Python dictionary'.format(keff_trigger)
            raise ValueError(msg)

        elif 'type' not in keff_trigger:
            msg = 'Unable to set a trigger on keff from "{0}" which ' \
                  'does not have a "type" key'.format(keff_trigger)
            raise ValueError(msg)

        elif keff_trigger['type'] not in ['variance', 'std_dev', 'rel_err']:
            msg = 'Unable to set a trigger on keff with ' \
                  'type "{0}"'.format(keff_trigger['type'])
            raise ValueError(msg)

        elif 'threshold' not in keff_trigger:
            msg = 'Unable to set a trigger on keff from "{0}" which ' \
                  'does not have a "threshold" key'.format(keff_trigger)
            raise ValueError(msg)

        elif not isinstance(keff_trigger['threshold'], Real):
            msg = 'Unable to set a trigger on keff with ' \
                  'threshold "{0}"'.format(keff_trigger['threshold'])
            raise ValueError(msg)

        self._keff_trigger = keff_trigger

    @source.setter
    def source(self, source):
        if isinstance(source, Source):
            self._source = [source,]
        else:
            check_type('source distribution', source, Iterable, Source)
            self._source = source

    @output.setter
    def output(self, output):
        if not isinstance(output, dict):
            msg = 'Unable to set output to "{0}" which is not a Python ' \
                  'dictionary of string keys and boolean values'.format(output)
            raise ValueError(msg)

        for element in output:
            keys = ['summary', 'cross_sections', 'tallies', 'distribmats']
            if element not in keys:
                msg = 'Unable to set output to "{0}" which is unsupported by ' \
                      'OpenMC'.format(element)
                raise ValueError(msg)

            if not isinstance(output[element], (bool, np.bool)):
                msg = 'Unable to set output for "{0}" to a non-boolean ' \
                      'value "{1}"'.format(element, output[element])
                raise ValueError(msg)

        self._output = output

    @output_path.setter
    def output_path(self, output_path):
        check_type('output path', output_path, basestring)
        self._output_path = output_path

    @verbosity.setter
    def verbosity(self, verbosity):
        check_type('verbosity', verbosity, Integral)
        check_greater_than('verbosity', verbosity, 1, True)
        check_less_than('verbosity', verbosity, 10, True)
        self._verbosity = verbosity

    @statepoint_batches.setter
    def statepoint_batches(self, batches):
        check_type('statepoint batches', batches, Iterable, Integral)
        for batch in batches:
            check_greater_than('statepoint batch', batch, 0)
        self._statepoint_batches = batches

    @statepoint_interval.setter
    def statepoint_interval(self, interval):
        check_type('statepoint interval', interval, Integral)
        self._statepoint_interval = interval

    @sourcepoint_batches.setter
    def sourcepoint_batches(self, batches):
        check_type('sourcepoint batches', batches, Iterable, Integral)
        for batch in batches:
            check_greater_than('sourcepoint batch', batch, 0)
        self._sourcepoint_batches = batches

    @sourcepoint_interval.setter
    def sourcepoint_interval(self, interval):
        check_type('sourcepoint interval', interval, Integral)
        self._sourcepoint_interval = interval

    @sourcepoint_separate.setter
    def sourcepoint_separate(self, source_separate):
        check_type('sourcepoint separate', source_separate, bool)
        self._sourcepoint_separate = source_separate

    @sourcepoint_write.setter
    def sourcepoint_write(self, source_write):
        check_type('sourcepoint write', source_write, bool)
        self._sourcepoint_write = source_write

    @sourcepoint_overwrite.setter
    def sourcepoint_overwrite(self, source_overwrite):
        check_type('sourcepoint overwrite', source_overwrite, bool)
        self._sourcepoint_overwrite = source_overwrite

    @confidence_intervals.setter
    def confidence_intervals(self, confidence_intervals):
        check_type('confidence interval', confidence_intervals, bool)
        self._confidence_intervals = confidence_intervals

    @cross_sections.setter
    def cross_sections(self, cross_sections):
        check_type('cross sections', cross_sections, basestring)
        self._cross_sections = cross_sections

    @energy_grid.setter
    def energy_grid(self, energy_grid):
        check_value('energy grid', energy_grid,
                    ['nuclide', 'logarithm', 'material-union'])
        self._energy_grid = energy_grid

    @ptables.setter
    def ptables(self, ptables):
        check_type('probability tables', ptables, bool)
        self._ptables = ptables

    @run_cmfd.setter
    def run_cmfd(self, run_cmfd):
        check_type('run_cmfd', run_cmfd, bool)
        self._run_cmfd = run_cmfd

    @seed.setter
    def seed(self, seed):
        check_type('random number generator seed', seed, Integral)
        check_greater_than('random number generator seed', seed, 0)
        self._seed = seed

    @survival_biasing.setter
    def survival_biasing(self, survival_biasing):
        check_type('survival biasing', survival_biasing, bool)
        self._survival_biasing = survival_biasing

    @weight.setter
    def weight(self, weight):
        check_type('weight cutoff', weight, Real)
        check_greater_than('weight cutoff', weight, 0.0)
        self._weight = weight

    @weight_avg.setter
    def weight_avg(self, weight_avg):
        check_type('average survival weight', weight_avg, Real)
        check_greater_than('average survival weight', weight_avg, 0.0)
        self._weight_avg = weight_avg

    @entropy_dimension.setter
    def entropy_dimension(self, dimension):
        check_type('entropy mesh dimension', dimension, Iterable, Integral)
        check_length('entropy mesh dimension', dimension, 3)
        self._entropy_dimension = dimension

    @entropy_lower_left.setter
    def entropy_lower_left(self, lower_left):
        check_type('entropy mesh lower left corner', lower_left,
                   Iterable, Real)
        check_length('entropy mesh lower left corner', lower_left, 3)
        self._entropy_lower_left = lower_left

    @entropy_upper_right.setter
    def entropy_upper_right(self, upper_right):
        check_type('entropy mesh upper right corner', upper_right,
                   Iterable, Real)
        check_length('entropy mesh upper right corner', upper_right, 3)
        self._entropy_upper_right = upper_right

    @trigger_active.setter
    def trigger_active(self, trigger_active):
        check_type('trigger active', trigger_active, bool)
        self._trigger_active = trigger_active

    @trigger_max_batches.setter
    def trigger_max_batches(self, trigger_max_batches):
        check_type('trigger maximum batches', trigger_max_batches, Integral)
        check_greater_than('trigger maximum batches', trigger_max_batches, 0)
        self._trigger_max_batches = trigger_max_batches

    @trigger_batch_interval.setter
    def trigger_batch_interval(self, trigger_batch_interval):
        check_type('trigger batch interval', trigger_batch_interval, Integral)
        check_greater_than('trigger batch interval', trigger_batch_interval, 0)
        self._trigger_batch_interval = trigger_batch_interval

    @no_reduce.setter
    def no_reduce(self, no_reduce):
        check_type('no reduction option', no_reduce, bool)
        self._no_reduce = no_reduce

    @threads.setter
    def threads(self, threads):
        check_type('number of threads', threads, Integral)
        check_greater_than('number of threads', threads, 0)
        self._threads = threads

    @trace.setter
    def trace(self, trace):
        check_type('trace', trace, Iterable, Integral)
        check_length('trace', trace, 3)
        check_greater_than('trace batch', trace[0], 0)
        check_greater_than('trace generation', trace[1], 0)
        check_greater_than('trace particle', trace[2], 0)
        self._trace = trace

    @track.setter
    def track(self, track):
        check_type('track', track, Iterable, Integral)
        if len(track) % 3 != 0:
            msg = 'Unable to set the track to "{0}" since its length is ' \
                  'not a multiple of 3'.format(track)
            raise ValueError(msg)
        for t in zip(track[::3], track[1::3], track[2::3]):
            check_greater_than('track batch', t[0], 0)
            check_greater_than('track generation', t[0], 0)
            check_greater_than('track particle', t[0], 0)
        self._track = track

    @ufs_dimension.setter
    def ufs_dimension(self, dimension):
        check_type('UFS mesh dimension', dimension, Iterable, Integral)
        check_length('UFS mesh dimension', dimension, 3)
        for dim in dimension:
            check_greater_than('UFS mesh dimension', dim, 1, True)
        self._ufs_dimension = dimension

    @ufs_lower_left.setter
    def ufs_lower_left(self, lower_left):
        check_type('UFS mesh lower left corner', lower_left, Iterable, Real)
        check_length('UFS mesh lower left corner', lower_left, 3)
        self._ufs_lower_left = lower_left

    @ufs_upper_right.setter
    def ufs_upper_right(self, upper_right):
        check_type('UFS mesh upper right corner', upper_right, Iterable, Real)
        check_length('UFS mesh upper right corner', upper_right, 3)
        self._ufs_upper_right = upper_right

    @dd_mesh_dimension.setter
    def dd_mesh_dimension(self, dimension):
        # TODO: remove this when domain decomposition is merged
        warnings.warn('This feature is not yet implemented in a release '
                      'version of openmc')

        check_type('DD mesh dimension', dimension, Iterable, Integral)
        check_length('DD mesh dimension', dimension, 3)

        self._dd_mesh_dimension = dimension

    @dd_mesh_lower_left.setter
    def dd_mesh_lower_left(self, lower_left):
        # TODO: remove this when domain decomposition is merged
        warnings.warn('This feature is not yet implemented in a release '
                      'version of openmc')

        check_type('DD mesh lower left corner', lower_left, Iterable, Real)
        check_length('DD mesh lower left corner', lower_left, 3)

        self._dd_mesh_lower_left = lower_left

    @dd_mesh_upper_right.setter
    def dd_mesh_upper_right(self, upper_right):
        # TODO: remove this when domain decomposition is merged
        warnings.warn('This feature is not yet implemented in a release '
                      'version of openmc')

        check_type('DD mesh upper right corner', upper_right, Iterable, Real)
        check_length('DD mesh upper right corner', upper_right, 3)

        self._dd_mesh_upper_right = upper_right

    @dd_nodemap.setter
    def dd_nodemap(self, nodemap):
        # TODO: remove this when domain decomposition is merged
        warnings.warn('This feature is not yet implemented in a release '
                      'version of openmc')

        check_type('DD nodemap', nodemap, Iterable)

        nodemap = np.array(nodemap).flatten()

        if self._dd_mesh_dimension is None:
            msg = 'Must set DD mesh dimension before setting the nodemap'
            raise ValueError(msg)
        else:
            len_nodemap = np.prod(self._dd_mesh_dimension)

        if len(nodemap) < len_nodemap or len(nodemap) > len_nodemap:
            msg = 'Unable to set DD nodemap with length "{0}" which ' \
                  'does not have the same dimensionality as the domain ' \
                  'mesh'.format(len(nodemap))
            raise ValueError(msg)

        self._dd_nodemap = nodemap

    @dd_allow_leakage.setter
    def dd_allow_leakage(self, allow):

        # TODO: remove this when domain decomposition is merged
        warnings.warn('This feature is not yet implemented in a release '
                      'version of openmc')

        check_type('DD allow leakage', allow, bool)

        self._dd_allow_leakage = allow

    @dd_count_interactions.setter
    def dd_count_interactions(self, interactions):

        # TODO: remove this when domain decomposition is merged
        warnings.warn('This feature is not yet implemented in a release '
                      'version of openmc')

        check_type('DD count interactions', interactions, bool)

        self._dd_count_interactions = interactions

    def _create_run_mode_subelement(self):

        if self.run_mode == 'eigenvalue':
            self._run_mode_subelement = \
                ET.SubElement(self._settings_file, "eigenvalue")
            self._create_particles_subelement()
            self._create_batches_subelement()
            self._create_inactive_subelement()
            self._create_generations_per_batch_subelement()
            self._create_keff_trigger_subelement()
        else:
            if self._run_mode_subelement is None:
                self._run_mode_subelement = \
                    ET.SubElement(self._settings_file, "fixed_source")
            self._create_particles_subelement()
            self._create_batches_subelement()

    def _create_batches_subelement(self):
        if self._batches is not None:
            element = ET.SubElement(self._run_mode_subelement, "batches")
            element.text = str(self._batches)

    def _create_generations_per_batch_subelement(self):
        if self._generations_per_batch is not None:
            element = ET.SubElement(self._run_mode_subelement,
                                    "generations_per_batch")
            element.text = str(self._generations_per_batch)

    def _create_inactive_subelement(self):
        if self._inactive is not None:
            element = ET.SubElement(self._run_mode_subelement, "inactive")
            element.text = str(self._inactive)

    def _create_particles_subelement(self):
        if self._particles is not None:
            element = ET.SubElement(self._run_mode_subelement, "particles")
            element.text = str(self._particles)

    def _create_keff_trigger_subelement(self):
        if self._keff_trigger is not None:
            element = ET.SubElement(self._run_mode_subelement, "keff_trigger")

            for key in self._keff_trigger:
                subelement = ET.SubElement(element, key)
                subelement.text = str(self._keff_trigger[key]).lower()

    def _create_source_subelement(self):
        if self.source is not None:
            for source in self.source:
                self._settings_file.append(source.to_xml())

    def _create_output_subelement(self):
        if self._output is not None:
            element = ET.SubElement(self._settings_file, "output")

            for key in self._output:
                subelement = ET.SubElement(element, key)
                subelement.text = str(self._output[key]).lower()

            self._create_output_path_subelement()

    def _create_output_path_subelement(self):
        if self._output_path is not None:
            element = ET.SubElement(self._settings_file, "output_path")
            element.text = self._output_path

    def _create_verbosity_subelement(self):
        if self._verbosity is not None:
            element = ET.SubElement(self._settings_file, "verbosity")
            element.text = str(self._verbosity)

    def _create_statepoint_subelement(self):
        # Batches subelement
        if self._statepoint_batches is not None:
            element = ET.SubElement(self._settings_file, "state_point")
            subelement = ET.SubElement(element, "batches")
            subelement.text = ' '.join(map(str, self._statepoint_batches))

        # Interval subelement
        elif self._statepoint_interval is not None:
            element = ET.SubElement(self._settings_file, "state_point")
            subelement = ET.SubElement(element, "interval")
            subelement.text = str(self._statepoint_interval)

    def _create_sourcepoint_subelement(self):
        # Batches subelement
        if self._sourcepoint_batches is not None:
            element = ET.SubElement(self._settings_file, "source_point")
            subelement = ET.SubElement(element, "batches")
            subelement.text = ' '.join(map(str, self._sourcepoint_batches))

        # Interval subelement
        elif self._sourcepoint_interval is not None:
            element = ET.SubElement(self._settings_file, "source_point")
            subelement = ET.SubElement(element, "interval")
            subelement.text = str(self._sourcepoint_interval)

        # Separate subelement
        if self._sourcepoint_separate is not None:
            element = ET.SubElement(self._settings_file, "source_point")
            subelement = ET.SubElement(element, "separate")
            subelement.text = str(self._sourcepoint_separate).lower()

        # Write subelement
        if self._sourcepoint_write is not None:
            element = ET.SubElement(self._settings_file, "source_point")
            subelement = ET.SubElement(element, "write")
            subelement.text = str(self._sourcepoint_write).lower()

        # Overwrite latest subelement
        if self._sourcepoint_overwrite is not None:
            element = ET.SubElement(self._settings_file, "source_point")
            subelement = ET.SubElement(element, "overwrite_latest")
            subelement.text = str(self._sourcepoint_overwrite).lower()

    def _create_confidence_intervals(self):
        if self._confidence_intervals is not None:
            element = ET.SubElement(self._settings_file, "confidence_intervals")
            element.text = str(self._confidence_intervals).lower()

    def _create_cross_sections_subelement(self):
        if self._cross_sections is not None:
            element = ET.SubElement(self._settings_file, "cross_sections")
            element.text = str(self._cross_sections)

    def _create_energy_grid_subelement(self):
        if self._energy_grid is not None:
            element = ET.SubElement(self._settings_file, "energy_grid")
            element.text = str(self._energy_grid)

    def _create_ptables_subelement(self):
        if self._ptables is not None:
            element = ET.SubElement(self._settings_file, "ptables")
            element.text = str(self._ptables).lower()

    def _create_run_cmfd_subelement(self):
        if self._run_cmfd is not None:
            element = ET.SubElement(self._settings_file, "run_cmfd")
            element.text = str(self._run_cmfd).lower()

    def _create_seed_subelement(self):
        if self._seed is not None:
            element = ET.SubElement(self._settings_file, "seed")
            element.text = str(self._seed)

    def _create_survival_biasing_subelement(self):
        if self._survival_biasing is not None:
            element = ET.SubElement(self._settings_file, "survival_biasing")
            element.text = str(self._survival_biasing).lower()

    def _create_cutoff_subelement(self):
        if self._weight is not None:
            element = ET.SubElement(self._settings_file, "cutoff")

            subelement = ET.SubElement(element, "weight")
            subelement.text = str(self._weight)

            subelement = ET.SubElement(element, "weight_avg")
            subelement.text = str(self._weight_avg)

    def _create_entropy_subelement(self):
        if self._entropy_lower_left is not None and \
           self._entropy_upper_right is not None:

            element = ET.SubElement(self._settings_file, "entropy")

            subelement = ET.SubElement(element, "dimension")
            subelement.text = ' '.join(map(str, self._entropy_dimension))

            subelement = ET.SubElement(element, "lower_left")
            subelement.text = ' '.join(map(str, self._entropy_lower_left))

            subelement = ET.SubElement(element, "upper_right")
            subelement.text = ' '.join(map(str, self._entropy_upper_right))

    def _create_trigger_subelement(self):
        self._create_trigger_active_subelement()
        self._create_trigger_max_batches_subelement()
        self._create_trigger_batch_interval_subelement()

    def _create_trigger_active_subelement(self):
        if self._trigger_active is not None:
            if self._trigger_subelement is None:
                self._trigger_subelement = ET.SubElement(self._settings_file,
                                                      "trigger")

            element = ET.SubElement(self._trigger_subelement, "active")
            element.text = str(self._trigger_active).lower()

    def _create_trigger_max_batches_subelement(self):
        if self._trigger_max_batches is not None:
            if self._trigger_subelement is None:
                self._trigger_subelement = ET.SubElement(self._settings_file,
                                                      "trigger")

            element = ET.SubElement(self._trigger_subelement, "max_batches")
            element.text = str(self._trigger_max_batches)

    def _create_trigger_batch_interval_subelement(self):
        if self._trigger_batch_interval is not None:
            if self._trigger_subelement is None:
                self._trigger_subelement = ET.SubElement(self._settings_file,
                                                      "trigger")

            element = ET.SubElement(self._trigger_subelement, "batch_interval")
            element.text = str(self._trigger_batch_interval)

    def _create_no_reduce_subelement(self):
        if self._no_reduce is not None:
            element = ET.SubElement(self._settings_file, "no_reduce")
            element.text = str(self._no_reduce).lower()

    def _create_threads_subelement(self):
        if self._threads is not None:
            element = ET.SubElement(self._settings_file, "threads")
            element.text = str(self._threads)

    def _create_trace_subelement(self):
        if self._trace is not None:
            element = ET.SubElement(self._settings_file, "trace")
            element.text = ' '.join(map(str, self._trace))

    def _create_track_subelement(self):
        if self._track is not None:
            element = ET.SubElement(self._settings_file, "track")
            element.text = ' '.join(map(str, self._track))

    def _create_ufs_subelement(self):
        if self._ufs_lower_left is not None and \
           self._ufs_upper_right is not None:

            element = ET.SubElement(self._settings_file, "uniform_fs")

            subelement = ET.SubElement(element, "dimension")
            subelement.text = str(self._ufs_dimension)

            subelement = ET.SubElement(element, "lower_left")
            subelement.text = ' '.join(map(str, self._ufs_lower_left))

            subelement = ET.SubElement(element, "upper_right")
            subelement.text = ' '.join(map(str, self._ufs_upper_right))

    def _create_dd_subelement(self):
        if self._dd_mesh_lower_left is not None and \
           self._dd_mesh_upper_right is not None and \
           self._dd_mesh_dimension is not None:

            element = ET.SubElement(self._settings_file, "domain_decomposition")

            subelement = ET.SubElement(element, "mesh")
            subsubelement = ET.SubElement(subelement, "dimension")
            subsubelement.text = ' '.join(map(str, self._dd_mesh_dimension))

            subsubelement = ET.SubElement(subelement, "lower_left")
            subsubelement.text = ' '.join(map(str, self._dd_mesh_lower_left))

            subsubelement = ET.SubElement(subelement, "upper_right")
            subsubelement.text = ' '.join(map(str, self._dd_mesh_upper_right))

            if self._dd_nodemap is not None:
                subelement = ET.SubElement(element, "nodemap")
                subelement.text = ' '.join(map(str, self._dd_nodemap))

            subelement = ET.SubElement(element, "allow_leakage")
            subelement.text = str(self._dd_allow_leakage).lower()

            subelement = ET.SubElement(element, "count_interactions")
            subelement.text = str(self._dd_count_interactions).lower()

    def export_to_xml(self):
        """Create a settings.xml file that can be used for a simulation.

        """

        # Reset xml element tree
        self._settings_file.clear()
        self._source_subelement = None
        self._trigger_subelement = None
        self._run_mode_subelement = None
        self._source_element = None

        self._create_run_mode_subelement()
        self._create_source_subelement()
        self._create_output_subelement()
        self._create_statepoint_subelement()
        self._create_sourcepoint_subelement()
        self._create_confidence_intervals()
        self._create_cross_sections_subelement()
        self._create_energy_grid_subelement()
        self._create_ptables_subelement()
        self._create_run_cmfd_subelement()
        self._create_seed_subelement()
        self._create_survival_biasing_subelement()
        self._create_cutoff_subelement()
        self._create_entropy_subelement()
        self._create_trigger_subelement()
        self._create_no_reduce_subelement()
        self._create_threads_subelement()
        self._create_verbosity_subelement()
        self._create_trace_subelement()
        self._create_track_subelement()
        self._create_ufs_subelement()
        self._create_dd_subelement()

        # Clean the indentation in the file to be user-readable
        clean_xml_indentation(self._settings_file)

        # Write the XML Tree to the settings.xml file
        tree = ET.ElementTree(self._settings_file)
        tree.write("settings.xml", xml_declaration=True,
                             encoding='utf-8', method="xml")


text:
<a href='<?=$this->url->create('users/id/' . $user->acronym)?>'><?=$user->acronym?>

<img style='float:left;margin-right:5px; border:solid 3px #222A38;'src="<?php echo 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($user->email))) . '.jpg?s=80';?>">
Email: <?=$user->email?>
Namn: <?=$user->firstname?> <?=$user->lastname?>
Skapad: <?=$user->created?>

<?php if(isset($logout)) {
	$loggaut = $this->url->create('users/logout/');
	$edit = $this->url->create('users/update/' . $user->acronym);
	$pass = $this->url->create('users/changePassword/' . $user->acronym);
	echo "
Logga ut -  Redigera info -  Ändra Lösenord";
}?>

Frågor skapade av <?=$user->acronym?>

<?php if (is_array($questions) && !empty($questions)) : ?>

	<?php $color=0; ?>
<?php foreach ($questions as $question) : ?>

<?php $url = $this->url->create('questions/show') .'/' . $question->id?>

<a href=<?php echo $url?>><div class="question"  style='overflow:auto; padding:10px; padding-bottom:10px;<?php if (($color % 2) == 0) {echo "background-color: lightgrey;";} else {echo "background-color: white;";} ?>position:relative;'>
<?php $color++; ?>

<?=$question->title?>

	 <?=$question->created?>

<?php endforeach;?>

<?php endif; ?>

Frågor besvarade av <?=$user->acronym?>

<?php if (is_array($answers) && !empty($answers)) : ?>

	<?php $color=0; ?>
<?php foreach ($answers as $question) : ?>

<?php $url = $this->url->create('questions/show') .'/' . $question[0]->id?>

<a href=<?php echo $url?>><div class="question"  style='overflow:auto; padding:10px; padding-bottom:10px;<?php if (($color % 2) == 0) {echo "background-color: lightgrey;";} else {echo "background-color: white;";} ?>position:relative;'>
<?php $color++; ?>

<?=$question[0]->title?>

	 <?=$question[0]->created?>

<?php endforeach;?>

<?php endif; ?>

redpajama_set_name: RedPajamaGithub


text:
package views.forms;

import models.User;
import play.data.validation.Constraints;
import play.data.validation.ValidationError;

import java.util.ArrayList;
import java.util.List;

public class RegisterForm {
    public String name = "";
    public String email = "";
    public String abn = "";
    public String password = "";
    public String passwordConfirmation = "";

    public RegisterForm() {
        // Required for form instantiation
    }

    public RegisterForm(String name, String email, String abn, String password, String passwordConfirmation) {
        this.name = name;
        this.email = email;
        this.password = password;
        this.passwordConfirmation = passwordConfirmation;
    }

    // If errors exist return as a list, otherwise return null
    public List<ValidationError> validate() {

        List<ValidationError> errors = new ArrayList<>();

        // Validate name field
        if(name == null || name.length() == 0) {
            errors.add(new ValidationError("name", "No name was given."));
        }

        // Validate email
        if(!(new Constraints.EmailValidator()).isValid(email)) {
            errors.add(new ValidationError("email", "Invalid email address."));
        }

        // Check that email has not been registered already
        if(User.findByEmail(email) != null) {
            errors.add(new ValidationError("email", "Email address is already registered."));
        }

        // Validate password
        if(password.length() < 8) {
            errors.add(new ValidationError("password", "Password must be at least 8 characters long."));
        }

        if(!password.equals(passwordConfirmation)) {
            errors.add(new ValidationError("password", "Passwords do not match."));
        }

        if(errors.size() > 0) {
            return errors;
        }

        return null;
    }

}


text:
package bytelang.compiler.annotations;

import bytelang.CompilationErrorException;
import bytelang.classes.Attribute;
import bytelang.classes.FieldInfo;
import bytelang.compiler.ConstantPoolBuilder;
import bytelang.compiler.annotations.general.AnnotationType;
import bytelang.compiler.annotations.general.BasicAnnotation;
import bytelang.helpers.Factory;
import bytelang.parser.container.elements.ElementAnnotation;
import bytelang.parser.container.values.Value;
import bytelang.parser.container.values.ValueInteger;
import bytelang.parser.container.values.ValueReference;
import bytelang.parser.container.values.ValueString;
import bytelang.parser.container.values.ValueType;

public class AnnotationField extends BasicAnnotation implements Factory<AnnotationField> {
	private int   accessFlags;
	private Value name;
	private Value descriptor;

	public AnnotationField() {
		this.addMandatoryParam("accessFlags", new ValueType[]{ValueType.INTEGER});
		this.addMandatoryParam("name", new ValueType[]{ValueType.INTEGER, ValueType.REFERENCE, ValueType.STRING});
		this.addMandatoryParam("descriptor", new ValueType[]{ValueType.INTEGER, ValueType.REFERENCE, ValueType.STRING});
	}

	@Override
	public void fromElementAnnotation(ElementAnnotation elementAnnotation) {
		this.validate(elementAnnotation);

		this.accessFlags = (int) ((ValueInteger) elementAnnotation.getParameters().get("accessFlags")).getValue();
		this.name        = elementAnnotation.getParameters().get("name");
		this.descriptor  = elementAnnotation.getParameters().get("descriptor");
	}

	@Override
	public AnnotationType getType() {
		return AnnotationType.FIELD;
	}

	@Override
	public AnnotationField newInstance() {
		return new AnnotationField();
	}

	public int getAccessFlags() {
		return accessFlags;
	}

	public Value getName() {
		return name;
	}

	public Value getDescriptor() {
		return descriptor;
	}

	public FieldInfo toFieldInfo(ConstantPoolBuilder constantPoolBuilder) {
		int accessFlags     = this.accessFlags;
		int nameIndex       = 0;
		int descriptorIndex = 0;

		if (this.name.getType() == ValueType.INTEGER) {
			nameIndex = (int) ((ValueInteger) name).getValue();
		} else if (this.name.getType() == ValueType.STRING) {
			nameIndex = constantPoolBuilder.addItemUtf8(((ValueString) name).getString());
		} else if (this.name.getType() == ValueType.REFERENCE) {
			nameIndex = constantPoolBuilder.getCPItemIndexById(((ValueReference) name).getValue());
		} else {
			throw new CompilationErrorException();
		}

		if (this.descriptor.getType() == ValueType.INTEGER) {
			descriptorIndex = (int) ((ValueInteger) descriptor).getValue();
		} else if (this.descriptor.getType() == ValueType.STRING) {
			descriptorIndex = constantPoolBuilder.addItemUtf8(((ValueString) descriptor).getString());
		} else if (this.descriptor.getType() == ValueType.REFERENCE) {
			descriptorIndex = constantPoolBuilder.getCPItemIndexById(((ValueReference) descriptor).getValue());
		} else {
			throw new CompilationErrorException();
		}

		return new FieldInfo(accessFlags, nameIndex, descriptorIndex, 0, new Attribute[0]);
	}
}


text:
require 'rails_helper'

RSpec.describe Tag, type: :model do
  subject { FactoryGirl.build :tag, name: 'CrazyTag' }

  describe 'validations' do
    it { is_expected.to be_valid }
    it { is_expected.to validate_presence_of(:name) }

    it { is_expected.to validate_uniqueness_of(:name) }
  end

  describe 'methods' do
    it { expect(subject.to_s).to eq(subject.name)}
  end
end


text:
<WebAccess version="11.0">
  <plugin name="TFS Color Coding By Activity Type" vendor="Springbrook Software, Inc." moreinfo="https://github.com/sprbrk/Ssi.TfsWebAccess" version="0.1.0" >
    <modules>
      <module namespace="ColorCodingEnhancement" loadAfter="TFS.Agile.TaskBoard"/>
      <module namespace="ColorCodingEnhancement" loadAfter="TFS.Agile.Boards.Controls"/>
    </modules>
  </plugin>
</WebAccess>


text:
/*	$NetBSD: handler.h,v 1.9.6.1 2008/01/11 14:12:01 vanhu Exp $	*/

/* Id: handler.h,v 1.19 2006/02/25 08:25:12 manubsd Exp */

/*
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _HANDLER_H
#define _HANDLER_H

#include <sys/queue.h>
#include <openssl/rsa.h>

#include <sys/time.h>

#include "isakmp_var.h"
#include "oakley.h"

/* Phase 1 handler */
/*
 * main mode:
 *      initiator               responder
 *  0   (---)                   (---)
 *  1   start                   start (1st msg received)
 *  2   (---)                   1st valid msg received
 *  3   1st msg sent	        1st msg sent
 *  4   1st valid msg received  2st valid msg received
 *  5   2nd msg sent            2nd msg sent
 *  6   2nd valid msg received  3rd valid msg received
 *  7   3rd msg sent            3rd msg sent
 *  8   3rd valid msg received  (---)
 *  9   SA established          SA established
 *
 * aggressive mode:
 *      initiator               responder
 *  0   (---)                   (---)
 *  1   start                   start (1st msg received)
 *  2   (---)                   1st valid msg received
 *  3   1st msg sent	        1st msg sent
 *  4   1st valid msg received  2st valid msg received
 *  5   (---)                   (---)
 *  6   (---)                   (---)
 *  7   (---)                   (---)
 *  8   (---)                   (---)
 *  9   SA established          SA established
 *
 * base mode:
 *      initiator               responder
 *  0   (---)                   (---)
 *  1   start                   start (1st msg received)
 *  2   (---)                   1st valid msg received
 *  3   1st msg sent	        1st msg sent
 *  4   1st valid msg received  2st valid msg received
 *  5   2nd msg sent            (---)
 *  6   (---)                   (---)
 *  7   (---)                   (---)
 *  8   (---)                   (---)
 *  9   SA established          SA established
 */
#define PHASE1ST_SPAWN			0
#define PHASE1ST_START			1
#define PHASE1ST_MSG1RECEIVED		2
#define PHASE1ST_MSG1SENT		3
#define PHASE1ST_MSG2RECEIVED		4
#define PHASE1ST_MSG2SENT		5
#define PHASE1ST_MSG3RECEIVED		6
#define PHASE1ST_MSG3SENT		7
#define PHASE1ST_MSG4RECEIVED		8
#define PHASE1ST_ESTABLISHED		9
#define PHASE1ST_EXPIRED		10
#define PHASE1ST_MAX			11

/* About address semantics in each case.
 *			initiator(addr=I)	responder(addr=R)
 *			src	dst		src	dst
 *			(local)	(remote)	(local)	(remote)
 * phase 1 handler	I	R		R	I
 * phase 2 handler	I	R		R	I
 * getspi msg		R	I		I	R
 * acquire msg		I	R
 * ID payload		I	R		I	R
 */
#ifdef ENABLE_HYBRID
struct isakmp_cfg_state;
#endif
struct ph1handle {
	isakmp_index index;

	int status;			/* status of this SA */
	int side;			/* INITIATOR or RESPONDER */

	struct sockaddr *remote;	/* remote address to negosiate ph1 */
	struct sockaddr *local;		/* local address to negosiate ph1 */
			/* XXX copy from rmconf due to anonymous configuration.
			 * If anonymous will be forbidden, we do delete them. */

	struct remoteconf *rmconf;	/* pointer to remote configuration */

	struct isakmpsa *approval;	/* pointer to SA(s) approved. */
	vchar_t *authstr;		/* place holder of string for auth. */
					/* for example pre-shared key */

	u_int8_t version;		/* ISAKMP version */
	u_int8_t etype;			/* Exchange type actually for use */
	u_int8_t flags;			/* Flags */
	u_int32_t msgid;		/* message id */

#ifdef ENABLE_NATT
	struct ph1natt_options *natt_options;	/* Selected NAT-T IKE version */
	u_int32_t natt_flags;		/* NAT-T related flags */
#endif
#ifdef ENABLE_FRAG
	int frag;			/* IKE phase 1 fragmentation */
	struct isakmp_frag_item *frag_chain;	/* Received fragments */
#endif

	struct sched *sce;		/* schedule for expire */

	struct sched *scr;		/* schedule for resend */
	int retry_counter;		/* for resend. */
	vchar_t *sendbuf;		/* buffer for re-sending */

	vchar_t *dhpriv;		/* DH; private value */
	vchar_t *dhpub;			/* DH; public value */
	vchar_t *dhpub_p;		/* DH; partner's public value */
	vchar_t *dhgxy;			/* DH; shared secret */
	vchar_t *nonce;			/* nonce value */
	vchar_t *nonce_p;		/* partner's nonce value */
	vchar_t *skeyid;		/* SKEYID */
	vchar_t *skeyid_d;		/* SKEYID_d */
	vchar_t *skeyid_a;		/* SKEYID_a, i.e. hash */
	vchar_t *skeyid_e;		/* SKEYID_e, i.e. encryption */
	vchar_t *key;			/* cipher key */
	vchar_t *hash;			/* HASH minus general header */
	vchar_t *sig;			/* SIG minus general header */
	vchar_t *sig_p;			/* peer's SIG minus general header */
	cert_t *cert;			/* CERT minus general header */
	cert_t *cert_p;			/* peer's CERT minus general header */
	cert_t *crl_p;			/* peer's CRL minus general header */
	cert_t *cr_p;			/* peer's CR not including general */
	RSA *rsa;			/* my RSA key */
	RSA *rsa_p;			/* peer's RSA key */
	struct genlist *rsa_candidates;	/* possible candidates for peer's RSA key */
	vchar_t *id;			/* ID minus gen header */
	vchar_t *id_p;			/* partner's ID minus general header */
					/* i.e. struct ipsecdoi_id_b*. */
	struct isakmp_ivm *ivm;		/* IVs */

	vchar_t *sa;			/* whole SA payload to send/to be sent*/
					/* to calculate HASH */
					/* NOT INCLUDING general header. */

	vchar_t *sa_ret;		/* SA payload to reply/to be replyed */
					/* NOT INCLUDING general header. */
					/* NOTE: Should be release after use. */

#ifdef HAVE_GSSAPI
	void *gssapi_state;		/* GSS-API specific state. */
					/* Allocated when needed */
	vchar_t *gi_i;			/* optional initiator GSS id */
	vchar_t *gi_r;			/* optional responder GSS id */
#endif

	struct isakmp_pl_hash *pl_hash;	/* pointer to hash payload */

	time_t created;			/* timestamp for establish */
#ifdef ENABLE_STATS
	struct timeval start;
	struct timeval end;
#endif

#ifdef ENABLE_DPD
	int		dpd_support;	/* Does remote supports DPD ? */
	time_t		dpd_lastack;	/* Last ack received */
	u_int16_t	dpd_seq;		/* DPD seq number to receive */
	u_int8_t	dpd_fails;		/* number of failures */
	struct sched	*dpd_r_u;
#endif

	u_int32_t msgid2;		/* msgid counter for Phase 2 */
	int ph2cnt;	/* the number which is negotiated by this phase 1 */
	LIST_HEAD(_ph2ofph1_, ph2handle) ph2tree;

	LIST_ENTRY(ph1handle) chain;
#ifdef ENABLE_HYBRID
	struct isakmp_cfg_state *mode_cfg;	/* ISAKMP mode config state */
#endif

};

/* Phase 2 handler */
/* allocated per a SA or SA bundles of a pair of peer's IP addresses. */
/*
 *      initiator               responder
 *  0   (---)                   (---)
 *  1   start                   start (1st msg received)
 *  2   acquire msg get         1st valid msg received
 *  3   getspi request sent     getspi request sent
 *  4   getspi done             getspi done
 *  5   1st msg sent            1st msg sent
 *  6   1st valid msg received  2nd valid msg received
 *  7   (commit bit)            (commit bit)
 *  8   SAs added               SAs added
 *  9   SAs established         SAs established
 * 10   SAs expired             SAs expired
 */
#define PHASE2ST_SPAWN		0
#define PHASE2ST_START		1
#define PHASE2ST_STATUS2	2
#define PHASE2ST_GETSPISENT	3
#define PHASE2ST_GETSPIDONE	4
#define PHASE2ST_MSG1SENT	5
#define PHASE2ST_STATUS6	6
#define PHASE2ST_COMMIT		7
#define PHASE2ST_ADDSA		8
#define PHASE2ST_ESTABLISHED	9
#define PHASE2ST_EXPIRED	10
#define PHASE2ST_MAX		11

struct ph2handle {
	struct sockaddr *src;		/* my address of SA. */
	struct sockaddr *dst;		/* peer's address of SA. */

		/*
		 * copy ip address from ID payloads when ID type is ip address.
		 * In other case, they must be null.
		 */
	struct sockaddr *src_id;
	struct sockaddr *dst_id;

	u_int32_t spid;			/* policy id by kernel */

	int status;			/* ipsec sa status */
	u_int8_t side;			/* INITIATOR or RESPONDER */

	struct sched *sce;		/* schedule for expire */
	struct sched *scr;		/* schedule for resend */
	int retry_counter;		/* for resend. */
	vchar_t *sendbuf;		/* buffer for re-sending */
	vchar_t *msg1;			/* buffer for re-sending */
				/* used for responder's first message */

	int retry_checkph1;		/* counter to wait phase 1 finished. */
					/* NOTE: actually it's timer. */

	u_int32_t seq;			/* sequence number used by PF_KEY */
			/*
			 * NOTE: In responder side, we can't identify each SAs
			 * with same destination address for example, when
			 * socket based SA is required.  So we set a identifier
			 * number to "seq", and sent kernel by pfkey.
			 */
	u_int8_t satype;		/* satype in PF_KEY */
			/*
			 * saved satype in the original PF_KEY request from
			 * the kernel in order to reply a error.
			 */

	u_int8_t flags;			/* Flags for phase 2 */
	u_int32_t msgid;		/* msgid for phase 2 */

	struct sainfo *sainfo;		/* place holder of sainfo */
	struct saprop *proposal;	/* SA(s) proposal. */
	struct saprop *approval;	/* SA(s) approved. */
	caddr_t spidx_gen;		/* policy from peer's proposal */

	struct dhgroup *pfsgrp;		/* DH; prime number */
	vchar_t *dhpriv;		/* DH; private value */
	vchar_t *dhpub;			/* DH; public value */
	vchar_t *dhpub_p;		/* DH; partner's public value */
	vchar_t *dhgxy;			/* DH; shared secret */
	vchar_t *id;			/* ID minus gen header */
	vchar_t *id_p;			/* peer's ID minus general header */
	vchar_t *nonce;			/* nonce value in phase 2 */
	vchar_t *nonce_p;		/* partner's nonce value in phase 2 */

	vchar_t *sa;			/* whole SA payload to send/to be sent*/
					/* to calculate HASH */
					/* NOT INCLUDING general header. */

	vchar_t *sa_ret;		/* SA payload to reply/to be replyed */
					/* NOT INCLUDING general header. */
					/* NOTE: Should be release after use. */

	struct isakmp_ivm *ivm;		/* IVs */

	int generated_spidx;	/* mark handlers whith generated policy */

#ifdef ENABLE_STATS
	struct timeval start;
	struct timeval end;
#endif
	struct ph1handle *ph1;	/* back pointer to isakmp status */

	LIST_ENTRY(ph2handle) chain;
	LIST_ENTRY(ph2handle) ph1bind;	/* chain to ph1handle */
};

/*
 * for handling initial contact.
 */
struct contacted {
	struct sockaddr *remote;	/* remote address to negosiate ph1 */
	LIST_ENTRY(contacted) chain;
};

/*
 * for checking a packet retransmited.
 */
struct recvdpkt {
	struct sockaddr *remote;	/* the remote address */
	struct sockaddr *local;		/* the local address */
	vchar_t *hash;			/* hash of the received packet */
	vchar_t *sendbuf;		/* buffer for the response */
	int retry_counter;		/* how many times to send */
	time_t time_send;		/* timestamp to send a packet */
	time_t created;			/* timestamp to create a queue */

	struct sched *scr;		/* schedule for resend, may not used */

	LIST_ENTRY(recvdpkt) chain;
};

/* for parsing ISAKMP header. */
struct isakmp_parse_t {
	u_char type;		/* payload type of mine */
	int len;		/* ntohs(ptr->len) */
	struct isakmp_gen *ptr;
};

/*
 * for IV management.
 *
 * - normal case
 * initiator                                     responder
 * -------------------------                     --------------------------
 * initialize iv(A), ive(A).                     initialize iv(A), ive(A).
 * encode by ive(A).
 * save to iv(B).            ---[packet(B)]-->   save to ive(B).
 *                                               decode by iv(A).
 *                                               packet consistency.
 *                                               sync iv(B) with ive(B).
 *                                               check auth, integrity.
 *                                               encode by ive(B).
 * save to ive(C).          <--[packet(C)]---    save to iv(C).
 * decoded by iv(B).
 *      :
 *
 * - In the case that a error is found while cipher processing,
 * initiator                                     responder
 * -------------------------                     --------------------------
 * initialize iv(A), ive(A).                     initialize iv(A), ive(A).
 * encode by ive(A).
 * save to iv(B).            ---[packet(B)]-->   save to ive(B).
 *                                               decode by iv(A).
 *                                               packet consistency.
 *                                               sync iv(B) with ive(B).
 *                                               check auth, integrity.
 *                                               error found.
 *                                               create notify.
 *                                               get ive2(X) from iv(B).
 *                                               encode by ive2(X).
 * get iv2(X) from iv(B).   <--[packet(Y)]---    save to iv2(Y).
 * save to ive2(Y).
 * decoded by iv2(X).
 *      :
 *
 * The reason why the responder synchronizes iv with ive after checking the
 * packet consistency is that it is required to leave the IV for decoding
 * packet.  Because there is a potential of error while checking the packet
 * consistency.  Also the reason why that is before authentication and
 * integirty check is that the IV for informational exchange has to be made
 * by the IV which is after packet decoded and checking the packet consistency.
 * Otherwise IV mismatched happens between the intitiator and the responder.
 */
struct isakmp_ivm {
	vchar_t *iv;	/* for decoding packet */
			/* if phase 1, it's for computing phase2 iv */
	vchar_t *ive;	/* for encoding packet */
};

/* for dumping */
struct ph1dump {
	isakmp_index index;
	int status;
	int side;
	struct sockaddr_storage remote;
	struct sockaddr_storage local;
	u_int8_t version;
	u_int8_t etype;
	time_t created;
	int ph2cnt;
};

struct sockaddr;
struct ph1handle;
struct ph2handle;
struct policyindex;

extern struct ph1handle *getph1byindex __P((isakmp_index *));
extern struct ph1handle *getph1byindex0 __P((isakmp_index *));
extern struct ph1handle *getph1byaddr __P((struct sockaddr *,
										   struct sockaddr *, int));
extern struct ph1handle *getph1byaddrwop __P((struct sockaddr *,
	struct sockaddr *));
extern struct ph1handle *getph1bydstaddrwop __P((struct sockaddr *));
#ifdef ENABLE_HYBRID
struct ph1handle *getph1bylogin __P((char *));
int purgeph1bylogin __P((char *));
#endif
extern vchar_t *dumpph1 __P((void));
extern struct ph1handle *newph1 __P((void));
extern void delph1 __P((struct ph1handle *));
extern int insph1 __P((struct ph1handle *));
extern void remph1 __P((struct ph1handle *));
extern void flushph1 __P((void));
extern void initph1tree __P((void));

extern struct ph2handle *getph2byspidx __P((struct policyindex *));
extern struct ph2handle *getph2byspid __P((u_int32_t));
extern struct ph2handle *getph2byseq __P((u_int32_t));
extern struct ph2handle *getph2bysaddr __P((struct sockaddr *,
	struct sockaddr *));
extern struct ph2handle *getph2bymsgid __P((struct ph1handle *, u_int32_t));
extern struct ph2handle *getph2byid __P((struct sockaddr *,
	struct sockaddr *, u_int32_t));
extern struct ph2handle *getph2bysaidx __P((struct sockaddr *,
	struct sockaddr *, u_int, u_int32_t));
extern struct ph2handle *newph2 __P((void));
extern void initph2 __P((struct ph2handle *));
extern void delph2 __P((struct ph2handle *));
extern int insph2 __P((struct ph2handle *));
extern void remph2 __P((struct ph2handle *));
extern void flushph2 __P((void));
extern void deleteallph2 __P((struct sockaddr *, struct sockaddr *, u_int));
extern void initph2tree __P((void));

extern void bindph12 __P((struct ph1handle *, struct ph2handle *));
extern void unbindph12 __P((struct ph2handle *));

extern struct contacted *getcontacted __P((struct sockaddr *));
extern int inscontacted __P((struct sockaddr *));
extern void initctdtree __P((void));

extern int check_recvdpkt __P((struct sockaddr *,
	struct sockaddr *, vchar_t *));
extern int add_recvdpkt __P((struct sockaddr *, struct sockaddr *,
	vchar_t *, vchar_t *));
extern void init_recvdpkt __P((void));

#ifdef ENABLE_HYBRID
extern int exclude_cfg_addr __P((const struct sockaddr *));
#endif

extern int revalidate_ph12(void);

#endif /* _HANDLER_H */


text:
describe('FizzBuzz', function(){
  var fizzBuzz;

  beforeEach(function() {
    fizzBuzz = new FizzBuzz();
  });

  it('should know that a number is divisible by a divisor', function() {
    expect(fizzBuzz.divisibleBy(3, 3)).toBe(true);
  });

  it('should know that a number is divisible by a divisor', function() {
    expect(fizzBuzz.divisibleBy(3, 1)).toBe(false);
  });

  it('should return Fizz for 3', function() {
    expect(fizzBuzz.play(3)).toBe('Fizz');
  });

  it('should return Buzz for 5', function() {
    expect(fizzBuzz.play(5)).toBe('Buzz');
  });

  it('should return FizzBuzz for 15', function() {
    expect(fizzBuzz.play(15)).toBe('FizzBuzz');
  });

  it('should return 1 for 1', function() {
    expect(fizzBuzz.play(1)).toBe(1);
  });

  it('should return 99 for 99', function() {
    expect(fizzBuzz.play(1)).toBe(1);
  });

});


text:
author: Donna Dubinsky
brief: As we look forward to a bright 2016 here at Numenta, I thought I'd pause and review the highlights of 2015.  From advancing our research to expanding our community, we had many accomplishments that laid the groundwork for significant
date: 2016/01/07
image: ../../../../images/numenta.png
org: CEO
tags: numenta 2015 review partners cortical.io avik grok nlp semantic folding nab numenta anomaly benchmark
title: Year in Review for Numenta 2015
type: post
---

### Numenta Newsletter — January 07, 2016

As we look forward to a bright 2016 here at Numenta, I thought I'd pause and
review the highlights of 2015.  From advancing our research to expanding our
community, we had many accomplishments that laid the groundwork for significant
progress going forward. I'd like to share four highlights:

### Highlight #1

In 2015, we signed up two key strategic partners:
[Cortical.io](http://www.cortical.io/) and
[Avik Partners](http://www.grokstream.com/#home).  In May, we
[announced our Cortical.io partnership](/press/2015/05/14/numenta-and-cortical-io-form-strategic-partnership/),
which focuses on advancing natural language processing (NLP).  We believe that
the combination of our HTM technology and Cortical's Semantic Folding
technology provides new possibilities to understanding text.  Co-founder
Francisco Webber has been a long-time supporter of Numenta. We look forward to
continued collaboration with him and his team.

In August, we
[announced our partnership with Avik Partners](/press/2015/08/19/numenta-announces-licensing-of-grok-for-it-to-avik-partners/),
an IT services and advanced analytics startup to whom we transferred our Grok
application.  In a short time, Avik CEO Casey Kindiger launched a
[commercial application](http://www.grokstream.com) to use Grok to
monitor IT infrastructure. This partnership is a great example of our
"technology transfer" business model where other companies complete and
commercialize our example applications.

We can't wait to see exciting results from Cortical.io and Avik Partners, and
I hope to share news of additional partnerships with you in the coming year.

### Highlight #2

We also released the
[Numenta Anomaly Benchmark (NAB)](/numenta-anomaly-benchmark/),
which was two years in the making.  We believe NAB offers the definitive way
to assess the performance of algorithms for detecting anomalies in streaming
data.  We presented NAB v1.0 at two conferences and continue to get positive
feedback supporting the need for this benchmark. The launch was just the
beginning for NAB. We are actively working to collect additional data sources
and publish additional algorithm results to incorporate in the next version.
Stay tuned for details on a NAB competition we are running this year in
conjunction with the
[IEEE World Congress on Computational Intelligence](http://www.wcci2016.org/programs.php?id=home).

### Highlight #3

This year also was a great year for our growing [NuPIC](http://numenta.org/)
community, which received a Top 3 ranking in a KDNuggets report on the
[Top 20 Python Machine Learning Open Source Projects](http://www.kdnuggets.com/2015/06/top-20-python-machine-learning-open-source-projects.html).
We increased the number of NuPIC followers by 750, put more of our technology
into open source, and had two hackathons that demonstrated the
[broad applicability of HTM applications](/blog/2015/12/01/htm-challenge-2015-results/).
I encourage you to
[view the videos](https://www.youtube.com/playlist?list=PL3yXMgtrZmDqZc2m7qI3Kkbmxechp2-Zs)
from our most recent hackathon.

### Highlight #4

Perhaps one of the most exciting achievements of 2015 was the progress toward
a goal that we set at the beginning of the year: to
[publish major papers](/papers/) about our work. We have several papers
undergoing peer review, which we've  made available pre-publication on arxiv, an
academic archive. I hope to be  sharing news of publication over the coming
months, but in the meantime, you can find all our papers
here: [http://numenta.com/papers/](/papers/)

---

As we kick off 2016, I am excited for what's in store.  We are working to make
Numenta technology even more accessible. We have several events and publications
planned. And we will remain dedicated to driving the creation of machine
intelligence through neocortical principles.

Best wishes for a Happy New Year.


text:
layout:            post
title:             "Cool Python functions"
short_description: "Normalize, textwrap, any and all can help you."
date_long:         "11 de Outubro de 2016"
tags:              python
date_short:        "2016-10-11T21:39"
body:              blog
---

**Hey, Joe!** Let's talk about Python and some nice functions.

![Bye](https://media.giphy.com/media/5GoVLqeAOo6PK/giphy.gif)

**Normalize**

If you already work with Decimal in tables, lists or grids and show any times values like 1.0000, 10.0000, 2.0500, 15.0200, because your database column is a decimal and don't need show 0 in decimal values when the number is like this: 1.5000, normalize can help you:

```
value = Decimal('1.5000')
value = value.normalize()
print(value)
1.5
```

With normalize you remove excessive 0.

**Textwrap**

Textwrap help you with strings. Checkout.

```
import textwrap
foo = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati atque porro architecto optio, nulla officiis, accusamus natus eos eius suscipit.'

print(textwrap.fill(foo, width=40))
Lorem ipsum dolor sit amet, consectetur
adipisicing elit. Obcaecati atque porro
architecto optio, nulla officiis,
accusamus natus eos eius suscipit.

bar = 'Hello world'
textwrap.shorten(bar, width=10, placeholder='...')
'Hello...'

```

**All and any**

Check if all values in your list are true:

```
all([True, True, True, False])
False
```

Now, check if any value is true:

```
any([True, True, True, False])
True
```

I'm going stop for here. I like to make short posts.
Ok, take care!


text:
using System;

namespace Nito.UniformResourceIdentifiers.Implementation.Components
{
    /// <summary>
    /// Container for a normalized host value.
    /// </summary>
#pragma warning disable CA1815 // Override equals and operator equals on value types
    public struct NormalizedHttpHost
#pragma warning restore CA1815 // Override equals and operator equals on value types
    {
        private readonly NormalizedHost _host;

        /// <summary>
        /// Normalizes a host value.
        /// </summary>
        public NormalizedHttpHost(string? host)
        {
            if (host == null)
                throw new ArgumentNullException(nameof(host));
            if (host.Length == 0)
                throw new ArgumentException("Host is required.", nameof(host));
            _host = new NormalizedHost(host);
        }

        /// <summary>
        /// The normalized host.
        /// </summary>
        public string? Value => _host.Value;
    }
}


text:
package edu.uci.ics.hyracks.storage.am.lsm.common.impls;

import java.util.concurrent.atomic.AtomicBoolean;

import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
import edu.uci.ics.hyracks.storage.am.lsm.common.api.ILSMComponentFilter;
import edu.uci.ics.hyracks.storage.am.lsm.common.api.IVirtualBufferCache;

public abstract class AbstractMemoryLSMComponent extends AbstractLSMComponent {

    private int writerCount;
    private final IVirtualBufferCache vbc;
    private final AtomicBoolean isModified;
    private boolean requestedToBeActive;

    public AbstractMemoryLSMComponent(IVirtualBufferCache vbc, boolean isActive, ILSMComponentFilter filter) {
        super(filter);
        this.vbc = vbc;
        writerCount = 0;
        if (isActive) {
            state = ComponentState.READABLE_WRITABLE;
        } else {
            state = ComponentState.INACTIVE;
        }
        isModified = new AtomicBoolean();
    }

    public AbstractMemoryLSMComponent(IVirtualBufferCache vbc, boolean isActive) {
        this(vbc, isActive, null);
    }

    @Override
    public boolean threadEnter(LSMOperationType opType, boolean isMutableComponent) throws HyracksDataException {
        if (state == ComponentState.INACTIVE && requestedToBeActive) {
            state = ComponentState.READABLE_WRITABLE;
            requestedToBeActive = false;
        }
        switch (opType) {
            case FORCE_MODIFICATION:
                if (isMutableComponent) {
                    if (state == ComponentState.READABLE_WRITABLE || state == ComponentState.READABLE_UNWRITABLE) {
                        writerCount++;
                    } else {
                        return false;
                    }
                } else {
                    if (state == ComponentState.READABLE_UNWRITABLE
                            || state == ComponentState.READABLE_UNWRITABLE_FLUSHING) {
                        readerCount++;
                    } else {
                        return false;
                    }
                }
                break;
            case MODIFICATION:
                if (isMutableComponent) {
                    if (state == ComponentState.READABLE_WRITABLE) {
                        writerCount++;
                    } else {
                        return false;
                    }
                } else {
                    if (state == ComponentState.READABLE_UNWRITABLE
                            || state == ComponentState.READABLE_UNWRITABLE_FLUSHING) {
                        readerCount++;
                    } else {
                        return false;
                    }
                }
                break;
            case SEARCH:
                if (state == ComponentState.READABLE_WRITABLE || state == ComponentState.READABLE_UNWRITABLE
                        || state == ComponentState.READABLE_UNWRITABLE_FLUSHING) {
                    readerCount++;
                } else {
                    return false;
                }
                break;
            case FLUSH:
                if (state == ComponentState.READABLE_WRITABLE || state == ComponentState.READABLE_UNWRITABLE) {

                    if (writerCount != 0) {
                        throw new IllegalStateException("Trying to flush when writerCount != 0");
                    }
                    state = ComponentState.READABLE_UNWRITABLE_FLUSHING;
                    readerCount++;
                } else {
                    return false;
                }
                break;
            default:
                throw new UnsupportedOperationException("Unsupported operation " + opType);
        }
        return true;
    }

    @Override
    public void threadExit(LSMOperationType opType, boolean failedOperation, boolean isMutableComponent)
            throws HyracksDataException {
        switch (opType) {
            case FORCE_MODIFICATION:
            case MODIFICATION:
                if (isMutableComponent) {
                    writerCount--;
                    //A failed operation should not change the component state since it's better for the failed operation's effect to be no-op.
                    if (state == ComponentState.READABLE_WRITABLE && !failedOperation && isFull()) {
                        state = ComponentState.READABLE_UNWRITABLE;
                    }
                } else {
                    readerCount--;
                    if (state == ComponentState.UNREADABLE_UNWRITABLE && readerCount == 0) {
                        state = ComponentState.INACTIVE;
                    }
                }
                break;
            case SEARCH:
                readerCount--;
                if (state == ComponentState.UNREADABLE_UNWRITABLE && readerCount == 0) {
                    state = ComponentState.INACTIVE;
                }
                break;
            case FLUSH:
                if (state != ComponentState.READABLE_UNWRITABLE_FLUSHING) {
                    throw new IllegalStateException("Flush sees an illegal LSM memory compoenent state: " + state);
                }
                readerCount--;
                if (readerCount == 0) {
                    state = ComponentState.INACTIVE;
                } else {
                    state = ComponentState.UNREADABLE_UNWRITABLE;
                }
                break;
            default:
                throw new UnsupportedOperationException("Unsupported operation " + opType);
        }

        if (readerCount <= -1 || writerCount <= -1) {
            throw new IllegalStateException("Invalid reader or writer count " + readerCount + " - " + writerCount);
        }
    }

    public boolean isReadable() {
        if (state == ComponentState.INACTIVE || state == ComponentState.UNREADABLE_UNWRITABLE) {
            return false;
        }
        return true;
    }

    @Override
    public LSMComponentType getType() {
        return LSMComponentType.MEMORY;
    }

    @Override
    public ComponentState getState() {
        return state;
    }

    public void setState(ComponentState state) {
        this.state = state;
    }

    public void setActive() {
        requestedToBeActive = true;
    }

    public void setIsModified() {
        isModified.set(true);
    }

    public boolean isModified() {
        return isModified.get();
    }

    public boolean isFull() {
        return vbc.isFull();
    }

    protected void reset() throws HyracksDataException {
        isModified.set(false);
        if (filter != null) {
            filter.reset();
        }
    }
}


text:
package com.zavtech.morpheus.reference;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.function.Consumer;

import com.zavtech.morpheus.frame.DataFrame;
import com.zavtech.morpheus.frame.DataFrameRead;
import com.zavtech.morpheus.frame.DataFrameSource;
import com.zavtech.morpheus.source.*;

/**
 * The default implementation of the DataFrame read interface
 *
 *
This is open source software released under the Apache 2.0 License

 *
 * @author  Xavier Witdouck
 */
class XDataFrameRead implements DataFrameRead {

    /**
     * Static initializer
     */
    static {
        DataFrameSource.register(new CsvSource<>());
        DataFrameSource.register(new JsonSource<>());
        DataFrameSource.register(new DbSource<>());
        DataFrameSource.register(new ExcelSource<>());
    }

    /**
     * Constructor
     */
    XDataFrameRead() {
        super();
    }

    @Override
    public <R> DataFrame<R, String> csv(File file) {
        return csv(options -> options.setFile(file));
    }

    @Override
    public <R> DataFrame<R, String> csv(URL url) {
        return csv(options -> options.setURL(url));
    }

    @Override
    public <R> DataFrame<R, String> csv(InputStream is) {
        return csv(options -> options.setInputStream(is));
    }

    @Override
    public <R> DataFrame<R,String> csv(String resource) {
        return csv(options -> options.setResource(resource));
    }

    @Override
    @SuppressWarnings("unchecked")
    public <R> DataFrame<R,String> csv(Consumer<CsvSourceOptions<R>> configurator) {
        return DataFrameSource.lookup(CsvSource.class).read(configurator);
    }

    @Override
    public <R> DataFrame<R, String> excel(InputStream is) {
        return excel(options -> options.setInputStream(is));
    }

    @Override
    public <R> DataFrame<R,String> excel(URL url) { return excel(options -> options.setURL(url)); }

    @Override
    public <R> DataFrame<R,String> excel(String resource) { return excel(options -> options.setResource(resource)); }

    @Override
    @SuppressWarnings("unchecked")
    public <R> DataFrame<R,String> excel(Consumer<ExcelSourceOptions<R>> configurator) {
        return DataFrameSource.lookup(ExcelSource.class).read(configurator);
    }

    @Override
    public <R, C> DataFrame<R, C> json(File file) {
        return json(options -> options.setFile(file));
    }

    @Override
    public <R, C> DataFrame<R, C> json(URL url) {
        return json(options -> options.setURL(url));
    }

    @Override
    public <R, C> DataFrame<R, C> json(InputStream is) {
        return json(options -> options.setInputStream(is));
    }

    @Override
    public <R,C> DataFrame<R,C> json(String resource) {
        return json(options -> options.setResource(resource));
    }

    @Override
    @SuppressWarnings("unchecked")
    public <R,C> DataFrame<R,C> json(Consumer<JsonSourceOptions<R,C>> configurator) {
        return DataFrameSource.lookup(JsonSource.class).read(configurator);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <R> DataFrame<R, String> db(Consumer<DbSourceOptions<R>> configurator) {
        return DataFrameSource.lookup(DbSource.class).read(configurator);
    }
}


text:
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2017 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
#include "config.h"
#include "hostname_utils.h"

#include <gtest/gtest.h>

class HostnameUtilsTest : public ::testing::Test {
protected:
    static void SetUpTestCase() {
        cb_initialize_sockets();
    }
};

TEST_F(HostnameUtilsTest, PlainPortByNumber) {
    std::string host;
    in_port_t in_port;
    sa_family_t family;

    std::tie(host, in_port, family) = cb::inet::parse_hostname("localhost",
                                                               "11210");
    EXPECT_EQ("localhost", host);
    EXPECT_EQ(11210, in_port);
    EXPECT_EQ(AF_UNSPEC, family);
}

TEST_F(HostnameUtilsTest, PlainPortByName) {
    std::string host;
    in_port_t in_port;
    sa_family_t family;

    std::tie(host, in_port, family) = cb::inet::parse_hostname("localhost",
                                                               "echo");
    EXPECT_EQ("localhost", host);
    EXPECT_EQ(7, ntohs(in_port));
    EXPECT_EQ(AF_UNSPEC, family);
}

TEST_F(HostnameUtilsTest, PlainInvalidPort) {
  EXPECT_THROW(cb::inet::parse_hostname("localhost", "11a"),
               std::runtime_error);
}

TEST_F(HostnameUtilsTest, IPv4PortByNumberInHost) {
    std::string host;
    in_port_t in_port;
    sa_family_t family;

    std::tie(host, in_port, family) = cb::inet::parse_hostname("localhost:11210",
                                                               "6666");
    EXPECT_EQ("localhost", host);
    EXPECT_EQ(11210, in_port);
    EXPECT_EQ(AF_INET, family);
}

TEST_F(HostnameUtilsTest, IPv4PortByNameInHost) {
    std::string host;
    in_port_t in_port;
    sa_family_t family;

    std::tie(host, in_port, family) = cb::inet::parse_hostname("localhost:echo",
                                                               "6666");
    EXPECT_EQ("localhost", host);
    EXPECT_EQ(7, ntohs(in_port));
    EXPECT_EQ(AF_INET, family);
}

TEST_F(HostnameUtilsTest, IPv6PortByNumberInHost) {
    std::string host;
    in_port_t in_port;
    sa_family_t family;

    std::tie(host, in_port, family) = cb::inet::parse_hostname("[::1]:11210",
                                                               "6666");
    EXPECT_EQ("::1", host);
    EXPECT_EQ(11210, in_port);
    EXPECT_EQ(AF_INET6, family);
}

TEST_F(HostnameUtilsTest, IPv6PortByNameInHost) {
    std::string host;
    in_port_t in_port;
    sa_family_t family;

    std::tie(host, in_port, family) = cb::inet::parse_hostname("[::1]:echo",
                                                               "6666");
    EXPECT_EQ("::1", host);
    EXPECT_EQ(7, ntohs(in_port));
    EXPECT_EQ(AF_INET6, family);
}

TEST_F(HostnameUtilsTest, InvalidIPv6PortSpec) {
    EXPECT_THROW(cb::inet::parse_hostname("::1[:11210]", ""),
                 std::runtime_error);
}


text:
import React from 'react'
import {Platform, StyleSheet, View} from 'react-native'
import {colors} from '../constants/colors'

export const Banner = () => <View style={styles.status} />

const styles = StyleSheet.create({
  status: {
    height: Platform.OS === 'ios' ? 20 : 0,
    backgroundColor: colors.banner,
  },
})


text:
using namespace PBD;

TimeManager* TimeManager::current = 0;

TimeManager::TimeManager ()
{
	time = 0;
	h = 0.005f;
}

TimeManager::~TimeManager ()
{
	current = 0;
}

TimeManager* TimeManager::getCurrent ()
{
	if (current == 0)
	{
		current = new TimeManager ();
	}
	return current;
}

void TimeManager::setCurrent (TimeManager* tm)
{
	current = tm;
}

bool TimeManager::hasCurrent()
{
	return (current != 0);
}

float TimeManager::getTime()
{
	return time;
}

void TimeManager::setTime(float t)
{
	time = t;
}

float TimeManager::getTimeStepSize()
{
	return h;
}

void TimeManager::setTimeStepSize(float tss)
{
	h = tss;
}


text:
namespace chromeos {
class DisplayConfigurationObserver;
}

namespace keyboard {
class KeyboardUI;
}

class ChromeLauncherControllerImpl;

class ChromeShellDelegate : public ash::ShellDelegate,
                            public content::NotificationObserver {
 public:
  ChromeShellDelegate();
  ~ChromeShellDelegate() override;

  // ash::ShellDelegate overrides;
  service_manager::Connector* GetShellConnector() const override;
  bool IsMultiProfilesEnabled() const override;
  bool IsIncognitoAllowed() const override;
  bool IsRunningInForcedAppMode() const override;
  bool CanShowWindowForUser(ash::WmWindow* window) const override;
  bool IsForceMaximizeOnFirstRun() const override;
  void PreInit() override;
  void PreShutdown() override;
  void Exit() override;
  keyboard::KeyboardUI* CreateKeyboardUI() override;
  void OpenUrlFromArc(const GURL& url) override;
  app_list::AppListPresenter* GetAppListPresenter() override;
  ash::ShelfDelegate* CreateShelfDelegate(ash::ShelfModel* model) override;
  ash::SystemTrayDelegate* CreateSystemTrayDelegate() override;
  std::unique_ptr<ash::WallpaperDelegate> CreateWallpaperDelegate() override;
  ash::SessionStateDelegate* CreateSessionStateDelegate() override;
  ash::AccessibilityDelegate* CreateAccessibilityDelegate() override;
  ash::MediaDelegate* CreateMediaDelegate() override;
  std::unique_ptr<ash::PaletteDelegate> CreatePaletteDelegate() override;
  ui::MenuModel* CreateContextMenu(ash::WmShelf* wm_shelf,
                                   const ash::ShelfItem* item) override;
  ash::GPUSupport* CreateGPUSupport() override;
  base::string16 GetProductName() const override;
  void OpenKeyboardShortcutHelpPage() const override;
  gfx::Image GetDeprecatedAcceleratorImage() const override;
  void ToggleTouchpad() override;
  void ToggleTouchscreen() override;

  // content::NotificationObserver override:
  void Observe(int type,
               const content::NotificationSource& source,
               const content::NotificationDetails& details) override;

 private:
  void PlatformInit();

  content::NotificationRegistrar registrar_;

  ChromeLauncherControllerImpl* shelf_delegate_;

  std::unique_ptr<chromeos::DisplayConfigurationObserver>
      display_configuration_observer_;

  DISALLOW_COPY_AND_ASSIGN(ChromeShellDelegate);
};

#endif  // CHROME_BROWSER_UI_ASH_CHROME_SHELL_DELEGATE_H_


text:
"""An ox_herd plugin for posting messages to github.

This module provides a single class: PostToGitHub to post messages to github.
It is useful as a tool for the pylint plugin and perhaps others.

Provided that you include this plugin in the OX_PLUGINS variable in
ox_herd.settings or in your OX_PLUGINS environment variable, this will be
picked up automatically and used as a plugin.

In general, there are a variety of more complicated things you can do
in configuring plugins. See documentation on plugins or see the pytest_plugin
example for more details.
"""

import configparser
import json

from eyap.core import github_comments

from ox_herd.core.plugins import base
from ox_herd.core.ox_tasks import OxHerdTask

class PostToGitHub(OxHerdTask, base.OxPluginComponent):
    """Class to post a message to github.

    """

    def __init__(self, msg, full_repo, title, number, conf_file, conf_sec,
                 *args, **kw):
        """Initializer.

        :arg msg:      String message to post.

        :arg full_repo: Full name of github repo (e.g., 'aocks/ox_herd').

        :arg title:     String title of issue to post to *ONLY* if no
                        github_issue is specified in the conf file (see below).
                        This will be ignored if github_issue is provided.

        :arg number:    Optional issue number. This is useful if you have
                        multiple issues with the same title and need to
                        distinguish between them.

        :arg conf_file: Path to configuration file to be read by python
                        configparser module.

        :arg conf_sec:  String name of section in conf_file to read.
                        This section should have entries for the following:

                           github_user:  Name of github user for login.
                           github_token: Token or password to access github.
                           github_issue: Optional issue title to use instead of
                                         title argument.

        :arg *args:    Argumnets to OxHerdTask.__init__.

        :arg **kw:     Keyword arguments to OxHerdTask.__init__.

        """
        OxHerdTask.__init__(self, *args, **kw)
        base.OxPluginComponent.__init__(self)
        self.msg = msg
        self.full_repo = full_repo
        self.title = title
        self.number = number
        self.conf_file = conf_file
        self.conf_sec = conf_sec

    @classmethod
    def main_call(cls, ox_herd_task):
        """Main method to post to github.

        :arg ox_herd_task:   Instance of a PostToGitHub task perhaps containing
                             additional data (e.g., ox_herd_task.name).

        ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

        :returns:       Dictionary with 'return_value' and 'json_blob' as
                        required for OxPluginComponent.

        ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

        PURPOSE:        Post a message to github.

        """
        my_config = configparser.ConfigParser()
        my_config.read(ox_herd_task.conf_file)
        my_csec = my_config[ox_herd_task.conf_sec]
        cthread = cls.prep_comment_thread(
            ox_herd_task.title, ox_herd_task.number, ox_herd_task.full_repo,
            my_csec)
        cthread.add_comment(ox_herd_task.msg, allow_create=True)

        return {
            'return_value': 'Task %s completed succesfully.' % (
                ox_herd_task.name), 'json_blob': json.dumps({})}

    @staticmethod
    def prep_comment_thread(title, number, full_repo, my_conf):
        """Prepare a CommentThread object to use in positing comments.

        :arg ox_herd_task: Ox Herd task with raw data.

        ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

        :returns:  A GitHubCommentThread object.

        ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

        PURPOSE:   This method reads from the configuration in the dictionary
                   in my_conf, figures out the github parameters, and creates a
                   GitHubCommentThread we can use in posting comments.
        """
        user = my_conf['github_user']
        token = my_conf['github_token']
        topic = my_conf['github_issue'] if 'github_issue' in my_conf else None

        if topic is None:
            topic = title
            thread_id = number
            if isinstance(thread_id, str):
                thread_id = thread_id.strip()
            if thread_id == '':
                thread_id = None
        else:
            thread_id = None

        owner, repo = full_repo.split('/')

        comment_thread = github_comments.GitHubCommentThread(
            owner, repo, topic, user, token, thread_id=thread_id)

        return comment_thread


text:
package com.jayqqaa12.jbase.cache.core.load;

import static com.jayqqaa12.jbase.cache.core.CacheConst.REFRESH_MIN_TIME;

import com.jayqqaa12.jbase.cache.core.CacheObject;
import com.jayqqaa12.jbase.cache.core.JbaseCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;

import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public class AutoLoadSchedule implements Runnable {

  public static final long MAX_IDLE_TIME = 60 * 1000 * 10;
  private final AutoLoadPool autoLoadPool;
  private final JbaseCache cache;
  private ScheduledExecutorService scheduledExecutorService;
  private ExecutorService executorService;

  public AutoLoadSchedule(int threadSize, JbaseCache cache) {
    this.autoLoadPool = new AutoLoadPool();
    this.cache = cache;
    scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
    scheduledExecutorService.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);

    executorService = new ThreadPoolExecutor(threadSize, threadSize,
        0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<>(1000), (r) -> new Thread(r, "Thread-Jbase-Cache-Auto-Load"));
  }

  public void shutdown() {
    autoLoadPool.clear();
    executorService.shutdown();
    scheduledExecutorService.shutdown();
  }

  @Override
  public void run() {
    for (AutoLoadObject obj : autoLoadPool.values()) {
      if (obj.canAutoLoad()) {
        executorService.execute(() -> {
          //如果可加载 就进行加载
          try {
            obj.setLock(true);

            // 先重新加载2级缓存的 如果也快到期了再加载数据源

            CacheObject cacheObject = cache.getProvider().getLevel2Provider(obj.getRegion())
                .get(obj.getKey());

            if (cacheObject != null && !cacheObject.canAutoLoad()) {

              cache.getProvider().getLevel1Provider(obj.getRegion(), cacheObject.getExpire())
                  .set(obj.getKey(), cacheObject);

              log.info("auto load data from level 2 key {}@{}", obj.getKey(), obj.getRegion());
              return;
            }

            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            cache.set(obj.getRegion(),
                obj.getKey(), obj.getFunction().get(),
                obj.getExpire());
            stopWatch.stop();
            log.info("auto load data from data source  key {}@{} cost time {}ms", obj.getKey(),
                obj.getRegion(), stopWatch.getTime());

            obj.setLastUpdateTime(System.currentTimeMillis());

          } catch (Exception e) {
            log.error("auto load data error {}", e);
          } finally {
            obj.setLock(false);
          }

        });
      } else {
        // 清理很久没有调用的
        if (System.currentTimeMillis() - obj.getLastUpdateTime() > MAX_IDLE_TIME
            && obj.isExpire()) {
          remove(obj.getRegion(), obj.getKey());
        }
      }
    }
  }

  public void add(AutoLoadObject autoLoadObject) {
    // expire<= REFRESH_MIN_TIME 不自动加载
    if (autoLoadObject.getExpire() >= REFRESH_MIN_TIME) {
      autoLoadPool.put(autoLoadObject);
    }
  }

  public void remove(String region, String key) {
    autoLoadPool.remove(region, key);

    log.info("remove auto load obj  key {}@{}", key, region);
  }

  public void refresh(String region, String key) {
    AutoLoadObject autoLoadObject = autoLoadPool.get(region, key);

    if (autoLoadObject != null) {
      autoLoadObject.setLastRequestTime(System.currentTimeMillis());
    }

  }
}


text:
package com.morgan.server.nav;

import javax.servlet.http.HttpServletRequest;

import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.morgan.server.constants.PageConstants;
import com.morgan.server.constants.PageConstantsSource;
import com.morgan.shared.nav.ClientApplication;
import com.morgan.shared.nav.NavigationConstant;

/**
 * {@link PageConstantsSource} for the navigation package.
 *
 * @author mark@mark-morgan.net (Mark Morgan)
 */
class NavigationPageConstantsSource implements PageConstantsSource {

  private final Provider<HttpServletRequest> requestProvider;
  private final Provider<ClientApplication> clientApplicationProvider;

  @Inject NavigationPageConstantsSource(
      Provider<HttpServletRequest> requestProvider,
      Provider<ClientApplication> clientApplicationProvider) {
    this.requestProvider = requestProvider;
    this.clientApplicationProvider = clientApplicationProvider;
  }

  @Override public void provideConstantsInto(PageConstants constantsSink) {
    HttpServletRequest request = requestProvider.get();

    String url = request.getRequestURL().toString();
    int colon = url.indexOf(':');
    Preconditions.checkState(colon == 4 || colon == 5);

    constantsSink.add(NavigationConstant.APPLICATION_URL, url);
    constantsSink.add(NavigationConstant.APPLICATION_HOST, request.getServerName());
    constantsSink.add(NavigationConstant.APPLICATION_PORT, request.getServerPort());
    constantsSink.add(NavigationConstant.APPLICATION_PROTOCOL, url.substring(0, colon));
    constantsSink.add(NavigationConstant.APPLICATION_TYPE, clientApplicationProvider.get());
  }
}