text:
using NMF.Collections.Generic;
using NMF.Collections.ObjectModel;
using NMF.Expressions;
using NMF.Expressions.Linq;
using NMF.Models;
using NMF.Models.Collections;
using NMF.Models.Expressions;
using NMF.Models.Meta;
using NMF.Models.Repository;
using NMF.Serialization;
using NMF.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using TTC2017.SmartGrids.CIM;
using TTC2017.SmartGrids.CIM.IEC61968.Assets;
using TTC2017.SmartGrids.CIM.IEC61968.PaymentMetering;
using TTC2017.SmartGrids.CIM.IEC61970.Core;
using TTC2017.SmartGrids.CIM.IEC61970.Informative.InfAssets;
using TTC2017.SmartGrids.CIM.IEC61970.Informative.InfCommon;
using TTC2017.SmartGrids.CIM.IEC61970.Informative.InfERPSupport;
using TTC2017.SmartGrids.CIM.IEC61970.Informative.InfGMLSupport;
using TTC2017.SmartGrids.CIM.IEC61970.Informative.InfLocations;
using TTC2017.SmartGrids.CIM.IEC61970.Informative.InfOperations;
using TTC2017.SmartGrids.CIM.IEC61970.Informative.InfWork;
using TTC2017.SmartGrids.CIM.IEC61970.Informative.MarketOperations;
using TTC2017.SmartGrids.CIM.IEC61970.Meas;

namespace TTC2017.SmartGrids.CIM.IEC61968.Common
{

    public class TimeScheduleTimePointsCollection : ObservableOppositeOrderedSet<ITimeSchedule, ITimePoint>
    {

        public TimeScheduleTimePointsCollection(ITimeSchedule parent) :
                base(parent)
        {
        }

        private void OnItemDeleted(object sender, System.EventArgs e)
        {
            this.Remove(((ITimePoint)(sender)));
        }

        protected override void SetOpposite(ITimePoint item, ITimeSchedule parent)
        {
            if ((parent != null))
            {
                item.Deleted += this.OnItemDeleted;
                item.TimeSchedule = parent;
            }
            else
            {
                item.Deleted -= this.OnItemDeleted;
                if ((item.TimeSchedule == this.Parent))
                {
                    item.TimeSchedule = parent;
                }
            }
        }
    }
}


text:
IMPLEMENT(Transform)

Transform::Transform()
{

}

Transform::~Transform()
{

}

redpajama_set_name: RedPajamaGithub


text:
if (typeof BaseZF == "undefined") var BaseZF = {};
if (typeof BaseZF.Class == "undefined") BaseZF.Class = {};
if (typeof BaseZF.Helper == "undefined") BaseZF.Helper = {};

// Some vars
BaseZF.version = '1.0';
BaseZF.debug = false;

// Debug and log function
BaseZF.log = function() {

}

// Js GetText handler
if (typeof _ == "undefined") {
  var _ = function(s) {
    return s
  };
}


text:
package com.netflix.spinnaker.clouddriver.google.deploy.ops;

import com.google.api.services.compute.model.InstanceGroupManager;
import com.google.api.services.compute.model.StatefulPolicy;
import com.google.api.services.compute.model.StatefulPolicyPreservedState;
import com.google.api.services.compute.model.StatefulPolicyPreservedStateDiskDevice;
import com.google.common.annotations.VisibleForTesting;
import com.netflix.spinnaker.clouddriver.data.task.Task;
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository;
import com.netflix.spinnaker.clouddriver.google.compute.GoogleComputeApiFactory;
import com.netflix.spinnaker.clouddriver.google.compute.GoogleServerGroupManagers;
import com.netflix.spinnaker.clouddriver.google.deploy.GCEUtil;
import com.netflix.spinnaker.clouddriver.google.deploy.description.SetStatefulDiskDescription;
import com.netflix.spinnaker.clouddriver.google.model.GoogleServerGroup;
import com.netflix.spinnaker.clouddriver.google.provider.view.GoogleClusterProvider;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.List;

public class SetStatefulDiskAtomicOperation extends GoogleAtomicOperation<Void> {

  private static final String BASE_PHASE = "SET_STATEFUL_DISK";

  private final GoogleClusterProvider clusterProvider;
  private final GoogleComputeApiFactory computeApiFactory;
  private final SetStatefulDiskDescription description;

  public SetStatefulDiskAtomicOperation(
      GoogleClusterProvider clusterProvider,
      GoogleComputeApiFactory computeApiFactory,
      SetStatefulDiskDescription description) {
    this.clusterProvider = clusterProvider;
    this.computeApiFactory = computeApiFactory;
    this.description = description;
  }

  /*
      curl -X POST -H "Content-Type: application/json" -d '
        [ { "setStatefulDisk": {
              "serverGroupName": "myapp-dev-v000",
              "region": "us-east1",
              "device-name": "myapp-dev-v000-1",
              "credentials": "my-account-name"
        } } ]' localhost:7002/gce/ops
  */
  @Override
  public Void operate(List priorOutputs) {

    Task task = TaskRepository.threadLocalTask.get();

    task.updateStatus(
        BASE_PHASE,
        String.format(
            "Initializing set stateful disk of instance group %s in region %s",
            description.getServerGroupName(), description.getRegion()));

    GoogleServerGroup.View serverGroup =
        GCEUtil.queryServerGroup(
            clusterProvider,
            description.getAccount(),
            description.getRegion(),
            description.getServerGroupName());

    try {
      GoogleServerGroupManagers managers =
          computeApiFactory.createServerGroupManagers(description.getCredentials(), serverGroup);

      task.updateStatus(BASE_PHASE, "Retrieving current instance group definition");

      InstanceGroupManager instanceGroupManager = managers.get().execute();

      setStatefulPolicy(instanceGroupManager);

      task.updateStatus(BASE_PHASE, "Storing updated instance group definition");

      managers
          .update(instanceGroupManager)
          .executeAndWait(TaskRepository.threadLocalTask.get(), BASE_PHASE);

      return null;
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  private void setStatefulPolicy(InstanceGroupManager instanceGroupManager) {

    if (instanceGroupManager.getStatefulPolicy() == null) {
      instanceGroupManager.setStatefulPolicy(new StatefulPolicy());
    }
    StatefulPolicy statefulPolicy = instanceGroupManager.getStatefulPolicy();
    if (statefulPolicy.getPreservedState() == null) {
      statefulPolicy.setPreservedState(new StatefulPolicyPreservedState());
    }
    StatefulPolicyPreservedState preservedState = statefulPolicy.getPreservedState();
    if (preservedState.getDisks() == null) {
      preservedState.setDisks(new HashMap<>());
    }
    preservedState
        .getDisks()
        .put(description.getDeviceName(), new StatefulPolicyPreservedStateDiskDevice());
  }

  @VisibleForTesting
  public SetStatefulDiskDescription getDescription() {
    return description;
  }
}


text:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MomentModule } from 'angular2-moment';

// routes
import { routing } from './app.routes';

// Components
import { AppComponent } from './app.component';
import { WelcomeComponent } from './welcome/welcome.component';
import { NavBarWelcomeComponent } from './welcome/components/index';
import { LoginComponent } from './login/login.component';
import { HomeComponent } from './home/home.component';
import { MessageCreateRVComponent } from './home/components/index';
import { SearchComponent } from './search/search.component';
import { CreateRVComponent } from './create-rv/create-rv.component';
import { ProfileUserComponent } from './profile-user/profile-user.component';

import {
  CreationFormComponent,
  CreationPanelComponent,
  PrivateUsersModalComponent
} from './create-rv/components/index';

import {
  NavBarComponent,
  UserDropdownComponent,
  NotificationDropdownComponent,
  NotificationItemComponent,
  NavBarLogoComponent,
  ProfileCardComponent,
  PreviewRVComponent,
  PreviewUserComponent,
  CheckboxUserComponent,
  ViewRVComponent,
  CommentComponent
} from './shared/components/index';

// Services
import { AuthService, UserService, RVService } from './shared/services/index';
import { MarkerManager, GoogleMapsAPIWrapper } from 'angular2-google-maps/core';

// Directives
import { GoogleplaceDirective } from 'angular2-google-map-auto-complete/directives/googleplace.directive';
import {SearchLocationTextBox} from './create-rv/components/search-location-textbox/search-location-textbox.component';
import {PostService} from "./shared/services/posts.service";
import {
  userEffectsConf, authEffectsConf, routerStoreConf, storeConf, ngGoogleMapsConf,
  rvEffectsConf, postEffectsConf, storeDevToolsConf
} from "./app.exports";

@NgModule({
  declarations: [
    AppComponent,
    WelcomeComponent,
    NavBarWelcomeComponent,
    LoginComponent,
    HomeComponent,
    MessageCreateRVComponent,
    CreateRVComponent,
    SearchLocationTextBox,
    CreationFormComponent,
    CreationPanelComponent,
    PrivateUsersModalComponent,
    NavBarComponent,
    UserDropdownComponent,
    NotificationDropdownComponent,
    NotificationItemComponent,
    NavBarLogoComponent,
    ProfileCardComponent,
    PreviewRVComponent,
    PreviewUserComponent,
    CheckboxUserComponent,
    ViewRVComponent,
    SearchComponent,
    CommentComponent,
    ProfileUserComponent,
    GoogleplaceDirective
  ],
  imports: [
    BrowserModule,
    HttpModule,
    CommonModule,
    ReactiveFormsModule,
    MomentModule,
    routing,
    ngGoogleMapsConf,
    storeConf,
    routerStoreConf,
    authEffectsConf,
    userEffectsConf,
    rvEffectsConf,
    postEffectsConf,
    storeDevToolsConf
  ],
  entryComponents: [
    AppComponent,
    WelcomeComponent,
    LoginComponent,
    HomeComponent,
    CreateRVComponent
  ],
  bootstrap: [
    AppComponent
  ],
  providers: [
    AuthService,
    UserService,
    MarkerManager,
    RVService,
    PostService,
    GoogleMapsAPIWrapper
  ]
})
export class AppModule { }


text:
package cn.gtmap.helium.client.converter;

import cn.gtmap.helium.client.exception.WrongAppConfigException;

/**
 * Author: yingxiufeng
 * Date:  2016/6/18 9:41
 */
public abstract class AbstractValueConverter<T> implements ValueConverter<T> {

    /***
     *
     * @param key   键
     * @param value 值
     * @return
     * @throws WrongAppConfigException
     */
    @Override
    public T convert(String key, String value) throws WrongAppConfigException {
        try {
            return doConvert(value);
        } catch (Exception e) {
            throw new WrongAppConfigException(key, value, e);
        }
    }

    /***
     *
     * @param key          键
     * @param value        值
     * @param defaultValue 默认值
     * @return
     */
    @Override
    public T convert(String key, String value, T defaultValue) {
        if (value == null) {
            return defaultValue;
        }

        try {
            doConvert(value);
        } catch (Exception e) {
            return defaultValue;
        }
        return null;
    }

    /**
     * 将属性值转换为目标类型.
     *
     * @param value 值
     * @return 值
     */
    protected abstract T doConvert(String value);
}


text:
/**
 * This file contains the functions for authentication mechanism via
 * vCenter
 */

var plugins = require('../plugins.api');
var config = process.mainModule.exports["config"],
    global = require('../../../common/global'),
    messages = require('../../../common/messages'),
    logutils = require('../../../utils/log.utils'),
    authApi = require('../../../common/auth.api'),
    crypto = require('crypto'),
    rest = require('../../../common/rest.api'),
    assert = require('assert'),
    commonUtils = require('./../../../utils/common.utils'),
    vcenterApi = require('../../../common/vcenter.api'),
    vCenterPluginApi = require('./vcenter.api'),
    configUtils = require('../../../common/configServer.utils'),
    authSoapServer = require('../../../common/auth.api');

var authServerIP = ((config.identityManager) && (config.identityManager.ip)) ?
    config.identityManager.ip : global.DFLT_SERVER_IP;
var authServerPort =
    ((config.identityManager) && (config.identityManager.port)) ?
    config.identityManager.port : '8080';

//authSOAPServer = rest.getSoapAPiServer({apiName:global.label.VCENTER_SERVER,
 //                                       vcenterParams: config.vcenter});
// var authSOAPServer = vcenterApi.createvCenterSoapApi('vcenter');

function authenticate (req, res, appData, callback)
{
    var urlHash = '',urlPath = '';
    var post = req.body,
        username = post.username,
        password = post.password;

    if (post.urlPath != null) {
        urlPath = post.urlPath + '/';
    }
    if (post.urlHash != null) {
        urlHash = post.urlHash;
    }
    var userData =
        {
        method    : 'Login',
        params : {
            _this : {
                _attributes : {
                    type: 'SessionManager'
                },
                _value : 'SessionManager'
            },
            userName : username,
            password: password
        }
    }
    console.log("getting urlPath as:", urlPath, urlHash);
    vcenterApi.doCall(userData, appData, function(err, data, resHeaders) {
        /* Once authenticated, then store the vmware_soap_session in session,
         * and pass this in header for next calls to vcenter server
         */
            console.log("getting err as:", err, JSON.stringify(data),
                        JSON.stringify(resHeaders));
        if (null != err) {
            var loginErrFile = 'webroot/html/login-error.html';
            commonUtils.changeFileContentAndSend(res, loginErrFile,
                                                 global.CONTRAIL_LOGIN_ERROR,
                                                 err.message,
                                                 //messages.error.invalid_user_pass,
                                             function() {
            });
            return;
        }
        req.session.isAuthenticated = true;
        req.session.userRole = [global.STR_ROLE_ADMIN];
        console.log("Getting urlPath as:", urlPath, urlHash);
        req.session['vmware_soap_session'] =
            resHeaders['set-cookie'][0];
            //res.redirect('/');
            //return;
        plugins.setAllCookies(req, res, appData, {'username': username}, function() {
            if ('' != urlPath) {
                res.redirect(urlPath + urlHash);
            } else {
                res.redirect('/' + urlHash);
            }
        });
    });
}

function getTenantList(req,appData,callback) {
    vCenterPluginApi.getProjectList(req,appData,function(err,projectList){
        console.log("Projects are "+JSON.stringify(projectList));
        if (err || (null == projectList)){
            callback(err,null);
            return;
        }
        var response = {
            "tenants_links": [],
            "tenants": []
        };
        for (var i = 0; i < commonUtils.ifNull(projectList['projects'],[]).length; i++) {
            var projectObj = projectList['projects'][i];
            var tenantObj = {
                "description": null,
                "enabled": true,
                "id": commonUtils.convertApiServerUUIDtoKeystoneUUID(projectObj['uuid']),
                "name": projectObj['fq_name'].pop()
            };
            response['tenants'].push(tenantObj);
        }
        callback(err,response);
    });
}
function getServiceCatalog (req, callback)
{
    callback(null);
}

function getAPIServerAuthParamsByReq (req)
{
    return null;
}

function getProjectList (req, appData, callback)
{
    vCenterPluginApi.getProjectList(req, appData,
                                         function(error, data) {
        callback(error, data);
    });
}

function getImageList (req, callback)
{
    var imgList = {"images": []};
    callback(null, imgList);
}

function getFlavors (req, callback)
{
    var list = {"flavors": []};
    callback(null, list);
}

function getOSHostList (req, callback)
{
    var list = {"hosts": []};
    callback(null, list);
}

function getAvailabilityZoneList (req, callback)
{
    var list = {"availabilityZoneInfo": []};
    callback(null, list);
}

function getServiceInstanceVMStatus (req, vmRefs, callback)
{
    callback(null, null);
}

function getVMStatsByProject (projUUID, req, callback)
{
    callback(null, null);
}

function getSessionExpiryTime (req, appData, callback)
{
    var cfgSessTimeout =
        ((null != config.session) && (null != config.session.timeout)) ?
        config.session.timeout : null;
    var defSessTimeout = global.MAX_AGE_SESSION_ID;
    if (null == cfgSessTimeout) {
        return defSessTimeout;
    }
    return cfgSessTimeout;
}

function getCookieObjs (req, appData, callback)
{
    var cookieObjs = {};
    getProjectList(req, appData, function(err, data) {
        if ((null != err) || (null == data) || (null == data['projects']) ||
            (!data['projects'].length)) {
            callback(cookieObjs);
            return;
        }
        var projectList = data['projects'];
        cookieObjs['domain'] = projectList[0]['fq_name'][0];
        cookieObjs['project'] = projectList[0]['fq_name'][1];
        callback(cookieObjs);
    });
}

function getUserAuthDataByConfigAuthObj (authObj, callback)
{
    callback(null, null);
}

function deleteAllTokens (req, callback)
{
    callback(null, null);
}

exports.getCookieObjs = getCookieObjs;
exports.getSessionExpiryTime = getSessionExpiryTime;
exports.authenticate = authenticate;
exports.getServiceCatalog = getServiceCatalog;
exports.getAPIServerAuthParamsByReq = getAPIServerAuthParamsByReq;
exports.getTenantList = getTenantList;
exports.getProjectList = getProjectList;
exports.getImageList = getImageList;
exports.getFlavors = getFlavors;
exports.getOSHostList = getOSHostList;
exports.getAvailabilityZoneList = getAvailabilityZoneList;
exports.getServiceInstanceVMStatus = getServiceInstanceVMStatus;
exports.getVMStatsByProject = getVMStatsByProject;
exports.getUserAuthDataByConfigAuthObj = getUserAuthDataByConfigAuthObj;
exports.deleteAllTokens = deleteAllTokens;


text:
package com.mapbox.api.directions.v5.models;

import androidx.annotation.NonNull;

import com.google.auto.value.AutoValue;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ShieldSprites.
 */
@AutoValue
public abstract class ShieldSprites extends DirectionsJsonObject implements Serializable {

  /**
   * Create a new instance of this class by using the {@link ShieldSprites.Builder} class.
   *
   * @return {@link ShieldSprites.Builder} for creating a new instance
   */
  public static Builder builder() {
    return new AutoValue_ShieldSprites.Builder();
  }

  /**
   * Create a new instance of this class by passing in a formatted valid JSON String.
   *
   * @param json a formatted valid JSON string defining a shield sprite
   * @return a new instance of this class defined by the values passed inside this static factory
   *   method
   */
  @NonNull
  public static ShieldSprites fromJson(@NonNull String json) {
    List<ShieldSprite> sprites = new ArrayList<>();
    GsonBuilder gson = new GsonBuilder();
    JsonObject jsonObject = gson.create().fromJson(json, JsonObject.class);
    Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();
    for (Map.Entry<String, JsonElement> entry : entries) {
      String spriteName = entry.getKey();
      ShieldSpriteAttribute spriteAttribute = ShieldSpriteAttribute.fromJson(
          jsonObject.get(spriteName).toString()
      );
      ShieldSprite sprite = ShieldSprite.builder()
          .spriteName(spriteName)
          .spriteAttributes(spriteAttribute)
          .build();
      sprites.add(sprite);
    }
    return ShieldSprites.builder()
        .sprites(sprites)
        .build();
  }

  /**
   * List of {@link ShieldSprite}.
   */
  @NonNull
  public abstract List<ShieldSprite> sprites();

  /**
   * Convert the current {@link ShieldSprites} to its builder holding the currently assigned
   * values. This allows you to modify a single property and then rebuild the object resulting in
   * an updated and modified {@link ShieldSprites}.
   *
   * @return a {@link ShieldSprites.Builder} with the same values set to match the ones defined
   *   in this {@link ShieldSprites}
   */
  public abstract Builder toBuilder();

  /**
   * This takes the currently defined values found inside the {@link ShieldSprites} instance and
   * converts it to a {@link ShieldSprites} string.
   *
   * @return a JSON string which represents a {@link ShieldSprites}
   */
  @NonNull
  public String toJson() {
    JsonParser parser = new JsonParser();
    Gson gson = new Gson();
    JsonObject json = new JsonObject();
    for (ShieldSprite sprite : this.sprites()) {
      json.add(sprite.spriteName(), parser.parse(sprite.spriteAttributes().toJson()));
    }
    return gson.toJson(json);
  }

  /**
   * Gson type adapter for parsing Gson to this class.
   *
   * @param gson the built {@link Gson} object
   * @return the type adapter for this class
   */
  public static TypeAdapter<ShieldSprites> typeAdapter(Gson gson) {
    return new AutoValue_ShieldSprites.GsonTypeAdapter(gson);
  }

  /**
   * This builder can be used to set the values describing the {@link ShieldSprites}.
   */
  @AutoValue.Builder
  public abstract static class Builder extends DirectionsJsonObject.Builder<Builder> {

    /**
     * List of {@link ShieldSprite}.
     *
     * @param sprites list of sprites
     */
    @NonNull
    public abstract Builder sprites(@NonNull List<ShieldSprite> sprites);

    /**
     * Build a new {@link ShieldSprites} object.
     *
     * @return a new {@link ShieldSprites} using the provided values in this builder
     */
    @NonNull
    public abstract ShieldSprites build();
  }
}


text:
<?php

namespace Magento\Quote\Model\ResourceModel\Quote\Address;

use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;

/**
 * Quote address item resource model
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Item extends AbstractDb
{
    /**
     * Main table and field initialization
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('quote_address_item', 'address_item_id');
    }
}


text:
package com.sksamuel.elastic4s.sttp

import java.io._
import java.nio.file.Files

import com.sksamuel.elastic4s.ElasticsearchClientUri
import com.sksamuel.elastic4s.http.HttpEntity.{FileEntity, InputStreamEntity, StringEntity}
import com.sksamuel.elastic4s.http.{ElasticRequest, HttpClient, HttpEntity, HttpResponse}
import com.sksamuel.exts.OptionImplicits._
import com.softwaremill.sttp._
import com.softwaremill.sttp.asynchttpclient.future.AsyncHttpClientFutureBackend

import scala.concurrent.Future
import scala.io.Source
import scala.util.{Failure, Success}

class SttpRequestHttpClient(clientUri: ElasticsearchClientUri) extends HttpClient {

  import scala.concurrent.ExecutionContext.Implicits._

  implicit val sttpBackend: SttpBackend[Future, Nothing] = AsyncHttpClientFutureBackend()

  private def request(method: String, endpoint: String, params: Map[String, Any]): Request[String, Nothing] = {
    val url = uri"http://${clientUri.hosts.head._1}:${clientUri.hosts.head._2}/$endpoint?$params"
    method.toUpperCase match {
      case "GET"    => sttp.get(url)
      case "HEAD"   => sttp.head(url)
      case "POST"   => sttp.get(url)
      case "PUT"    => sttp.put(url)
      case "DELETE" => sttp.delete(url)
    }
  }

  private def processResponse(resp: Response[String]): HttpResponse = {
    val entity = resp.body match {
      case Left(e)     => None
      case Right(body) => HttpEntity.StringEntity(body, resp.contentType).some
    }
    HttpResponse(resp.code, entity, resp.headers.toMap)
  }

  def async(method: String, endpoint: String, params: Map[String, Any]): Request[String, Nothing] =
    request(method, endpoint, params)

  def async(method: String,
            endpoint: String,
            params: Map[String, Any],
            entity: HttpEntity): Request[String, Nothing] = {
    val r  = request(method, endpoint, params)
    val r2 = entity.contentType.fold(r)(r.contentType)
    entity match {
      case StringEntity(content: String, _) => r2.body(content)
      case InputStreamEntity(in: InputStream, _) =>
        r2.body(Source.fromInputStream(in, "UTF8").getLines().mkString("\n"))
      case FileEntity(file: File, _) => r2.body(Files.readAllBytes(file.toPath))
    }
  }

  override def close(): Unit = sttpBackend.close()

  /**
    * Sends the given request to elasticsearch.
    *
    * Implementations should invoke the callback function once the response is known.
    *
    * The callback function should be invoked with a HttpResponse for all requests that received
    * a response, including 4xx and 5xx responses. The callback function should only be invoked
    * with an exception if the client failed.
    */
  override def send(request: ElasticRequest, callback: Either[Throwable, HttpResponse] => Unit): Unit = {
    val f = request.entity match {
      case Some(entity) => async(request.method, request.endpoint, request.params, entity).send()
      case None         => async(request.method, request.endpoint, request.params).send()
    }
    f.onComplete {
      case Success(resp) => callback(Right(processResponse(resp)))
      case Failure(t)    => callback(Left(t))
    }
  }
}


text:
<?php

namespace common\assets;

use yii\web\AssetBundle;

class Html5shiv extends AssetBundle
{
    public $sourcePath = '@bower/html5shiv';
    public $js         = [
        'dist/html5shiv.min.js',
    ];

    public $jsOptions = [
        'condition' => 'lt IE 9',
    ];
}


text:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
  <item
      android:id="@+id/new_game"
      android:icon="@android:drawable/ic_menu_search"
      android:title="Search"
      app:showAsAction="always"/>
  <item
      android:id="@+id/help"
      android:icon="@android:drawable/ic_partial_secure"
      android:title="Help"
      app:showAsAction="never">
    <menu>
      <item android:id="@+id/docs"
            android:title="Doc's" />
      <item android:id="@+id/faq"
            android:title="FAQ" />
    </menu>
  </item>
</menu>


text:
BIO_NS_START

template <class MatrixIt, class SeqIt>
size_t
cache_normalisations(
	MatrixIt matrix_begin,
	MatrixIt matrix_end,
	SeqIt seq_begin,
	SeqIt seq_end,
	BIO_NS::OttNormaliserCache & cache,
	size_t num_quanta)
{
	size_t num_normalised = 0;
	//for each matrix
	for ( ; matrix_begin != matrix_end; ++matrix_begin) {
		const std::string name = matrix_begin->second->id.get_text();
		OttNormaliser::ptr_t ott_normaliser(new OttNormaliser(make_pssm(matrix_begin->second), seq_begin, seq_end, num_quanta));
		cache.put_normaliser(name, ott_normaliser);
		++num_normalised;
	}
	return num_normalised;
}

BIO_NS_END

#endif //BIOBASE_NORMALISER_TRANSFAC_H_


text:
package io.scalac.slack.bots

import akka.actor.{Actor, ActorLogging, ActorRef}
import akka.util.Timeout
import io.scalac.slack.MessageEventBus
import io.scalac.slack.common.{Incoming, MessageEvent, Outgoing, _}

import scala.concurrent.ExecutionContext
import scala.language.implicitConversions

trait MessagePublisher {

  import akka.pattern._

  def bus: MessageEventBus

  implicit def publish(event: MessageEvent): Unit = {
    bus.publish(event)
  }

  def publish(directMessage: DirectMessage)(implicit context: ExecutionContext, userStorage: ActorRef, timeout: Timeout): Unit = {
    userStorage ? FindChannel(directMessage.key) onSuccess {
      case Some(channel: String) =>
        val eventToSend = directMessage.event match {
          case message: OutboundMessage => message.copy(channel = channel)
          case message: RichOutboundMessage => message.copy(channel = channel)
          case other => other
        }
        publish(eventToSend)
    }
  }
}

abstract class MessageListener extends Actor with ActorLogging with MessagePublisher

/**
 * A raw messaging interface used to create internal system level bots.
 * For user facing bots use AbstractBot
 */
abstract class IncomingMessageListener extends MessageListener {
  @throws[Exception](classOf[Exception])
  override def preStart(): Unit = bus.subscribe(self, Incoming)
}

abstract class OutgoingMessageListener extends MessageListener {
  @throws[Exception](classOf[Exception])
  override def preStart(): Unit = bus.subscribe(self, Outgoing)
}

/**
 * The class to extend when creating a bot.
 */
abstract class AbstractBot extends IncomingMessageListener {
  log.debug(s"Starting ${self.path.name} on $bus")

  override val bus: MessageEventBus

  def name: String = self.path.name

  def help(channel: String): OutboundMessage

  def act: Actor.Receive

  def handleSystemCommands: Actor.Receive = {
    case HelpRequest(t, ch) if t.map(_ == name).getOrElse(true) => publish(help(ch))
  }

  override final def receive: Actor.Receive = act.orElse(handleSystemCommands)
}


text:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>

<?php echo $this->template->section;?> » Add Certificate

				<?php
					$attributes = array('method' => 'post', 'name' => 'addcertificateform_admin', 'id' => 'addcertificateform_admin');
					echo form_open('admin/storesettings/addcertificate', $attributes);
					?>

						  Certificate Name
						  <input type="text" placeholder="Certificate Name" id="name" name="name" class="form-control" value="<?php echo (isset($certificate->name))?$certificate->name:(isset($name)?$name:'');?>">
						  <?php echo form_error('name'); ?>
						  <?php $certificateid = (isset($certificate->id))?$certificate->id:(isset($certificateid)?$certificateid:'');?>
						  <input type="hidden" id="certificateid" name="certificateid" value="<?php echo $certificateid;?>">

						  Description
						  <textarea placeholder="Description" id="description" name="description" class="form-control" ><?php echo (isset($certificate->description))?$certificate->description:(isset($description)?$description:'');?></textarea>
						  <?php echo form_error('description'); ?>

						  Status
						  <select class="form-control" name="status" id="status">
							<option value="1" <?php echo (isset($status) && $status == 1)?'selected="selected"':'';?>>Active</option>
							<option value="0" <?php echo (isset($status) && $status == 0)?'selected="selected"':'';?>>Inactive</option>
						  </select>

					<button class="btn btn-primary" type="submit">Submit</button>

				 <?php echo form_close();?>


text:
package org.apache.helix.mock.participant;

import org.apache.helix.api.StateTransitionHandlerFactory;
import org.apache.helix.api.id.PartitionId;
import org.apache.helix.api.id.ResourceId;

/**
 * Mock STORAGE_DEFAULT_SM_SCHEMATA state model factory
 */
public class MockSchemataModelFactory extends StateTransitionHandlerFactory<MockSchemataStateModel> {
  @Override
  public MockSchemataStateModel createStateTransitionHandler(ResourceId resource,
      PartitionId partition) {
    MockSchemataStateModel model = new MockSchemataStateModel();
    return model;
  }
}


text:
<!DOCTYPE html>

  {# Grab the title and description from the CMS general settings. #}
  {% set site_title = getSetting('site_name')%}
  {% set site_description = getSetting('site_description')%}

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>{% block title %}{{ site_title|default('Webhook site') }}{% endblock %}</title>
    <meta name="description" content="{% block description %}{{ site_description|default('Built with Webhook.') }}{% endblock %}">
    <meta name="viewport" content="width=device-width">

    <link rel="shortcut icon" href="/static/images/favicon.ico">
    <link rel="apple-touch-icon icon-precomposed" href="/static/images/apple-touch-icon.png"/>

    {% block css %}

    <link rel="stylesheet" href="/static/css/main.css"/>
    <link rel="stylesheet" href="/static/css/wysiwyg_figures.css"/>

    {% endblock %}

    {% block head_extra %}{% endblock %}

    {% block body %}

      {% block header %}{% include "templates/partials/header.html" %}{% endblock %}

      {% block content %}

      {% endblock %}

      {% include "templates/partials/footer.html" %}

      {# Google Analytics code. Feel free to remove. #}

    {% endblock %}


text:
+++
date = "Fri Jan 10 13:15:11 UTC 2020"
title = "Dagger2でRetrofit用のOkHttpClientをバックグラウンドで作ったときにうまくいかない場合がある"
tags = ["android", "dagger", "startuptime", "retrofit", "di"]
blogimport = true
type = "post"
draft = true
+++

[Dagger Party Tricks: Deferred OkHttp Initialization](https://www.zacsweers.dev/dagger-party-tricks-deferred-okhttp-init/)を参考にして、バックグラウンドでOkHttpClientインスタンスを生成しようとしたけど、メインスレッドで生成されてしまった話をします。

結論から言うと、`RxJava2CallAdapterFactory`を使うと、OkHttpClientを生成する部分が、IOスレッドで呼び出されるので、バックグラウンドで生成されます。この場合は問題ありません。

しかし、Coroutineを使っている場合は、OkHttpClientを生成する部分が、呼び出したスレッドに依存します。メインスレッドから呼び出したら、メインスレッドでOkHttpClientが生成されてしまいます。

今回は、Coroutineを使っていたため、メインスレッドで生成されてしまいました。

## なんでこんな動作になるのかコードを追ってみる。

まず最初にサンプルコードになります。

```kotlin
// MainActivity
@Inject lateinit var gitHubApi: GitHubApi

lifecycleScope.launch { // ktxのlifecycleScope関数
  gitHubApi.test()
}

// GitHubApi
interface GitHubApi {
  @GET("hoge")
  suspend fun test()
}
```

`gitHubApi.test()`からコードを追っていきます。

まず、`gitHubApi.test()`はメインスレッド上で実行されます。これは`lifecycleScope`が`Dispatchers.Main.immediate`に紐付けられているためです。この


text:
phpunit AbstractTest ../_files/AbstractTest.php
--FILE--
<?php
$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = 'AbstractTest';
$_SERVER['argv'][3] = dirname(dirname(__FILE__)) . '/_files/AbstractTest.php';

require_once dirname(dirname(dirname(__FILE__))) . '/PHPUnit/Autoload.php';
PHPUnit_TextUI_Command::main();
?>
--EXPECTF--
PHPUnit %s by Sebastian Bergmann.

F

Time: %i %s, Memory: %sMb

There was 1 failure:

1) Warning
Cannot instantiate class "AbstractTest".
%s/abstract-test-class.php:%i

FAILURES!
Tests: 1, Assertions: 0, Failures: 1.


text:
+ This Orderer Traffic Engine (OTE) tool creates and tests the operation of a
hyperledger fabric ordering service.
+ The focus is strictly on the orderers themselves.
No peers are involved: no endorsements or validations or committing to ledgers.
No SDK is used.

+ OTE sends transactions to
every channel on every orderer, and verifies that the correct number
of transactions and blocks are delivered on every channel from every orderer.
+ OTE generates report logs and returns
a pass/fail boolean and a resultSummaryString.

## How does OTE do it?

+ OTE invokes a local copy of the tool driver.sh (including helper files
network.json and json2yml.js) -
which is a close copy of the original version at
https://github.com/dongmingh/v1FabricGenOption.
+ The driver.sh launches an orderer service network per the user-provided
parameters including number of orderers, orderer type,
number of channels, and more.
+ Producer clients are created to connect via
grpc ports to the orderers to concurrently send traffic until the
requested number of transactions are sent.
Each client generates unique transactions - a fraction of the total
requested number of transactions.
+ Consumer clients are created to connect via
grpc ports to the orderers to concurrently receive delivered traffic
until all batches of transactions are tallied.
OTE checks if the correct number of blocks and TXs are delivered
by all the orderers on all the channels

## Prerequisites
- Git client
- Docker v1.12 or higher
- [Docker-Compose v1.8 or higher](https://docs.docker.com/compose/overview/)
- GO

Check your Docker and Docker-Compose versions with the following commands:
```bash
    docker version
    docker-compose version
```

### Prepare binaries and images:

- Alternative 1: Prepare all binaries and images using a script
```bash
      cd $GOPATH/src/github.com/hyperledger/fabric/bddtests/regression/ote
      ./docker_images.sh
```

- Alternative 2: Prepare binaries and images manually
- - Clone the fabric repository, build the binaries and images
```bash
        cd $GOPATH/src/github.com/hyperledger/fabric
        make native docker
```
- - Clone the fabric-ca repository, build the images
```bash
        cd $GOPATH/src/github.com/hyperledger/

        # Use ONE of these methods to clone the repository:
            go get github.com/hyperledger/fabric-ca
            git clone https://github.com/hyperledger/fabric-ca.git
            git clone ssh://YOUR-ID@gerrit.hyperledger.org:29418/fabric-ca

        cd $GOPATH/src/github.com/hyperledger/fabric-ca
        make docker
```

### Environment Variables for test setup, with defaults:
```
  OTE_TXS                                      55
  OTE_CHANNELS                                 1
  OTE_ORDERERS                                 1
  OTE_KAFKABROKERS                             0
  OTE_MASTERSPY                                false
  OTE_PRODUCERS_PER_CHANNEL                    1
```

### Environment Variables for configuration
Find default values of all variables in hyperledger/fabric/orderer/orderer.yaml
and hyperledger/fabric/peer/core.yaml.
```
  CONFIGTX_ORDERER_ORDERERTYPE                 solo
  CONFIGTX_ORDERER_BATCHSIZE_MAXMESSAGECOUNT   10
  CONFIGTX_ORDERER_BATCHTIMEOUT                10

  Others:
  CORE_LOGGING_LEVEL                           <unset>
  CORE_LEDGER_STATE_STATEDATABASE              leveldb
  CORE_SECURITY_LEVEL                          256
  CORE_SECURITY_HASHALGORITHM                  SHA2
```

## Execute OTE on shell command line
There are several environment variables to control the test parameters,
such as number of transactions, number of orderers, ordererType, and more.
To see an example test using default settings, simply execute the following.
```bash
  cd $GOPATH/src/github.com/hyperledger/fabric/bddtests/regression/ote
  go build
  ./ote
  CONFIGTX_ORDERER_BATCHSIZE_MAXMESSAGECOUNT=20 ./ote
  OTE_TXS=100  OTE_CHANNELS=4  ./ote
  CONFIGTX_ORDERER_ORDERERTYPE=kafka OTE_KAFKABROKERS=3  ./ote
```

Choose which variables to modify from default values. For example:

+ This test will create eight Producer clients.
+ Each Producer will send 125 transactions to a different orderer and channel.
+ 250 total TXs will be broadcast on each channel.
+ 500 total TXs will be broadcast to each orderer.
+ Four Consumer clients will be created to receive the delivered
  batches on each channel on each orderer.
+ 50 batches (with 10 TX each) will be delivered on channel 0, and
  a different 50 batches will be delivered on channel 1. On both Orderers.
+ 100 batches will be received on every orderer; this is the sum of the
  totals received on each channel on the orderer.
```bash
  OTE_TXS=1000 OTE_CHANNELS=4 OTE_ORDERERS=2 CONFIGTX_ORDERER_ORDERERTYPE=kafka  ./ote
```

## Execute OTE GO Tests
The tester may optionally define environment variables to
set the test parameters and to
override certain orderer configuration parameters.
Then use "go test" to execute Test functions
to execute either one test, or all go tests, or
a subset of existing functional go tests using a regular expression
to choose tests in local test files.
```bash
  cd $GOPATH/src/github.com/hyperledger/fabric/bddtests/regression/ote
  go test -run ORD77
  go test -run ORD7[79]
  go test -run batchSz -timeout 20m
  go test -timeout 90m

  go get github.com/jstemmer/go-junit-report
  go test -run ORD7 -v | go-junit-report > report.xml

```

## Execute OTE GO Tests for Continuous Improvement
Optionally, one can translate the "go test" output to xml for reports.
This is useful for automated test suites that are automatically
executed from Jenkins by Continuous Improvement processes.

#### Pre-requisite to convert "go test" output to xml
```bash
  cd $GOPATH/src/github.com/hyperledger/fabric/bddtests/regression/ote
  go get github.com/jstemmer/go-junit-report
```
#### Example command to execute all "go tests" and convert to xml:
```
  cd $GOPATH/src/github.com/hyperledger/fabric/bddtests/regression/ote
  go test -v -timeout 120m | go-junit-report > ote_report.xml
```


text:
#ifndef _ASMAXP_SHMPARAM_H
#define _ASMAXP_SHMPARAM_H

/*
 * Address range for shared memory attaches if no address passed to
 * shmat().  These ought to be changed to something >4GB so 32-bit
 * errors are caught more easily.  However, they don't seem to be used
 * except for ELF stuff, so it's not really critical until we get ELF
 * support for the Alpha.
 */
#define SHM_RANGE_START	0x50000000
#define SHM_RANGE_END	0x60000000

/*
 * Format of a swap-entry for shared memory pages currently out in
 * swap space (see also mm/swap.c).
 *
 * SWP_TYPE = SHM_SWP_TYPE
 * SWP_OFFSET is used as follows:
 *
 *  bits 0..6 : id of shared memory segment page belongs to (SHM_ID)
 *  bits 7..21: index of page within shared memory segment (SHM_IDX)
 *		(actually fewer bits get used since SHMMAX is so low)
 */

/*
 * Keep _SHM_ID_BITS as low as possible since SHMMNI depends on it and
 * there is a static array of size SHMMNI.
 */
#define _SHM_ID_BITS	7
#define SHM_ID_MASK	((1<<_SHM_ID_BITS)-1)

#define SHM_IDX_SHIFT	(_SHM_ID_BITS)
#define _SHM_IDX_BITS	15
#define SHM_IDX_MASK	((1<<_SHM_IDX_BITS)-1)

/*
 * _SHM_ID_BITS + _SHM_IDX_BITS must be <= 24 on the Alpha and
 * SHMMAX <= (PAGE_SIZE << _SHM_IDX_BITS).
 */

#define SHMMAX 0x3fa000			/* max shared seg size (bytes) */
#define SHMMIN 1 /* really PAGE_SIZE */	/* min shared seg size (bytes) */
#define SHMMNI (1<<_SHM_ID_BITS)	/* max num of segs system wide */
#define SHMALL				/* max shm system wide (pages) */ \
	(1<<(_SHM_IDX_BITS+_SHM_ID_BITS))
#define	SHMLBA PAGE_SIZE		/* attach addr a multiple of this */
#define SHMSEG SHMMNI			/* max shared segs per process */

#endif /* _ASMAXP_SHMPARAM_H */


text:
int main(int argc, char* argv[]) {
  printf("%s, %s\n", GetStaticText(), GetSharedText());
  return 0;
}


text:
<?php

declare(strict_types=1);

/*
 * This file is part of the RollerworksSearch package.
 *
 * (c) Sebastiaan Stok <s.stok@rollerscapes.net>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Rollerworks\Component\Search\Value;

/**
 * RequiresComparatorValueHolder indicates the value-holder
 * requires a comparator in the field's type.
 */
interface RequiresComparatorValueHolder extends ValueHolder
{
}


text:
package org.openmhealth.shim.runkeeper.mapper;

import com.fasterxml.jackson.databind.JsonNode;
import org.openmhealth.schema.domain.omh.*;
import org.openmhealth.shim.common.mapper.DataPointMapperUnitTests;
import org.springframework.core.io.ClassPathResource;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;

import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.openmhealth.schema.domain.omh.DataPointModality.SELF_REPORTED;
import static org.openmhealth.schema.domain.omh.DataPointModality.SENSED;
import static org.openmhealth.schema.domain.omh.DurationUnit.SECOND;
import static org.openmhealth.schema.domain.omh.LengthUnit.METER;
import static org.openmhealth.shim.runkeeper.mapper.RunkeeperDataPointMapper.RESOURCE_API_SOURCE_NAME;

/**
 * @author Emerson Farrugia
 */
public class RunkeeperPhysicalActivityDataPointMapperUnitTests extends DataPointMapperUnitTests {

    private final RunkeeperPhysicalActivityDataPointMapper mapper = new RunkeeperPhysicalActivityDataPointMapper();

    private JsonNode responseNode;

    @BeforeTest
    public void initializeResponseNode() throws IOException {

        ClassPathResource resource =
                new ClassPathResource("org/openmhealth/shim/runkeeper/mapper/runkeeper-fitness-activities.json");

        responseNode = objectMapper.readTree(resource.getInputStream());
    }

    @Test
    public void asDataPointsShouldReturnCorrectNumberOfDataPoints() {

        List<DataPoint<PhysicalActivity>> dataPoints = mapper.asDataPoints(singletonList(responseNode));

        assertThat(dataPoints, notNullValue());
        assertThat(dataPoints.size(), equalTo(2));
    }

    @Test
    public void asDataPointsShouldReturnCorrectSensedDataPoints() {

        List<DataPoint<PhysicalActivity>> dataPoints = mapper.asDataPoints(singletonList(responseNode));

        assertThat(dataPoints, notNullValue());
        assertThat(dataPoints.size(), greaterThan(0));

        TimeInterval effectiveTimeInterval = TimeInterval.ofStartDateTimeAndDuration(
                OffsetDateTime.of(2014, 10, 19, 13, 17, 27, 0, ZoneOffset.ofHours(2)),
                new DurationUnitValue(SECOND, 4364.74158141667));

        PhysicalActivity physicalActivity = new PhysicalActivity.Builder("Cycling")
                .setDistance(new LengthUnitValue(METER, 10128.7131871337))
                .setEffectiveTimeFrame(effectiveTimeInterval)
                .build();

        DataPoint<PhysicalActivity> dataPoint = dataPoints.get(0);

        assertThat(dataPoint.getBody(), equalTo(physicalActivity));

        DataPointAcquisitionProvenance acquisitionProvenance = dataPoint.getHeader().getAcquisitionProvenance();

        assertThat(acquisitionProvenance, notNullValue());
        assertThat(acquisitionProvenance.getSourceName(), equalTo(RESOURCE_API_SOURCE_NAME));
        assertThat(acquisitionProvenance.getModality(), equalTo(SENSED));
        assertThat(acquisitionProvenance.getAdditionalProperty("external_id").isPresent(), equalTo(true));
        assertThat(acquisitionProvenance.getAdditionalProperty("external_id").get(),
                equalTo("/fitnessActivities/465161536"));
    }

    @Test
    public void asDataPointsShouldReturnCorrectSelfReportedDataPoints() {

        List<DataPoint<PhysicalActivity>> dataPoints = mapper.asDataPoints(singletonList(responseNode));

        assertThat(dataPoints, notNullValue());
        assertThat(dataPoints.size(), equalTo(2));

        DataPoint<PhysicalActivity> dataPoint = dataPoints.get(1);

        DataPointAcquisitionProvenance acquisitionProvenance = dataPoint.getHeader().getAcquisitionProvenance();

        assertThat(acquisitionProvenance, notNullValue());
        assertThat(acquisitionProvenance.getModality(), equalTo(SELF_REPORTED));
    }

    @Test
    public void asDataPointsShouldNotCreateDataPointWhenOffsetMissing() throws IOException {

        ClassPathResource resource =
                new ClassPathResource(
                        "org/openmhealth/shim/runkeeper/mapper/runkeeper-fitness-activities-no-offset.json");
        JsonNode responseNodeWithoutUtcOffset = objectMapper.readTree(resource.getInputStream());

        List<DataPoint<PhysicalActivity>> dataPoints = mapper.asDataPoints(singletonList(responseNodeWithoutUtcOffset));

        assertThat(dataPoints.size(), equalTo(0));
    }
}


text:
/**
 * xkcd-today: Logic for popup
 */

var POPUP = {};
/**
 * An url to the api domain
 * @constant {string}
 */
POPUP.XKCD_API_MAIN = 'http://xkcd.com/';
/**
 * A path to the main file of api
 * @constant {string}
 */
POPUP.XKCD_API_PATH = 'info.0.json';
/**
 * Id of the latest entry
 * @type {number} [latestEntryId=-1]
 */
POPUP.latestEntryId = -1;
/**
 * Last loaded entry's id
 * @type {number} [lastEntryId=-1]
 */
POPUP.lastEntryId = -1;
/**
 * Shows loading error
 */
POPUP.onLoadingError = function () {
    $xkcdContent.removeClass('loading');
    var errorMessage =
        '
 \

 \
        Sorry, this post cannot be loaded.. \

 \

Try reloading.\

';
    $xkcdContent.html(errorMessage);
    $('[href=#reload]').click(function () {
        location.reload();
    });
};
/**
 * Fetches specified entry and shows it.
 * @param {number} [id=-1] Id of the entry to work with, if 0 (1 is the oldest)
 *                         the latest post is fetched.
 * @param {function} [callback=null] A function to be called after
 *                                   the entry has been loaded.
 */
POPUP.getEntryById = function getEntryById(id, callback) {
    entryLoading();

    if (typeof id !== 'number' || id < 0) {
        id = 0;
    }

    var apiUrl = this.XKCD_API_MAIN + this.XKCD_API_PATH;
    if (id > 0) {
        apiUrl = this.XKCD_API_MAIN + id + '/' + this.XKCD_API_PATH;
    }

    $.get(apiUrl, function (data) {
        if (!data['img']) {
            POPUP.onLoadingError();
        }

        $('.xkcd-title').text(data['safe_title']);
        $('.xkcd-alt').text(
            (data['alt'] !== "") ? data['alt'] : "No description"
        );

        var $img = $('#xkcd-img');
        $img.attr('src', data['img']);
        $img.on('load', function () {
            var $xkcdUrl = $('.xkcd-url');
            // Replace events from old entry
            $xkcdUrl.off('click');
            $xkcdUrl.on('click', function () {
                chrome.tabs.create({ url: 'http://xkcd.com/'+data['num'] });
            });

            entryReady();
        });

        var postDate = new Date(data['year'], data['month'], data['day']);
        $('.xkcd-date').text(postDate.toLocaleDateString());

        // Update lastEntryId
        POPUP.lastEntryId = data['num'];

        // Change icon to default (mark the notification as read)
        chrome.browserAction.setIcon({
            path: "images/icon19.png"
        });
        // Call the callback
        if (typeof callback === 'function') {
            callback();
        }

    }).fail(this.onLoadingError);
};
/**
 * Polls xkcd.com/info.0.json, gets the latest post and then shows it.
 */
POPUP.getLatestEntry = function getLatestEntry() {
    this.getEntryById(0, function () {
        $('.main.segment')
            .mouseenter(function () {
                $(this).dimmer('show');
            })
            .mouseleave(function () {
                $(this).dimmer('hide');
            });

        POPUP.latestEntryId = POPUP.lastEntryId;
        enableButton($btnPrev);
    });
};

/**
 * Enables button by removing .disabled class
 * @param {*|jQuery|HTMLElement} $button a JQuery element
 */
function enableButton($button) {
    $button.removeClass('disabled');
}
/**
 * Disables button by adding .disabled class
 * @param {*|jQuery|HTMLElement} $button a JQuery element
 */
function disableButton($button) {
    $button.addClass('disabled');
}
/**
 * Shows spinner, that indicates something is in progress.
 */
function entryLoading() {
    $xkcdContent.parent().addClass('loading');
}
/**
 * Hides spinner, indicates that content has been loaded
 */
function entryReady() {
    $xkcdContent.parent().removeClass('loading');
}

var $btnPrev = $('.btn-prev');
var $btnNext = $('.btn-next');

var $xkcdContent = $('#xkcd-content');

// Setup ui
$(function () {
    $btnPrev.click(function () {
        if ($btnPrev.hasClass('disabled')) {
            return;
        }

        POPUP.getEntryById(POPUP.lastEntryId - 1, function () {
            if (POPUP.lastEntryId < POPUP.latestEntryId) {
                enableButton($btnNext);
            }

            if (POPUP.lastEntryId <= 0) {
                disableButton($btnPrev);
            }
        });

        $('.xkcd-url').detach('click');
    });
    $btnNext.click(function () {
        if ($btnNext.hasClass('disabled')) {
            return;
        }

        POPUP.getEntryById(POPUP.lastEntryId + 1, function () {
            if (POPUP.lastEntryId >= POPUP.latestEntryId) {
                disableButton($btnNext);
            }

            if (POPUP.lastEntryId > 0) {
                enableButton($btnPrev);
            }
        });
    });
});

POPUP.getLatestEntry();


text:
"""Transformer model from "Attention Is All You Need".

The Transformer model consists of an encoder and a decoder. Both are stacks
of self-attention layers followed by feed-forward layers. This model yields
good results on a number of problems, especially in NLP and machine translation.

See "Attention Is All You Need" (https://arxiv.org/abs/1706.03762) for the full
description of the model and the results obtained with its early version.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import six
from six.moves import range  # pylint: disable=redefined-builtin

import tensorflow as tf

from tensorflow.python.ops import inplace_ops
from tensorflow.python.util import nest
from layers import common_attention
from layers import common_hparams
from layers import common_layers
from layers import transformer_layers
from utils import beam_search
from utils import registry
from utils import t2t_model

# Alias some commonly reused layers, here and elsewhere.
transformer_prepare_encoder = transformer_layers.transformer_prepare_encoder
transformer_encoder = transformer_layers.transformer_encoder
transformer_ffn_layer = transformer_layers.transformer_ffn_layer

@registry.register_model
class Transformer(t2t_model.T2TModel):
  """Attention net.  See file docstring."""

  def __init__(self, *args, **kwargs):
    super(Transformer, self).__init__(*args, **kwargs)

  def encode(self, inputs, target_space, hparams, features=None):
    """Encode transformer inputs.

    Args:
      inputs: Transformer inputs [batch_size, input_length, 1, hidden_dim] which
        will be flattened along the two spatial dimensions.
      target_space: scalar, target space ID.
      hparams: hyperparameters for model.
      features: optionally pass the entire features dictionary as well.
        This is needed now for "packed" datasets.

    Returns:
      Tuple of:
          encoder_output: Encoder representation.
              [batch_size, input_length, hidden_dim]
          encoder_decoder_attention_bias: Bias and mask weights for
              encoder-decoder attention. [batch_size, input_length]
    """
    inputs = common_layers.flatten4d3d(inputs)

    encoder_input, self_attention_bias, encoder_decoder_attention_bias = (
        transformer_prepare_encoder(
            inputs, target_space, hparams, features=features))

    encoder_input = tf.nn.dropout(encoder_input,
                                  1.0 - hparams.layer_prepostprocess_dropout)

    encoder_output = transformer_encoder(
        encoder_input,
        self_attention_bias,
        hparams,
        nonpadding=features_to_nonpadding(features, "inputs"))

    return encoder_output, encoder_decoder_attention_bias

  def decode(self,
             decoder_input,
             encoder_output,
             encoder_decoder_attention_bias,
             decoder_self_attention_bias,
             hparams,
             cache=None,
             kv_encdecs=None,
             decode_loop_step=None,
             nonpadding=None):
    """Decode Transformer outputs from encoder representation.

    Args:
      decoder_input: inputs to bottom of the model.
        [batch_size, decoder_length, hidden_dim]
      encoder_output: Encoder representation.
        [batch_size, input_length, hidden_dim]
      encoder_decoder_attention_bias: Bias and mask weights for
        encoder-decoder attention. [batch_size, input_length]
      decoder_self_attention_bias: Bias and mask weights for decoder
        self-attention. [batch_size, decoder_length]
      hparams: hyperparameters for model.
      cache: A dict, containing tensors which are the results of previous
        attentions, used for fast decoding.
      kv_encdecs: A dict, representing the keys and values for encoder-decoder
        attention used by decoding (inference).
      decode_loop_step: An integer, step number of the decoding loop.
        Only used for inference on TPU.
      nonpadding: optional Tensor with shape [batch_size, decoder_length]

    Returns:
      Final decoder representation. [batch_size, decoder_length, hidden_dim]
    """
    decoder_input = tf.nn.dropout(decoder_input,
                                  1.0 - hparams.layer_prepostprocess_dropout)

    decoder_output = transformer_decoder(
        decoder_input,
        encoder_output,
        decoder_self_attention_bias,
        encoder_decoder_attention_bias,
        hparams,
        cache=cache,
        kv_encdecs=kv_encdecs,
        decode_loop_step=decode_loop_step,
        nonpadding=nonpadding)

    if hparams.mode == tf.estimator.ModeKeys.TRAIN:
      # TPU does not react kindly to extra dimensions.
      # TODO(noam): remove this once TPU is more forgiving of extra dims.
      return decoder_output
    else:
      # Expand since t2t expects 4d tensors.
      return tf.expand_dims(decoder_output, axis=2)

  def body(self, features):
    """Transformer main model_fn.

    Args:
      features: Map of features to the model. Should contain the following:
          "inputs": Transformer inputs.
              [batch_size, input_length, 1, hidden_dim].
          "targets": Target decoder outputs.
              [batch_size, decoder_length, 1, hidden_dim]
          "target_space_id": A scalar int from data_generators.problem.SpaceID.

    Returns:
      Final decoder representation. [batch_size, decoder_length, hidden_dim]
    """
    hparams = self._hparams

    if self.has_input:
      inputs = features["inputs"]
      target_space = features["target_space_id"]
      encoder_output, encoder_decoder_attention_bias = self.encode(
          inputs, target_space, hparams, features=features)
    else:
      encoder_output, encoder_decoder_attention_bias = (None, None)

    targets = features["targets"]
    targets_shape = common_layers.shape_list(targets)
    targets = common_layers.flatten4d3d(targets)
    decoder_input, decoder_self_attention_bias = transformer_prepare_decoder(
        targets, hparams, features=features)
    decoder_output = self.decode(
        decoder_input,
        encoder_output,
        encoder_decoder_attention_bias,
        decoder_self_attention_bias,
        hparams,
        nonpadding=features_to_nonpadding(features, "targets"))

    return tf.reshape(decoder_output, targets_shape)

  # TODO(b/124008972): Refactor the code and change tests accordingly to test
  # public API rather than internal methods.
  def _beam_decode(self,
                   features,
                   decode_length,
                   beam_size,
                   top_beams,
                   alpha,
                   use_tpu=False):
    """Beam search decoding.

    Args:
      features: an map of string to `Tensor`
      decode_length: an integer.  How many additional timesteps to decode.
      beam_size: number of beams.
      top_beams: an integer. How many of the beams to return.
      alpha: Float that controls the length penalty. larger the alpha, stronger
        the preference for longer translations.
      use_tpu: A bool, whether to do beam decode on TPU.

    Returns:
      A dict of decoding results {
          "outputs": integer `Tensor` of decoded ids of shape
              [batch_size, top_beams, <= decode_length]
          "scores": decoding log probs from the beam search.
      }
    """
    with tf.variable_scope(self.name):
      return (
          self._fast_decode_tpu(
              features, decode_length, beam_size, top_beams, alpha))

  def _fast_decode_tpu(self,
                       features,
                       decode_length,
                       beam_size,
                       top_beams=1,
                       alpha=1.0):
    """Fast decoding.

    Implements beam search decoding on TPU.

    Args:
      features: A map of string to model features.
      decode_length: An integer, how many additional timesteps to decode.
      beam_size: An integer, number of beams.
      top_beams: An integer, how many of the beams to return.
      alpha: A float that controls the length penalty. Larger the alpha,
        stronger the preference for longer translations.

    Returns:
      A dict of decoding results {
          "outputs": integer `Tensor` of decoded ids of shape
              [batch_size, top_beams, <= decode_length]
          "scores": decoding log probs from the beam search.
      }.

    Raises:
      NotImplementedError: If there are multiple data shards or
        beam_size is one.
    """
    if beam_size == 1:
      raise NotImplementedError(
            "Greedy Decoding is not supported in this MLPerf version.")
    if "targets_segmentation" in features:
      raise NotImplementedError(
          "Decoding not supported on packed datasets "
          " If you want to decode from a dataset, use the non-packed version"
          " of the dataset when decoding.")
    hparams = self._hparams
    target_modality = self._problem_hparams.modality["targets"]

    if self.hparams.activation_dtype == "bfloat16":
      for k, v in sorted(six.iteritems(features)):
        if v.dtype == tf.float32:
          features[k] = tf.cast(v, tf.bfloat16)

    if self.has_input:
      inputs = features["inputs"]
      if target_modality.is_class_modality:
        decode_length = 1
      else:
        decode_length = (
            common_layers.shape_list(inputs)[1] + features.get(
                "decode_length", decode_length))

      # TODO(llion): Clean up this reshaping logic.
      inputs = tf.expand_dims(inputs, axis=1)
      if len(inputs.shape) < 5:
        inputs = tf.expand_dims(inputs, axis=4)
      s = common_layers.shape_list(inputs)
      batch_size = s[0]
      inputs = tf.reshape(inputs, [s[0] * s[1], s[2], s[3], s[4]])
      input_modality = self._problem_hparams.modality["inputs"]
      with tf.variable_scope(input_modality.name):
        inputs = input_modality.bottom(inputs)
      if self.hparams.activation_dtype == "bfloat16":
        inputs = tf.cast(inputs, tf.bfloat16)
      with tf.variable_scope("body"):
        encoder_output, encoder_decoder_attention_bias = self.encode(
            inputs,
            features["target_space_id"],
            hparams,
            features=features)
      partial_targets = None
    else:
      # The problem has no inputs.
      encoder_output = None
      encoder_decoder_attention_bias = None

      # Prepare partial targets.
      # In either features["inputs"] or features["targets"].
      # We force the outputs to begin with these sequences.
      partial_targets = features.get("inputs")
      if partial_targets is None:
        partial_targets = features["targets"]
      assert partial_targets is not None
      partial_targets = common_layers.expand_squeeze_to_nd(partial_targets, 2)
      partial_targets = tf.to_int64(partial_targets)
      partial_targets_shape = common_layers.shape_list(partial_targets)
      partial_targets_length = partial_targets_shape[1]
      decode_length = (
          partial_targets_length + features.get("decode_length", decode_length))
      batch_size = partial_targets_shape[0]

    def preprocess_targets(targets, i):
      """Performs preprocessing steps on the targets to prepare for the decoder.

      This includes:
        - Embedding the ids.
        - Flattening to 3D tensor.
        - Optionally adding timing signals.

      Args:
        targets: A tensor, inputs ids to the decoder. [batch_size, 1].
        i: An integer, Step number of the decoding loop.

      Returns:
        A tensor, processed targets [batch_size, 1, hidden_dim].
      """
      with tf.variable_scope(target_modality.name):
        targets = target_modality.targets_bottom(targets)
      if self.hparams.activation_dtype == "bfloat16":
        targets = tf.cast(targets, tf.bfloat16)
      targets = common_layers.flatten4d3d(targets)

      # TODO(llion): Explain! Is this even needed?
      targets = tf.cond(
          tf.equal(i, 0), lambda: tf.zeros_like(targets), lambda: targets)

      positional_encoding = common_attention.get_timing_signal_1d(
          decode_length + 1, hparams.hidden_size)
      positional_encoding_shape = positional_encoding.shape.as_list()
      positional_encoding = common_layers.cast_like(
          positional_encoding, targets)
      targets += tf.slice(
          positional_encoding, [0, i, 0],
          [positional_encoding_shape[0], 1, positional_encoding_shape[2]])
      return targets

    decoder_self_attention_bias = (
        common_attention.attention_bias_lower_triangle(decode_length))

    def symbols_to_logits_tpu_fn(ids, i, cache, kv_encdecs):
      """Go from ids to logits for next symbol on TPU.

      Args:
        ids: A tensor, symbol IDs.
        i: An integer, step number of the decoding loop. Only used for inference
          on TPU.
        cache: A dict, containing tensors which are the results of previous
          attentions, used for fast decoding.
        kv_encdecs: A dict, representing the keys and values for encoder-decoder
          attention used by decoding (inference).

      Returns:
        ret: A tensor, computed logits.
        cache: A dict, containing tensors which are the results of previous
            attentions, used for fast decoding.
      """
      ids = ids[:, -1:]
      targets = tf.expand_dims(tf.expand_dims(ids, axis=2), axis=3)
      targets = preprocess_targets(targets, i)

      bias_shape = decoder_self_attention_bias.shape.as_list()
      bias = tf.slice(decoder_self_attention_bias, [0, 0, i, 0],
                      [bias_shape[0], bias_shape[1], 1, bias_shape[3]])

      # All other states in the cache are batch major to accomendate gather
      # op for permutation.
      tiled_encoder_output = beam_search.merge_beam_dim(
          beam_search.expand_to_beam_size(encoder_output, beam_size))
      tiled_encoder_decoder_attention_bias = beam_search.merge_beam_dim(
          beam_search.expand_to_beam_size(
              encoder_decoder_attention_bias, beam_size))

      with tf.variable_scope("body"):
        body_outputs = self.decode(
            targets,
            tiled_encoder_output,
            tiled_encoder_decoder_attention_bias,
            bias,
            hparams,
            cache,
            kv_encdecs,
            i,
            nonpadding=features_to_nonpadding(features, "targets"))

      with tf.variable_scope(target_modality.name):
        logits = target_modality.top(body_outputs, None)

      ret = tf.squeeze(logits, axis=[1, 2, 3])
      if partial_targets is not None:
        # If the position is within the given partial targets, we alter the
        # logits to always return those values.
        # A faster approach would be to process the partial targets in one
        # iteration in order to fill the corresponding parts of the cache.
        # This would require broader changes, though.
        vocab_size = tf.shape(ret)[1]

        def forced_logits():
          return tf.one_hot(
              tf.tile(
                  tf.slice(partial_targets, [0, i],
                           [partial_targets.shape.as_list()[0], 1]),
                  [beam_size]), vocab_size, 0.0, -1e9)

        ret = tf.cond(
            tf.less(i, partial_targets_length), forced_logits, lambda: ret)
      return ret, cache

    ret = fast_decode_tpu(
        encoder_output=encoder_output,
        symbols_to_logits_fn=symbols_to_logits_tpu_fn,
        hparams=hparams,
        decode_length=decode_length,
        vocab_size=target_modality.top_dimensionality,
        beam_size=beam_size,
        top_beams=top_beams,
        alpha=alpha,
        batch_size=batch_size)
    if partial_targets is not None:
      ret["outputs"] = ret["outputs"][:, :, partial_targets_length:]
    return ret

def fast_decode_tpu(encoder_output,
                    symbols_to_logits_fn,
                    hparams,
                    decode_length,
                    vocab_size,
                    beam_size,
                    top_beams=1,
                    alpha=1.0,
                    sos_id=0,
                    eos_id=beam_search.EOS_ID,
                    batch_size=None,
                    scope_prefix="body/"):
  """Given encoder output and a symbols to logits function, does fast decoding.

  Implements beam search decoding for TPU.

  Args:
    encoder_output: A tensor, output from encoder.
    symbols_to_logits_fn: Incremental decoding, function mapping triple
      `(ids, step, cache)` to symbol logits.
    hparams: Run hyperparameters.
    decode_length: An integer, how many additional timesteps to decode.
    vocab_size: Output vocabulary size.
    beam_size: An integer, number of beams.
    top_beams: An integer, how many of the beams to return.
    alpha: A float that controls the length penalty. Larger the alpha, stronger
      the preference for longer translations.
    sos_id: Start-of-sequence symbol.
    eos_id: End-of-sequence symbol.
    batch_size: An integer, must be passed if there is no input.
    scope_prefix: str, prefix for decoder layer variable scopes.

  Returns:
    A dict of decoding results {
        "outputs": integer `Tensor` of decoded ids of shape
            [batch_size, top_beams, <= decode_length]
        "scores": decoding log probs from the beam search.
    }.

  Raises:
    NotImplementedError: If beam size > 1 with partial targets.
  """
  if encoder_output is not None:
    batch_size = common_layers.shape_list(encoder_output)[0]

  key_channels = hparams.attention_key_channels or hparams.hidden_size
  value_channels = hparams.attention_value_channels or hparams.hidden_size
  num_layers = hparams.num_decoder_layers or hparams.num_hidden_layers

  cache = {
      "layer_%d" % layer: {
          "k":
              tf.zeros([
                  batch_size,
                  hparams.num_heads,
                  key_channels // hparams.num_heads,
                  decode_length
              ], dtype=encoder_output.dtype),
          "v":
              tf.zeros([
                  batch_size,
                  hparams.num_heads,
                  value_channels // hparams.num_heads,
                  decode_length
              ], dtype=encoder_output.dtype),
      } for layer in range(num_layers)
  }

  kv_encdecs = {
      "layer_%d" % layer: {} for layer in range(num_layers)
  }
  if encoder_output is not None:
    for layer in range(num_layers):
      layer_name = "layer_%d" % layer
      with tf.variable_scope(
          "%sdecoder/%s/encdec_attention/multihead_attention" % (scope_prefix,
                                                                 layer_name)):
        k_encdec = common_attention.compute_attention_component(
            encoder_output, key_channels, hparams.num_heads, name="k")
        k_encdec = beam_search.merge_beam_dim(
            beam_search.expand_to_beam_size(k_encdec, beam_size))
        v_encdec = common_attention.compute_attention_component(
            encoder_output, value_channels, hparams.num_heads, name="v")
        v_encdec = beam_search.merge_beam_dim(
            beam_search.expand_to_beam_size(v_encdec, beam_size))
      kv_encdecs[layer_name]["k_encdec"] = k_encdec
      kv_encdecs[layer_name]["v_encdec"] = v_encdec

  initial_ids = sos_id * tf.ones([batch_size], dtype=tf.int32)
  decoded_ids, scores = beam_search.beam_search(
      symbols_to_logits_fn,
      initial_ids,
      beam_size,
      decode_length,
      vocab_size,
      alpha,
      states=cache,
      kv_encdecs=kv_encdecs,
      eos_id=eos_id,
      stop_early=(top_beams == 1))

  if top_beams == 1:
    decoded_ids = decoded_ids[:, 0, 1:]
    scores = scores[:, 0]
  else:
    decoded_ids = decoded_ids[:, :top_beams, 1:]
    scores = scores[:, :top_beams]

  return {"outputs": decoded_ids, "scores": scores}

def features_to_nonpadding(features, inputs_or_targets="inputs"):
  key = inputs_or_targets + "_segmentation"
  if features and key in features:
    return tf.minimum(tf.to_float(features[key]), 1.0)
  return None

def transformer_prepare_decoder(targets, hparams, features=None):
  """Prepare one shard of the model for the decoder.

  Args:
    targets: a Tensor.
    hparams: run hyperparameters
    features: optionally pass the entire features dictionary as well.
      This is needed now for "packed" datasets.

  Returns:
    decoder_input: a Tensor, bottom of decoder stack
    decoder_self_attention_bias: a bias tensor for use in decoder self-attention
  """
  decoder_self_attention_bias = (
      common_attention.attention_bias_lower_triangle(
          common_layers.shape_list(targets)[1]))

  if features and "targets_segmentation" in features:
    # "Packed" dataset - keep the examples from seeing each other.
    targets_segmentation = features["targets_segmentation"]
    targets_position = features["targets_position"]
    decoder_self_attention_bias += common_attention.attention_bias_same_segment(
        targets_segmentation, targets_segmentation)
  else:
    targets_position = None
  decoder_input = common_layers.shift_right_3d(targets)
  if targets_position is not None:
    decoder_input = common_attention.add_timing_signal_1d_given_position(
        decoder_input, targets_position)
  else:
    decoder_input = common_attention.add_timing_signal_1d(decoder_input)

  if hparams.activation_dtype == "bfloat16":
    decoder_self_attention_bias = tf.cast(decoder_self_attention_bias,
                                          tf.bfloat16)
  return (decoder_input, decoder_self_attention_bias)

def transformer_decoder(decoder_input,
                        encoder_output,
                        decoder_self_attention_bias,
                        encoder_decoder_attention_bias,
                        hparams,
                        cache=None,
                        kv_encdecs=None,
                        decode_loop_step=None,
                        name="decoder",
                        nonpadding=None):
  """A stack of transformer layers.

  Args:
    decoder_input: a Tensor
    encoder_output: a Tensor
    decoder_self_attention_bias: bias Tensor for self-attention
      (see common_attention.attention_bias())
    encoder_decoder_attention_bias: bias Tensor for encoder-decoder attention
      (see common_attention.attention_bias())
    hparams: hyperparameters for model
    cache: A dict, containing tensors which are the results of previous
      attentions, used for fast decoding.
    kv_encdecs: A dict, representing the keys and values for encoder-decoder
      attention used by decoding (inference).
    decode_loop_step: An integer, step number of the decoding loop.
      Only used for inference on TPU.
    name: a string
    nonpadding: optional Tensor with shape [batch_size, encoder_length]
      indicating what positions are not padding.  This is used
      to mask out padding in convolutional layers.  We generally only
      need this mask for "packed" datasets, because for ordinary datasets,
      no padding is ever followed by nonpadding.

  Returns:
    y: a Tensors
  """
  x = decoder_input
  attention_dropout_broadcast_dims = (
      common_layers.comma_separated_string_to_integer_list(
          getattr(hparams, "attention_dropout_broadcast_dims", "")))

  with tf.variable_scope(name):
    for layer in range(hparams.num_decoder_layers or hparams.num_hidden_layers):
      layer_name = "layer_%d" % layer
      layer_cache = cache[layer_name] if cache is not None else None
      layer_kv_encdecs = (kv_encdecs[layer_name] if kv_encdecs is not None
                          else None)
      with tf.variable_scope(layer_name):
        with tf.variable_scope("self_attention"):
          y = common_attention.multihead_attention(
              common_layers.layer_preprocess(x, hparams),
              None,
              decoder_self_attention_bias,
              hparams.attention_key_channels or hparams.hidden_size,
              hparams.attention_value_channels or hparams.hidden_size,
              hparams.hidden_size,
              hparams.num_heads,
              hparams.attention_dropout,
              cache=layer_cache,
              dropout_broadcast_dims=attention_dropout_broadcast_dims,
              max_length=hparams.get("max_length"),
              decode_loop_step=decode_loop_step)
          x = common_layers.layer_postprocess(x, y, hparams)
        if encoder_output is not None:
          with tf.variable_scope("encdec_attention"):
            y = common_attention.multihead_attention(
                common_layers.layer_preprocess(x, hparams),
                encoder_output,
                encoder_decoder_attention_bias,
                hparams.attention_key_channels or hparams.hidden_size,
                hparams.attention_value_channels or hparams.hidden_size,
                hparams.hidden_size,
                hparams.num_heads,
                hparams.attention_dropout,
                cache=layer_cache,
                kv_encdecs=layer_kv_encdecs,
                dropout_broadcast_dims=attention_dropout_broadcast_dims,
                max_length=hparams.get("max_length"))
            x = common_layers.layer_postprocess(x, y, hparams)
        with tf.variable_scope("ffn"):
          y = transformer_ffn_layer(
              common_layers.layer_preprocess(x, hparams),
              hparams,
              cache=layer_cache)
          x = common_layers.layer_postprocess(x, y, hparams)
    # if normalization is done in layer_preprocess, then it should also be done
    # on the output, since the output can grow very large, being the sum of
    # a whole stack of unnormalized layer outputs.
    return common_layers.layer_preprocess(x, hparams)

@registry.register_hparams
def transformer_base_v1():
  """Set of hyperparameters."""
  hparams = common_hparams.basic_params1()
  hparams.norm_type = "layer"
  hparams.hidden_size = 512
  hparams.batch_size = 4096
  hparams.max_length = 256
  hparams.clip_grad_norm = 0.  # i.e. no gradient clipping
  hparams.optimizer_adam_epsilon = 1e-9
  hparams.learning_rate_schedule = "legacy"
  hparams.learning_rate_decay_scheme = "noam"
  hparams.learning_rate = 0.1
  hparams.learning_rate_warmup_steps = 4000
  hparams.initializer_gain = 1.0
  hparams.num_hidden_layers = 6
  hparams.initializer = "uniform_unit_scaling"
  hparams.weight_decay = 0.0
  hparams.optimizer_adam_beta1 = 0.9
  hparams.optimizer_adam_beta2 = 0.98
  hparams.num_sampled_classes = 0
  hparams.label_smoothing = 0.1
  hparams.shared_embedding_and_softmax_weights = True

  # Add new ones like this.
  hparams.add_hparam("filter_size", 2048)
  # Layer-related flags. If zero, these fall back on hparams.num_hidden_layers.
  hparams.add_hparam("num_encoder_layers", 0)
  hparams.add_hparam("num_decoder_layers", 0)
  # Attention-related flags.
  hparams.add_hparam("num_heads", 8)
  hparams.add_hparam("attention_key_channels", 0)
  hparams.add_hparam("attention_value_channels", 0)
  hparams.add_hparam("parameter_attention_key_channels", 0)
  hparams.add_hparam("parameter_attention_value_channels", 0)
  # All hyperparameters ending in "dropout" are automatically set to 0.0
  # when not in training mode.
  hparams.add_hparam("attention_dropout", 0.0)
  hparams.add_hparam("attention_dropout_broadcast_dims", "")
  hparams.add_hparam("relu_dropout", 0.0)
  hparams.add_hparam("relu_dropout_broadcast_dims", "")
  hparams.add_hparam("pos", "timing")  # timing, none
  hparams.add_hparam("nbr_decoder_problems", 1)
  hparams.add_hparam("conv_first_kernel", 3)
  hparams.add_hparam("attention_variables_3d", False)
  hparams.add_hparam("use_target_space_embedding", True)
  # If specified, use this value instead of problem name in metrics.py.
  # This is useful for programs that can automatically compare experiments side
  #   by side based on the same metric names.
  hparams.add_hparam("overload_eval_metric_name", "")
  return hparams

@registry.register_hparams
def transformer_base_v2():
  """Set of hyperparameters."""
  hparams = transformer_base_v1()
  hparams.layer_preprocess_sequence = "n"
  hparams.layer_postprocess_sequence = "da"
  hparams.layer_prepostprocess_dropout = 0.1
  hparams.attention_dropout = 0.1
  hparams.relu_dropout = 0.1
  hparams.learning_rate_warmup_steps = 8000
  hparams.learning_rate = 0.2
  return hparams

@registry.register_hparams
def transformer_base_v3():
  """Base parameters for Transformer model."""
  # Update parameters here, then occasionally cut a versioned set, e.g.
  # transformer_base_v2.
  hparams = transformer_base_v2()
  hparams.optimizer_adam_beta2 = 0.997
  # New way of specifying learning rate schedule.
  # Equivalent to previous version.
  hparams.learning_rate_schedule = (
      "constant*linear_warmup*rsqrt_decay*rsqrt_hidden_size")
  hparams.learning_rate_constant = 2.0
  return hparams

@registry.register_hparams
def transformer_base():
  """Base parameters for Transformer model."""
  hparams = transformer_base_v3()
  return hparams

@registry.register_hparams
def transformer_test():
  hparams = transformer_base()
  hparams.num_hidden_layers = 2
  hparams.hidden_size = 16
  hparams.filter_size = 8
  hparams.num_heads = 2
  return hparams

@registry.register_hparams
def transformer_small():
  hparams = transformer_base()
  hparams.num_hidden_layers = 2
  hparams.hidden_size = 256
  hparams.filter_size = 1024
  hparams.num_heads = 4
  return hparams

@registry.register_hparams
def transformer_mlperf_tpu():
  """HParams for Transformer model on TPU for MLPerf on TPU 2x2."""
  hparams = transformer_base_v3()
  hparams.mlperf_mode = True
  hparams.symbol_modality_num_shards = 1
  hparams.max_length = 256  # ignored when using "_packed" problems
  hparams.batch_size = 2048  # per-chip batch size matches the reference model
  hparams.hidden_size = 1024
  hparams.filter_size = 4096
  hparams.num_heads = 16
  hparams.learning_rate_warmup_steps = 2000
  hparams.pad_batch = True
  hparams.activation_dtype = "bfloat16"
  return hparams

@registry.register_hparams
def transformer_mlperf_tpu_nan_test():
  hparams = transformer_mlperf_tpu()
  hparams.num_hidden_layers = 1
  hparams.hidden_size = 128
  hparams.filter_size = 512
  hparams.num_heads = 4
  return hparams

@registry.register_hparams
def transformer_mlperf_8_heads_tpu():
  hparams = transformer_mlperf_tpu()
  hparams.num_heads = 8
  return hparams

@registry.register_hparams
def transformer_mlperf_tpu_sm3():
  """HParams for Transformer model on TPU for MLPerf with SM3 Optimizer."""
  hparams = transformer_mlperf_tpu()
  hparams.optimizer = "SM3"
  hparams.optimizer_momentum_momentum=0.9
  hparams.learning_rate_schedule = ("constant*linear_warmup")
  hparams.learning_rate_constant = 0.25
  hparams.learning_rate_warmup_steps = 40000
  return hparams

@registry.register_hparams
def transformer_small_tpu():
  hparams = transformer_mlperf_tpu()
  hparams.num_hidden_layers = 2
  hparams.hidden_size = 128
  hparams.filter_size = 512
  hparams.num_heads = 4
  return hparams

@registry.register_ranged_hparams
def transformer_mlperf_tpu_base_range(rhp):
  """Small range of hyperparameters."""
  rhp.set_float("learning_rate_constant", 1.0, 8.0)
  rhp.set_discrete("learning_rate_warmup_steps",
                   [400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900,
                    950, 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400,
                    1450, 1500, 1550, 1600, 1650, 1700, 1750, 1800, 1850, 1900,
                    1950, 2000])
  rhp.set_float("optimizer_adam_beta1", 0.76, 0.95)
  rhp.set_float("optimizer_adam_beta2", 0.75, 0.998)


text:
require 'spec_helper'
require 'range_intersection'

RSpec.describe Range, '.&' do
  using RangeIntersection

  context 'when separate ranges' do
    it 'returns nil' do
      intersection = (1..3) & (4..5)
      expect(intersection).to be_nil
    end
  end

  context 'when first covers second' do
    it 'returns second' do
      intersection = (1..3) & (1.5..2.5)
      expect(intersection).to eq 1.5..2.5
    end
  end

  context 'when overlap each other' do
    it 'returns common part' do
      intersection = (1..3) & (2..4)
      expect(intersection).to eq 2..3
    end
  end

  context 'when diminishing ranges' do
    it 'works properly' do
      intersection = (3..1) & (1..3)
      expect(intersection).to eq 1..3
    end
  end
end


text:
void InitFreeImage(bool deinit=false){
    // need a new bool to avoid c++ "deinitialization order fiasco":
    // http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15
    static bool * bFreeImageInited = new bool(false);
    if(!*bFreeImageInited && !deinit){
        FreeImage_Initialise();
        *bFreeImageInited = true;
    }
    if(*bFreeImageInited && deinit){
        FreeImage_DeInitialise();
    }
}

FIBITMAP* loadImage(std::string _path){
    InitFreeImage();

    //  From http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=9
    //
    FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
    FIBITMAP* dib(0);
    fif = FreeImage_GetFileType(_path.c_str(), 0); // Check the file signature and deduce its format

    if(fif == FIF_UNKNOWN) // If still unknown, try to guess the file format from the file extension
        fif = FreeImage_GetFIFFromFilename(_path.c_str());

    if(fif == FIF_UNKNOWN){
        // If still unkown, return failure
        std::cerr << "Texture: Unknown type" << std::endl;
        return NULL;
    }

    if(FreeImage_FIFSupportsReading(fif)){
        // Check if the plugin has reading capabilities and load the file
        dib = FreeImage_Load(fif, _path.c_str());
        // std::cout << "Texture: loading " << _path << std::endl;
    }

    if(!dib){
        std::cerr << "Texture: unhable to open" << std::endl;
        return NULL;
    }

    FIBITMAP *image = FreeImage_ConvertTo32Bits(dib);
    FreeImage_Unload(dib);

    return image;
}

bool saveImage(const std::string& _path, unsigned char * _pixels, int _width, int _height, bool _dither = false) {
    InitFreeImage();

    FREE_IMAGE_FORMAT fif = FIF_PNG;//FreeImage_GetFileType(_path.c_str());

    // if (fif != FIF_UNKNOWN)
    {
        int bpp = 32;
        int pitch = ((((bpp * _width) + 31) / 32) * 4);

        FIBITMAP* bmp = FreeImage_ConvertFromRawBits(_pixels, _width, _height, pitch, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, false);

        if(_dither){
            FIBITMAP* ditherImg = FreeImage_Dither(bmp,FID_FS);
            FreeImage_Save(fif,ditherImg,_path.c_str());
            if (ditherImg != NULL){
                FreeImage_Unload(ditherImg);
            }
        } else {
            FreeImage_Save(fif,bmp,_path.c_str());
        }

        if (bmp != NULL){
            FreeImage_Unload(bmp);
            return true;
        } else {
            return false;
        }
    }
}


text:
package cognitosync_test

import (
	"context"
	"testing"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/awstesting/integration"
	"github.com/aws/aws-sdk-go/service/cognitosync"
)

var _ aws.Config
var _ awserr.Error
var _ request.Request

func TestInteg_00_ListIdentityPoolUsage(t *testing.T) {
	ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancelFn()

	sess := integration.SessionWithDefaultRegion("us-west-2")
	svc := cognitosync.New(sess)
	params := &cognitosync.ListIdentityPoolUsageInput{}
	_, err := svc.ListIdentityPoolUsageWithContext(ctx, params, func(r *request.Request) {
		r.Handlers.Validate.RemoveByName("core.ValidateParametersHandler")
	})
	if err != nil {
		t.Errorf("expect no error, got %v", err)
	}
}
func TestInteg_01_DescribeIdentityPoolUsage(t *testing.T) {
	ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancelFn()

	sess := integration.SessionWithDefaultRegion("us-west-2")
	svc := cognitosync.New(sess)
	params := &cognitosync.DescribeIdentityPoolUsageInput{
		IdentityPoolId: aws.String("us-east-1:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"),
	}
	_, err := svc.DescribeIdentityPoolUsageWithContext(ctx, params, func(r *request.Request) {
		r.Handlers.Validate.RemoveByName("core.ValidateParametersHandler")
	})
	if err == nil {
		t.Fatalf("expect request to fail")
	}
	aerr, ok := err.(awserr.RequestFailure)
	if !ok {
		t.Fatalf("expect awserr, was %T", err)
	}
	if len(aerr.Code()) == 0 {
		t.Errorf("expect non-empty error code")
	}
	if len(aerr.Message()) == 0 {
		t.Errorf("expect non-empty error message")
	}
	if v := aerr.Code(); v == request.ErrCodeSerialization {
		t.Errorf("expect API error code got serialization failure")
	}
}


text:
@interface WithDrawDepositConfigurator ()

@property (nonatomic,strong) WithDrawDepositInteractorImpl *interactor;

@property (nonatomic,strong) WithDrawDepositTableAdapter *tableAdapter;

@end

@implementation WithDrawDepositConfigurator

- (void)setup:(WithDrawDepositController *)vc{

    UITableView *tableView = (UITableView *)vc.tjs_listView;

    //1.

    WithDrawDepositDataSourceImpl *dataSoureImpl = [[WithDrawDepositDataSourceImpl alloc]init];
    WithDrawDepositLayoutImpl *layoutImpl = [[WithDrawDepositLayoutImpl alloc]initWithTableView:tableView];

    _interactor = [[WithDrawDepositInteractorImpl alloc]init];
    _interactor.delegate       = vc;
    _interactor.dataSource     = dataSoureImpl;
    _interactor.layout         = layoutImpl;
    [layoutImpl setDelegate:_interactor];

    //2.
    _tableAdapter = [[WithDrawDepositTableAdapter alloc]initWithTableView:tableView];
    _tableAdapter.interactor   = _interactor;
    _tableAdapter.cellDelegate = vc;

    //3.
    tableView.dataSource = _tableAdapter;
    tableView.delegate   = _tableAdapter;

    //4.
    [vc setInteractor:_interactor];

    //5.
    [layoutImpl beginRefresh];
}

@end


text:
(function($) {
  $.fn.tableFilter = function() {
    $(this).each(function() {
      var tableSelector = $(this).metadata().tableSelector;
      if (tableSelector == undefined)
        tableSelector = 'table.tablefilter';

      var initMsg = $(this).metadata().initMsg;
      if (initMsg == undefined)
        initMsg = 'search table ...';

      // initialize message in the search box
      $(this).val(initMsg);
      $(this).css('font-style', 'italic');

      // remove initial message on focus
      $(this).focus(function() {
        if ($(this).val() == initMsg)
          $(this).val('');
        $(this).css('font-style', 'normal');
      });

      // reinitialize message if search box is empty
      $(this).blur(function() {
        if ($(this).val() == '') {
          $(this).val(initMsg);
          $(this).css('font-style', 'italic');
        }
      });

      // init hidden search column
      $(tableSelector + " tr:has(td)").each(function() {
        var t = $(this).text().toLowerCase();
        $("	").hide().text(t).appendTo(this);
      });

      // hide rows not matching the query
      $(this).keyup(function() {
        var words = $(this).val().toLowerCase().split(" ");
        $(tableSelector + " tr:hidden").show();
        $.each(words, function() {
          $(tableSelector + " tr:visible .indexColumn:not(:contains('"
              + this + "'))").parent().hide();
        });
      });
    });
  }
})(jQuery);

$(function() {
  $("input.tablefilter").tableFilter();
});


text:
DR_CTL="${BASEURL}index.php?/module/inventory/"

# Get the scripts in the proper directories
${CURL} "${DR_CTL}get_script/inventory_add_plugins" -o "${MUNKIPATH}postflight.d/inventory_add_plugins.py"

# Check exit status of curl
if [ $? = 0 ]; then
	# Make executable
	chmod a+x "${MUNKIPATH}postflight.d/inventory_add_plugins.py"

	# Set preference to include this file in the preflight check
	setreportpref "inventory" "/Library/Managed Installs/ApplicationInventory.plist"

else
	echo "Failed to download all required components!"
	rm -f "${MUNKIPATH}postflight.d/inventory_add_plugins.py"
	ERR=1
fi


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

using OpenSim.Framework;
using OpenSim.Region.Framework.Scenes;

using OpenMetaverse;

namespace OpenSim.Region.Framework.Interfaces
{
    public interface IInventoryAccessModule
    {
        UUID CapsUpdateInventoryItemAsset(IClientAPI remoteClient, UUID itemID, byte[] data);
        UUID DeleteToInventory(DeRezAction action, UUID folderID, List<SceneObjectGroup> objectGroups, IClientAPI remoteClient);
        SceneObjectGroup RezObject(IClientAPI remoteClient, UUID itemID, Vector3 RayEnd, Vector3 RayStart,
                                    UUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
                                    bool RezSelected, bool RemoveItem, UUID fromTaskID, bool attachment);
        void TransferInventoryAssets(InventoryItemBase item, UUID sender, UUID receiver);
        bool GetAgentInventoryItem(IClientAPI remoteClient, UUID itemID, UUID requestID);

        // Must be here because of textures in user's inventory
        bool IsForeignUser(UUID userID, out string assetServerURL);
    }
}


text:
<?php

namespace Symfony\Component\Security\Tests\Core\Authentication\Voter;

use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;

/**
 * @author Roman Marintšenko <inoryy@gmail.com>
 */
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var AbstractVoter
     */
    private $voter;

    private $token;

    protected function setUp()
    {
        $this->voter = new VoterFixture();

        $tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
        $tokenMock
            ->expects($this->any())
            ->method('getUser')
            ->will($this->returnValue('user'));

        $this->token = $tokenMock;
    }

    /**
     * @dataProvider getData
     */
    public function testVote($expectedVote, $object, $attributes, $message)
    {
        $this->assertEquals($expectedVote, $this->voter->vote($this->token, $object, $attributes), $message);
    }

    public function getData()
    {
        return array(
            array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'),
            array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'),
            array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'),
            array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'),
            array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'),
            array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
            array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
            array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'),
        );
    }
}

class VoterFixture extends AbstractVoter
{
    protected function getSupportedClasses()
    {
        return array(
            'Symfony\Component\Security\Tests\Core\Authentication\Voter\ObjectFixture',
        );
    }

    protected function getSupportedAttributes()
    {
        return array('foo', 'bar', 'baz');
    }

    protected function isGranted($attribute, $object, $user = null)
    {
        return $attribute === 'foo';
    }
}

class ObjectFixture
{
}

class UnsupportedObjectFixture
{
}


text:
<?php

use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model app\master\models\MasterCustomer */

$this->title = 'Update Master Customer: ' . $model->CustomerId;
$this->params['breadcrumbs'][] = ['label' => 'Master Customers', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->CustomerId, 'url' => ['view', 'id' => $model->CustomerId]];
$this->params['breadcrumbs'][] = 'Update';
?>

<?= Html::encode($this->title) ?>

    <?= $this->render('_form', [
        'model' => $model,
    ]) ?>


text:
const lineHeight = 24;
const userListWidth = 200;
const smallScreen = 600;

export function findBreakpoints(text) {
  const breakpoints = [];

  for (let i = 0; i < text.length; i++) {
    const char = text.charAt(i);

    if (char === ' ') {
      breakpoints.push({ end: i, next: i + 1 });
    } else if (char === '-' && i !== text.length - 1) {
      breakpoints.push({ end: i + 1, next: i + 1 });
    }
  }

  return breakpoints;
}

export function messageHeight(
  message,
  wrapWidth,
  charWidth,
  indent = 0,
  windowWidth
) {
  let pad = (6 + (message.from ? message.from.length + 1 : 0)) * charWidth;
  let height = lineHeight + 8;

  if (message.channel && windowWidth > smallScreen) {
    wrapWidth -= userListWidth;
  }

  if (pad + message.length * charWidth < wrapWidth) {
    return height;
  }

  const breaks = message.breakpoints;
  let prevBreak = 0;
  let prevPos = 0;

  for (let i = 0; i < breaks.length; i++) {
    if (pad + (breaks[i].end - prevBreak) * charWidth >= wrapWidth) {
      prevBreak = prevPos;
      pad = indent;
      height += lineHeight;
    }

    prevPos = breaks[i].next;
  }

  if (pad + (message.length - prevBreak) * charWidth >= wrapWidth) {
    height += lineHeight;
  }

  return height;
}