text:
module Bosh::AwsCloud
  class BlockDeviceManager
    DEFAULT_INSTANCE_STORAGE_DISK_MAPPING = { device_name: '/dev/sdb', virtual_name: 'ephemeral0' }.freeze
    NVME_EBS_BY_ID_DEVICE_PATH_PREFIX = '/dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_'

    # Newer, nitro-based instances use NVMe storage volumes.
    # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html#ec2-nitro-instances
    NVME_INSTANCE_FAMILIES = %w[a1 c5 c5a c5ad c5d c5n c6g c6gd c6gn d3 d3en g4 i3en inf1 m5 m5a m5ad m5d m5dn m5n m5zn m6g m6gd p3dn p4 r5 r5a r5ad r5b r5d r5dn r5n r6g r6gd t3 t3a t4g z1d].freeze

    def initialize(logger, stemcell, vm_cloud_props)
      @logger = logger
      @vm_cloud_props = vm_cloud_props
      @virtualization_type = stemcell.ami.virtualization_type
      @root_device_name = stemcell.ami.root_device_name
      @ami_block_device_names = stemcell.ami.block_device_mappings.map { |blk| blk.device_name }
    end

    def mappings_and_info
      info = build_info

      return mappings(info), agent_info(info)
    end

    def self.device_path(device_name, instance_type, volume_id)
      if BlockDeviceManager.requires_nvme_device(instance_type)
        NVME_EBS_BY_ID_DEVICE_PATH_PREFIX + volume_id.sub('-', '')
      else
        device_name
      end
    end

    def self.block_device_ready?(device_path)
      candidatePaths = [device_path]
      unless device_path.start_with?(NVME_EBS_BY_ID_DEVICE_PATH_PREFIX)
        xvd_name = device_path.gsub(/^\/dev\/sd/, '/dev/xvd')
        candidatePaths << xvd_name
      end

      Bosh::AwsCloud::CloudCore::DEVICE_POLL_TIMEOUT.times do
        candidatePaths.each do |path|
          if File.blockdev?(path)
            return path
          end
        end

        sleep(1)
      end

      cloud_error('Cannot find EBS volume on current instance')
    end

    def self.requires_nvme_device(instance_type)
      instance_type = instance_type.nil? ? 'unspecified' : instance_type
      instance_family = instance_type.split(".")[0]
      NVME_INSTANCE_FAMILIES.include?(instance_family)
    end

    private

    def mappings(info)
      instance_type = @vm_cloud_props.instance_type.nil? ? 'unspecified' : @vm_cloud_props.instance_type
      if instance_type =~ /^i3\./
        info = info.reject { |device| device[:bosh_type] == 'raw_ephemeral' }
      end

      info.map { |entry| entry.reject { |k| k == :bosh_type } }
    end

    def agent_info(info)
      info.group_by { |v| v[:bosh_type] }.map do |type, devices|
        {
          type => devices.map do |device|
            @logger.info("Mapping device #{device.inspect} to path: #{device[:device_name].inspect}")
            { 'path' => device[:device_name] }
          end
        }
      end.select do |elem|
        elem[nil].nil?
      end.inject({}) do |a, b|
        a.merge(b)
      end
    end

    def build_info
      instance_type = @vm_cloud_props.instance_type.nil? ? 'unspecified' : @vm_cloud_props.instance_type

      disk_info = DiskInfo.for(instance_type)
      if @vm_cloud_props.raw_instance_storage? && disk_info.nil?
        raise Bosh::Clouds::CloudError, "raw_instance_storage requested for instance type '#{instance_type}' that does not have instance storage"
      end

      block_devices = []
      ephemeral_disk = @vm_cloud_props.ephemeral_disk
      unless ephemeral_disk.use_root_disk
        block_devices << ephemeral_disk_mapping(instance_type, disk_info)

        if @vm_cloud_props.raw_instance_storage?
          block_devices += raw_instance_mappings(disk_info.count)
        end
      end

      if @vm_cloud_props.root_disk.specified?
        block_devices << user_specified_root_disk_mapping
      else
        block_devices << default_root_disk_mapping
      end

      block_devices
    end

    def ephemeral_disk_mapping(instance_type, disk_info)
      ephemeral_disk = @vm_cloud_props.ephemeral_disk

      if ephemeral_disk.use_instance_storage
        if @vm_cloud_props.raw_instance_storage?
          raise Bosh::Clouds::CloudError, 'ephemeral_disk.use_instance_storage and raw_instance_storage cannot both be true'
        end

        if ephemeral_disk.invalid_instance_storage_config?
          raise Bosh::Clouds::CloudError, 'use_instance_storage cannot be combined with additional ephemeral_disk properties'
        end

        if disk_info.nil?
          raise Bosh::Clouds::CloudError, "use_instance_storage requested for instance type '#{instance_type}' that does not have instance storage"
        end

        @logger.debug('Use instance storage to create the virtual machine')
        result = DEFAULT_INSTANCE_STORAGE_DISK_MAPPING.dup
      else
        @logger.debug('Use EBS storage to create the virtual machine')
        disk_size = DiskInfo.default.size_in_mb

        if ephemeral_disk.size
          disk_size = ephemeral_disk.size
        elsif disk_info && !@vm_cloud_props.raw_instance_storage?
          disk_size = disk_info.size_in_mb
        end

        result = VolumeProperties.new(
          size: disk_size,
          type: ephemeral_disk.type,
          iops: ephemeral_disk.iops,
          encrypted: ephemeral_disk.encrypted,
          kms_key_arn: ephemeral_disk.kms_key_arn,
        ).ephemeral_disk_config
      end

      result[:bosh_type] = 'ephemeral'
      result
    end

    def first_raw_ephemeral_device
      instance_type = @vm_cloud_props.instance_type.nil? ? 'unspecified' : @vm_cloud_props.instance_type
      case @virtualization_type

        when 'hvm'
          if instance_type =~ /^i3\./
            '/dev/nvme0n1'
          else
            '/dev/xvdba'
          end
        when 'paravirtual'
          '/dev/sdc'
        else
          raise Bosh::Clouds::CloudError, "unknown virtualization type #{@virtualization_type}"
      end
    end

    def raw_instance_mappings(num_of_devices)
      next_device = first_raw_ephemeral_device

      num_of_devices.times.map do |index|
        result = {
          virtual_name: "ephemeral#{index}",
          device_name: next_device,
          bosh_type: 'raw_ephemeral',
        }
        next_device = next_raw_ephemeral_disk(next_device)
        result
      end
    end

    def user_specified_root_disk_mapping
      disk_properties = VolumeProperties.new(
        size: @vm_cloud_props.root_disk.size,
        type: @vm_cloud_props.root_disk.type,
        iops: @vm_cloud_props.root_disk.iops,
        virtualization_type: @virtualization_type,
        root_device_name: root_device_name,
      )
      disk_properties.root_disk_config
    end

    def default_root_disk_mapping
      disk_properties = VolumeProperties.new(
        virtualization_type: @virtualization_type,
        root_device_name: root_device_name,
      )
      disk_properties.root_disk_config
    end

    def root_device_name
      if @root_device_name
        # covers two cases:
        # 1. root and block device match exactly
        # 2. root is a partition and block device is the entire device
        #    e.g. root == /dev/sda1 and block device == /dev/sda
        block_device_to_override = (@ami_block_device_names || {}).find do |name|
          @root_device_name == name
        end
        block_device_to_override ||= (@ami_block_device_names || {}).find do |name|
          @root_device_name.start_with?(name)
        end

        return block_device_to_override if block_device_to_override
      end

      # fallback
      if @virtualization_type == 'paravirtual'
        return '/dev/sda'
      else
        return '/dev/xvda'
      end
    end

    def next_raw_ephemeral_disk(current_disk)
      if current_disk =~ /^\/dev\/nvme/
        disk_id = /^\/dev\/nvme(\d+)n.*/.match(current_disk)[1]
        disk_id = disk_id.next
        "/dev/nvme#{disk_id}n1"
      else
        current_disk.next
      end
    end

    class DiskInfo
      INSTANCE_TYPE_DISK_MAPPING = {
        # previous generation
        'm1.small' => [160, 1],
        'm1.medium' => [410, 1],
        'm1.large' => [420, 2],
        'm1.xlarge' => [420, 4],

        'c1.medium' => [350, 1],
        'c1.xlarge' => [420, 4],

        'cc2.8xlarge' => [840, 4],

        'cg1.4xlarge' => [840, 2],

        'm2.xlarge' => [420, 1],
        'm2.2xlarge' => [850, 1],
        'm2.4xlarge' => [840, 2],

        'cr1.8xlarge' => [120, 2],

        'hi1.4xlarge' => [1024, 2],

        'hs1.8xlarge' => [2000, 24],

        'm3.medium' => [4, 1],
        'm3.large' => [32, 1],
        'm3.xlarge' => [40, 2],
        'm3.2xlarge' => [80, 2],

        'c3.large' => [16, 2],
        'c3.xlarge' => [40, 2],
        'c3.2xlarge' => [80, 2],
        'c3.4xlarge' => [160, 2],
        'c3.8xlarge' => [320, 2],

        'r3.large' => [32, 1],
        'r3.xlarge' => [80, 1],
        'r3.2xlarge' => [160, 1],
        'r3.4xlarge' => [320, 1],
        'r3.8xlarge' => [320, 2],

        'g2.2xlarge' => [60, 1],
        'g2.8xlarge' => [120, 2],

        'i2.xlarge' => [800, 1],
        'i2.2xlarge' => [800, 2],
        'i2.4xlarge' => [800, 4],
        'i2.8xlarge' => [800, 8],

        'i3.large' => [475, 1],
        'i3.xlarge' => [950, 1],
        'i3.2xlarge' => [1900, 1],
        'i3.4xlarge' => [1900, 2],
        'i3.8xlarge' => [1900, 4],
        'i3.16xlarge' => [1900, 8],

        'd2.xlarge' => [2000, 3],
        'd2.2xlarge' => [2000, 6],
        'd2.4xlarge' => [2000, 12],
        'd2.8xlarge' => [2000, 24],

        'x1.16xlarge' => [1920, 1],
        'x1.32xlarge' => [1920, 2],
      }

      attr_reader :size, :count

      def self.default
        self.new(10, 1)
      end

      def self.for(instance_type)
        values = INSTANCE_TYPE_DISK_MAPPING[instance_type]
        DiskInfo.new(*values) if values
      end

      def initialize(size, count)
        @size = size
        @count = count
      end

      def size_in_mb
        @size * 1024
      end
    end
  end
end


text:
package com.datastax.oss.driver.internal.core.type.codec;

public class ParseUtils {

  /**
   * Returns the index of the first character in toParse from idx that is not a "space".
   *
   * @param toParse the string to skip space on.
   * @param idx the index to start skipping space from.
   * @return the index of the first character in toParse from idx that is not a "space.
   */
  public static int skipSpaces(String toParse, int idx) {
    while (idx < toParse.length() && isBlank(toParse.charAt(idx))) ++idx;
    return idx;
  }

  /**
   * Assuming that idx points to the beginning of a CQL value in toParse, returns the index of the
   * first character after this value.
   *
   * @param toParse the string to skip a value form.
   * @param idx the index to start parsing a value from.
   * @return the index ending the CQL value starting at {@code idx}.
   * @throws IllegalArgumentException if idx doesn't point to the start of a valid CQL value.
   */
  public static int skipCQLValue(String toParse, int idx) {
    if (idx >= toParse.length()) throw new IllegalArgumentException();

    if (isBlank(toParse.charAt(idx))) throw new IllegalArgumentException();

    int cbrackets = 0;
    int sbrackets = 0;
    int parens = 0;
    boolean inString = false;

    do {
      char c = toParse.charAt(idx);
      if (inString) {
        if (c == '\'') {
          if (idx + 1 < toParse.length() && toParse.charAt(idx + 1) == '\'') {
            ++idx; // this is an escaped quote, skip it
          } else {
            inString = false;
            if (cbrackets == 0 && sbrackets == 0 && parens == 0) return idx + 1;
          }
        }
        // Skip any other character
      } else if (c == '\'') {
        inString = true;
      } else if (c == '{') {
        ++cbrackets;
      } else if (c == '[') {
        ++sbrackets;
      } else if (c == '(') {
        ++parens;
      } else if (c == '}') {
        if (cbrackets == 0) return idx;

        --cbrackets;
        if (cbrackets == 0 && sbrackets == 0 && parens == 0) return idx + 1;
      } else if (c == ']') {
        if (sbrackets == 0) return idx;

        --sbrackets;
        if (cbrackets == 0 && sbrackets == 0 && parens == 0) return idx + 1;
      } else if (c == ')') {
        if (parens == 0) return idx;

        --parens;
        if (cbrackets == 0 && sbrackets == 0 && parens == 0) return idx + 1;
      } else if (isBlank(c) || !isCqlIdentifierChar(c)) {
        if (cbrackets == 0 && sbrackets == 0 && parens == 0) return idx;
      }
    } while (++idx < toParse.length());

    if (inString || cbrackets != 0 || sbrackets != 0 || parens != 0)
      throw new IllegalArgumentException();
    return idx;
  }

  /**
   * Assuming that idx points to the beginning of a CQL identifier in toParse, returns the index of
   * the first character after this identifier.
   *
   * @param toParse the string to skip an identifier from.
   * @param idx the index to start parsing an identifier from.
   * @return the index ending the CQL identifier starting at {@code idx}.
   * @throws IllegalArgumentException if idx doesn't point to the start of a valid CQL identifier.
   */
  public static int skipCQLId(String toParse, int idx) {
    if (idx >= toParse.length()) throw new IllegalArgumentException();

    char c = toParse.charAt(idx);
    if (isCqlIdentifierChar(c)) {
      while (idx < toParse.length() && isCqlIdentifierChar(toParse.charAt(idx))) idx++;
      return idx;
    }

    if (c != '"') throw new IllegalArgumentException();

    while (++idx < toParse.length()) {
      c = toParse.charAt(idx);
      if (c != '"') continue;

      if (idx + 1 < toParse.length() && toParse.charAt(idx + 1) == '\"')
        ++idx; // this is an escaped double quote, skip it
      else return idx + 1;
    }
    throw new IllegalArgumentException();
  }

  public static boolean isBlank(int c) {
    return c == ' ' || c == '\t' || c == '\n';
  }

  public static boolean isCqlIdentifierChar(int c) {
    return (c >= '0' && c <= '9')
        || (c >= 'a' && c <= 'z')
        || (c >= 'A' && c <= 'Z')
        || c == '-'
        || c == '+'
        || c == '.'
        || c == '_'
        || c == '&';
  }
}

redpajama_set_name: RedPajamaGithub


text:
namespace arbitrary_format
{
namespace xml
{

class element_counter_impl
{
private:
    boost::optional<std::string> name;

public:
    explicit element_counter_impl(const boost::optional<std::string>& name = boost::none)
        : name(name)
    {
    }

    explicit element_counter_impl(const char* name)
        : element_counter_impl(std::string(name))
    {
    }

    template<typename T, typename XmlTree>
    void save(XmlTree& xmlTree, const T& object) const
    {
        // @note Nothing to save.
    }

    template<typename T, typename XmlTree>
    void load(XmlTree& xmlTree, T& object) const
    {
        object = static_cast<T>(xmlTree.elementCount(name));
    }

    /// @brief Special overload for bools to avoid warnings in this common case.
    template<typename XmlTree>
    void load(XmlTree& xmlTree, bool& object) const
    {
        object = (xmlTree.elementCount(name) > 0);
    }
};

template<typename CompileTimeString>
class element_counter
{
private:
    static const element_counter_impl formatter;
public:
    template<typename T, typename XmlTree>
    void save(XmlTree& xmlTree, const T& object) const
    {
        formatter.save(xmlTree, object);
    }

    template<typename T, typename XmlTree>
    void load(XmlTree& xmlTree, T& object) const
    {
        formatter.load(xmlTree, object);
    }
};

template<typename CompileTimeString>
const element_counter_impl element_counter<CompileTimeString>::formatter = element_counter_impl( boost::optional<std::string>(!compile_time_string_print<CompileTimeString>::str().empty(), compile_time_string_print<CompileTimeString>::str()) );

element_counter_impl create_element_counter(const boost::optional<std::string>& name = boost::none)
{
    return element_counter_impl(name);
}

element_counter_impl create_element_counter(const char* name)
{
    return create_element_counter(std::string(name));
}

} // namespace xml
} // namespace arbitrary_format

#endif // ArbitraryFormatSerializer_element_counter_H


text:
@implementation NSTextStorage (ParagraphStyle)

- (void)setFirstLineHeadIndent:(CGFloat)headIndent {
    __block NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.firstLineHeadIndent = headIndent;

    [self enumerateAttributesInRange:NSMakeRange(0, self.length)
                             options:NSAttributedStringEnumerationReverse
                          usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop){
                              NSMutableDictionary *attributesM = [attributes mutableCopy];
                              NSParagraphStyle *originalParagraphStyle = attributesM[NSParagraphStyleAttributeName];
                              if (originalParagraphStyle) {
                                  paragraphStyle = [originalParagraphStyle mutableCopy];
                                  paragraphStyle.firstLineHeadIndent = headIndent;
                              }

                              [attributesM setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];
                              [self addAttributes:attributesM range:range];
                          }];
}

- (void)setLineSpacing:(CGFloat)lineSpacing {
    __block NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;

    [self enumerateAttributesInRange:NSMakeRange(0, self.length)
                             options:NSAttributedStringEnumerationReverse
                          usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop){
                              NSMutableDictionary *attributesM = [attributes mutableCopy];
                              NSParagraphStyle *originalParagraphStyle = attributesM[NSParagraphStyleAttributeName];
                              if (originalParagraphStyle) {
                                  paragraphStyle = [originalParagraphStyle mutableCopy];
                                  paragraphStyle.lineSpacing = lineSpacing;
                              }

                              [attributesM setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];
                              [self addAttributes:attributesM range:range];
                          }];
}

- (void)setParagraphSpacingBefore:(CGFloat)spacing {
    __block NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.paragraphSpacingBefore = spacing;

    [self enumerateAttributesInRange:NSMakeRange(0, self.length)
                             options:NSAttributedStringEnumerationReverse
                          usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop){
                              NSMutableDictionary *attributesM = [attributes mutableCopy];
                              NSParagraphStyle *originalParagraphStyle = attributesM[NSParagraphStyleAttributeName];
                              if (originalParagraphStyle) {
                                  paragraphStyle = [originalParagraphStyle mutableCopy];
                                  paragraphStyle.paragraphSpacingBefore = spacing;
                              }

                              [attributesM setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];
                              [self addAttributes:attributesM range:range];
                          }];

}

@end


text:
"""
Module for deriving kinematic velocity distributions.

WARNING: This module works in physical space (kpc) *not* angular space.

Louie points us to this reference:
https://arxiv.org/abs/1003.4268

"""

import sys
import copy
from collections import OrderedDict as odict
import logging

import numpy as np
import scipy.stats
from scipy.integrate import romberg, quad, simps, trapz
from scipy.interpolate import interp1d
from scipy.misc import derivative
import matplotlib.tri as mtri

try:
    import vegas
except ImportError:
    msg = "Vegas not found; velocity calculation disabled"

from ugali.analysis.model import Model,Parameter

# Constants and sampling parameters (left over from Fortran)
nx=200;nes=200;nts=100;nvs=100;nrs=25
# Newton's constant in (km^3 Msun^-1 s^-1)
Gn=6.67e-11*1.99e30*1.e-9
# Vegas MCMC integration parameters
n_grid = 1000; n_final = 5000

def loginterp1d(x,y,**kwargs):
    """
    Interpolate a 1-D function applying a logarithmic transform to y-values.

    See scipy.interpolate.interp1d for more details.
    """
    err = np.seterr(divide='ignore')
    loginterp = interp1d(x,np.log(y),**kwargs)
    np.seterr(**err)
    return lambda x: np.exp(loginterp(x))

def triinterp2d(x,y,z,**kwargs):
    """
    Create a linear triangular interpolation using the mlab implementation.

    See `matplotlib.tri.LinearTriInterpolator`

    Returns a numpy masked array.
    """
    import warnings
    warnings.simplefilter(action="ignore", category=FutureWarning)
    triang = mtri.Triangulation(x.flat, y.flat)
    return mtri.LinearTriInterpolator(triang, z.flat)

def plot_values_residuals(x1,y1,x2,y2,**kwargs):
    fig = plt.figure()
    return draw_values_residuals(x1,y1,x2,y2,**kwargs)

def draw_values_residuals(x1,y1,x2,y2,**kwargs):
    ax = plt.gca()
    ax.set_yscale('log',nonposy='clip')

    divider = make_axes_locatable(ax)
    rax = divider.append_axes("bottom", 1.2, pad=0.05, sharex=ax)
    plt.setp(ax.get_xticklabels(), visible=False)
    #ax.yaxis.set_major_locator(MaxNLocator(prune='lower'))
    rax.yaxis.set_major_locator(MaxNLocator(4,prune='both'))

    ax.plot(x1,y1,'o-k')
    ax.plot(x2,y2,'o-r')

    #loginterp = interp1d(x1,np.log(y1),bounds_error=False)
    #interp = lambda x: np.exp(loginterp(x))
    interp = loginterp1d(x1,y1,bounds_error=False)

    rax.plot(x1,(y1-interp(x1))/y1,'o-k')
    rax.plot(x2,(y2-interp(x2))/y2,'o-r')

    return ax, rax

def nfw_potential(x):
    """
    Normalized gravitational potential for NFW profile.

    Parameters:
    x : Radial distance normalized to NFW scale radius (r/rs)

    Returns:
    phi : Normalized gravitational potential
    """
    scalar = np.isscalar(x)
    x = np.atleast_1d(np.clip(x, 1e-10, None))
    Phi = -np.log(1. + x)/x
    if scalar: return np.asscalar(Phi)
    else: return Phi

def deriv_rhostar(x,y):
    """
    Derivative of the stellar density for Plummer profile.

    Parameters:
    x : Radial distance normalized to NFW scale radius (r/rs)
        NOTE: this is *not* normalized to the Plummer radius
    y : Ratio between plummer and NFW scale radius

    Returns:
    drho : Derivative of the stellar density
    """
    #y = rpl/r0
    return (-2.5/(1.0+ x**2/y**2)**3.5) * 2.0* x/y**2

def deriv_nfw_potential(x):
    """
    Derivative of the NFW potential with respect to x.

    Parameters:
    x : Radial distance normalized to scale radius (r/rs)

    Returns:
    dPhi : Derivative (with respect to x) of gravitational potential
    """
    scalar = np.isscalar(x)
    x = np.atleast_1d(np.where(x==0, 1e-30, x))
    dPhi = np.log(1.0 + x) / x**2 - 1.0 / (x * (1.0 + x) )
    if scalar:
        return np.asscalar(dPhi)
    else:
        return dPhi

def deriv_rhostar_deriv_Psi(x,y):
    """
    Derivative of the Plummer density with respect to the NFW potential.

    Parameters:
    x : Radial distance normalized to NFW scale radius (r/rs)
    y : Ratio between Plummer half-light radius and NFW scale radius

    Returns:
    dPhi : Derivative of the stellar density with respect to
           the gravitational potential
    """
    return deriv_rhostar(x,y)/deriv_nfw_potential(x)

def rhostar(x,y):
    """
    Physical projected (2D) Plummer stellar density.

    Parameters:
    x : Radial distance normalized to NFW scale radius (r/rs)
    y : Ratio between Plummer half-light radius and NFW scale radius

    Returns:
    rhostar : 2D projected stellar density
    """
    #y = 0.105d0/r0
    rhostar = 1.0/(1.0+ x**2/y**2)**2.0
    return rhostar

#class VelocityDistribution(object):
class VelocityDistribution(Model):
    """
    Base class for velocity distribution.
    """
    _params = odict([
        ('vmean', Parameter(0.0) ),  # km/s
        ('vdisp', Parameter(1.0) ),    # Velocity dispersion (km/s)
    ])

    def _sample(self, radius, sync=True):
        """
        Do the sampling (overloaded by subclass)
        """
        return self.vdisp * np.ones_like(radius)

    def sample(self, radius, sync=True):
        """
        Draw a random sample of velocities from the inverse CDF.

        Parameters:
        radius : 2D physical projected radius (kpc)

        Returns:
        velocity : Randomly sampled velocities for each star (km/s)
        """
        scalar = np.isscalar(radius)
        vel = self._sample(np.atleast_1d(radius),sync)
        # Don't forget the mean velocity
        vel += self.vmean
        if scalar: return np.asscalar(vel)
        return vel

    def sample_angsep(self, angsep, distance, sync=True):
        """
        Draw a random sample of velocities from the inverse CDF.

        Parameters:
        angsep   : Angular projected radius (deg)
        distance : Heliocentric distance to dwarf (kpc)

        Returns:
        velocity : Randomly sampled velocities for each star (km/s)
        """
        radius = distance * np.tan(np.radians(angsep))
        return self.sample(radius,sync)

class GaussianVelocity(VelocityDistribution):
    """
    Simple Gaussian velocity dispersion Norm(mu=0, sigma=vdisp).
    """
    _params = odict([
        ('vmean', Parameter(0.0) ),  # km/s
        ('vdisp', Parameter(3.3) ),  # Velocity dispersion (km/s)
    ])

    def _sample(self, radius, hold=False):
        return np.random.normal(0.0,self.vdisp,size=len(radius))

class PhysicalVelocity(VelocityDistribution):
    """
    Class to calculate and apply a dwarf galaxy velocity dispersion.
    """

    _params = odict([
        ('vmean', Parameter(0.0) ),   # Systemic velocity (km/s)
        ('vmax',  Parameter(10.0) ),  # Maximum circular velocity (km/s)
        ('rvmax', Parameter(0.4) ),   # Radius of maximum circular velocity (kpc)
        ('rpl',   Parameter(0.04) ),  # Projected Plummer radius (kpc)
    ])

    def _cache(self, name=None):
        velmin = getattr(self,'velmin',-1.5 * self.vmax)
        velmax = getattr(self,'velmax', 1.5 * self.vmax)
        #velmin = -1.5 * self.vmax
        #velmax = 1.5 * self.vmax
        self.velocities = np.linspace(velmin,velmax,nvs)

        xmin = getattr(self,'xmin',1e-4)
        xmax = getattr(self,'xmax',5)
        self.xvalues = np.logspace(np.log10(xmin),np.log10(xmax),nrs)

        self.build_interps()

    @classmethod
    def rs2rvmax(cls, rs):
        return 2.163 * rs

    @classmethod
    def rvmax2rs(cls, rvmax):
        return rvmax/2.163

    @classmethod
    def rhos2vmax(cls, rhos, rs):
        G = 4.302e-6 # kpc Msun^-1 (km/s)^2
        return rs  * np.sqrt( rhos * (4*np.pi*G)/4.625 )

    @classmethod
    def vmax2rhos(cls, vmax, rvmax):
        G = 4.302e-6 # kpc Msun^-1 (km/s)^2
        rs = cls.rvmax2rs(rvmax)
        return 4.625/(4*np.pi*G) * (vmax/rs)**2

    @property
    def Phis(self):
        # Central potential as in 1406.6079 in (km/s)^2
        return self.vmax**2/0.465**2

    @property
    def rs(self):
        """
        NFW scale radius (kpc) from Eqn 9 of:
        http://arxiv.org/abs/0805.4416
        """
        return self.rvmax/2.163

    @property
    def rhos(self):
        """
        NFW scale density (Msun/kpc^3) from Eqn 10 of:
        http://arxiv.org/abs/0805.4416
        """
        G = 4.302e-6 # kpc Msun^-1 (km/s)^2
        return 4.625/(4*np.pi*G) * (self.vmax/self.rs)**2

    def integrate_energy(self, energy, interp, method='simps'):
        """
        Eddington inversion formula. Corresponds to Eqn (5) of:
        https://arxiv.org/abs/1003.4268

        f(e) ~ \int^0_e d2rho/dpsi2 dpsi/sqrt(psi-e)

        Parameters:
        energy : Energies at which to evaluate f(e)
        interp : Interpolation of integrandd2rho/dpsi2
        method : Integration technique

        Returns:
        integral : array as a function of e
        """
        err = np.seterr(divide='ignore',invalid='ignore')
        method = method.lower()
        emin = np.min(energy)

        def fa(x,e):
            return np.nan_to_num(interp(x)/np.sqrt(e-x))

        if method in ['quad','romberg']:
            # Integrate from function object
            int_e = []
            for e in energy:
                a,b = emin, 0.99*e
                if method == 'quad':
                    int_eappend(quad(fa,a,b,args=(e),epsrel=1e-8)[0])
                elif method == 'romberg':
                    int_e.append(romberg(fa,a,b,args=(e,),divmax=15,tol=1e-5))
            int_e = int_e
        elif method in ['trapz','simps']:
            # Integrate from array
            nstep = 1000
            x_array = emin + (0.99*energy-emin)*np.linspace(0,1,nstep)[:,np.newaxis]
            fa_array = fa(x_array,energy)
            if method == 'trapz':
                int_e = trapz(fa_array,x_array,axis=0)
            if method == 'simps':
                int_e = simps(fa_array,x_array,axis=0)
        else:
            msg = "Unrecognized method: %s"%method
            raise Exception(msg)

        np.seterr(**err)
        return np.asarray(int_e)

    def build_interps(self):
        # calculate the derivative to the stellar density with respect
        # to the potential xmin, xmax is in units of the dark matter
        # scale radius
        xmin = 1.e-6
        xmax = 1.e2
        #xstep = np.log(xmax/xmin)/float(nx-1)
        rx = np.exp(np.linspace(np.log(xmin),np.log(xmax),nx))
        rx = np.append(0,rx)

        # Calculate the approximate derivative of the NFW potential for
        # the Eddington formula
        Phi = nfw_potential(rx)
        Psi = -Phi

        self.interp_psi = loginterp1d(rx,Psi,bounds_error=False)

        drhostardPsi = -deriv_rhostar_deriv_Psi(rx,self.rpl/self.rs)

        # get the arrays in increasing numerical order
        Psi_reverse = Psi[::-1]
        drhostardPsi_reverse = drhostardPsi[::-1]
        rx_reverse = rx[::-1]

        # Interpolate derivative as a function of potential
        self.interp_e = loginterp1d(Psi_reverse,drhostardPsi_reverse,
                                    kind='linear',bounds_error=False)

        # Define the range of (binding) energies to evaluate at
        emin = np.min(Psi_reverse)
        emax = np.max(Psi_reverse)
        #estep = np.log(emax/emin)/float(nes-1)
        #print emin, emax, estep

        # Prepend a zero, something Fortran was doing...
        #energy = emin*np.exp(np.arange(1,nes)*estep)
        energy = np.exp(np.linspace(np.log(emin),np.log(emax),nes))
        energy = np.append(0,energy)

        int_e = self.integrate_energy(energy,self.interp_e,method='simps')

        ## Prepend a zero, something Fortran was doing...
        #energy    = np.append([0],energy)
        #int_e     = np.append([0],int_e)

        self.interp_inte = interp1d(energy,int_e,kind='linear',bounds_error=False)
        #self.interp_inte = UnivariateSpline(energy,int_e,k=1,s=0)

        def fb(e, emax=emax):
            return np.where(e >= emax, 0, self.interp_inte(e))

        temp_int = derivative(fb,energy[:-2],1e-4)
        temp_int[0] = 0

        logfe_temp = np.array(temp_int)
        kcut       = ~np.isnan(logfe_temp)
        energy_cut = self.Phis*energy[kcut]
        logfe_cut  = np.array(logfe_temp)[kcut]

        # Interpolation for log(f) (?) as a function of energy
        self.energy = energy_cut
        self.interp_logfe = interp1d(energy_cut,logfe_cut,
                                     kind='linear',bounds_error=False)

        return emin,emax

    def velocity_distribution(self, radius, velocities=None):
        """
        Calculate the velocity distribution.

        Parameters:
        radius     : Projected radius (in units of the scale radius?)
        velocities : Array of velocity to evalute f(v)

        Returns:
        fv         : The velocity distribution pdf
        """

        if not np.isscalar(radius):
            msg = "'radius' must be scalar (at least for now)"
            raise Exception(msg)

        # Projected scale(?) radius
        Rp_in = radius

        # Calculate the max radius for this velocity.
        # Assume something large small error incurred but come back and refine this
        maxr = 10. # in units of the scale radius?

        # If star is outside the maximum radius
        if Rp_in > maxr:
            # warning....
            return np.zeros_like(self.velocities)

        # Define the projected LOS velocity range
        if velocities is None:
            velocities = self.velocities

        # Define the min of the variable t to evaluate at
        tmin = 1.e-4
        tmax = np.sqrt(maxr-Rp_in)
        #tstep = np.log(tmax/tmin)/float(nts-1)

        # Energy range
        emin = np.min(self.energy)
        emax = np.max(self.energy)

        # Bounds of integration
        region = [[tmin,tmax],  # t1
                  [emin,emax],  # velocity
                  [0, 2*np.pi], # eta
        ]
        self.region = region

        @vegas.batchintegrand
        def dfunc(y):
            """
            Probability density function?

            Parameters:
            y : (array-like) t1,vt,eta

            Returns:
            fe : density of f(e)
            """
            y = np.atleast_2d(y)
            t1,vt,eta = y.T

            rspline = t1**2 + Rp_in
            Psi_spline = self.interp_psi(rspline)

            sel = (emin <= vt) & (vt <= (self.Phis*Psi_spline - 0.5*v_in**2))

            val = self.interp_logfe(vt) * (t1**2+Rp_in)/np.sqrt(2*Rp_in+t1**2)
            return np.where(sel,val,0)

        # For debugging
        self.dfunc = dfunc
        # self.dfunc = lambda self,y: return dfunc(y)

        # Setup the integrator
        integrator = vegas.Integrator(region)

        fv_out = []
        for v_in in velocities:
            # burn-in
            #integrator(dfunc,nitn=5,neval=n_grid)
            integrator(dfunc,nitn=5,neval=n_grid)
            # evaluate
            result = integrator(dfunc,nitn=5,neval=n_final)
            fv_out.append(result.val)

        return np.asarray(fv_out)

    def interp_vdist(self, xproj, nsteps=nrs):
        """
        Interpolate the velocity distribution.

        Parameters:
        xproj : Projected radius in units of the scale radius
        nsteps: Number of radii for interpolated velocity distribution

        Returns:
        None : (sets interp_pdf,interp_cdf,interp_icdf)
        """
        xproj = np.atleast_1d(xproj)

        #epsilon = 1e-4
        ##xmin =max(np.min(xproj)-epsilon,0)
        ##xmax = np.max(xproj)+epsilon
        #xmin = epsilon
        #xmax = max(np.max(xproj)+epsilon,5)
        #
        ##xsteps = np.linspace(xmin,xmax,nsteps)
        #xstep = np.logspace(np.log10(xmin),np.log10(xmax),nsteps)

        xsteps = self.xvalues
        vsteps = self.velocities

        y,x = np.meshgrid(vsteps,xsteps)

        z = []
        for i,_x in enumerate(xsteps):
            #print >> sys.stderr, '(%i) x = %.3f'%(i,_x)
            logging.debug('(%i) x = %.3f'%(i,_x))
            fv = self.velocity_distribution(_x,vsteps)
            z.append(fv)

        z = np.array(z)

        # The nan_to_num is for 0/0 division (not too safe)
        u = np.nan_to_num(z/np.sum(z,axis=1)[:,np.newaxis])
        w = np.cumsum(z,axis=1)
        w = np.nan_to_num(w/np.max(w,axis=1)[:,np.newaxis])
        # Clip tiny values to avoid "Triangulation is invalid" error
        w = np.where(w < 1e-13, 0, w)

        self.x = x
        self.y = y
        self.z = z
        self.w = w

        self.pdf  = z  # PDF
        self.npdf = u  # Normalized PDF
        self.cdf  = w  # Normalized CDF

        # Velocity and velocity function
        self.v  = y
        self.fv = z

        # Triangular mesh interpolation from matplotlib
        self.interp_pdf  = triinterp2d(x,y,z)
        self.interp_cdf  = triinterp2d(x,y,w)
        self.interp_icdf = triinterp2d(x,w,y)

    def _sample(self, radius, sync=True):
        """
        Sample from the inverse CDF.
        """
        xproj = np.atleast_1d(radius)/self.rs
        if sync:
            self.interp_vdist(xproj)
        i = np.random.uniform(size=len(xproj))
        vel = self.interp_icdf(xproj,i).filled(np.nan)
        return vel

    def radial_vdisp(self):
        if not hasattr(self,'interp_pdf'):
            self.interp_vdist()

        xproj = self.x
        vel = self.y

        pdf = self.interp_pdf(xproj,vel)

        sigma_r2 = simps(vel**2 * pdf, vel, axis=1)/simps(pdf, vel, axis=1)
        return np.sqrt(sigma_r2)

    def avg_vdisp(self):
        if not hasattr(self,'interp_pdf'):
            self.interp_vdist()

        xproj = self.x[:,0]
        sigma_r = self.radial_vdisp()
        I = rhostar(xproj,y=self.rpl/self.rs)
        sigma2 = simps(I*sigma_r**2*xproj, xproj)/simps(I*xproj, xproj)

        return np.sqrt(sigma2)

Gaussian = GaussianVelocity
Physical = PhysicalVelocity

# ADW: It would be good to replace this with ugali.utils.factory;
# however, this needs to accept unused kwargs.
def factory(name, **kwargs):
    """
    """
    import inspect

    cls = name
    module = __name__
    fn = lambda member: inspect.isclass(member) and member.__module__==module
    classes = odict(inspect.getmembers(sys.modules[module], fn))
    members = odict([(k.lower(),v) for k,v in classes.items()])

    lower = cls.lower()
    if lower not in members.keys():
        msg = "Unrecognized class: %s.%s"%(module,cls)
        raise KeyError(msg)

    kw = {k:v for k,v in kwargs.items() if k in members[lower]._params.keys()}
    return members[lower](**kw)

velocityFactory = factory

if __name__ == "__main__":
    import argparse
    description = __doc__
    parser = argparse.ArgumentParser(description=description)
    args = parser.parse_args()


text:
import * as es from 'event-stream';
import * as fs from 'fs';
import * as glob from 'glob';
import * as gulp from 'gulp';
import * as path from 'path';
import { Stream } from 'stream';
import * as File from 'vinyl';
import * as vsce from 'vsce';
import { createStatsStream } from './stats';
import * as util2 from './util';
import remote = require('gulp-remote-retry-src');
const vzip = require('gulp-vinyl-zip');
import filter = require('gulp-filter');
import rename = require('gulp-rename');
import * as fancyLog from 'fancy-log';
import * as ansiColors from 'ansi-colors';
const buffer = require('gulp-buffer');
import json = require('gulp-json-editor');
const webpack = require('webpack');
const webpackGulp = require('webpack-stream');
const util = require('./util');
const root = path.dirname(path.dirname(__dirname));
const commit = util.getVersion(root);
const sourceMappingURLBase = `https://ticino.blob.core.windows.net/sourcemaps/${commit}`;

function minimizeLanguageJSON(input: Stream): Stream {
	const tmLanguageJsonFilter = filter('**/*.tmLanguage.json', { restore: true });
	return input
		.pipe(tmLanguageJsonFilter)
		.pipe(buffer())
		.pipe(es.mapSync((f: File) => {
			f.contents = Buffer.from(JSON.stringify(JSON.parse(f.contents.toString('utf8'))));
			return f;
		}))
		.pipe(tmLanguageJsonFilter.restore);
}

function updateExtensionPackageJSON(input: Stream, update: (data: any) => any): Stream {
	const packageJsonFilter = filter('extensions/*/package.json', { restore: true });
	return input
		.pipe(packageJsonFilter)
		.pipe(buffer())
		.pipe(es.mapSync((f: File) => {
			const data = JSON.parse(f.contents.toString('utf8'));
			f.contents = Buffer.from(JSON.stringify(update(data)));
			return f;
		}))
		.pipe(packageJsonFilter.restore);
}

function fromLocal(extensionPath: string, forWeb: boolean): Stream {
	const webpackConfigFileName = forWeb ? 'extension-browser.webpack.config.js' : 'extension.webpack.config.js';

	const isWebPacked = fs.existsSync(path.join(extensionPath, webpackConfigFileName));
	let input = isWebPacked
		? fromLocalWebpack(extensionPath, webpackConfigFileName)
		: fromLocalNormal(extensionPath);

	if (forWeb) {
		input = updateExtensionPackageJSON(input, (data: any) => {
			if (data.browser) {
				data.main = data.browser;
			}
			data.extensionKind = ['web'];
			return data;
		});
	} else if (isWebPacked) {
		input = updateExtensionPackageJSON(input, (data: any) => {
			if (data.main) {
				data.main = data.main.replace('/out/', /dist/);
			}
			return data;
		});
	}

	return minimizeLanguageJSON(input);
}

function fromLocalWebpack(extensionPath: string, webpackConfigFileName: string): Stream {
	const result = es.through();

	const packagedDependencies: string[] = [];
	const packageJsonConfig = require(path.join(extensionPath, 'package.json'));
	if (packageJsonConfig.dependencies) {
		const webpackRootConfig = require(path.join(extensionPath, webpackConfigFileName));
		for (const key in webpackRootConfig.externals) {
			if (key in packageJsonConfig.dependencies) {
				packagedDependencies.push(key);
			}
		}
	}

	vsce.listFiles({ cwd: extensionPath, packageManager: vsce.PackageManager.Yarn, packagedDependencies }).then(fileNames => {
		const files = fileNames
			.map(fileName => path.join(extensionPath, fileName))
			.map(filePath => new File({
				path: filePath,
				stat: fs.statSync(filePath),
				base: extensionPath,
				contents: fs.createReadStream(filePath) as any
			}));

		// check for a webpack configuration files, then invoke webpack
		// and merge its output with the files stream.
		const webpackConfigLocations = (<string[]>glob.sync(
			path.join(extensionPath, '**', webpackConfigFileName),
			{ ignore: ['**/node_modules'] }
		));

		const webpackStreams = webpackConfigLocations.map(webpackConfigPath => {

			const webpackDone = (err: any, stats: any) => {
				fancyLog(`Bundled extension: ${ansiColors.yellow(path.join(path.basename(extensionPath), path.relative(extensionPath, webpackConfigPath)))}...`);
				if (err) {
					result.emit('error', err);
				}
				const { compilation } = stats;
				if (compilation.errors.length > 0) {
					result.emit('error', compilation.errors.join('\n'));
				}
				if (compilation.warnings.length > 0) {
					result.emit('error', compilation.warnings.join('\n'));
				}
			};

			const webpackConfig = {
				...require(webpackConfigPath),
				...{ mode: 'production' }
			};
			const relativeOutputPath = path.relative(extensionPath, webpackConfig.output.path);

			return webpackGulp(webpackConfig, webpack, webpackDone)
				.pipe(es.through(function (data) {
					data.stat = data.stat || {};
					data.base = extensionPath;
					this.emit('data', data);
				}))
				.pipe(es.through(function (data: File) {
					// source map handling:
					// * rewrite sourceMappingURL
					// * save to disk so that upload-task picks this up
					const contents = (<Buffer>data.contents).toString('utf8');
					data.contents = Buffer.from(contents.replace(/\n\/\/# sourceMappingURL=(.*)$/gm, function (_m, g1) {
						return `\n//# sourceMappingURL=${sourceMappingURLBase}/extensions/${path.basename(extensionPath)}/${relativeOutputPath}/${g1}`;
					}), 'utf8');

					this.emit('data', data);
				}));
		});

		es.merge(...webpackStreams, es.readArray(files))
			// .pipe(es.through(function (data) {
			// 	// debug
			// 	console.log('out', data.path, data.contents.length);
			// 	this.emit('data', data);
			// }))
			.pipe(result);

	}).catch(err => {
		console.error(extensionPath);
		console.error(packagedDependencies);
		result.emit('error', err);
	});

	return result.pipe(createStatsStream(path.basename(extensionPath)));
}

function fromLocalNormal(extensionPath: string): Stream {
	const result = es.through();

	vsce.listFiles({ cwd: extensionPath, packageManager: vsce.PackageManager.Yarn })
		.then(fileNames => {
			const files = fileNames
				.map(fileName => path.join(extensionPath, fileName))
				.map(filePath => new File({
					path: filePath,
					stat: fs.statSync(filePath),
					base: extensionPath,
					contents: fs.createReadStream(filePath) as any
				}));

			es.readArray(files).pipe(result);
		})
		.catch(err => result.emit('error', err));

	return result.pipe(createStatsStream(path.basename(extensionPath)));
}

const baseHeaders = {
	'X-Market-Client-Id': 'VSCode Build',
	'User-Agent': 'VSCode Build',
	'X-Market-User-Id': '291C1CD0-051A-4123-9B4B-30D60EF52EE2',
};

export function fromMarketplace(extensionName: string, version: string, metadata: any): Stream {
	const [publisher, name] = extensionName.split('.');
	const url = `https://marketplace.visualstudio.com/_apis/public/gallery/publishers/${publisher}/vsextensions/${name}/${version}/vspackage`;

	fancyLog('Downloading extension:', ansiColors.yellow(`${extensionName}@${version}`), '...');

	const options = {
		base: url,
		requestOptions: {
			gzip: true,
			headers: baseHeaders
		}
	};

	const packageJsonFilter = filter('package.json', { restore: true });

	return remote('', options)
		.pipe(vzip.src())
		.pipe(filter('extension/**'))
		.pipe(rename(p => p.dirname = p.dirname!.replace(/^extension\/?/, '')))
		.pipe(packageJsonFilter)
		.pipe(buffer())
		.pipe(json({ __metadata: metadata }))
		.pipe(packageJsonFilter.restore);
}

const excludedExtensions = [
	'vscode-api-tests',
	'vscode-web-playground',
	'vscode-colorize-tests',
	'vscode-test-resolver',
	'ms-vscode.node-debug',
	'ms-vscode.node-debug2',
	'vscode-notebook-tests'
];

interface IBuiltInExtension {
	name: string;
	version: string;
	repo: string;
	metadata: any;
}

const builtInExtensions: IBuiltInExtension[] = JSON.parse(fs.readFileSync(path.join(__dirname, '../../product.json'), 'utf8')).builtInExtensions;

export function packageLocalExtensionsStream(): NodeJS.ReadWriteStream {
	const localExtensionDescriptions = (<string[]>glob.sync('extensions/*/package.json'))
		.map(manifestPath => {
			const extensionPath = path.dirname(path.join(root, manifestPath));
			const extensionName = path.basename(extensionPath);
			return { name: extensionName, path: extensionPath };
		})
		.filter(({ name }) => excludedExtensions.indexOf(name) === -1)
		.filter(({ name }) => builtInExtensions.every(b => b.name !== name));

	const localExtensions = localExtensionDescriptions.map(extension => {
		return fromLocal(extension.path, false)
			.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
	});

	const nodeModules = gulp.src('extensions/node_modules/**', { base: '.' });
	return es.merge(nodeModules, ...localExtensions)
		.pipe(util2.setExecutableBit(['**/*.sh']));
}

export function packageLocalWebExtensionsStream(): NodeJS.ReadWriteStream {
	const localExtensionDescriptions = (<string[]>glob.sync('extensions/*/package.json'))
		.filter(manifestPath => {
			const packageJsonConfig = require(path.join(root, manifestPath));
			return !packageJsonConfig.main || packageJsonConfig.browser;
		})
		.map(manifestPath => {
			const extensionPath = path.dirname(path.join(root, manifestPath));
			const extensionName = path.basename(extensionPath);
			return { name: extensionName, path: extensionPath };
		});

	return es.merge(...localExtensionDescriptions.map(extension => {
		return fromLocal(extension.path, true)
			.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
	}));
}

export function packageMarketplaceExtensionsStream(): NodeJS.ReadWriteStream {
	const extensions = builtInExtensions.map(extension => {
		return fromMarketplace(extension.name, extension.version, extension.metadata)
			.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
	});

	return es.merge(extensions)
		.pipe(util2.setExecutableBit(['**/*.sh']));
}

export function packageMarketplaceWebExtensionsStream(builtInExtensions: IBuiltInExtension[]): NodeJS.ReadWriteStream {
	const extensions = builtInExtensions
		.map(extension => {
			const input = fromMarketplace(extension.name, extension.version, extension.metadata)
				.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
			return updateExtensionPackageJSON(input, (data: any) => {
				if (data.main) {
					data.browser = data.main;
				}
				data.extensionKind = ['web'];
				return data;
			});
		});
	return es.merge(extensions);
}

export interface IScannedBuiltinExtension {
	extensionPath: string,
	packageJSON: any,
	packageNLSPath?: string,
	readmePath?: string,
	changelogPath?: string,
}

export function scanBuiltinExtensions(extensionsRoot: string, forWeb: boolean): IScannedBuiltinExtension[] {
	const scannedExtensions: IScannedBuiltinExtension[] = [];
	const extensionsFolders = fs.readdirSync(extensionsRoot);
	for (const extensionFolder of extensionsFolders) {
		const packageJSONPath = path.join(extensionsRoot, extensionFolder, 'package.json');
		if (!fs.existsSync(packageJSONPath)) {
			continue;
		}
		let packageJSON = JSON.parse(fs.readFileSync(packageJSONPath).toString('utf8'));
		const extensionKind: string[] = packageJSON['extensionKind'] || [];
		if (forWeb && extensionKind.indexOf('web') === -1) {
			continue;
		}
		const children = fs.readdirSync(path.join(extensionsRoot, extensionFolder));
		const packageNLS = children.filter(child => child === 'package.nls.json')[0];
		const readme = children.filter(child => /^readme(\.txt|\.md|)$/i.test(child))[0];
		const changelog = children.filter(child => /^changelog(\.txt|\.md|)$/i.test(child))[0];

		if (packageNLS) {
			// temporary
			packageJSON = translatePackageJSON(packageJSON, path.join(extensionsRoot, extensionFolder, packageNLS))
		}
		scannedExtensions.push({
			extensionPath: extensionFolder,
			packageJSON,
			packageNLSPath: packageNLS ? path.join(extensionFolder, packageNLS) : undefined,
			readmePath: readme ? path.join(extensionFolder, readme) : undefined,
			changelogPath: changelog ? path.join(extensionFolder, changelog) : undefined,
		});
	}
	return scannedExtensions;
}

export function translatePackageJSON(packageJSON: string, packageNLSPath: string) {
	const CharCode_PC = '%'.charCodeAt(0);
	const packageNls = JSON.parse(fs.readFileSync(packageNLSPath).toString());
	const translate = (obj: any) => {
		for (let key in obj) {
			const val = obj[key];
			if (Array.isArray(val)) {
				val.forEach(translate);
			} else if (val && typeof val === 'object') {
				translate(val);
			} else if (typeof val === 'string' && val.charCodeAt(0) === CharCode_PC && val.charCodeAt(val.length - 1) === CharCode_PC) {
				const translated = packageNls[val.substr(1, val.length - 2)];
				if (translated) {
					obj[key] = translated;
				}
			}
		}
	};
	translate(packageJSON);
	return packageJSON;
}


text:
<sect1 id="zend.feed.findFeeds">
    <title>Individuazione e recupero di feed da pagine web</title>
    <para>
        Le pagine web spesso contengono un tag <link> con riferimento ai feed con contenuti rilevanti per quella particolare pagina.
        Zend_Feed consente di recuperare tutti i feed indicati in una pagina web chiamando semplicemente un metodo:
    </para>
    <programlisting role="php"><![CDATA[<?php
$feedArray = Zend_Feed::findFeeds('http://www.example.com/news.html');]]>
    </programlisting>
    <para>
        Il metodo findFeeds() restituisce un array di oggetti Zend_Feed_Abstract indicati dai tag <link> contenuti nella pagina web news.html.
        A seconda del tipo di feed, ogni elemento di $feedArray potrà essere una istanza di Zend_Feed_Rss o Zend_Feed_Atom.
        Zend_Feed genera un'eccezione Zend_Feed_Exception in caso di errore, come ad esempio un codice di risposta HTTP 404 o un feed non valido.
    </para>
</sect1>


text:
package chapter2.item2.javabeans;

/**
 * Created by wuxinshui on 2017/2/7.
 */
//JavaBeans pattern - allows inconsistency,mandates mutability
public class NutritionFacts {
	//Parameters initialized to default values (if any)
	private  int servingSize=-1; //(ml)		required
	private  int servings=-1;//(per container) required
	private  int calories=0;//				optional
	private  int fat=0;//(g)					optional
	private  int sodium=0;//(mg)				optional
	private  int carbohydrate=0;//(g)		optional

	public NutritionFacts() {
	}

	public void setServingSize(int servingSize) {
		this.servingSize = servingSize;
	}

	public void setServings(int servings) {
		this.servings = servings;
	}

	public void setCalories(int calories) {
		this.calories = calories;
	}

	public void setFat(int fat) {
		this.fat = fat;
	}

	public void setSodium(int sodium) {
		this.sodium = sodium;
	}

	public void setCarbohydrate(int carbohydrate) {
		this.carbohydrate = carbohydrate;
	}

	@Override
	public String toString() {
		return "NutritionFacts{" +
				"servingSize=" + servingSize +
				", servings=" + servings +
				", calories=" + calories +
				", fat=" + fat +
				", sodium=" + sodium +
				", carbohydrate=" + carbohydrate +
				'}';
	}
}


text:
package ontopoly.components;

import java.util.List;

import ontopoly.model.Topic;
import ontopoly.utils.TopicChoiceRenderer;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;

public class TopicDropDownChoice<T extends Topic> extends DropDownChoice<T> {

  public TopicDropDownChoice(String id, IModel<T> model, IModel<List<T>> choices) {
    super(id, model, choices, new TopicChoiceRenderer<T>());
  }

  public TopicDropDownChoice(String id, IModel<T> model, IModel<List<T>> choices, IChoiceRenderer<T> renderer) {
    super(id, model, choices, renderer);
  }

  @Override
  protected void onComponentTag(ComponentTag tag) {
    tag.setName("select");
    super.onComponentTag(tag);
  }

}


text:
class AddIncompleteTicketInfoToEvent < ActiveRecord::Migration
  def change
    add_column :events, :incomplete_ticket_info, :boolean
  end
end


text:
package me.chanjar.weixin.cp.api.impl;

import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import static org.testng.Assert.assertNotNull;

/**
 *

 *
 * Created by Binary Wang on 2017-6-25.
 * @author  Binary Wang

 */
@Guice(modules = ApiTestModule.class)
public class WxCpMenuServiceImplTest {
  /**
   * The Wx service.
   */
  @Inject
  protected WxCpService wxService;

  /**
   * Menu data object [ ] [ ].
   *
   * @return the object [ ] [ ]
   */
  @DataProvider
  public Object[][] menuData() {
    WxMenu menu = new WxMenu();
    WxMenuButton button1 = new WxMenuButton();
    button1.setType(WxConsts.MenuButtonType.CLICK);
    button1.setName("今日歌曲");
    button1.setKey("V1001_TODAY_MUSIC");

    WxMenuButton button2 = new WxMenuButton();
    button2.setType(WxConsts.MenuButtonType.CLICK);
    button2.setName("歌手简介");
    button2.setKey("V1001_TODAY_SINGER");

    WxMenuButton button3 = new WxMenuButton();
    button3.setName("菜单");

    menu.getButtons().add(button1);
    menu.getButtons().add(button2);
    menu.getButtons().add(button3);

    WxMenuButton button31 = new WxMenuButton();
    button31.setType(WxConsts.MenuButtonType.VIEW);
    button31.setName("搜索");
    button31.setUrl("http://www.soso.com/");

    WxMenuButton button32 = new WxMenuButton();
    button32.setType(WxConsts.MenuButtonType.VIEW);
    button32.setName("视频");
    button32.setUrl("http://v.qq.com/");

    WxMenuButton button33 = new WxMenuButton();
    button33.setType(WxConsts.MenuButtonType.CLICK);
    button33.setName("赞一下我们");
    button33.setKey("V1001_GOOD");

    button3.getSubButtons().add(button31);
    button3.getSubButtons().add(button32);
    button3.getSubButtons().add(button33);

    return new Object[][]{
      new Object[]{
        menu
      }
    };

  }

  /**
   * Test create.
   *
   * @param wxMenu the wx menu
   * @throws Exception the exception
   */
  @Test(dataProvider = "menuData")
  public void testCreate(WxMenu wxMenu) throws Exception {
    this.wxService.getMenuService().create(wxMenu);
  }

  /**
   * Test get.
   *
   * @throws Exception the exception
   */
  @Test(dependsOnMethods = "testCreate")
  public void testGet() throws Exception {
    WxMenu menu = this.wxService.getMenuService().get();
    assertNotNull(menu);
    System.out.println(menu.toJson());
  }

  /**
   * Test delete.
   *
   * @throws Exception the exception
   */
  @Test(dependsOnMethods = {"testGet", "testCreate"})
  public void testDelete() throws Exception {
    this.wxService.getMenuService().delete();
  }

}


text:
function test(expdef)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

addSignalsJava();

persistent defdir lastParams;

if isempty(defdir)
  defdir = '\\zserver\code\Rigging\ExpDefinitions';
end

if isempty(lastParams)
  lastParams = containers.Map('KeyType', 'char', 'ValueType', 'any');
end

if nargin < 1
  [mfile, mpath] = uigetfile(...
    '*.m', 'Select the experiment definition function', defdir);
  if mfile == 0
    return
  end
  defdir = mpath;
  [~, expdefname] = fileparts(mfile);
  expdef = fileFunction(mpath, mfile);
else
  expdefname = func2str(expdef);
end

parsStruct = exp.inferParameters(expdef);
parsStruct = rmfield(parsStruct, 'defFunction');
parsStruct.numRepeats = 100;

%% boring UI stuff
parsWindow = figure('Name', sprintf('%s', expdefname),...
  'NumberTitle', 'off', 'Toolbar', 'none', 'Menubar', 'none',...
  'Position', [30 100 1600 580]);
mainsplit = uiextras.HBox('Parent', parsWindow);
leftbox = uiextras.VBox('Parent', mainsplit);

parsEditor = eui.ParamEditor(exp.Parameters(parsStruct), leftbox);
ctrlgrid = uiextras.Grid('Parent', leftbox);
uicontrol('Parent', ctrlgrid, 'Style', 'pushbutton',...
  'String', 'Apply parameters', 'Callback', @applyPars);
uicontrol('Parent', ctrlgrid, 'Style', 'text',...
  'String', 'Trial');
uicontrol('Parent', ctrlgrid, 'Style', 'text',...
  'String', 'Reward delivered');
uicontrol('Parent', ctrlgrid, 'Style', 'text',...
  'String', 'Wheel Position');

uicontrol('Parent', ctrlgrid, 'Style', 'pushbutton',...
  'String', 'Start experiment', 'Callback', @startExp);
trialNumCtrl = uicontrol('Parent', ctrlgrid, 'Style', 'text',...
  'String', '0');
rewardCtrl = uicontrol('Parent', ctrlgrid, 'Style', 'text',...
  'String', '0');
wheelslider = uicontrol('Parent', ctrlgrid, 'Style', 'slider',...
  'Callback', @wheelSliderChanged, 'Min', -50, 'Max', 50, 'Value', 0);

ctrlgrid.ColumnSizes = [-1 -1];
ctrlgrid.RowSizes = [30 20*ones(1, 3)];

leftbox.Sizes = [-1 100];
% leftbox.Sizes = [-1 30 25];
% parslist = addlistener(parsEditor, 'Changed', @appl);
%% experiment framework
[t, setElems] = sig.playground(expdefname, mainsplit);
mainsplit.Sizes = [700 -1];
net = t.Node.Net;
% inputs & outputs
inputs = sig.Registry;
inputs.wheel = net.origin('wheel');
outputs = sig.Registry;
% video and audio registries
vs = StructRef;
audio = audstream.Registry(192e3);
% events registry
evts = sig.Registry;
evts.expStart = net.origin('expStart');
evts.newTrial = net.origin('newTrial');
evts.trialNum = evts.newTrial.scan(@plus, 0); % track trial number
advanceTrial = net.origin('advanceTrial');
% parameters
globalPars = net.origin('globalPars');
allCondPars = net.origin('condPars');

[pars, hasNext, repeatNum] = exp.trialConditions(...
  globalPars, allCondPars, advanceTrial);
expdef(t, evts, pars, vs, inputs, outputs, audio);

setCtrlStr = @(h)@(v)set(h, 'String', toStr(v));
listeners = [
  evts.expStart.into(advanceTrial) %expStart signals advance
  evts.endTrial.into(advanceTrial) %endTrial signals advance
  advanceTrial.map(true).keepWhen(hasNext).into(evts.newTrial) %newTrial if more
  evts.trialNum.onValue(setCtrlStr(trialNumCtrl))
  ];

if isfield(outputs, 'reward')
  listeners = [listeners
    outputs.reward.scan(@plus, 0).onValue(setCtrlStr(rewardCtrl))];
end

% plotting the signals
sigsFig = figure;
tmr = timer('ExecutionMode', 'fixedSpacing', 'Period', 100e-3,...
    'TimerFcn', @(~, ~)plotSignals(sigsFig, evts));
 start(tmr);
set(sigsFig, 'CloseRequestFcn', @(s,c)stopAndClose(s,c,tmr));

  function applyPars(~,~)
    setElems(vs);
    [~, gpars, cpars] = toConditionServer(parsEditor.Parameters);
    globalPars.post(gpars);
    allCondPars.post(cpars);
    disp('pars applied');
  end

  function startExp(~,~)
    applyPars();
    evts.expStart.post(true);
    inputs.wheel.post(get(wheelslider, 'Value'));
  end

  function wheelSliderChanged(src, ~)
    pos = get(src, 'Value');
    set(src, 'Min', pos - 50, 'Max', pos + 50);
    inputs.wheel.post(get(src, 'Value'));
  end

    function stopAndClose(~,~,tmr)
        stop(tmr);
        delete(tmr);
        delete(gcf);
    end

end


text:
// Copyright (c) 2008-2009 by Kris Maglione <maglione.k at Gmail>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the License.txt file included with this file.

/** @scope modules */

/**
 * Cached XPCOM services and classes.
 *
 * @constructor
 */
const Services = Module("services", {
    init: function () {
        this.classes = {};
        this.jsm = window.Services;
        this.services = {
            "autoCompleteSearch": {
                class_: "@mozilla.org/autocomplete/search;1?name=history",
                iface:  Ci.nsIAutoCompleteSearch
            },
            "bookmarks": {
                class_: "@mozilla.org/browser/nav-bookmarks-service;1",
                iface:  Ci.nsINavBookmarksService
            },
            "liberator:": {
                class_: "@mozilla.org/network/protocol;1?name=liberator"
            },
            "debugger": {
                class_: "@mozilla.org/js/jsd/debugger-service;1",
                iface:  Ci.jsdIDebuggerService
            },
            "environment": {
                class_: "@mozilla.org/process/environment;1",
                iface:  Ci.nsIEnvironment
            },
            "favicon": {
                class_: "@mozilla.org/browser/favicon-service;1",
                iface:  Ci.nsIFaviconService
            },
            "history": {
                class_: "@mozilla.org/browser/nav-history-service;1",
                iface:  [Ci.nsINavHistoryService, Ci.nsIBrowserHistory]
            },
            "privateBrowsing": {
                class_: "@mozilla.org/privatebrowsing;1",
                iface:  Ci.nsIPrivateBrowsingService
            },
            "profile": {
                class_: "@mozilla.org/toolkit/profile-service;1",
                iface:  Ci.nsIToolkitProfileService
            },
            "rdf": {
                class_: "@mozilla.org/rdf/rdf-service;1",
                iface:  Ci.nsIRDFService
            },
            "sessionStore": {
                class_: "@mozilla.org/browser/sessionstore;1",
                iface:  Ci.nsISessionStore
            },
            "threadManager": {
                class_: "@mozilla.org/thread-manager;1",
                iface:  Ci.nsIThreadManager
            },
            "UUID": {
                class_: "@mozilla.org/uuid-generator;1",
                iface:  Ci.nsIUUIDGenerator
            },
            "textToSubURI": {
                class_: "@mozilla.org/intl/texttosuburi;1",
                iface: Ci.nsITextToSubURI
            },
        };

        this.addClass("file",       "@mozilla.org/file/local;1",                 Ci.nsILocalFile);
        this.addClass("file:",      "@mozilla.org/network/protocol;1?name=file", Ci.nsIFileProtocolHandler);
        this.addClass("find",       "@mozilla.org/embedcomp/rangefind;1",        Ci.nsIFind);
        this.addClass("process",    "@mozilla.org/process/util;1",               Ci.nsIProcess);
        this.addClass("timer",     "@mozilla.org/timer;1",                      Ci.nsITimer);
    },

    _create: function (classes, ifaces, meth) {
        try {
            let res = Cc[classes][meth || "getService"]();
            if (!ifaces)
                return res.wrappedJSObject;
            ifaces = Array.concat(ifaces);
            ifaces.forEach(function (iface) res.QueryInterface(iface));
            return res;
        }
        catch (e) {
            // liberator.log() is not defined at this time, so just dump any error
            dump("Service creation failed for '" + classes + "': " + e + "\n");
            return null;
        }
    },

    /**
     * Adds a new XPCOM service to the cache.
     *
     * @param {string} name The service's cache key.
     * @param {string} class The class's contract ID.
     * @param {nsISupports|nsISupports[]} ifaces The interface or array of
     *     interfaces implemented by this service.
     * @param {string} meth The name of the function used to instanciate
     *     the service.
     */
    add: function (name, class_, ifaces, meth) {
        this.services[name] = {"class_": class_, "iface": ifaces, "meth": meth};
    },

    /**
     * Adds a new XPCOM class to the cache.
     *
     * @param {string} name The class's cache key.
     * @param {string} class The class's contract ID.
     * @param {nsISupports|nsISupports[]} ifaces The interface or array of
     *     interfaces implemented by this class.
     */
    addClass: function (name, class_, ifaces) {
        const self = this;
        return this.classes[name] = function () self._create(class_, ifaces, "createInstance");
    },

    /**
     * Returns the cached service with the specified name.
     *
     * @param {string} name The service's cache key.
     */
    get: function (name) {
        if (this.jsm.hasOwnProperty(name))
            return this.jsm[name];

        if (!this.services.hasOwnProperty(name))
            throw Error("Could not get service: " + name);

        if (!this.services[name]["reference"]) {
            var currentService = this.services[name];

            this.services[name]["reference"] = this._create(currentService["class_"], currentService["iface"], currentService["meth"]);
        }

        return this.services[name]["reference"];
    },

    /**
     * Returns a new instance of the cached class with the specified name.
     *
     * @param {string} name The class's cache key.
     */
    create: function (name) this.classes[name]()
},
window.Services,
{
    completion: function () {
        JavaScript.setCompleter(this.get, [
            function () Object.keys(services.jsm).concat(Object.keys(services.services)).map(function(key) [key, ""])
        ]);
        JavaScript.setCompleter(this.create, [function () [[c, ""] for (c in services.classes)]]);

    }
});

// vim: set fdm=marker sw=4 ts=4 et:


text:
package com.github.esfbench.ashley;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.PooledEngine;
import com.github.esfbench.JmhSettings;
import com.github.esfbench.ashley.component.PlainPosition;
import com.github.esfbench.ashley.component.PlainStructComponentA;
import com.github.esfbench.ashley.system.PlainPositionSystem;
import com.github.esfbench.ashley.system.PlainPositionSystem2;
import com.github.esfbench.ashley.system.PlainPositionSystem3;
import com.github.esfbench.ashley.system.PooledEntityDeleterSystem;

public class PooledComponentBenchmark extends JmhSettings {

	private Engine engine;

//	@Setup
	public void init() {
		engine = new PooledEngine();
		engine.addSystem(new PlainPositionSystem());
		engine.addSystem(new PlainPositionSystem2());
		engine.addSystem(new PlainPositionSystem3());
		engine.addSystem(new PooledEntityDeleterSystem(JmhSettings.SEED, entityCount, PlainPosition.class, PlainStructComponentA.class));
	}

//	@Benchmark
	public void pooled() {
		engine.update(0);
	}

	public static void main(String[] args) throws Exception {
		new Runner(
			new OptionsBuilder()
				.include(".*pooled.*")
				.param("entityCount", "1024", "4096")
				.build())
		.run();
	}
}


text:
class CreatePetitionMailshot < ActiveRecord::Migration[6.1]
  def change
    create_table :archived_petition_mailshots, id: :serial do |t|
      t.belongs_to :petition, index: true, foreign_key: { to_table: :archived_petitions }
      t.string "subject", null: false
      t.text "body"
      t.string "sent_by"
      t.timestamps
    end

    create_table :petition_mailshots, id: :serial do |t|
      t.belongs_to :petition, index: true, foreign_key: { to_table: :petitions }
      t.string "subject", null: false
      t.text "body"
      t.string "sent_by"
      t.timestamps
    end
  end
end


text:
class LazyBackgroundPageApiTest : public ExtensionApiTest {
public:
  void SetUpCommandLine(CommandLine* command_line) {
    ExtensionApiTest::SetUpCommandLine(command_line);
    command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
  }
};

IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest, BrowserActionCreateTab) {
  FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
      AppendASCII("browser_action_create_tab");
  ASSERT_TRUE(LoadExtension(extdir));

  // Lazy Background Page doesn't exist yet.
  ExtensionProcessManager* pm =
      browser()->profile()->GetExtensionProcessManager();
  EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
  int num_tabs_before = browser()->tab_count();

  // Observe background page being created and closed after
  // the browser action is clicked.
  ui_test_utils::WindowedNotificationObserver bg_pg_created(
      chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
      content::NotificationService::AllSources());
  ui_test_utils::WindowedNotificationObserver bg_pg_closed(
      chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
      content::NotificationService::AllSources());
  BrowserActionTestUtil(browser()).Press(0);
  bg_pg_created.Wait();
  bg_pg_closed.Wait();

  // Background page created a new tab before it closed.
  EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
  EXPECT_EQ(num_tabs_before + 1, browser()->tab_count());
  EXPECT_EQ("chrome://extensions/",
            browser()->GetSelectedWebContents()->GetURL().spec());
}

IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest,
                       BrowserActionCreateTabAfterCallback) {
  FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
      AppendASCII("browser_action_with_callback");
  ASSERT_TRUE(LoadExtension(extdir));

  // Lazy Background Page doesn't exist yet.
  ExtensionProcessManager* pm =
      browser()->profile()->GetExtensionProcessManager();
  EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
  int num_tabs_before = browser()->tab_count();

  // Observe background page being created and closed after
  // the browser action is clicked.
  ui_test_utils::WindowedNotificationObserver bg_pg_created(
      chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
      content::NotificationService::AllSources());
  ui_test_utils::WindowedNotificationObserver bg_pg_closed(
      chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
      content::NotificationService::AllSources());
  BrowserActionTestUtil(browser()).Press(0);
  bg_pg_created.Wait();
  bg_pg_closed.Wait();

  // Background page is closed after creating a new tab.
  EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
  EXPECT_EQ(num_tabs_before + 1, browser()->tab_count());
}

IN_PROC_BROWSER_TEST_F(LazyBackgroundPageApiTest,
                       BroadcastEvent) {
  FilePath extdir = test_data_dir_.AppendASCII("lazy_background_page").
      AppendASCII("broadcast_event");
  ASSERT_TRUE(LoadExtension(extdir));

  // Lazy Background Page doesn't exist yet.
  ExtensionProcessManager* pm =
      browser()->profile()->GetExtensionProcessManager();
  EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
  int num_page_actions = browser()->window()->GetLocationBar()->
      GetLocationBarForTesting()->PageActionVisibleCount();

  // Open a tab to a URL that will trigger the page action to show.
  // stegosaurus.html doesn't actually exist but that doesn't seem to matter.
  GURL stego_url = GURL("stegosaurus.html");
  ui_test_utils::NavigateToURL(browser(), stego_url);

  // New page action is never shown because background page is never created.
  // TODO(tessamac): Implement! Broadcast events (like tab updates) should
  //                 cause lazy background pages to be created.
  EXPECT_FALSE(pm->GetBackgroundHostForExtension(last_loaded_extension_id_));
  EXPECT_EQ(num_page_actions,   // should be + 1
            browser()->window()->GetLocationBar()->
            GetLocationBarForTesting()->PageActionVisibleCount());
}

// TODO: background page with timer.
// TODO: background page that interacts with popup.
// TODO: background page with menu.


text:
<!doctype html>

	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>NYMSA | Porteria</title>
	<link rel="stylesheet" href="../css/foundation.css" />
	<link rel="stylesheet" href="../css/style.css" />

	<?php
	include ('../html/base/header.php');
	include('./config/db.php');
	$conn=get_db_conn();
	$sql="SELECT estilo from producto";
	$consulta=mysql_query($sql,$conn);
	$sql1="SELECT nombre from color";
	$consulta1=mysql_query($sql1,$conn);
	$sql2="SELECT talla from talla";
	$consulta2=mysql_query($sql2,$conn);
	?>

Salida de cambios

						<form method="post" action="insercion2.php">

								Factura: <input title="Ingrese una Factura Valida" type="number" name="Facturas" min="1" value="1" placeholder="Busqueda de Facturas" required/>
								Producto: <select name="Producto">
								<?php while($row = mysql_fetch_assoc($consulta)){
									?><option><?php echo $row['estilo'];
								}?>
							</option>
						</select>

						Color: <select name="color">
						<?php while($row = mysql_fetch_assoc($consulta1)){
							?><option><?php echo $row['nombre'];
						}?>
					</option>
				</select>Talla: <select name="talla">
				<?php while($row = mysql_fetch_assoc($consulta2)){
					?><option><?php echo $row['talla'];
				}?>
			</option>
		</select>

<input class="button expand" type="submit" value="Aceptar">

Cancelar
			</form>

	<?php
	include ('../html/base/footer.php')
	?>


text:
title: "All flipped up"
excerpt: "PaperFaces portrait of @nikkiccross drawn with Paper by 53 on an iPad."
image:
  feature: paperfaces-nikkiccross-twitter-lg.jpg
  thumb: paperfaces-nikkiccross-twitter-150.jpg
tags: [portrait, illustration, Paper by 53]
---

PaperFaces portrait of [@nikkiccross](http://twitter.com/nikkiccross).

{% include boilerplate/paperfaces-2.html %}


text:
require 'helper'

class TestChronic < Test::Unit::TestCase

  def setup
    # Wed Aug 16 14:00:00 UTC 2006
    @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
  end

  def test_pre_normalize_numerized_string
    string = 'two and a half years'
    assert_equal Chronic::Numerizer.numerize(string), Chronic.pre_normalize(string)
  end

  def test_post_normalize_am_pm_aliases
    # affect wanted patterns

    tokens = [Chronic::Token.new("5:00"), Chronic::Token.new("morning")]
    tokens[0].tag(Chronic::RepeaterTime.new("5:00"))
    tokens[1].tag(Chronic::RepeaterDayPortion.new(:morning))

    assert_equal :morning, tokens[1].tags[0].type

    tokens = Chronic::Handlers.dealias_and_disambiguate_times(tokens, {})

    assert_equal :am, tokens[1].tags[0].type
    assert_equal 2, tokens.size

    # don't affect unwanted patterns

    tokens = [Chronic::Token.new("friday"), Chronic::Token.new("morning")]
    tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
    tokens[1].tag(Chronic::RepeaterDayPortion.new(:morning))

    assert_equal :morning, tokens[1].tags[0].type

    tokens = Chronic::Handlers.dealias_and_disambiguate_times(tokens, {})

    assert_equal :morning, tokens[1].tags[0].type
    assert_equal 2, tokens.size
  end

  def test_guess
    span = Chronic::Span.new(Time.local(2006, 8, 16, 0), Time.local(2006, 8, 17, 0))
    assert_equal Time.local(2006, 8, 16, 12), Chronic.guess(span)

    span = Chronic::Span.new(Time.local(2006, 8, 16, 0), Time.local(2006, 8, 17, 0, 0, 1))
    assert_equal Time.local(2006, 8, 16, 12), Chronic.guess(span)

    span = Chronic::Span.new(Time.local(2006, 11), Time.local(2006, 12))
    assert_equal Time.local(2006, 11, 16), Chronic.guess(span)
  end

  def test_now
    Chronic.parse('now', :now => Time.local(2006, 01))
    assert_equal Time.local(2006, 01), Chronic.now

    Chronic.parse('now', :now => Time.local(2007, 01))
    assert_equal Time.local(2007, 01), Chronic.now
  end

  def test_endian_definitions
    # middle, little
    endians = [
      Chronic::Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
      Chronic::Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
    ]

    assert_equal endians, Chronic.definitions[:endian]

    defs = Chronic.definitions(:endian_precedence => :little)
    assert_equal endians.reverse, defs[:endian]

    defs = Chronic.definitions(:endian_precedence => [:little, :middle])
    assert_equal endians.reverse, defs[:endian]

    assert_raises(ArgumentError) do
      Chronic.definitions(:endian_precedence => :invalid)
    end
  end

  def test_passing_options
    assert_raises(ArgumentError) do
      Chronic.parse('now', :invalid => :option)
    end

    assert_raises(ArgumentError) do
      Chronic.parse('now', :context => :invalid_context)
    end
  end

  def test_debug
    require 'stringio'
    $stdout = StringIO.new
    Chronic.debug = true

    Chronic.parse 'now'
    assert $stdout.string.include?('this(grabber-this)')
  ensure
    $stdout = STDOUT
    Chronic.debug = false
  end

  # Chronic.construct

  def test_normal
    assert_equal Time.local(2006, 1, 2, 0, 0, 0), Chronic.construct(2006, 1, 2, 0, 0, 0)
    assert_equal Time.local(2006, 1, 2, 3, 0, 0), Chronic.construct(2006, 1, 2, 3, 0, 0)
    assert_equal Time.local(2006, 1, 2, 3, 4, 0), Chronic.construct(2006, 1, 2, 3, 4, 0)
    assert_equal Time.local(2006, 1, 2, 3, 4, 5), Chronic.construct(2006, 1, 2, 3, 4, 5)
  end

  def test_second_overflow
    assert_equal Time.local(2006, 1, 1, 0, 1, 30), Chronic.construct(2006, 1, 1, 0, 0, 90)
    assert_equal Time.local(2006, 1, 1, 0, 5, 0), Chronic.construct(2006, 1, 1, 0, 0, 300)
  end

  def test_minute_overflow
    assert_equal Time.local(2006, 1, 1, 1, 30), Chronic.construct(2006, 1, 1, 0, 90)
    assert_equal Time.local(2006, 1, 1, 5), Chronic.construct(2006, 1, 1, 0, 300)
  end

  def test_hour_overflow
    assert_equal Time.local(2006, 1, 2, 12), Chronic.construct(2006, 1, 1, 36)
    assert_equal Time.local(2006, 1, 7), Chronic.construct(2006, 1, 1, 144)
  end

  def test_day_overflow
    assert_equal Time.local(2006, 2, 1), Chronic.construct(2006, 1, 32)
    assert_equal Time.local(2006, 3, 5), Chronic.construct(2006, 2, 33)
    assert_equal Time.local(2004, 3, 4), Chronic.construct(2004, 2, 33)
    assert_equal Time.local(2000, 3, 4), Chronic.construct(2000, 2, 33)

    assert_nothing_raised do
      Chronic.construct(2006, 1, 56)
    end

    assert_raise(RuntimeError) do
      Chronic.construct(2006, 1, 57)
    end
  end

  def test_month_overflow
    assert_equal Time.local(2006, 1), Chronic.construct(2005, 13)
    assert_equal Time.local(2005, 12), Chronic.construct(2000, 72)
  end

end


text:
package net.daw.bean.implementation;

import net.daw.bean.publicinterface.GenericBean;
import com.google.gson.annotations.Expose;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import net.daw.dao.implementation.AsignaturaDao;
import net.daw.dao.implementation.TipodocumentoDao;
import net.daw.dao.implementation.AsignaturaDao;
import net.daw.helper.statics.EncodingUtilHelper;

public class AsignaturaxcursoBean implements GenericBean {

    @Expose
    private Integer id;
    @Expose
    private String titulo = "";
    @Expose
    private String descripcion = "";
    @Expose
    ArrayList<AsignaturaBean> alAsignaturas = new ArrayList();

    public AsignaturaxcursoBean() {
        this.id = 0;
    }

    public AsignaturaxcursoBean(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public ArrayList<AsignaturaBean> getAlAsignaturas() {
        return alAsignaturas;
    }

    public void setAlAsignaturas(ArrayList<AsignaturaBean> alAsignaturas) {
        this.alAsignaturas = alAsignaturas;
    }

    public String getColumns() {
        String strColumns = "";
        strColumns += "id,";
        strColumns += "titulo,";
        strColumns += "descripcion";
        return strColumns;
    }

    public String getValues() {
        String strColumns = "";
        strColumns += id + ",";
        strColumns += EncodingUtilHelper.quotate(titulo) + ",";
        strColumns += EncodingUtilHelper.quotate(descripcion);

        return strColumns;
    }

    @Override
    public String toPairs() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String strPairs = "";
        //strPairs += "id=" + id + ",";
        strPairs += "titulo=" + EncodingUtilHelper.quotate(titulo) + ",";
        strPairs += "descripcion=" +  EncodingUtilHelper.quotate(descripcion);

        return strPairs;
    }

    public AsignaturaxcursoBean fills(ResultSet oResultSetCurso, ResultSet oResultSetAsignaturaCurso, Connection pooledConnection, Integer expand) throws SQLException, Exception {
        this.setId(oResultSetCurso.getInt("id"));
        this.setTitulo(oResultSetCurso.getString("titulo"));
        this.setDescripcion(oResultSetCurso.getString("descripcion"));

        if (expand > 0) {
            AsignaturaDao oAsignaturaDao = new AsignaturaDao(pooledConnection);
            if (oResultSetAsignaturaCurso != null) {
                while (oResultSetAsignaturaCurso.next()) {
                    AsignaturaBean oAsignaturaBean = new AsignaturaBean();
                    oAsignaturaBean.setId(oResultSetAsignaturaCurso.getInt("id"));
                    oAsignaturaBean = oAsignaturaDao.get(oAsignaturaBean, expand - 1);
                    alAsignaturas.add(oAsignaturaBean);
                }
            }

        }

        return this;
    }

    @Override
    public GenericBean fill(ResultSet oResultSet, Connection pooledConnection, Integer expand) throws SQLException, Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}


text:
    #if($DOMAINS.size() == 0)
    <nobr>$Encoder.bodyEscape($ResourceBundle.getString('NuxeoRepositoryConnector.NoDomainsConfigured'))</nobr>
    	 #else
    <nobr>$Encoder.bodyEscape($ResourceBundle.getString('NuxeoRepositoryConnector.Domains')):</nobr>

    #set($domaincounter = 0) #foreach($domain in
      $DOMAINS) <nobr>$Encoder.bodyEscape($domain)</nobr>

      #set($domaincounter = $domaincounter + 1) #end
    	 #end

    #if($DOCUMENTSTYPE.size() == 0)
    <nobr>$Encoder.bodyEscape($ResourceBundle.getString('NuxeoRepositoryConnector.NoDocumentsTypeConfigured'))</nobr>
    	 #else
    <nobr>$Encoder.bodyEscape($ResourceBundle.getString('NuxeoRepositoryConnector.DocumentsType')):</nobr>

    #set($documentTypecounter = 0)
      #foreach($documentType in $DOCUMENTSTYPE) <nobr>$Encoder.bodyEscape($documentType)</nobr>

      #set($documentTypecounter = $documentTypecounter + 1) #end
    	 #end

    <nobr>
        $Encoder.bodyEscape($ResourceBundle.getString('NuxeoRepositoryConnector.ProcessTags')):
      </nobr>
    <nobr>$Encoder.bodyEscape($PROCESS_TAGS)</nobr>

    <nobr>
        $Encoder.bodyEscape($ResourceBundle.getString('NuxeoRepositoryConnector.ProcessAttachments')):
      </nobr>
    <nobr>$Encoder.bodyEscape($PROCESS_ATTACHMENTS)</nobr>


text:
<?php

declare(strict_types=1);

namespace Symnedi\Security\Tests\DI\SecurityExtension;

use Nette\Application\Application;
use Symnedi\Security\Tests\ContainerFactory;
use Symnedi\Security\Tests\DI\AbstractSecurityExtensionTestCase;

final class ListenerTest extends AbstractSecurityExtensionTestCase
{
    /**
     * @expectedException \Nette\Application\AbortException
     */
    public function testDispatch()
    {
        $container = (new ContainerFactory())->createWithConfig(__DIR__ . '/ListenerSource/config.neon');

        /** @var Application $application */
        $application = $container->getByType(Application::class);
        $application->run();
    }
}


text:
<BroadcastMonitor>
  <updated>2015-08-04T21:46:26</updated>
  <stationName>Radio 6</stationName>
  <Current>
    <startTime>2015-08-04T21:42:38</startTime>
    <itemId>1000080531:0</itemId>
    <titleId>216125907</titleId>
    <itemCode></itemCode>
    <itemReference></itemReference>
    <titleName>Just Friends</titleName>
    <artistName>Musiq Soulchild</artistName>
    <albumName></albumName>
    <CategoryCode>879</CategoryCode>
    <CategoryName>ARCHIEF</CategoryName>
    <itemDuration>248</itemDuration>
    <MultiMedia1></MultiMedia1>
    <MultiMedia2></MultiMedia2>
    <MultiMedia3></MultiMedia3>
    <MultiMedia4></MultiMedia4>
  </Current>
  <Next>
    <startTime>2015-08-04T21:46:46</startTime>
    <itemId>1000080531:1243614</itemId>
    <titleId>216138852</titleId>
    <itemCode></itemCode>
    <itemReference></itemReference>
    <titleName>Doralice</titleName>
    <artistName>Gretchen Parlato</artistName>
    <albumName></albumName>
    <CategoryCode>751</CategoryCode>
    <CategoryName>INVOER</CategoryName>
    <itemDuration>204</itemDuration>
    <MultiMedia1></MultiMedia1>
    <MultiMedia2></MultiMedia2>
    <MultiMedia3></MultiMedia3>
    <MultiMedia4></MultiMedia4>
  </Next>
</BroadcastMonitor>


text:
package javax.portlet.tck.portlets;

import static javax.portlet.ResourceURL.PAGE;
import static javax.portlet.tck.constants.Constants.QUERY_STRING;
import static javax.portlet.tck.constants.Constants.SERVLET_PREFIX;
import static javax.portlet.tck.constants.Constants.SERVLET_SUFFIX;
import static javax.portlet.tck.constants.Constants.THREADID_ATTR;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceServingPortlet;
import javax.portlet.ResourceURL;

/**
 * This portlet implements several test cases for the JSR 362 TCK. The test case names are defined in the
 * /src/main/resources/xml-resources/additionalTCs.xml file. The build process will integrate the test case names
 * defined in the additionalTCs.xml file into the complete list of test case names for execution by the driver.
 *
 * This is the main portlet for the test cases. If the test cases call for events, this portlet will initiate the
 * events, but not process them. The processing is done in the companion portlet
 * DispatcherTests_SPEC2_19_IncludeServletResource_event
 *
 */
public class DispatcherTests_SPEC2_19_IncludeServletResource implements Portlet, ResourceServingPortlet {

   private PortletConfig portletConfig = null;

   @Override
   public void init(PortletConfig config) throws PortletException {
      this.portletConfig = config;
   }

   @Override
   public void destroy() {
   }

   @Override
   public void processAction(ActionRequest portletReq, ActionResponse portletResp) throws PortletException, IOException {

      portletResp.setRenderParameters(portletReq.getParameterMap());
      long tid = Thread.currentThread().getId();
      portletReq.setAttribute(THREADID_ATTR, tid);

      new StringWriter();

   }

   @Override
   public void serveResource(ResourceRequest portletReq, ResourceResponse portletResp) throws PortletException,
         IOException {

      long tid = Thread.currentThread().getId();
      portletReq.setAttribute(THREADID_ATTR, tid);

      portletResp.getWriter();

      // Now do the actual dispatch
      String target = SERVLET_PREFIX + "DispatcherTests_SPEC2_19_IncludeServletResource_servlet" + SERVLET_SUFFIX + "?"
            + QUERY_STRING;
      PortletRequestDispatcher rd = portletConfig.getPortletContext().getRequestDispatcher(target);
      rd.include(portletReq, portletResp);
   }

   @Override
   public void render(RenderRequest portletReq, RenderResponse portletResp) throws PortletException, IOException {

      long tid = Thread.currentThread().getId();
      portletReq.setAttribute(THREADID_ATTR, tid);

      PrintWriter writer = portletResp.getWriter();

      writer.write("
no resource output.
\n");
      ResourceURL resurl = portletResp.createResourceURL();
      resurl.setCacheability(PAGE);
      writer.write("\n");
   }

}


text:
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>W29477_text</title>
        <link rel="stylesheet" type="text/css" href="style.css" />

                «

    <p class="styleSans24.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">'1 . Cgptinental
SOURCES

    <p class="styleSans8.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">9/8/2014

    <p class="styleSans11.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">Indushial Commission of North Dakota Oil & Gas Division
600 East Boulevard, Dept 405 Bismarck, North Dakota 58505

    <p class="styleSans8.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">Re: Kukla 5-16H

    <p class="styleSans11.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">Please charge the Continental Resources, Inc. credit card that is on file with your agency for the application fee of this well.
Please Note: A request for a legal street address has been sent to Denise Brew from Dunn County on 9/8/2014 at demise.brewgwdunncomitvndorg for this well location.
Thank you for your prompt attention to this matter. If you have any questions, you may contact me at (405) 234—9000 or by email at bec .bames@clr.com.

    <p class="styleSans11.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">Sin7erely, /~('[C//gfl'/L lug; CONTINENTAL RESOURCES, INC.
Becky Barnes Regulatory Compliance Specialist

    <p class="styleSans7.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">405-234—9000

    <p class="styleSans7.0000<enum PANGO_WEIGHT_NORMAL of type PangoWeight><enum PANGO_STYLE_NORMAL of type PangoStyle>">www.clr.com


text:
RepOverview for Bitbucket
=========================

Compact overview for Bitbucket repositories.

http://repoverview.designfankhauser.ch

## Credits
- [OAuth-API](http://www.phpclasses.org/package/7700-PHP-Authorize-and-access-APIs-using-OAuth.html)
- [HTTP Client](http://www.phpclasses.org/httpclient)
- [Bootstrap](http://getbootstrap.com)
- [Table search 1](http://www.willstrohl.com/Blog/PostId/468/Using-jQuery-to-Search-an-HTML-Table)
- [Table search 2](https://gist.github.com/jakebresnehan/2288330)
- [Search field with clear icon](http://www.awmcreative.com/jquery/jquery-search-field-with-clear-icon/)