text:
'use strict';

module.exports = function (grunt) {

  // Project configuration.
  grunt.initConfig({
    jshint: {
      all: [
        'Gruntfile.js',
        'tasks/*.js',
        '<%= nodeunit.tests %>'
      ],
      options: {
        jshintrc: '.jshintrc'
      }
    },

    // Before generating any new files, remove any previously-created files.
    clean: {
      tests: ['tmp', '.pkg']
    },

    // Unit tests.
    nodeunit: {
      tests: ['test/*_test.js']
    }

  });

  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-nodeunit');
  grunt.loadNpmTasks('shelljs');
  grunt.loadTasks('tasks/');

  grunt.registerTask('test', ['clean', 'nodeunit']); //, 'clean'
  grunt.registerTask('autoupdate-test', ['autoupdate', 'jshint']);
  grunt.registerTask('default', ['jshint', 'test', 'clean']);

};

redpajama_set_name: RedPajamaGithub


text:
/*
 * $Id$
 */

/*

Copyright (c) 2000-2014 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

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

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

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

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.alert;

import java.io.*;
import java.util.*;
import org.lockss.test.*;
import org.lockss.util.*;

/**
 * This is the test class for org.lockss.alert.AlertConfig
 */
public class TestAlertConfig extends LockssTestCase {

  public void testNew() {
    AlertConfig c1 = new AlertConfig();
    assertEmpty(c1.getFilters());
    List lst = ListUtil.list(new AlertFilter(null, null));
    c1.setFilters(lst);
    assertEquals(lst, c1.getFilters());
  }

  public void testCopy() {
    AlertConfig c1 = new AlertConfig();
    List lst = ListUtil.list(new AlertFilter(null, null));
    c1.setFilters(lst);
    AlertConfig c2 = new AlertConfig(c1);
    assertEquals(lst, c2.getFilters());
  }

  public void testHashCodeEqual() {
    List list = new ArrayList();
    AlertConfig c1 = new AlertConfig(list);
    AlertConfig c2 = new AlertConfig(list);
    assertEquals(c1.hashCode(), c2.hashCode());
  }

  /**
   * While it is allowed for hashCode for different objects to be equal, these
   * tests are meant to catch simple errors which break the hash function by
   * making it insensitive to one of the unique vars
   */
  public void testHashCodeNotEqual() {
    List list1 = new ArrayList();
    list1.add("blah");
    List list2 = new ArrayList();
    list2.add("blah2");
    AlertConfig c1 = new AlertConfig(list1);
    AlertConfig c2 = new AlertConfig(list2);
    AlertConfig c3 = new AlertConfig();
    assertNotEquals(c1.hashCode(), c2.hashCode());
    assertNotEquals(c1.hashCode(), c3.hashCode());
    assertNotEquals(c2.hashCode(), c3.hashCode());
  }

  public void testGen() throws Exception {
    MockLockssDaemon daemon = getMockLockssDaemon();
    AlertManagerImpl mgr = new AlertManagerImpl();
    daemon.setAlertManager(mgr);
    mgr.initService(daemon);
    daemon.setDaemonInited(true);
    mgr.startService();

    List pwdAlertNames = ListUtil.list(Alert.PASSWORD_REMINDER.getName(),
				       Alert.ACCOUNT_DISABLED.getName());
    AlertFilter passwdFilt =
      new AlertFilter(AlertPatterns.CONTAINS(Alert.ATTR_NAME,
					     pwdAlertNames),
		      new AlertActionMail());
    AlertFilter crawlExclFilt =
      new AlertFilter(AlertPatterns.EQ(Alert.ATTR_NAME,
				       Alert.CRAWL_EXCLUDED_URL.getName()),
		      new AlertActionMail("exclmail"));
    AlertFilter devFilt =
      new AlertFilter(AlertPatterns.CONTAINS(Alert.ATTR_NAME,
					     ListUtil.list("devAlert1",
							   "devAlert2")),
		      new AlertActionMail("devmail"));

    List pats =
      ListUtil.list(AlertPatterns.MATCHES(Alert.ATTR_TEXT, "foo[123]pat"),
		    AlertPatterns.MATCHES(Alert.ATTR_TEXT, "foo[123]pat"));
    AlertFilter matchFilt =
      new AlertFilter(AlertPatterns.And(pats),
		      new AlertActionMail("regexpmail"));

    AlertConfig conf =
      new AlertConfig(ListUtil.list(passwdFilt, crawlExclFilt, devFilt,
				    matchFilt));

    File file = FileTestUtil.tempFile("alertconf", ".xml");
    File file2 = FileTestUtil.tempFile("alertconf", ".asc");
    mgr.storeAlertConfig(file, conf);

    Reader rdr =
      new org.lockss.filter.WhiteSpaceFilter(new BufferedReader(new FileReader(file)));
    String str = StringUtil.fromReader(rdr);
    AlertConfig lconf = mgr.loadAlertConfig(str);
    assertEquals(conf, lconf);

    String str2 = org.apache.commons.lang3.StringEscapeUtils.escapeXml(str);
    FileTestUtil.writeFile(file2, str2);

    // Ensure that the MATCHES predicate is processed correction when
    // deserialized.
    Alert alert = Alert.auAlert(Alert.CRAWL_FINISHED, new MockArchivalUnit());
    alert.setAttribute(Alert.ATTR_TEXT, "foo2pat");
    Collection foo = mgr.findMatchingActions(alert, lconf.getFilters());
    assertEquals("Didn't find MATCHES pattern. ", 1, foo.size());
    AlertAction act = (AlertAction)CollectionUtil.getAnElement(foo);
    assertEquals("regexpmail", ((AlertActionMail)act).getRecipients(alert));
  }
}


text:
Try finally (with long goto)
--FILE--
<?php
function foo () {
   try {
   } finally {
      goto label;
   }
label:
   return 1;
}

foo();
?>
--EXPECTF--
Fatal error: jump out of a finally block is disallowed in %stry_finally_005.php on line %d


text:
class Trap : public SpritesObject
{
public:
    Trap();
    ~Trap();

    virtual void Update() override;
    virtual void Render() override;
};


text:
require 'popper_js/version'
require 'popper_js/engine' if defined?(Rails)


text:
Sort Filter
===========

Filter used for sorting the documents.

For example, let's say we have `item` repository which contains the following data:

+---------+------------+
| item_id | item_color |
+=========+============+
| 1       | red        |
+---------+------------+
| 2       | red        |
+---------+------------+
| 3       | blue       |
+---------+------------+
| 4       | green      |
+---------+------------+
| 5       | blue       |
+---------+------------+

If we have a sort filter with choice to sort on `item_color` in descending order and this choice is selected, we will get:

+---------+------------+
| item_id | item_color |
+=========+============+
| 1       | red        |
+---------+------------+
| 2       | red        |
+---------+------------+
| 4       | green      |
+---------+------------+
| 3       | blue       |
+---------+------------+
| 5       | blue       |
+---------+------------+

Configuration
-------------

First, you have to specify the request field:

+------------------------+--------------------------------------------------------------------------------------+
| Setting name           | Meaning                                                                              |
+========================+======================================================================================+
| `request_field`        | Request field used to pass the sort choice id (e.g. `www.page.com/?request_field=4`) |
+------------------------+--------------------------------------------------------------------------------------+
| `tags`                 | Array of filter specific tags that will be accessible at Twig view data.             |
+------------------------+--------------------------------------------------------------------------------------+

After which you can specify multiple sort options/choices:

+------------------------+--------------------------------------------------------------------+
| Setting name           | Meaning                                                            |
+========================+====================================================================+
| `label`                | Choice name to be used in templates. (e.g. `Title descending`)     |
+------------------------+--------------------------------------------------------------------+
| `field`                | Specifies the field in repository to sort on. (e.g. `item_color`)  |
+------------------------+--------------------------------------------------------------------+
| `order`                | Order to sort by. Default `asc`. Valid values: `asc`,  `desc`.     |
+------------------------+--------------------------------------------------------------------+
| `default`              | Specifies whether this choice is the default one. Default `false`. |
+------------------------+--------------------------------------------------------------------+
| `mode`                 | For any arrays: `min`, `max`, for numeric arrays `avg`, `sum`.     |
+------------------------+--------------------------------------------------------------------+
| `fields`               | Array of fields to sort on. For more information see table below.  |
+------------------------+--------------------------------------------------------------------+

.. note:: `field`, `order`, and `mode` are ignored if at least one of fields is defined.

Each object in `fields` array specifies sorting condition. Available parameters are defined below:

+------------------------+--------------------------------------------------------------------+
| Setting name           | Meaning                                                            |
+========================+====================================================================+
| `field`                | Specifies the field in repository to sort on. (e.g. `item_color`)  |
+------------------------+--------------------------------------------------------------------+
| `order`                | Order to sort by. Default `asc`. Valid values: `asc`,  `desc`.     |
+------------------------+--------------------------------------------------------------------+
| `mode`                 | For any arrays: `min`, `max`, for numeric arrays `avg`, `sum`.     |
+------------------------+--------------------------------------------------------------------+

Example:

.. code-block:: yaml

    # app/config/config.yml

    ongr_filter_manager:
        managers:
            item_list:
                filters:
                    - list_sorting
                repository: 'item'
        filters:
            sort:
                list_sorting:
                    request_field: 'sort'
                    choices:
                        - { label: Color ascending, field: item_color, default: true }
                        - { label: Color descending, field: item_color, order: desc }
                        - { label: 'In stock & cheap', fields: [{field: stock, order: desc}, {field: price}] }

..

Twig view data
--------------

View data returned by this filter to be used in template:

+-------------------------+--------------------------------------------------+
| Method                  | Value                                            |
+=========================+==================================================+
| getName()               | Filter name                                      |
+-------------------------+--------------------------------------------------+
| getResetUrlParameters() | Url parameters required to reset filter          |
+-------------------------+--------------------------------------------------+
| getState()              | Filter state                                     |
+-------------------------+--------------------------------------------------+
| getUrlParameters()      | Url parameters representing current filter state |
+-------------------------+--------------------------------------------------+
| getChoices()            | Returns a list of available sort choices         |
+-------------------------+--------------------------------------------------+
| getTags()               | Lists all tags specified at filter configuration |
+-------------------------+--------------------------------------------------+
| hasTag($tag)            | Checks if filter has the specific tag            |
+-------------------------+--------------------------------------------------+

Each choice has its own data:

+--------------------+--------------------------------------------+
| Method             | Value                                      |
+====================+============================================+
| isActive()         | Is this choice currently applied           |
+--------------------+--------------------------------------------+
| isDefault()        | Is this choice the default one             |
+--------------------+--------------------------------------------+
| getLabel()         | Choice label                               |
+--------------------+--------------------------------------------+
| getUrlParameters() | Returns a list of available choices        |
+--------------------+--------------------------------------------+
| getMode()          | Returns a mode value if is set             |
+--------------------+--------------------------------------------+


text:
<?php
namespace Hackcat\Zhima\Certification;

/**
 * ZHIMA API: zhima.customer.certification.initialize request
 *
 * @author auto create
 * @since 1.0, 2017-05-31 10:19:55
 */
class ZhimaCustomerCertificationInitializeRequest
{
	/**
	 * 认证场景码,支持的场景码有:
FACE:多因子活体人脸认证,
SMART_FACE:多因子快捷活体人脸认证,
FACE_SDK:SDK活体人脸认证
签约的协议决定了可以使用的场景
	 **/
	private $bizCode;

	/**
	 * 扩展业务参数,暂时没有用到,接口预留
	 **/
	private $extBizParam;

	/**
	 * 值为一个json串,无入参时值为"{}",有入参时必须指定身份类型identity_type,不同的身份类型对应的身份信息不同
当前支持:
身份信息为证件信息,identity_type值为CERT_INFO:
证件类型为身份证cert_type值为IDENTITY_CARD,必要信息cert_name和cert_no;
身份信息为短信手机号,适用于短信认证,identity_type值为SMS_MOBILE_NO:
证件类型可以为空,当证件类型为身份证cert_type值为IDENTITY_CARD,必要信息cert_name和cert_no,mobile_no可以为空,以上信息没有传入的时候会上用户录入;
身份信息为支付宝UID,identity_type值为USER_ID:
必要信息user_id
示例:{"identity_type": "USER_ID", "user_id": "2088172637486509"}
	 **/
	private $identityParam;

	/**
	 * 认证商户自定义配置,支持一些商户可选的功能
need_user_authorization: 值为true或者false
当值为true时,在认证用户引导页会展示用户授权协议,在认证通过后会进行授权,但是授权是否成功不影响认证结果
	 **/
	private $merchantConfig;

	/**
	 * 芝麻认证产品码,示例值为真实的产品码
	 **/
	private $productCode;

	/**
	 * 商户请求的唯一标志,32位长度的字母数字下划线组合。该标识作为对账的关键信息,商户要保证其唯一性.建议:前面几位字符是商户自定义的简称,中间可以使用一段日期,结尾可以使用一个序列
	 **/
	private $transactionId;

	private $apiParas = array();
	private $fileParas = array();
	private $apiVersion="1.0";
	private $scene;
	private $channel;
	private $platform;
	private $extParams;

	public function setBizCode($bizCode)
	{
		$this->bizCode = $bizCode;
		$this->apiParas["biz_code"] = $bizCode;
	}

	public function getBizCode()
	{
		return $this->bizCode;
	}

	public function setExtBizParam($extBizParam)
	{
		$this->extBizParam = $extBizParam;
		$this->apiParas["ext_biz_param"] = $extBizParam;
	}

	public function getExtBizParam()
	{
		return $this->extBizParam;
	}

	public function setIdentityParam($identityParam)
	{
		$this->identityParam = $identityParam;
		$this->apiParas["identity_param"] = $identityParam;
	}

	public function getIdentityParam()
	{
		return $this->identityParam;
	}

	public function setMerchantConfig($merchantConfig)
	{
		$this->merchantConfig = $merchantConfig;
		$this->apiParas["merchant_config"] = $merchantConfig;
	}

	public function getMerchantConfig()
	{
		return $this->merchantConfig;
	}

	public function setProductCode($productCode)
	{
		$this->productCode = $productCode;
		$this->apiParas["product_code"] = $productCode;
	}

	public function getProductCode()
	{
		return $this->productCode;
	}

	public function setTransactionId($transactionId)
	{
		$this->transactionId = $transactionId;
		$this->apiParas["transaction_id"] = $transactionId;
	}

	public function getTransactionId()
	{
		return $this->transactionId;
	}

	public function getApiMethodName()
	{
		return "zhima.customer.certification.initialize";
	}

	public function setScene($scene)
	{
		$this->scene=$scene;
	}

	public function getScene()
	{
		return $this->scene;
	}

	public function setChannel($channel)
	{
		$this->channel=$channel;
	}

	public function getChannel()
	{
		return $this->channel;
	}

	public function setPlatform($platform)
	{
		$this->platform=$platform;
	}

	public function getPlatform()
	{
		return $this->platform;
	}

	public function setExtParams($extParams)
	{
		$this->extParams=$extParams;
	}

	public function getExtParams()
	{
		return $this->extParams;
	}

	public function getApiParas()
	{
		return $this->apiParas;
	}

	public function getFileParas()
	{
		return $this->fileParas;
	}

	public function setApiVersion($apiVersion)
	{
		$this->apiVersion=$apiVersion;
	}

	public function getApiVersion()
	{
		return $this->apiVersion;
	}

}


text:
static NSString *const kReuqestMethod = @"POST";
static NSString *const kContentType = @"Content-Type";
static NSString *const kRequestContentTypeFormat = @"multipart/form-data; boundary=%@";
static NSString *const kMultiPartBoundary = @"--boundary_split_20130620";
static NSString *const kMultiStringPartFormat = @"\r\n--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@";
static NSString *const kDefaultMIME = @"application/octet-stream";
static NSString *const kMultiFilePartHeaderFormat = @"\r\n--%@\r\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n";
static NSString *const kMultiFilePartMIMEFormat = @"Content-Type: %@\r\n\r\n";
static NSString *const kMultipartEndFormat = @"\r\n--%@--\r\n";

@implementation ATHttpMultipartPost
{
    NSMutableDictionary *_stringDict;
    NSMutableDictionary *_fileDict;
    NSMutableDictionary *_fileMIMEDict;
}

- (id)init
{
    self = [super init];
    if (self) {
        _stringDict = [NSMutableDictionary dictionary];
        _fileDict = [NSMutableDictionary dictionary];
        _fileMIMEDict = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)dealloc
{
    _fileMIMEDict = nil;
    _fileDict = nil;
    _stringDict = nil;
}

- (void)clearAllParts
{
    [_stringDict removeAllObjects];
    [_fileDict removeAllObjects];
    [_fileMIMEDict removeAllObjects];
}

- (void)addPart:(NSString *)value forName:(NSString *)name
{
    if (!value || !name)
        return;
    [_stringDict setObject:value forKey:name];
}

- (void)addFilePart:(NSString *)filePath forName:(NSString *)name fileMIME:(NSString *)fileMIME
{
    if (!filePath || !name)
        return;
    [_fileDict setObject:filePath forKey:name];
    if (fileMIME != nil) {
        [_fileMIMEDict setObject:fileMIME forKey:name];
    }
}

- (NSString *)getRequestContentType
{
    return [NSString stringWithFormat:kRequestContentTypeFormat, kMultiPartBoundary];
}

- (NSData *)getRequestBodyData
{
    NSMutableData *data = [NSMutableData data];
    for (NSString *name in [_stringDict allKeys]) {
        NSString *value = [_stringDict objectForKey:name];
        NSString *partString = [NSString stringWithFormat:kMultiStringPartFormat, kMultiPartBoundary, name, value];
        [data appendData:[partString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    for (NSString *name in [_fileDict allKeys]) {
        NSString *filePath = [_fileDict objectForKey:name];
        NSString *fileName = [filePath lastPathComponent];
        NSString *mime = [_fileMIMEDict objectForKey:name];
        if (mime == nil) {
            mime = kDefaultMIME;
        }
        NSString *partHeader = [NSString stringWithFormat:kMultiFilePartHeaderFormat, kMultiPartBoundary, name, fileName];
        [data appendData:[partHeader dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *partMIME = [NSString stringWithFormat:kMultiFilePartMIMEFormat, mime];
        [data appendData:[partMIME dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[NSData dataWithContentsOfFile:filePath]];
    }
    NSString *endString = [NSString stringWithFormat:kMultipartEndFormat, kMultiPartBoundary];
    [data appendData:[endString dataUsingEncoding:NSUTF8StringEncoding]];
    return data;
}

- (void)postToUrl:(NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url
                                                                   cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                               timeoutInterval:ATHttpRequestTimeout];
    [urlRequest setValue:[self getRequestContentType] forHTTPHeaderField:kContentType];
    [urlRequest setHTTPMethod: kReuqestMethod];
    [urlRequest setHTTPBody:[self getRequestBodyData]];
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    NSHTTPURLResponse *response = nil;
    NSError* error = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
    [self.delegate onHttpMultipartPostResponse:response data:responseData error:error];
}

@end


text:
<!DOCTYPE html>

  <meta charset="UTF-8">
  <title></title>
  <link rel="import" href="elements/elements.html">

<youtube-element></youtube-element>


text:
( function() {
	'use strict';

	function TVPopular(TVPopularService){
		return{
			restrict: 'EA',
			replace: true,
			templateUrl: './src/app/TVPopular/tvpopular.template.html',
			scope: {}, // = scope privé
			controllerAs: 'vm', //controller de directive
			bindToController: true,
			controller: function($log) {
				var vm = this;
				TVPopularService.getMovie().then(function(data) {
					console.log(data.data.results);
					vm.movies = data.data.results;
				});
			},
			link: function(scope, elm, attrs){
			}
		};
	}

	angular.module('TVPopularDirective', ['service.TVPopular'])
	.directive('TVPopular', TVPopular);
})();


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Security;
using Renci.SshNet.Messages.Connection;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Authentication;
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
using Renci.SshNet.Security.Cryptography.Ciphers;

namespace Renci.SshNet
{
    ///
    /// Represents remote connection information class.
    ///
    /// <remarks>
    /// This class is NOT thread-safe. Do not use the same <see cref="ConnectionInfo"/> with multiple
    /// client instances.
    /// </remarks>
    public class ConnectionInfo : IConnectionInfoInternal
    {
        internal static int DefaultPort = 22;

        ///
        /// The default connection timeout.
        ///
        /// <value>
        /// 30 seconds.
        /// </value>
        private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);

        ///
        /// The default channel close timeout.
        ///
        /// <value>
        /// 1 second.
        /// </value>
        private static readonly TimeSpan DefaultChannelCloseTimeout = TimeSpan.FromSeconds(1);

        ///
        /// Gets supported key exchange algorithms for this connection.
        ///
        public IDictionary<string, Type> KeyExchangeAlgorithms { get; private set; }

        ///
        /// Gets supported encryptions for this connection.
        ///
        public IDictionary<string, CipherInfo> Encryptions { get; private set; }

        ///
        /// Gets supported hash algorithms for this connection.
        ///
        public IDictionary<string, HashInfo> HmacAlgorithms { get; private set; }

        ///
        /// Gets supported host key algorithms for this connection.
        ///
        public IDictionary<string, Func<byte[], KeyHostAlgorithm>> HostKeyAlgorithms { get; private set; }

        ///
        /// Gets supported authentication methods for this connection.
        ///
        public IList<AuthenticationMethod> AuthenticationMethods { get; private set; }

        ///
        /// Gets supported compression algorithms for this connection.
        ///
        public IDictionary<string, Type> CompressionAlgorithms { get; private set; }

        ///
        /// Gets the supported channel requests for this connection.
        ///
        /// <value>
        /// The supported channel requests for this connection.
        /// </value>
        public IDictionary<string, RequestInfo> ChannelRequests { get; private set; }

        ///
        /// Gets a value indicating whether connection is authenticated.
        ///
        /// <value>
        /// <c>true</c> if connection is authenticated; otherwise, <c>false</c>.
        /// </value>
        public bool IsAuthenticated { get; private set; }

        ///
        /// Gets connection host.
        ///
        /// <value>
        /// The connection host.
        /// </value>
        public string Host { get; private set; }

        ///
        /// Gets connection port.
        ///
        /// <value>
        /// The connection port. The default value is 22.
        /// </value>
        public int Port { get; private set; }

        ///
        /// Gets connection username.
        ///
        public string Username { get; private set; }

        ///
        /// Gets proxy type.
        ///
        /// <value>
        /// The type of the proxy.
        /// </value>
        public ProxyTypes ProxyType { get; private set; }

        ///
        /// Gets proxy connection host.
        ///
        public string ProxyHost { get; private set; }

        ///
        /// Gets proxy connection port.
        ///
        public int ProxyPort { get; private set; }

        ///
        /// Gets proxy connection username.
        ///
        public string ProxyUsername { get; private set; }

        ///
        /// Gets proxy connection password.
        ///
        public string ProxyPassword { get; private set; }

        ///
        /// Gets or sets connection timeout.
        ///
        /// <value>
        /// The connection timeout. The default value is 30 seconds.
        /// </value>
        /// <example>
        ///
        /// </example>
        public TimeSpan Timeout { get; set; }

        ///
        /// Gets or sets the timeout to use when waiting for a server to acknowledge closing a channel.
        ///
        /// <value>
        /// The channel close timeout. The default value is 1 second.
        /// </value>
        /// <remarks>
        /// If a server does not send a <c>SSH_MSG_CHANNEL_CLOSE</c> message before the specified timeout
        /// elapses, the channel will be closed immediately.
        /// </remarks>
        public TimeSpan ChannelCloseTimeout { get; set; }

        ///
        /// Gets or sets the character encoding.
        ///
        /// <value>
        /// The character encoding. The default is <see cref="System.Text.Encoding.UTF8"/>.
        /// </value>
        public Encoding Encoding { get; set; }

        ///
        /// Gets or sets number of retry attempts when session channel creation failed.
        ///
        /// <value>
        /// The number of retry attempts when session channel creation failed. The default
        /// value is 10.
        /// </value>
        public int RetryAttempts { get; set; }

        ///
        /// Gets or sets maximum number of session channels to be open simultaneously.
        ///
        /// <value>
        /// The maximum number of session channels to be open simultaneously. The default
        /// value is 10.
        /// </value>
        public int MaxSessions { get; set; }

        ///
        /// Occurs when authentication banner is sent by the server.
        ///
        /// <example>
        ///
        /// </example>
        public event EventHandler<AuthenticationBannerEventArgs> AuthenticationBanner;

        ///
        /// Gets the current key exchange algorithm.
        ///
        public string CurrentKeyExchangeAlgorithm { get; internal set; }

        ///
        /// Gets the current server encryption.
        ///
        public string CurrentServerEncryption { get; internal set; }

        ///
        /// Gets the current client encryption.
        ///
        public string CurrentClientEncryption { get; internal set; }

        ///
        /// Gets the current server hash algorithm.
        ///
        public string CurrentServerHmacAlgorithm { get; internal set; }

        ///
        /// Gets the current client hash algorithm.
        ///
        public string CurrentClientHmacAlgorithm { get; internal set; }

        ///
        /// Gets the current host key algorithm.
        ///
        public string CurrentHostKeyAlgorithm { get; internal set; }

        ///
        /// Gets the current server compression algorithm.
        ///
        public string CurrentServerCompressionAlgorithm { get; internal set; }

        ///
        /// Gets the server version.
        ///
        public string ServerVersion { get; internal set; }

        ///
        /// Get the client version.
        ///
        public string ClientVersion { get; internal set; }

        ///
        /// Gets the current client compression algorithm.
        ///
        public string CurrentClientCompressionAlgorithm { get; internal set; }

        ///
        /// Initializes a new instance of the <see cref="ConnectionInfo"/> class.
        ///
        /// <param name="host">The host.</param>
        /// <param name="username">The username.</param>
        /// <param name="authenticationMethods">The authentication methods.</param>
        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="host"/> is a zero-length string.</exception>
        /// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
        public ConnectionInfo(string host, string username, params AuthenticationMethod[] authenticationMethods)
            : this(host, DefaultPort, username, ProxyTypes.None, null, 0, null, null, authenticationMethods)
        {
        }

        ///
        /// Initializes a new instance of the <see cref="ConnectionInfo"/> class.
        ///
        /// <param name="host">The host.</param>
        /// <param name="port">The port.</param>
        /// <param name="username">The username.</param>
        /// <param name="authenticationMethods">The authentication methods.</param>
        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
        public ConnectionInfo(string host, int port, string username, params AuthenticationMethod[] authenticationMethods)
            : this(host, port, username, ProxyTypes.None, null, 0, null, null, authenticationMethods)
        {
        }

        ///
        /// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
        ///
        /// <param name="host">Connection host.</param>
        /// <param name="port">Connection port.</param>
        /// <param name="username">Connection username.</param>
        /// <param name="proxyType">Type of the proxy.</param>
        /// <param name="proxyHost">The proxy host.</param>
        /// <param name="proxyPort">The proxy port.</param>
        /// <param name="proxyUsername">The proxy username.</param>
        /// <param name="proxyPassword">The proxy password.</param>
        /// <param name="authenticationMethods">The authentication methods.</param>
        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="username" /> is <c>null</c>, a zero-length string or contains only whitespace characters.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyHost" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyPort" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
        public ConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods)
        {
            if (host == null)
                throw new ArgumentNullException("host");
            port.ValidatePort("port");

            if (username == null)
                throw new ArgumentNullException("username");
            if (username.All(char.IsWhiteSpace))
                throw new ArgumentException("Cannot be empty or contain only whitespace.", "username");

            if (proxyType != ProxyTypes.None)
            {
                if (proxyHost == null)
                    throw new ArgumentNullException("proxyHost");
                proxyPort.ValidatePort("proxyPort");
            }

            if (authenticationMethods == null)
                throw new ArgumentNullException("authenticationMethods");
            if (authenticationMethods.Length == 0)
                throw new ArgumentException("At least one authentication method should be specified.", "authenticationMethods");

            //  Set default connection values
            Timeout = DefaultTimeout;
            ChannelCloseTimeout = DefaultChannelCloseTimeout;
            RetryAttempts = 10;
            MaxSessions = 10;
            Encoding = Encoding.UTF8;

            KeyExchangeAlgorithms = new Dictionary<string, Type>
                {
                    {"curve25519-sha256", typeof(KeyExchangeECCurve25519)},
                    {"curve25519-sha256@libssh.org", typeof(KeyExchangeECCurve25519)},
                    {"ecdh-sha2-nistp256", typeof(KeyExchangeECDH256)},
                    {"ecdh-sha2-nistp384", typeof(KeyExchangeECDH384)},
                    {"ecdh-sha2-nistp521", typeof(KeyExchangeECDH521)},
                    {"diffie-hellman-group-exchange-sha256", typeof (KeyExchangeDiffieHellmanGroupExchangeSha256)},
                    {"diffie-hellman-group-exchange-sha1", typeof (KeyExchangeDiffieHellmanGroupExchangeSha1)},
                    {"diffie-hellman-group16-sha512", typeof(KeyExchangeDiffieHellmanGroup16Sha512)},
                    {"diffie-hellman-group14-sha256", typeof (KeyExchangeDiffieHellmanGroup14Sha256)},
                    {"diffie-hellman-group14-sha1", typeof (KeyExchangeDiffieHellmanGroup14Sha1)},
                    {"diffie-hellman-group1-sha1", typeof (KeyExchangeDiffieHellmanGroup1Sha1)},
                };

            Encryptions = new Dictionary<string, CipherInfo>
                {
                    {"aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, new CtrCipherMode(iv), null))},
                    {"3des-cbc", new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), null))},
                    {"aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), null))},
                    {"aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), null))},
                    {"aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), null))},
                    {"blowfish-cbc", new CipherInfo(128, (key, iv) => new BlowfishCipher(key, new CbcCipherMode(iv), null))},
                    {"twofish-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), null))},
                    {"twofish192-cbc", new CipherInfo(192, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), null))},
                    {"twofish128-cbc", new CipherInfo(128, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), null))},
                    {"twofish256-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), null))},
                    ////{"serpent256-cbc", typeof(CipherSerpent256CBC)},
                    ////{"serpent192-cbc", typeof(...)},
                    ////{"serpent128-cbc", typeof(...)},
                    {"arcfour", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, false))},
                    {"arcfour128", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, true))},
                    {"arcfour256", new CipherInfo(256, (key, iv) => new Arc4Cipher(key, true))},
                    ////{"idea-cbc", typeof(...)},
                    {"cast128-cbc", new CipherInfo(128, (key, iv) => new CastCipher(key, new CbcCipherMode(iv), null))},
                    ////{"rijndael-cbc@lysator.liu.se", typeof(...)},
                    {"aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, new CtrCipherMode(iv), null))},
                    {"aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, new CtrCipherMode(iv), null))},
                };

            HmacAlgorithms = new Dictionary<string, HashInfo>
                {
                    {"hmac-md5", new HashInfo(16*8, CryptoAbstraction.CreateHMACMD5)},
                    {"hmac-md5-96", new HashInfo(16*8, key => CryptoAbstraction.CreateHMACMD5(key, 96))},
                    {"hmac-sha1", new HashInfo(20*8, CryptoAbstraction.CreateHMACSHA1)},
                    {"hmac-sha1-96", new HashInfo(20*8, key => CryptoAbstraction.CreateHMACSHA1(key, 96))},
                    {"hmac-sha2-256", new HashInfo(32*8, CryptoAbstraction.CreateHMACSHA256)},
                    {"hmac-sha2-256-96", new HashInfo(32*8, key => CryptoAbstraction.CreateHMACSHA256(key, 96))},
                    {"hmac-sha2-512", new HashInfo(64 * 8, CryptoAbstraction.CreateHMACSHA512)},
                    {"hmac-sha2-512-96", new HashInfo(64 * 8,  key => CryptoAbstraction.CreateHMACSHA512(key, 96))},
                    //{"umac-64@openssh.com", typeof(HMacSha1)},
                    {"hmac-ripemd160", new HashInfo(160, CryptoAbstraction.CreateHMACRIPEMD160)},
                    {"hmac-ripemd160@openssh.com", new HashInfo(160, CryptoAbstraction.CreateHMACRIPEMD160)},
                    //{"none", typeof(...)},
                };

            HostKeyAlgorithms = new Dictionary<string, Func<byte[], KeyHostAlgorithm>>
                {
                    {"ssh-ed25519", data => new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(), data)},
#if FEATURE_ECDSA
                    {"ecdsa-sha2-nistp256", data => new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(), data)},
                    {"ecdsa-sha2-nistp384", data => new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(), data)},
                    {"ecdsa-sha2-nistp521", data => new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(), data)},
#endif
                    {"ssh-rsa", data => new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data)},
                    {"ssh-dss", data => new KeyHostAlgorithm("ssh-dss", new DsaKey(), data)},
                    //{"x509v3-sign-rsa", () => { ... },
                    //{"x509v3-sign-dss", () => { ... },
                    //{"spki-sign-rsa", () => { ... },
                    //{"spki-sign-dss", () => { ... },
                    //{"pgp-sign-rsa", () => { ... },
                    //{"pgp-sign-dss", () => { ... },
                };

            CompressionAlgorithms = new Dictionary<string, Type>
                {
                    //{"zlib@openssh.com", typeof(ZlibOpenSsh)},
                    //{"zlib", typeof(Zlib)},
                    {"none", null},
                };

            ChannelRequests = new Dictionary<string, RequestInfo>
                {
                    {EnvironmentVariableRequestInfo.Name, new EnvironmentVariableRequestInfo()},
                    {ExecRequestInfo.Name, new ExecRequestInfo()},
                    {ExitSignalRequestInfo.Name, new ExitSignalRequestInfo()},
                    {ExitStatusRequestInfo.Name, new ExitStatusRequestInfo()},
                    {PseudoTerminalRequestInfo.Name, new PseudoTerminalRequestInfo()},
                    {ShellRequestInfo.Name, new ShellRequestInfo()},
                    {SignalRequestInfo.Name, new SignalRequestInfo()},
                    {SubsystemRequestInfo.Name, new SubsystemRequestInfo()},
                    {WindowChangeRequestInfo.Name, new WindowChangeRequestInfo()},
                    {X11ForwardingRequestInfo.Name, new X11ForwardingRequestInfo()},
                    {XonXoffRequestInfo.Name, new XonXoffRequestInfo()},
                    {EndOfWriteRequestInfo.Name, new EndOfWriteRequestInfo()},
                    {KeepAliveRequestInfo.Name, new KeepAliveRequestInfo()},
                };

            Host = host;
            Port = port;
            Username = username;

            ProxyType = proxyType;
            ProxyHost = proxyHost;
            ProxyPort = proxyPort;
            ProxyUsername = proxyUsername;
            ProxyPassword = proxyPassword;

            AuthenticationMethods = authenticationMethods;
        }

        ///
        /// Authenticates the specified session.
        ///
        /// <param name="session">The session to be authenticated.</param>
        /// <param name="serviceFactory">The factory to use for creating new services.</param>
        /// <exception cref="ArgumentNullException"><paramref name="session"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <c>null</c>.</exception>
        /// <exception cref="SshAuthenticationException">No suitable authentication method found to complete authentication, or permission denied.</exception>
        internal void Authenticate(ISession session, IServiceFactory serviceFactory)
        {
            if (serviceFactory == null)
                throw new ArgumentNullException("serviceFactory");

            IsAuthenticated = false;
            var clientAuthentication = serviceFactory.CreateClientAuthentication();
            clientAuthentication.Authenticate(this, session);
            IsAuthenticated = true;
        }

        ///
        /// Signals that an authentication banner message was received from the server.
        ///
        /// <param name="sender">The session in which the banner message was received.</param>
        /// <param name="e">The banner message.{</param>
        void IConnectionInfoInternal.UserAuthenticationBannerReceived(object sender, MessageEventArgs<BannerMessage> e)
        {
            var authenticationBanner = AuthenticationBanner;
            if (authenticationBanner != null)
            {
                authenticationBanner(this,
                    new AuthenticationBannerEventArgs(Username, e.Message.Message, e.Message.Language));
            }
        }

        IAuthenticationMethod IConnectionInfoInternal.CreateNoneAuthenticationMethod()
        {
            return new NoneAuthenticationMethod(Username);
        }

        IList<IAuthenticationMethod> IConnectionInfoInternal.AuthenticationMethods
        {
            get { return AuthenticationMethods.Cast<IAuthenticationMethod>().ToList(); }
        }
    }
}


text:
package org.apache.beam.sdk.extensions.sql.impl;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage;

import java.sql.Connection;
import java.sql.ResultSet;
import org.apache.beam.sdk.extensions.sql.meta.provider.test.TestTableProvider;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/** Tests for {@link BeamSqlEnv}. */
public class BeamSqlEnvTest {
  @Rule public ExpectedException exceptions = ExpectedException.none();

  @Test
  public void testCreateExternalTableInNestedTableProvider() throws Exception {
    TestTableProvider root = new TestTableProvider();
    TestTableProvider nested = new TestTableProvider();
    TestTableProvider anotherOne = new TestTableProvider();

    BeamSqlEnv env =
        BeamSqlEnv.builder(root)
            .addSchema("nested", nested)
            .addSchema("anotherOne", anotherOne)
            .setPipelineOptions(PipelineOptionsFactory.create())
            .build();

    Connection connection = env.connection;
    connection.createStatement().execute("CREATE EXTERNAL TABLE nested.person (id INT) TYPE test");
    connection.createStatement().execute("INSERT INTO nested.person(id) VALUES (1), (2), (6)");

    ResultSet rs = connection.createStatement().executeQuery("SELECT SUM(id) FROM nested.person");
    rs.next();

    assertEquals(9, rs.getInt(1));
  }

  @Test
  public void testPlannerClassNotFound() {
    exceptions.expect(RuntimeException.class);
    exceptions.expectCause(hasMessage(containsString("org.test.ClassNotFound")));

    TestTableProvider root = new TestTableProvider();
    BeamSqlEnv.builder(root)
        .setQueryPlannerClassName("org.test.ClassNotFound")
        .setPipelineOptions(PipelineOptionsFactory.create())
        .build();
  }
}


text:
require 'spec_helper'

describe "ConfigKeyException" do
  it "keeps the path elements that caused the exception as a String array" do
    SugarfreeConfig::ConfigKeyException.new(:el1).key_path_elements.must_equal(["el1"])
    SugarfreeConfig::ConfigKeyException.new([:el1, :el2]).key_path_elements.must_equal(["el1", "el2"])
  end
end


text:
$(document).on('ready', function(){
	"use strict";

	var API_KEY = '25ba1d228c97c25bfacf83fe70a1ec60';
	var url = 'http://gateway.marvel.com:80/v1/public/characters';

	var $searchInput = $('#search');
	var $searchBtn = $('#searchBtn');
	var $form = $('#form');
	var $name = $('#name');
	var $image = $('#image');
	var $description = $('#description');
	var $preloader = $('.preloader');
	var $seriesList = $('ul#series-list');
	var series = [];

	$searchInput.focus();

	//clear all the fields
	clearFields($name, $image, $description);

	$form.on('submit', function(e){
		e.preventDefault();

		$searchInput.focus();

		hideFields($name, $image, $description, $seriesList);
		clearFields($name, $image, $description, series);
		$preloader.show();

		$.ajax({
			type: 'GET',
			url: url + '?' + 'name=' + $searchInput.val() + '&' +'apikey=' + API_KEY,

			success: function(data) {
				var hero = data.data.results[0];

				showFields($name, $image, $description, $seriesList);

				if (typeof hero === "undefined") {
					hideFields($name, $image, $description, $seriesList);
					$name.text("humm, try it again... :/");
				} else {
					series = hero.series.items; // getting the series realted with this character
					$name.text(hero.name);
					$image.attr('src', hero.thumbnail.path + '.' + hero.thumbnail.extension);
					$description.text(hero.description);

					for(var item in series) {
						$seriesList.append(
							$('

').append (
								series[item].name
							)
						);
					}
				}
			},
			error: function(err) {
				console.error(err);
			},
			complete: function() {
				$preloader.hide();
				$searchInput.val('');
			}
		});
	});

	function clearFields($name, $image, $description, series) {
		$name.text('');
		$image.attr('src', 'images/placeholder.png');
		$description.text('');
	}
	function hideFields($name, $image, $description, $seriesList) {
		$name.hide();
		$image.hide();
		$description.hide();
		$seriesList.hide();
	}
	function showFields($name, $image, $description, $seriesList) {
		$name.show();
		$image.show();
		$description.show();
		$seriesList.show();
	}
});

/*Promise.resolve($.get(url))
	.then(function(results){
		var item = results.data.results[0];

		var name 	= item.name;
		var avatar	= item.thumbnail.path + "." + item.thumbnail.extension;
		var description	= item.description;

		$('h1')[0].innerHTML = name;
		$('img')[0].src = avatar;
		$('p')[0].innerHTML = description;
	})
	.catch(function(err){
		console.error(err.statusText);
	});*/


text:
using System;

namespace Octokit.Reactive
{
    public interface IObservableOauthClient
    {
        /// <summary>
        /// Gets the URL used in the first step of the web flow. The Web application should redirect to this URL.
        /// </summary>
        /// <param name="request">Parameters to the Oauth web flow login url</param>
        /// <returns></returns>
        Uri GetGitHubLoginUrl(OauthLoginRequest request);

        /// <summary>
        /// Makes a request to get an access token using the code returned when GitHub.com redirects back from the URL
        /// <see cref="GetGitHubLoginUrl">GitHub login url</see> to the application.
        /// </summary>
        /// <remarks>
        /// If the user accepts your request, GitHub redirects back to your site with a temporary code in a code
        /// parameter as well as the state you provided in the previous step in a state parameter. If the states don't
        /// match, the request has been created by a third party and the process should be aborted. Exchange this for
        /// an access token using this method.
        /// </remarks>
        /// <param name="request"></param>
        /// <returns></returns>
        IObservable<OauthToken> CreateAccessToken(OauthTokenRequest request);
    }
}


text:
<?php
// Heading
$_['heading_title']      = 'Stellar.org Network Payment';

// Text
$_['text_extension']     = 'Extensions';
$_['text_success']       = 'Success: You have modified Stellar.org Net payment module!';
$_['text_edit']          = 'Edit Stellar.org net Delivery';

// Entry
$_['entry_publicid']     = 'PublicId';
$_['entry_asset_code']   = 'Asset Code';
$_['entry_issuer']       = 'Issuer';
$_['entry_wallet_url']   = 'My_wallet URL';
$_['entry_tx_callback_token'] = 'Callback Security Token';
$_['entry_testnet_mode'] = 'TestNet Mode';
$_['entry_escrows_publicId'] = 'Escrow Services PublicId';
$_['entry_escrows_email'] = 'Escrow Services Email';
$_['entry_enable_escrow'] = 'Enable Escrow';
$_['entry_escrow_expire_hours'] = 'Escrow Expire hours';
$_['entry_escrow_vendor_signer_secret'] = 'Vendor Signer Secret Key';

$_['entry_escrow_min_fee'] = 'Escrow Min base fee XLM';
$_['entry_escrow_pct_fee'] = 'Escrow Percent Gross fee';
$_['entry_escrow_currency_value_mult'] = 'Escrow Currency Multiplier';

$_['entry_total']        = 'Total';
$_['entry_order_status'] = 'Order Status';
$_['entry_geo_zone']     = 'Geo Zone';
$_['entry_status']       = 'Status';
$_['entry_sort_order']   = 'Sort Order';

// Help
$_['help_total']         = 'The checkout total the order must reach before this payment method becomes active.';
$_['help_asset_code']    = 'The asset_code accepted to be recieved on stellar.org net example USD, if XLM is used, issuer input is not needed';
$_['help_publicid']      = 'The stellar.org net PublicId that payment of purchases will be sent to, example GDUPQ...';
$_['help_issuer']        = 'The stellar.org net Issuer Address of the asset_code that will be used in payment , example GBUY...';
$_['help_wallet_url']    = 'URL address of the My_wallet web app that is an option to make payments';
$_['help_tx_callback_token'] = 'Security Token sent from Stellar bridge to prove that stellar bridge sent the payment update info';
$_['help_testnet_mode'] = 'Tell the customer that you are running in steller.org testnet mode (not real money), Yes for testnet, No for Live mode';
$_['help_escrows_publicId'] = 'The Stellar.org PublicId address of the 3rd party escrow signer.';
$_['help_escrows_email'] = 'Email address of the 3rd party escrow signer';
$_['help_enable_escrow'] = 'If value is set to no or blank then escrow service mode will not be an option in checkout, default is blank (disabled) ';
$_['help_escrow_expire_hours'] = 'The number of hours before the time based transaction becomes valid for payment of the purchase in the event buyer doesnt clear';
$_['help_escrow_vendor_signer_secret'] = 'Vendor Signer Secret key that is used to sign incoming escrows transaction captures, it is not the same account that money is sent to';

$_['help_escrow_min_fee'] = 'Minimum base fee that the Escrow service charges per contract paid in XLM';
$_['help_escrow_pct_fee'] = 'Percentage of the gross purchase that the Escrow service charges per contract paid in XLM';
$_['help_escrow_currency_value_mult'] = 'The value that the Escrow service has set for the asset the vendor is using to calculate percentage fee example 500 might be used for USD to XLM if vendor used USD for product payment, contact Escrow service to get number';

// Error
$_['error_permission']   = 'Warning: You do not have permission to modify payment Stellar.org Net Delivery!';


text: export { default } from 'ember-es-adapter/adapters/adapter';


text:
Another theme for [Ghost](http://github.com/tryghost/ghost/).

To download, visit the [releases](https://github.com/landru29/another-ghost/releases) page.

## Copyright & License

Released under the [MIT license](LICENSE).


text:
namespace SIS.FRAMEWORK.Attributes.Methods
{
    public class HttpDeleteAttribute : HttpMethodAttribute
    {
        public override bool IsValid(string requestMethod)
        {
            return requestMethod.ToUpper() == "DELETE";
        }
    }
}


text:
title: "Unsupervised learning with K-means"
author: matt_upson
comments: yes
date: '2015-11-11'
modified: 2015-12-24
layout: post
excerpt: A simple implementation
published: true
status: publish
tags:
- R
- k means
- clustering
- unsupervised learning
- machine learning
categories: Rstats
---

*K* means is one of the simplest algorithms for unsupervised learning.
It works iteratively to cluster together similar values in a dataset of one to many dimensions ($X\in\mathbb{R}^{m \times n}$).
The algorithmic steps are simple, it relies only on arithmetic means, so it's pretty simple to understand, but can also be quite powerful.
Because it relies on random sampling to initiate the algorithm it can be quite slow however, as there is a need to complete many replications to get a robust result.

*K*-means features as an exercise in [Coursera](http://www.coursera.com)'s machine learning course, which I am working through.
In this post I produce a simple implementation of the *K* means algorithm in R, and because I'm trying to improve my package writing, I wrap it up into a simple package called [knn](https://github.com/ivyleavedtoadflax/knn).

### The problem

*K*-means can be divided into two steps.
In a two-dimensional example, given a dataset $X\in\mathbb{R}^{m \times 2}$, the first step is to calculate the closest centroid for each training example $x$.
In my implementation, centroids $\\{1,...,K\\}$ can be assigned manually, or are automatically generated by selecting $K$ random training examples, and setting these as the centroids.

Mathematically, for each training example $x^{i}$ we are minimising

$$
||x^{i}-\mu_j||^{2}
$$

i.e. calculating the Euclidean norm of the vector $x^{(i)}$.
This is very simply achieved through pythagorean trigonometry, and is already implemented in R with `norm`.

#### Generate dummy data

The first step is to generate dummy two dimensional data which will demonstrate the principles well.
It's expedient to functionalise this.

{% highlight r %}
set.seed(1337)
dummy_group <- function(x = 30, mean = 10, sd = 2) {

  cbind(
    rnorm(x, mean, sd),
    rnorm(x, mean, sd)
  )

}

X <- rbind(
  dummy_group(mean = 10),
  dummy_group(mean = 20),
  dummy_group(mean = 30)
)
{% endhighlight %}

Which gives us a small dataset with some clear clusters to work with.

{% highlight r %}
plot(
  X,
  xlab = expression(x[1]),
  ylab = expression(x[2])
)
{% endhighlight %}

[![plot of chunk 2015-11-11-plot-clusters](/figures/2015-11-11-plot-clusters-1.png)](/figures/2015-11-11-plot-clusters-1.png)

#### Calculating norms

Calculating norms can be done with base R using `norm`.

{% highlight r %}
## Note norm only accepts a matrix

norm(as.matrix(c(2,4)),type = "f")
{% endhighlight %}

{% highlight text %}
## [1] 4.472136
{% endhighlight %}

But, the `norm` function isn't too useful in the present case, as we really want to vectorise this process, so I use my own function `rowNorms`:

{% highlight r %}
## Define rowNorms()

rowNorms <- function(x) {

norms <- sqrt(rowSums(x ^ 2))
return(norms)

}
{% endhighlight %}

`rowNorms`  works across a matrix, and will return a vector of norms, whilst `norm` will return the norm of the matrix.

{% highlight r %}
rowNorms(X) %>% str
{% endhighlight %}

{% highlight text %}
##  num [1:90] 16.3 14.1 12.1 21.5 12.1 ...
{% endhighlight %}

This function is implemented in the [`knn`]() package.

#### Calculate distances

So now I can produce a vector of euclidean norms, I can put this into practice for calculating the distance between points $x^i$ and the initial centroids.

In the first instance I will specify some centroids, although as noted, more usual practice is to randomly initiate these from $X$.

So to calculate the distance between some pre-specified centroids and $X$:

{% highlight r %}
## First create a matrix of initial centroids

centroids <- rbind(
c(15, 15),
c(25, 15),
c(25, 35)
)

## Create a matrix with m rows, consisting entirely of the first centroid.

first_centroid <- matrix(
  rep(centroids[1,], nrow(X)),
  ncol = 2,
  byrow = TRUE
  )

diff <- X - first_centroid

distances <- rowNorms(diff)

distances %>% str
{% endhighlight %}

{% highlight text %}
##  num [1:90] 5.24 8.38 9.19 2.58 9.06 ...
{% endhighlight %}

Rather than repeating this for all three initial centroids, I've wrapped this up into a function.
Here `knn::find_group()` calculates the closest centroid and returns this as a vector of values $\{1,..,K\}$, where $K$ is the number of centroids.

{% highlight r %}
groups <- find_group(X, centroids = centroids)

## Returns a vector of groups based on proximity to the initial centroids

groups %>% str
{% endhighlight %}

{% highlight text %}
##  int [1:90] 1 1 1 1 1 1 1 1 1 1 ...
{% endhighlight %}

#### Calculate centroids

The next step of the process is to now calculate the new centroid from the groups that were established in the previous step.

Again, this is qute a simple thing to code.
I achieved it by sequentially subsetting $X$ by $k$, and outputting the new centroids in a matrix  $Y\in\mathbb{R}^{K \times n}$.
I've wrapped this step into a function called `centroid_mean()`.

{% highlight r %}
Y <- centroid_mean(X, groups)
{% endhighlight %}

With the centroids calculated, now is a good chance to plot again, and check the sanity of the new centroids.

I've written a function `plot_knn()` to do this (it would make sense to roll this up into a plot method one day...).

Start with the original centroids...

{% highlight r %}
plot_knn(X, centroids)
{% endhighlight %}

[![plot of chunk 2015-11-11-first-plot](/figures/2015-11-11-first-plot-1.png)](/figures/2015-11-11-first-plot-1.png)

And now with the new centroids...

{% highlight r %}
plot_knn(X, Y)
{% endhighlight %}

[![plot of chunk 2015-11-11-new-centroids](/figures/2015-11-11-new-centroids-1.png)](/figures/2015-11-11-new-centroids-1.png)

Not bad, so it looks things are moving in the right direction, and with one further iteration, it looks like we are pretty close to the original centroids.

{% highlight r %}
groups1 <- find_group(X, centroids = Y)
Y1 <- centroid_mean(X, groups1)
plot_knn(X, Y1)
{% endhighlight %}

[![plot of chunk 2015-11-11-groups1](/figures/2015-11-11-groups1-1.png)](/figures/2015-11-11-groups1-1.png)

I could keep on going manually, but I have already implemented the whole process in the function `knn()`. So I'll try that instead, this time randomly initiating the centroids.

{% highlight r %}
# Set seed to make results reproducible
set.seed(1)
foo <- knn(X, k = 3)
plot_knn(X, foo$centroids, foo$groups)
{% endhighlight %}

[![plot of chunk 2015-11-11-knn](/figures/2015-11-11-knn-1.png)](/figures/2015-11-11-knn-1.png)

Great, so what are the final centroids?

{% highlight r %}
foo$centroids %>% round
{% endhighlight %}

{% highlight text %}
##      [,1] [,2]
## [1,]   11   10
## [2,]   30   30
## [3,]   20   20
{% endhighlight %}

...which as you'll remember were the starting values about which I generated the normal distributions.
Of course, it wouldn't normally be possible to assess the success of the algorithm so easily, and note that the order of the centroids has changed, although this doesn't matter.

### A harder test

I'll try again with a more difficult example.
Here I am still relying on three normally distributed clusters about the same centroids, but I specify a much greater standard deviation, so it is much more difficult with the human eye alone to separate the clusters.
I'll specify $x = 100$ so that the algorithm has more data to work with.

{% highlight r %}
X <- rbind(
  dummy_group(x = 100, mean = 10, sd = 8),
  dummy_group(x = 100, mean = 20, sd = 8),
  dummy_group(x = 100, mean = 30, sd = 8)
)

plot(
  X,
  xlab = expression(x[1]),
  ylab = expression(x[2])
    )
{% endhighlight %}

[![plot of chunk 2015-11-11-harder-test](/figures/2015-11-11-harder-test-1.png)](/figures/2015-11-11-harder-test-1.png)

Hard to see three distinct clusters...now what the algorithm makes of it.

Again, I use the `knn()` function which takes the leg work out of the programming.

In the example below I also set a seed which will make the results reproducible.
As I noted in the pre-amble, the solutions from *K*-means are dependent on the initial centroids, so it is usual practice to repeat the process many times, and then choose the group that has the highest probability.

I've not yet implemented this repetition in `knn` yet (and probably won't go that far), so for now I will use `set.seed()` to demonstrate the ideal and not-so-ideal outcomes.

{% highlight r %}
## set.seed(2) for a good outcome

set.seed(2)
foo <- knn(X)

plot_knn(X, foo$centroids, foo$groups)
{% endhighlight %}

[![plot of chunk 2015-11-11-good-outcome](/figures/2015-11-11-good-outcome-1.png)](/figures/2015-11-11-good-outcome-1.png)

{% highlight r %}
foo$centroids %>% round
{% endhighlight %}

{% highlight text %}
##      [,1] [,2]
## [1,]    9    7
## [2,]   33   29
## [3,]   18   21
{% endhighlight %}

So we're a little bit out this time after 3 steps, but not too far.
This could probably be improved by setting a more precise endpoint, which at present has a very simplistic implementation.

So what happens if I change the seed?

{% highlight r %}
set.seed(100)
bar <- knn(X)
plot_knn(X, bar$centroids, bar$groups)
{% endhighlight %}

[![plot of chunk 2015-11-11-bad-outcome](/figures/2015-11-11-bad-outcome-1.png)](/figures/2015-11-11-bad-outcome-1.png)

{% highlight r %}
bar$centroids %>% round
{% endhighlight %}

{% highlight text %}
##      [,1] [,2]
## [1,]   27   24
## [2,]    9    5
## [3,]    9   21
{% endhighlight %}

So the solution given by the algorithm has changed, and this highlights one of the issues with *K*-means: it can be very dependent on the initial location of centroids.
This is why it is usual to repeat the process many times, then essentially average the results.
I haven't gone that far with my simple implementation, but the in-built implementation in base R (`kmeans`) does include an argument `nstarts` which will give you $n$ possible scenarios.

So how well does `kmeans()` perform in the current scenario? Well not vastly different it turns out.

{% highlight r %}
foobar <- kmeans(X, centers = 3, nstart = 1)
plot_knn(X, foobar$centers, foobar$cluster)
{% endhighlight %}

[![plot of chunk 2015-11-11-base-kmeans](/figures/2015-11-11-base-kmeans-1.png)](/figures/2015-11-11-base-kmeans-1.png)

{% highlight r %}
foobar$centers %>% round
{% endhighlight %}

{% highlight text %}
##   [,1] [,2]
## 1    9    7
## 2   33   29
## 3   18   22
{% endhighlight %}

What about the improvement with additional replications?

{% highlight r %}
foobar <- kmeans(X, centers = 3, nstart = 1000)
plot_knn(X, foobar$centers, foobar$cluster)
{% endhighlight %}

[![plot of chunk 2015-11-11-base-kmeans-replication](/figures/2015-11-11-base-kmeans-replication-1.png)](/figures/2015-11-11-base-kmeans-replication-1.png)

{% highlight r %}
foobar$centers %>% round
{% endhighlight %}

{% highlight text %}
##   [,1] [,2]
## 1    9    6
## 2   18   21
## 3   33   29
{% endhighlight %}

At two decimal places, there is only a small difference between the final centroids, and those produced by only a single repetition using `kmeans()`.
Of course I should note that `kmeans()` uses a more complicated (and more accurate) algorithm than the simplified implementation I have used in [`knn`](https://github.com/ivyleavedtoadflax/knn).

#### A more interesting application

One of the ways that *K*-means can be used is for image compression.
Images are just arrays of values, which code for intensity.
A grayscale image requires just a single matrix, while a colour image typically requires three matrices.
By clustering similar colour values in an image, we can compress an image by reducing the number of colours required to render it.
Note that png images may also have a fourth channel which codes for alpha, or transparency.

I use the [jpeg]() package to load and image, in an example inspired by both [Markus Gesmann](http://www.magesblog.com/2012/12/now-i-see-it-k-means-cluster-analysis.html) and the Coursera machine learning course, but using my own implementation of *K*-means.

First I load in a holiday snap in glorious full colour...

{% highlight r %}
library(jpeg)

img <- readJPEG(source = "figures/2015-11-11-knn.jpeg")

## Get dimensions of image

dm <- dim(img)

## Create a matrix of x and y pixel positions and intensity values, and convert
## into rgb

img_df <- data_frame(
  x = rep(1:dm[2], each = dm[1]),
  y = rep(dm[1]:1, dm[2]),
  red = as.vector(img[,,1]),
  green = as.vector(img[,,2]),
  blue = as.vector(img[,,3]),
  cols = rgb(red, green, blue)
)

## Plot the original image

plot(
  y ~ x,
  data = img_df,
  main = "Venice canal (original)",
  col = cols,
  pch = ".",
  axes = FALSE,
  xlab = "",
  ylab = ""
)
{% endhighlight %}

[![plot of chunk 2015-11-11-256-colors](/figures/2015-11-11-256-colors-1.png)](/figures/2015-11-11-256-colors-1.png)

Now the same image again after clustering the colours into sixteen clusters.

{% highlight r %}
## Get clusters from the three intensity values.

kmeans16 <- img_df %>%
  select(red:blue) %>%
  knn(k = 16)

## Convert clusters into rgb values, and append as column to img_df

img_df$cols16 <- rgb(kmeans16$centroids[kmeans16$groups,])

## Plot the image again, this time using the new clustered colours.

plot(
  y ~ x,
  data = img_df,
  main = "Venice canal (16 colours)",
  col = cols16,
  pch = ".",
  axes = FALSE,
  xlab = "",
  ylab = ""
)
{% endhighlight %}

[![plot of chunk 2015-11-11-16-colors](/figures/2015-11-11-16-colors-1.png)](/figures/2015-11-11-16-colors-1.png)

So it works!
And whilst I have not attempted to calculate the resulting image compression, the reduction in possible colour values should lead to a pretty considerable reduction in the file size.

I'm not going to develop this package any further, because far better implementations are already out there, but it was a good exercise to learn more about *K*-means, and package writing in R.

{% highlight r %}
sessionInfo()
{% endhighlight %}

{% highlight text %}
## R version 3.2.3 (2015-12-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 14.04.3 LTS
##
## locale:
##  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C
##  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8
##  [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8
##  [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] methods   stats     graphics  grDevices utils     datasets  base
##
## other attached packages:
## [1] knn_0.1.3       jpeg_0.1-8      dplyr_0.4.3     testthat_0.11.0
## [5] knitr_1.11
##
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.2     digest_0.6.8    crayon_1.3.1    assertthat_0.1
##  [5] R6_2.1.1        DBI_0.3.1       formatR_1.2.1   magrittr_1.5
##  [9] evaluate_0.8    stringi_1.0-1   lazyeval_0.1.10 tools_3.2.3
## [13] stringr_1.0.0   parallel_3.2.3  memoise_0.2.1
{% endhighlight %}


text:
package org.javacore.initAndCleanup;

/**
 * Created by Guosi Chen on 2017/4/20.
 * 简单构造器的展示
 */
public class SimpleConstructor {

    static {
        System.out.println("hello");
    }

    public static void main(String[] args) {
        new Child();
        new Child("","");
    }
}

class Child extends Parent {

    private String lastName;

    static {
        System.out.println("Child static A... ");
    }

    public Child() {
        System.out.println("Child init... ");
    }

    public Child(String lastName) {
        this.lastName = lastName;
        System.out.println("Child lastName init... ");
    }

    public Child(String firstName, String lastName) {
        super(firstName);
        this.lastName = lastName;
        System.out.println("Child WHY init... ");
    }

    static {
        System.out.println("Child static B... ");
    }

}

class Parent {

    private String firstName;

    static {
        System.out.println("Parent static A... ");
    }

    public Parent() {
        System.out.println("Parent init... ");
    }

    public Parent(String firstName) {
        this.firstName = firstName;
        System.out.println("Parent firstName init... ");
    }

    static {
        System.out.println("Parent static B... ");
    }

}


text:
package com.travelersun.jpa.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import com.travelersun.utils.annotation.MetaData;
import com.travelersun.utils.json.JsonViews;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Access(AccessType.FIELD)
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "javassistLazyInitializer", "revisionEntity", "handler" }, ignoreUnknown = true)
@MappedSuperclass
public abstract class BaseEntity<ID extends Serializable> extends PersistableEntity<ID> {

    private static final long serialVersionUID = 2476761516236455260L;

    @MetaData(value = "乐观锁版本")
    @Column(name = "version", nullable = false)
    @JsonProperty
    @JsonView(JsonViews.Admin.class)
    private Integer version = 0;

    @Column(name = "create_by",length = 100)
    @JsonProperty
    @JsonView(JsonViews.Admin.class)
    private String createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @JsonProperty
    @Column(name = "created_date")
    protected Date createdDate;

    @JsonProperty
    @JsonView(JsonViews.Admin.class)
    @Column(name = "last_modified_by",length = 100)
    private String lastModifiedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @JsonProperty
    @JsonView(JsonViews.Admin.class)
    @Column(name = "last_modified_date")
    private Date lastModifiedDate;

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public Date getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(Date lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    private static final String[] PROPERTY_LIST = new String[] { "id", "version" };

    public String[] retriveCommonProperties() {
        return PROPERTY_LIST;
    }

    @Override
    @Transient
    @JsonProperty
    @JsonView(JsonViews.Admin.class)
    public String getDisplay() {
        return "[" + getId() + "]" + this.getClass().getSimpleName();
    }
}


text:
require 'haml'
require 'rails'

module Haml
  module Rails
    class Railtie < ::Rails::Railtie
      if ::Rails.version.to_f >= 3.1
        config.app_generators.template_engine :haml
      else
        config.generators.template_engine :haml
      end

      config.before_initialize do
        Haml.init_rails(binding)
        Haml::Template.options[:format] = :html5
      end
    end
  end
end


text:
#ifndef __XLSLIB_PUSH_PACK_4_H__
#define __XLSLIB_PUSH_PACK_4_H__

#if defined(HAVE_PSHPACK4_H)

#include <pshpack4.h>

#else

#if defined(HAVE_PRAGMA_PACK_PUSH_POP)

#pragma pack(push, 4)

#elif defined(HAVE_PRAGMA_PACK)

#pragma pack(4)

#else

#warning "No packing #pragma exists on this system - formulas will not work"

#endif
#endif

#endif


text:
<?php

// ------------------------------------------------------------------------

namespace O2System\Database\DataObjects\Result\Row\Record;

// ------------------------------------------------------------------------

use O2System\Spl\DataStructures\SplArrayObject;

/**
 * Class Update
 * @package O2System\Database\DataObjects\Result\Row\Record
 */
class Update extends SplArrayObject
{
    /**
     * Update::__construct
     */
    public function __construct()
    {
        parent::__construct([
            'user' => null,
            'timestamp' => null
        ]);
    }

    // ------------------------------------------------------------------------

    /**
     * Update::offsetSet
     *
     * @param string $index
     * @param mixed  $value
     *
     * @throws \Exception
     */
    public function offsetSet($index, $value)
    {
        if ($index === 'timestamp') {
            $value = new Timestamp($value);
        }

        parent::offsetSet($index, $value);
    }
}


text:
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gstdio.h>

/* Represents a word-frequency pair. */
typedef struct {
    gint freq;
    gchar *word;
} Pair;

/* Key and Value destroyer functions */
void free_data (gpointer data)
{
  g_free (data);
}

/* Compares two key-value pairs by frequency. */
gint compare_pair (gpointer v1, gpointer v2, gpointer user_data)
{
    Pair *p1 = (Pair *) v1;
    Pair *p2 = (Pair *) v2;
    return p1->freq - p2->freq;
}

/* Iterator that prints pairs. */
void pair_printor (gpointer value, gpointer user_data)
{
    Pair *pair = (Pair *) value;
    printf ("%d\t %s\n", pair->freq, pair->word);
}

/* Iterator that prints keys and values. */
void printor (gpointer key, gpointer value, gpointer user_data)
{
    printf (user_data, key, * (gint *) value);
}

/* Iterator that add key-value pairs to a sequence. */
void accumulator (gpointer key, gpointer value, gpointer user_data)
{
    GSequence *seq = (GSequence *) user_data;
    Pair *pair = g_new(Pair, 1);
    pair->word = (gchar*) key;
    pair->freq = * (gint *) value;

    g_sequence_insert_sorted (seq,
			      (gpointer) pair,
			      (GCompareDataFunc) compare_pair,
			      NULL);
}

/* Increments the frequency associated with key. */
void incr (GHashTable* hash, gchar *key)
{
    gint *val = (gint *) g_hash_table_lookup (hash, key);

    if (val == NULL) {
	gint *val1 = g_new (gint, 1);
	*val1 = 1;
	g_hash_table_insert (hash, g_strdup(key), val1);
    } else {
	*val += 1;
    }
}

int main (int argc, char** argv)
{
    gchar *filename;

    // open the file
    if (argc > 1) {
	filename = argv[1];
    } else {
	filename = "emma.txt";
    }

    FILE *fp = g_fopen(filename, "r");
    if (fp == NULL) {
	perror (filename);
	exit (-10);
    }

    /* string array is a (two-L) NULL terminated array of pointers to
       (one-L) NUL terminated strings */

    gchar line[128];
    GHashTable* hash = g_hash_table_new_full (g_str_hash, g_str_equal, free_data, free_data);
    int i;

    // read lines from the file and build the hash table
    while (1) {
  gchar **array; // create array here
	gchar *res = fgets (line, sizeof(line), fp);
	if (res == NULL) break;

	array = g_strsplit(line, " ", 0);
	for (i=0; array[i] != NULL; i++) {
	    incr(hash, array[i]);
	}
  g_strfreev(array); // then destroy array here
    }

    fclose (fp);

    // print the hash table
    // g_hash_table_foreach (hash,  (GHFunc) printor, "Word %s freq %d\n");

    // iterate the hash table and build the sequence
    GSequence *seq = g_sequence_new (free_data);
    // GSequence *seq = g_sequence_new (NULL);
    g_hash_table_foreach (hash,  (GHFunc) accumulator, (gpointer) seq);

    // iterate the sequence and print the pairs
    g_sequence_foreach (seq,  (GFunc) pair_printor, NULL);

    // try (unsuccessfully) to free everything
    // (in a future exercise, we will fix the memory leaks)
    puts("Free g_hash");
    g_hash_table_destroy (hash);
    puts("Free g_sequence");
    g_sequence_free (seq);

    return 0;
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Encoding Hint: 雀の往来 -->
<project>
 <property name="jnlp.title"    value="JST リストセルのアニメーション" />
 <property name="jnlp.Name"     value="ListCellAnimation" />
 <property name="jnlp.name"     value="listcellanimation" />
 <property name="jnlp.codebase" value="http://ateraimemo.com/swing/${jnlp.name}/" />
 <property name="jnlp.homepage" value="http://ateraimemo.com/Swing/${jnlp.Name}.html" />
</project>


text:
import requests
import json
import os

from .base import ScribdBase
from .. import internals
from .. import const

class ScribdBook(ScribdBase):
    """
    A class for downloading books off Scribd.

    Parameters
    ----------
    url : `str`
        A string containing Scribd book URL.
    """

    def __init__(self, book_url):
        super().__init__(book_url)
        self.filename = self.sanitized_title + ".md"
        self.url = book_url
        self._book_id = None

    @property
    def book_id(self):
        """
        Extracts the book ID.
        """
        if not self._book_id:
            splits = self.url.split("/")
            for split in splits:
                try:
                    book_id = int(split)
                except ValueError:
                    continue
            self._book_id = book_id
        return self._book_id

    def download(self, filename=None):
        """
        Processing text and image extraction.
        """
        if not filename:
            filename = self.filename

        token = self._get_token()
        chapter = 1

        while True:
            response = self.fetch_response(chapter, token)

            if response.status_code == 403:
                token = self._get_token()
                response = self.fetch_response(chapter, token)

                if response.status_code == 403:
                    print("No more content being exposed by Scribd!")
                    break

            try:
                json_response = json.loads(response.text)
            except ValueError:
                print("Completed downloading book!")
                break

            self._extract_text_blocks(json_response, chapter, token, filename)

            chapter += 1

        return filename

    def _extract_text(self, content, chapter, token):
        """
        Extracts text given a block of raw html.
        """
        words = []
        for word in content["words"]:
            if word.get("break_map", None):
                words.append(word["break_map"]["text"])
            elif word.get("text", None):
                words.append(word["text"])
            elif word.get("type", None) == "image":
                image_url = self._format_image_url(chapter, word["src"], token)
                string_text = self._process_image_text(word, image_url)
                words.append(string_text)
            else:
                words += self._extract_text(word, chapter, token)
        return words

    def fetch_response(self, chapter, token):
        url = self._format_content_url(chapter, token)
        response = requests.get(url)
        return response

    def _extract_text_blocks(self, response_dict, chapter, token, filename):
        """
        Extracts small blocks of raw book text and image
        URLs and writes them to a file.
        """
        for block in response_dict["blocks"]:
            if block["type"] == "text":
                string_text = (
                    " ".join(self._extract_text(block, chapter, token)) + "\n\n"
                )
            elif block["type"] == "image":
                image_url = self._format_image_url(chapter, block["src"], token)
                string_text = self._process_image_text(block, image_url)

            if block["type"] in ("text", "image"):
                print(string_text)
                self.save_text(string_text, filename)

    def _process_image_text(self, block, image_url):
        image_name = block["src"].replace("images/", "")
        image_path = os.path.join(self.sanitized_title, image_name)
        self._download_image(image_url, image_path)
        string_text = "![{}]({})\n\n".format(image_name, image_path)
        return string_text

    def _download_image(self, url, path):
        try:
            os.makedirs(os.path.dirname(path))
        except OSError:
            pass
        internals.download_stream(url, path)

    def _extract_image_path_from_url(self, url):
        image_name = url.split("/")[-1].split("?token=")[0]
        return os.path.join(self.book_id, image_name)

    def _format_content_url(self, chapter, token):
        """
        Generates a string which points to a URL containing
        the raw book text.
        """
        unformatted_url = (
            "https://www.scribd.com/scepub/{}/chapters/{}/" "contents.json?token={}"
        )
        return unformatted_url.format(self.book_id, chapter, token)

    def _format_image_url(self, chapter, image, token):
        """
        Generates a string which points to an image URL.
        """
        unformatted_url = "https://www.scribd.com/scepub/{}/chapters/{}/" "{}?token={}"
        return unformatted_url.format(self.book_id, chapter, image, token)

    def _get_token(self):
        """
        Fetches a uniquely generated token for the current
        session.
        """
        headers = const.premium_headers
        cookies = const.premium_cookies
        data = "data"

        token_url = "https://www.scribd.com/read2/{}/access_token".format(self.book_id)
        token = requests.post(token_url, headers=headers, cookies=cookies, data=data)
        return json.loads(token.text)["response"]

    def save_text(self, string_text, filename):
        """
        Appends text to the passed file.
        """
        with open(filename, "a") as f:
            f.write(string_text)