text:
package server

import (
	"context"
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"math/rand"
	"regexp"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"

	"github.com/Sirupsen/logrus"
	"github.com/drone/drone/model"
	"github.com/drone/drone/remote"
	"github.com/drone/drone/shared/httputil"
	"github.com/drone/drone/shared/token"
	"github.com/drone/drone/store"
	"github.com/drone/envsubst"

	"github.com/cncd/pipeline/pipeline/backend"
	"github.com/cncd/pipeline/pipeline/frontend"
	"github.com/cncd/pipeline/pipeline/frontend/yaml"
	"github.com/cncd/pipeline/pipeline/frontend/yaml/compiler"
	"github.com/cncd/pipeline/pipeline/frontend/yaml/linter"
	"github.com/cncd/pipeline/pipeline/frontend/yaml/matrix"
	"github.com/cncd/pipeline/pipeline/rpc"
	"github.com/cncd/pubsub"
	"github.com/cncd/queue"
)

//
// CANARY IMPLEMENTATION
//
// This file is a complete disaster because I'm trying to wedge in some
// experimental code. Please pardon our appearance during renovations.
//

var skipRe = regexp.MustCompile(`\[(?i:ci *skip|skip *ci)\]`)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func GetQueueInfo(c *gin.Context) {
	c.IndentedJSON(200,
		Config.Services.Queue.Info(c),
	)
}

func PostHook(c *gin.Context) {
	remote_ := remote.FromContext(c)

	tmprepo, build, err := remote_.Hook(c.Request)
	if err != nil {
		logrus.Errorf("failure to parse hook. %s", err)
		c.AbortWithError(400, err)
		return
	}
	if build == nil {
		c.Writer.WriteHeader(200)
		return
	}
	if tmprepo == nil {
		logrus.Errorf("failure to ascertain repo from hook.")
		c.Writer.WriteHeader(400)
		return
	}

	// skip the build if any case-insensitive combination of the words "skip" and "ci"
	// wrapped in square brackets appear in the commit message
	skipMatch := skipRe.FindString(build.Message)
	if len(skipMatch) > 0 {
		logrus.Infof("ignoring hook. %s found in %s", skipMatch, build.Commit)
		c.Writer.WriteHeader(204)
		return
	}

	repo, err := store.GetRepoOwnerName(c, tmprepo.Owner, tmprepo.Name)
	if err != nil {
		logrus.Errorf("failure to find repo %s/%s from hook. %s", tmprepo.Owner, tmprepo.Name, err)
		c.AbortWithError(404, err)
		return
	}

	// get the token and verify the hook is authorized
	parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
		return repo.Hash, nil
	})
	if err != nil {
		logrus.Errorf("failure to parse token from hook for %s. %s", repo.FullName, err)
		c.AbortWithError(400, err)
		return
	}
	if parsed.Text != repo.FullName {
		logrus.Errorf("failure to verify token from hook. Expected %s, got %s", repo.FullName, parsed.Text)
		c.AbortWithStatus(403)
		return
	}

	if repo.UserID == 0 {
		logrus.Warnf("ignoring hook. repo %s has no owner.", repo.FullName)
		c.Writer.WriteHeader(204)
		return
	}
	var skipped = true
	if (build.Event == model.EventPush && repo.AllowPush) ||
		(build.Event == model.EventPull && repo.AllowPull) ||
		(build.Event == model.EventDeploy && repo.AllowDeploy) ||
		(build.Event == model.EventTag && repo.AllowTag) {
		skipped = false
	}

	if skipped {
		logrus.Infof("ignoring hook. repo %s is disabled for %s events.", repo.FullName, build.Event)
		c.Writer.WriteHeader(204)
		return
	}

	user, err := store.GetUser(c, repo.UserID)
	if err != nil {
		logrus.Errorf("failure to find repo owner %s. %s", repo.FullName, err)
		c.AbortWithError(500, err)
		return
	}

	// if the remote has a refresh token, the current access token
	// may be stale. Therefore, we should refresh prior to dispatching
	// the build.
	if refresher, ok := remote_.(remote.Refresher); ok {
		ok, _ := refresher.Refresh(user)
		if ok {
			store.UpdateUser(c, user)
		}
	}

	// fetch the build file from the database
	confb, err := remote_.File(user, repo, build, repo.Config)
	if err != nil {
		logrus.Errorf("failure to get build config for %s. %s", repo.FullName, err)
		c.AbortWithError(404, err)
		return
	}
	sha := shasum(confb)
	conf, err := Config.Storage.Config.ConfigFind(repo, sha)
	if err != nil {
		conf = &model.Config{
			RepoID: repo.ID,
			Data:   string(confb),
			Hash:   sha,
		}
		err = Config.Storage.Config.ConfigCreate(conf)
		if err != nil {
			logrus.Errorf("failure to persist config for %s. %s", repo.FullName, err)
			c.AbortWithError(500, err)
			return
		}
	}
	build.ConfigID = conf.ID

	netrc, err := remote_.Netrc(user, repo)
	if err != nil {
		c.String(500, "Failed to generate netrc file. %s", err)
		return
	}

	// verify the branches can be built vs skipped
	branches, err := yaml.ParseString(conf.Data)
	if err == nil {
		if !branches.Branches.Match(build.Branch) && build.Event != model.EventTag && build.Event != model.EventDeploy {
			c.String(200, "Branch does not match restrictions defined in yaml")
			return
		}
	}

	// update some build fields
	build.RepoID = repo.ID
	build.Verified = true
	build.Status = model.StatusPending

	if repo.IsGated {
		allowed, _ := Config.Services.Senders.SenderAllowed(user, repo, build, conf)
		if !allowed {
			build.Status = model.StatusBlocked
		}
	}

	build.Trim()
	err = store.CreateBuild(c, build, build.Procs...)
	if err != nil {
		logrus.Errorf("failure to save commit for %s. %s", repo.FullName, err)
		c.AbortWithError(500, err)
		return
	}

	c.JSON(200, build)

	if build.Status == model.StatusBlocked {
		return
	}

	secs, err := Config.Services.Secrets.SecretListBuild(repo, build)
	if err != nil {
		logrus.Debugf("Error getting secrets for %s#%d. %s", repo.FullName, build.Number, err)
	}

	regs, err := Config.Services.Registries.RegistryList(repo)
	if err != nil {
		logrus.Debugf("Error getting registry credentials for %s#%d. %s", repo.FullName, build.Number, err)
	}

	// get the previous build so that we can send
	// on status change notifications
	last, _ := store.GetBuildLastBefore(c, repo, build.Branch, build.ID)

	//
	// BELOW: NEW
	//

	defer func() {
		uri := fmt.Sprintf("%s/%s/%d", httputil.GetURL(c.Request), repo.FullName, build.Number)
		err = remote_.Status(user, repo, build, uri)
		if err != nil {
			logrus.Errorf("error setting commit status for %s/%d", repo.FullName, build.Number)
		}
	}()

	b := builder{
		Repo:  repo,
		Curr:  build,
		Last:  last,
		Netrc: netrc,
		Secs:  secs,
		Regs:  regs,
		Link:  httputil.GetURL(c.Request),
		Yaml:  conf.Data,
	}
	items, err := b.Build()
	if err != nil {
		build.Status = model.StatusError
		build.Started = time.Now().Unix()
		build.Finished = build.Started
		build.Error = err.Error()
		store.UpdateBuild(c, build)
		return
	}

	var pcounter = len(items)

	for _, item := range items {
		build.Procs = append(build.Procs, item.Proc)
		item.Proc.BuildID = build.ID

		for _, stage := range item.Config.Stages {
			var gid int
			for _, step := range stage.Steps {
				pcounter++
				if gid == 0 {
					gid = pcounter
				}
				proc := &model.Proc{
					BuildID: build.ID,
					Name:    step.Alias,
					PID:     pcounter,
					PPID:    item.Proc.PID,
					PGID:    gid,
					State:   model.StatusPending,
				}
				build.Procs = append(build.Procs, proc)
			}
		}
	}
	err = store.FromContext(c).ProcCreate(build.Procs)
	if err != nil {
		logrus.Errorf("error persisting procs %s/%d: %s", repo.FullName, build.Number, err)
	}

	//
	// publish topic
	//
	message := pubsub.Message{
		Labels: map[string]string{
			"repo":    repo.FullName,
			"private": strconv.FormatBool(repo.IsPrivate),
		},
	}
	buildCopy := *build
	buildCopy.Procs = model.Tree(buildCopy.Procs)
	message.Data, _ = json.Marshal(model.Event{
		Type:  model.Enqueued,
		Repo:  *repo,
		Build: buildCopy,
	})
	// TODO remove global reference
	Config.Services.Pubsub.Publish(c, "topic/events", message)
	//
	// end publish topic
	//

	for _, item := range items {
		task := new(queue.Task)
		task.ID = fmt.Sprint(item.Proc.ID)
		task.Labels = map[string]string{}
		task.Labels["platform"] = item.Platform
		for k, v := range item.Labels {
			task.Labels[k] = v
		}

		task.Data, _ = json.Marshal(rpc.Pipeline{
			ID:      fmt.Sprint(item.Proc.ID),
			Config:  item.Config,
			Timeout: b.Repo.Timeout,
		})

		Config.Services.Logs.Open(context.Background(), task.ID)
		Config.Services.Queue.Push(context.Background(), task)
	}
}

// return the metadata from the cli context.
func metadataFromStruct(repo *model.Repo, build, last *model.Build, proc *model.Proc, link string) frontend.Metadata {
	return frontend.Metadata{
		Repo: frontend.Repo{
			Name:    repo.FullName,
			Link:    repo.Link,
			Remote:  repo.Clone,
			Private: repo.IsPrivate,
		},
		Curr: frontend.Build{
			Number:   build.Number,
			Parent:   build.Parent,
			Created:  build.Created,
			Started:  build.Started,
			Finished: build.Finished,
			Status:   build.Status,
			Event:    build.Event,
			Link:     build.Link,
			Target:   build.Deploy,
			Commit: frontend.Commit{
				Sha:     build.Commit,
				Ref:     build.Ref,
				Refspec: build.Refspec,
				Branch:  build.Branch,
				Message: build.Message,
				Author: frontend.Author{
					Name:   build.Author,
					Email:  build.Email,
					Avatar: build.Avatar,
				},
			},
		},
		Prev: frontend.Build{
			Number:   last.Number,
			Created:  last.Created,
			Started:  last.Started,
			Finished: last.Finished,
			Status:   last.Status,
			Event:    last.Event,
			Link:     last.Link,
			Target:   last.Deploy,
			Commit: frontend.Commit{
				Sha:     last.Commit,
				Ref:     last.Ref,
				Refspec: last.Refspec,
				Branch:  last.Branch,
				Message: last.Message,
				Author: frontend.Author{
					Name:   last.Author,
					Email:  last.Email,
					Avatar: last.Avatar,
				},
			},
		},
		Job: frontend.Job{
			Number: proc.PID,
			Matrix: proc.Environ,
		},
		Sys: frontend.System{
			Name: "drone",
			Link: link,
			Arch: "linux/amd64",
		},
	}
}

type builder struct {
	Repo  *model.Repo
	Curr  *model.Build
	Last  *model.Build
	Netrc *model.Netrc
	Secs  []*model.Secret
	Regs  []*model.Registry
	Link  string
	Yaml  string
}

type buildItem struct {
	Proc     *model.Proc
	Platform string
	Labels   map[string]string
	Config   *backend.Config
}

func (b *builder) Build() ([]*buildItem, error) {

	axes, err := matrix.ParseString(b.Yaml)
	if err != nil {
		return nil, err
	}
	if len(axes) == 0 {
		axes = append(axes, matrix.Axis{})
	}

	var items []*buildItem
	for i, axis := range axes {
		proc := &model.Proc{
			BuildID: b.Curr.ID,
			PID:     i + 1,
			PGID:    i + 1,
			State:   model.StatusPending,
			Environ: axis,
		}

		metadata := metadataFromStruct(b.Repo, b.Curr, b.Last, proc, b.Link)
		environ := metadata.Environ()
		for k, v := range metadata.EnvironDrone() {
			environ[k] = v
		}
		for k, v := range axis {
			environ[k] = v
		}

		var secrets []compiler.Secret
		for _, sec := range b.Secs {
			if !sec.Match(b.Curr.Event) {
				continue
			}
			secrets = append(secrets, compiler.Secret{
				Name:  sec.Name,
				Value: sec.Value,
				Match: sec.Images,
			})
		}

		y := b.Yaml
		s, err := envsubst.Eval(y, func(name string) string {
			return environ[name]
		})
		if err != nil {
			return nil, err
		}
		y = s

		parsed, err := yaml.ParseString(y)
		if err != nil {
			return nil, err
		}
		metadata.Sys.Arch = parsed.Platform
		if metadata.Sys.Arch == "" {
			metadata.Sys.Arch = "linux/amd64"
		}

		lerr := linter.New(
			linter.WithTrusted(b.Repo.IsTrusted),
		).Lint(parsed)
		if lerr != nil {
			return nil, lerr
		}

		var registries []compiler.Registry
		for _, reg := range b.Regs {
			registries = append(registries, compiler.Registry{
				Hostname: reg.Address,
				Username: reg.Username,
				Password: reg.Password,
				Email:    reg.Email,
			})
		}

		ir := compiler.New(
			compiler.WithEnviron(environ),
			compiler.WithEscalated(Config.Pipeline.Privileged...),
			compiler.WithVolumes(Config.Pipeline.Volumes...),
			compiler.WithNetworks(Config.Pipeline.Networks...),
			compiler.WithLocal(false),
			compiler.WithOption(
				compiler.WithNetrc(
					b.Netrc.Login,
					b.Netrc.Password,
					b.Netrc.Machine,
				),
				b.Repo.IsPrivate,
			),
			compiler.WithRegistry(registries...),
			compiler.WithSecret(secrets...),
			compiler.WithPrefix(
				fmt.Sprintf(
					"%d_%d",
					proc.ID,
					rand.Int(),
				),
			),
			compiler.WithEnviron(proc.Environ),
			compiler.WithProxy(),
			compiler.WithWorkspaceFromURL("/drone", b.Curr.Link),
			compiler.WithMetadata(metadata),
		).Compile(parsed)

		// for _, sec := range b.Secs {
		// 	if !sec.MatchEvent(b.Curr.Event) {
		// 		continue
		// 	}
		// 	if b.Curr.Verified || sec.SkipVerify {
		// 		ir.Secrets = append(ir.Secrets, &backend.Secret{
		// 			Mask:  sec.Conceal,
		// 			Name:  sec.Name,
		// 			Value: sec.Value,
		// 		})
		// 	}
		// }

		item := &buildItem{
			Proc:     proc,
			Config:   ir,
			Labels:   parsed.Labels,
			Platform: metadata.Sys.Arch,
		}
		if item.Labels == nil {
			item.Labels = map[string]string{}
		}
		items = append(items, item)
	}

	return items, nil
}

func shasum(raw []byte) string {
	sum := sha256.Sum256(raw)
	return fmt.Sprintf("%x", sum)
}


text:
@echo off

SETLOCAL

Python.exe -c '1+1'
IF ERRORLEVEL 0 (
  goto :HASPYTHON
) ELSE (
  goto :NOPYTHON
)

:NOPYTHON
echo "No python executable found. Please install python in order to use crash"
exit /b %ERRORLEVEL%

:HASPYTHON
REM Must set these first thing due to bug in Windows 7 when batch script filename has spaces in it
SET BATCH_SCRIPT_FOLDER_PATHNAME=%~dp0
Python.exe "%BATCH_SCRIPT_FOLDER_PATHNAME%/crash_standalone" %*
exit /b %ERRORLEVEL%

ENDLOCAL

redpajama_set_name: RedPajamaGithub


text:
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright 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 THE AUTHORS OR
// COPYRIGHT HOLDERS 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.

#import <sqlite3.h>

#import <AudioToolbox/AudioToolbox.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

#import <FBSDKCoreKit/FBSDKMacros.h>

/*!
 @class FBSDKDynamicFrameworkLoader

 @abstract
 This class provides a way to load constants and methods from Apple Frameworks in a dynamic
 fashion.  It allows the SDK to be just dragged into a project without having to specify additional
 frameworks to link against.  It is an internal class and not to be used by 3rd party developers.

 As new types are needed, they should be added and strongly typed.
 */
@interface FBSDKDynamicFrameworkLoader : NSObject

#pragma mark - Security Constants

/*!
 @abstract
 Load the kSecRandomDefault value from the Security Framework

 @return The kSecRandomDefault value or nil.
 */
+ (SecRandomRef)loadkSecRandomDefault;

/*!
 @abstract
 Load the kSecAttrAccessible value from the Security Framework

 @return The kSecAttrAccessible value or nil.
 */
+ (CFTypeRef)loadkSecAttrAccessible;

/*!
 @abstract
 Load the kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly value from the Security Framework

 @return The kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly value or nil.
 */
+ (CFTypeRef)loadkSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;

/*!
 @abstract
 Load the kSecAttrAccount value from the Security Framework

 @return The kSecAttrAccount value or nil.
 */
+ (CFTypeRef)loadkSecAttrAccount;

/*!
 @abstract
 Load the kSecAttrService value from the Security Framework

 @return The kSecAttrService value or nil.
 */
+ (CFTypeRef)loadkSecAttrService;

/*!
 @abstract
 Load the kSecAttrGeneric value from the Security Framework

 @return The kSecAttrGeneric value or nil.
 */
+ (CFTypeRef)loadkSecAttrGeneric;

/*!
 @abstract
 Load the kSecValueData value from the Security Framework

 @return The kSecValueData value or nil.
 */
+ (CFTypeRef)loadkSecValueData;

/*!
 @abstract
 Load the kSecClassGenericPassword value from the Security Framework

 @return The kSecClassGenericPassword value or nil.
 */
+ (CFTypeRef)loadkSecClassGenericPassword;

/*!
 @abstract
 Load the kSecAttrAccessGroup value from the Security Framework

 @return The kSecAttrAccessGroup value or nil.
 */
+ (CFTypeRef)loadkSecAttrAccessGroup;

/*!
 @abstract
 Load the kSecMatchLimitOne value from the Security Framework

 @return The kSecMatchLimitOne value or nil.
 */
+ (CFTypeRef)loadkSecMatchLimitOne;

/*!
 @abstract
 Load the kSecMatchLimit value from the Security Framework

 @return The kSecMatchLimit value or nil.
 */
+ (CFTypeRef)loadkSecMatchLimit;

/*!
 @abstract
 Load the kSecReturnData value from the Security Framework

 @return The kSecReturnData value or nil.
 */
+ (CFTypeRef)loadkSecReturnData;

/*!
 @abstract
 Load the kSecClass value from the Security Framework

 @return The kSecClass value or nil.
 */
+ (CFTypeRef)loadkSecClass;

@end

#pragma mark - Security APIs

// These are local wrappers around the corresponding methods in Security/SecRandom.h
FBSDK_EXTERN int fbsdkdfl_SecRandomCopyBytes(SecRandomRef rnd, size_t count, uint8_t *bytes);

// These are local wrappers around Keychain API
FBSDK_EXTERN OSStatus fbsdkdfl_SecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate);
FBSDK_EXTERN OSStatus fbsdkdfl_SecItemAdd(CFDictionaryRef attributes, CFTypeRef *result);
FBSDK_EXTERN OSStatus fbsdkdfl_SecItemCopyMatching(CFDictionaryRef query, CFTypeRef *result);
FBSDK_EXTERN OSStatus fbsdkdfl_SecItemDelete(CFDictionaryRef query);

#pragma mark - sqlite3 APIs

// These are local wrappers around the corresponding sqlite3 method from /usr/include/sqlite3.h
FBSDK_EXTERN SQLITE_API const char *fbsdkdfl_sqlite3_errmsg(sqlite3 *db);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_reset(sqlite3_stmt *pStmt);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_finalize(sqlite3_stmt *pStmt);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void *, int, char **, char **), void *arg, char **errmsg);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_close(sqlite3 *db);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_bind_double(sqlite3_stmt *stmt, int index, double value);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_bind_int(sqlite3_stmt *stmt, int index, int value);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_bind_text(sqlite3_stmt *stmt, int index, const char *value, int n, void(*callback)(void *));
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_step(sqlite3_stmt *stmt);
FBSDK_EXTERN SQLITE_API double fbsdkdfl_sqlite3_column_double(sqlite3_stmt *stmt, int iCol);
FBSDK_EXTERN SQLITE_API int fbsdkdfl_sqlite3_column_int(sqlite3_stmt *stmt, int iCol);
FBSDK_EXTERN SQLITE_API const unsigned char *fbsdkdfl_sqlite3_column_text(sqlite3_stmt *stmt, int iCol);

#pragma mark - Social Constants

FBSDK_EXTERN NSString *fbsdkdfl_SLServiceTypeFacebook(void);

#pragma mark - Social Classes

FBSDK_EXTERN Class fbsdkdfl_SLComposeViewControllerClass(void);

#pragma mark - QuartzCore Classes

FBSDK_EXTERN Class fbsdkdfl_CATransactionClass(void);

#pragma mark - QuartzCore APIs

// These are local wrappers around the corresponding transform methods from QuartzCore.framework/CATransform3D.h
FBSDK_EXTERN CATransform3D fbsdkdfl_CATransform3DMakeScale (CGFloat sx, CGFloat sy, CGFloat sz);
FBSDK_EXTERN CATransform3D fbsdkdfl_CATransform3DMakeTranslation (CGFloat tx, CGFloat ty, CGFloat tz);
FBSDK_EXTERN CATransform3D fbsdkdfl_CATransform3DConcat (CATransform3D a, CATransform3D b);

FBSDK_EXTERN const CATransform3D fbsdkdfl_CATransform3DIdentity;

#pragma mark - AudioToolbox APIs

// These are local wrappers around the corresponding methods in AudioToolbox/AudioToolbox.h
FBSDK_EXTERN OSStatus fbsdkdfl_AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID);
FBSDK_EXTERN OSStatus fbsdkdfl_AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID);
FBSDK_EXTERN void fbsdkdfl_AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID);

#pragma mark - AdSupport Classes

FBSDK_EXTERN Class fbsdkdfl_ASIdentifierManagerClass(void);

#pragma mark - Accounts Constants

FBSDK_EXTERN NSString *fbsdkdfl_ACFacebookAppIdKey(void);
FBSDK_EXTERN NSString *fbsdkdfl_ACFacebookAudienceEveryone(void);
FBSDK_EXTERN NSString *fbsdkdfl_ACFacebookAudienceFriends(void);
FBSDK_EXTERN NSString *fbsdkdfl_ACFacebookAudienceKey(void);
FBSDK_EXTERN NSString *fbsdkdfl_ACFacebookAudienceOnlyMe(void);
FBSDK_EXTERN NSString *fbsdkdfl_ACFacebookPermissionsKey(void);

#pragma mark - Accounts Classes

FBSDK_EXTERN Class fbsdkdfl_ACAccountStoreClass(void);

#pragma mark - StoreKit classes

FBSDK_EXTERN Class fbsdkdfl_SKPaymentQueueClass(void);
FBSDK_EXTERN Class fbsdkdfl_SKProductsRequestClass(void);

#pragma mark - AssetsLibrary Classes

FBSDK_EXTERN Class fbsdkdfl_ALAssetsLibraryClass(void);


text:
import mzCoder from '../index';

const dictionaryLength = 50;

describe('Test geo coder', () => {
  it('should test alphabet', () => {
    let alphabet = mzCoder.$.alphabet;

    expect(alphabet.length).toBe(dictionaryLength);
    expect(alphabet[0]).toBe('a');
    expect(alphabet[5]).toBe('f');
    expect(alphabet[25 + 1]).toBe('B');
  });

  it('should test septenary alphabet', () => {
    let septenaryAlphabet = mzCoder.$.septenaryAlphabet;

    expect(septenaryAlphabet[0]).toBe('0');
    expect(septenaryAlphabet[6]).toBe('6');
  });

  it('should test code number', () => {
    let codeNumber = mzCoder.$.codeNumber;

    expect(codeNumber(0)).toBe('a');
    expect(codeNumber(dictionaryLength)).toBe('ba');
    expect(codeNumber(dictionaryLength + 1)).toBe('bb');
    expect(codeNumber(5 * dictionaryLength + 5)).toBe('ff');
  });

  it('should test first triple', () => {
    let getFirstTriple = mzCoder.$.getFirstTriple;
    let codeNumber = mzCoder.$.codeNumber;

    expect(getFirstTriple(-90, -180)).toBe('aaa');
    expect(getFirstTriple(0, 0)).toBe(codeNumber(90 + 180 * 180));
    expect(getFirstTriple(90, 180)).toBe(codeNumber(180 + 360 * 180));
  });

  it('should test get code', () => {
    let getCode = mzCoder.getCode;

    expect(getCode(11.342045, 108.682251)).not.toBe('null');
    expect(getCode(11.342045, 108.682251)).toBe('vORtajRcz');
    expect(getCode(-26.273714, 137.109375)).toBe('xSzhHsREm');
  });

  it('should test get coordinates', () => {
    let getCoordinates = mzCoder.decode;
    let coordinates = getCoordinates('tkTsajPcr');

    expect(coordinates).not.toBeNull();
    expect(coordinates.Latitude).toBe(-87.34204);
    expect(coordinates.Longitude).toBe(73.51319);

    coordinates = getCoordinates('vnEwOtxtN');

    expect(coordinates.Latitude).toBe(-41.63278);
    expect(coordinates.Longitude).toBe(101.08654);

    coordinates = getCoordinates('vgChGrPDk');

    expect(coordinates.Latitude).toBe(17.27371);
    expect(coordinates.Longitude).toBe(99.08088);

    coordinates = getCoordinates('aaaaaaaaa');
    expect(coordinates).not.toBeNull();
  });

  it('should test code decode number', () => {
    let code = mzCoder.$.codeNumber(123456789);

    let decodeNumber = mzCoder.$.decodeNumber(code);

    expect(decodeNumber).toBe(123456789);
  });

  it('should test code decode', () => {
    let result = codeDecode(11.00186, 108.21945);

    expect(result.lat).toBe(result.roundedLat);
    expect(result.lng).toBe(result.roundedLng);

    result = codeDecode(11.34204, 108.682251);

    expect(result.lat).toBe(result.roundedLat);
    expect(result.lng).toBe(result.roundedLng);
  });

  it('fill with zeroes test', () => {
    let filledString = mzCoder.$.fillWithZeroesTillLimit('11', 6, '0');

    expect(filledString).toBe('000011');
  });

  it('should test pre defined array of codes', () => {
    let codeTestCases = [
      {
        code: 'funnnnnnn',
        lat: 52.19608,
        lng: -106.9804
      },
      {
        code: 'fxHkerTxv',
        lat: 42.1781,
        lng: -105.60773
      },
      {
        code: 'ySNLCFQEk',
        lat: -33.92884,
        lng: 151.15347
      }
    ];

    for (let testCase of codeTestCases) {
      let decodeResults = mzCoder.decode(testCase.code);
      expect(decodeResults.Latitude).toBe(testCase.lat);
      expect(decodeResults.Longitude).toBe(testCase.lng);

      let code = mzCoder.getCode(testCase.lat, testCase.lng);
      expect(code).toBe(testCase.code);
    }
  });

  it('should test calculating distance between coordinates', () => {
    let getDistance = mzCoder.getDistance;

    expect(getDistance([48.46799, 34.89118], [48.429505, 35.020269], 10)).toBe(10450);
    expect(getDistance([48.46799, 34.89118], [48.429505, 35.020269], 100)).toBe(10500);
    expect(getDistance([59.3293371, 13.4877472], [59.3225525, 13.4619422])).toBe(1649);
    expect(getDistance([59.3293371, 13.4877472], [59.3225525, 13.4619422], 10)).toBe(1650);
    expect(getDistance([52.518611, 13.408056], [55.751667, 37.617778], 100)).toBe(1610500);
  });
});

function codeDecode(lat, lng) {
  let result = {
    lat: null,
    lng: null,
    roundedLat: null,
    roundedLng: null
  };

  let code = mzCoder.getCode(lat, lng);

  let coordinates = mzCoder.decode(code);

  let roundedLat = Number(lat.toFixed(5));
  let roundedLng = Number(lng.toFixed(5));

  result.lat = coordinates.Latitude;
  result.lng = coordinates.Longitude;
  result.roundedLat = roundedLat;
  result.roundedLng = roundedLng;

  return result;
}


text:
using UnityEngine;
using System.Collections;

public class Explosion : MonoBehaviour {

	public RandomSound ExplosionSound;
	private bool firstUpdate = true;

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {
		if ( firstUpdate && ExplosionSound != null )
		{
			firstUpdate = false;
			ExplosionSound.PlayRandom();
		}
	}
}


text:
\documentclass{beamer}
%\documentclass[aspectratio=169]{beamer}
%
\mode<presentation>
{
  \usetheme{default}
  \usecolortheme{default}
  \usefonttheme{default}
  \setbeamertemplate{navigation symbols}{}
  \setbeamertemplate{caption}[numbered]
}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

\title[Classification]{Introduction to Machine Learning}
\subtitle{Lecture 4: Clustering}
\author{Alexis Zubiolo\newline\texttt{alexis.zubiolo@gmail.com}}
\institute{Data Science Team Lead @ Adcash}
\date{\today}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}{What is clustering?}
\textbf{Clustering} algorithms aim at \textbf{grouping unlabeled objects}: In the datasets, we have $x$, but no $y$!
\vfill
\pause
\textbf{Goal}: Find clusters such that objects in the same cluster are more similar to each other than to objects in other clusters.
\pause
\vfill
Main \textbf{challenges}:
\begin{itemize}
	\item What does \textit{similar} mean?
	\item Given a similarity definition, how do we define clusters?
	\item How many clusters do we choose?
\end{itemize}
\end{frame}

\begin{frame}{Clustering: A few applications}
\textbf{Ecology}: Define comparisons of animal/plant communities over time
\vfill
\textbf{Web}: Recognize communities of users
\vfill
\textbf{Marketing}: Define groups of customers with similar behavior/interests to target them more efficiently
\vfill
\textbf{Image processing}: Segment images (\textit{e.g.} segment different tissues in biomedical imaging)
\vfill
And sometimes, we just have no label in our data, so clustering can be a good first approach to tackle some problems.
\end{frame}

\begin{frame}{Practical example: Image segmentation}
\centering
\begin{figure}
\includegraphics[width=\textwidth]{images/segmentation.jpg}
\end{figure}
Very basic example of image segmentation with clustering, just based on the intensity level.
\end{frame}

\begin{frame}{Course outline}
In this course, we will see 2 clustering algorithms:
\pause
\begin{itemize}
	\item $k$-means
	\item Hierarchical clustering
\end{itemize}
\end{frame}

\begin{frame}
	\center \Huge{$k$-means}
\end{frame}

\begin{frame}{$k$-means: Problem definition}
\textbf{Purpose}: Split the set of points into $k$ classes. $k$ is a \textbf{parameter}!
\pause
\vfill
We look for a partition $S = \left\{ S_1, S_2, \dots, S_k\right\}$ minimizing the within-cluster sum of squares.
\begin{equation*}
\min_S \sum_{i = 1}^{k} \sum_{x \in S_i} \norm{x - \mu_i}_2^2
\end{equation*}
\pause
where
\begin{equation*}
\mu_i = \dfrac{1}{|S_i|} \sum_{x \in S_i} x
\end{equation*}
is the centroid (mean) of points in $S_i$.
\end{frame}

\begin{frame}{$k$-means: The algorithm}
$k$-means applies the following 2 iterations:
\pause
\begin{itemize}
	\item Define the \textbf{Voronoi diagram} generated by the $\mu_i$s:
	\begin{equation*}
		S_i^t = \left\{ x_p \mid \norm{x - \mu_i^t} \leq \norm{x - \mu_j^t}, 1 \leq j \leq k \right\}
	\end{equation*}
	\pause
	\item Update the centroid:
	\begin{equation*}
		\mu_i^{t+1} = \text{centroid}(S_i^t) = \dfrac{1}{S_i^t} \sum_{x \in S_i^t} x
	\end{equation*}
\end{itemize}
for all $i \in \left\{ 1, \dots, k\right\}$
\vfill
\pause
Problem in the above formulas: \textbf{initial value} for the $\mu_i$s?
\end{frame}

\begin{frame}{$k$-means: Initialization}
\textbf{Remark}: The $k$-means solution depends on the initial position of the $\mu_i$s centroids.

(see animation by Andrey Shabalin)
\vfill
\pause
2 related questions:
\begin{enumerate}
	\item How to choose the initial $\mu_i$s?
	\item How to have more stable results?
\end{enumerate}
\vfill
\pause
Unfortunately, no miracle strategy for Q1. A common strategy:
\begin{itemize}
	\item Several $k$-means with random initializations
	\item Majority vote
\end{itemize}
\end{frame}

\begin{frame}{Speeding up $k$-means}
Each $k$-means iteration is done over all the points in the dataset. This can be computationally expensive, especially if
\begin{itemize}
	\item There are many points
	\item The point density is big
\end{itemize}
What to do to \textbf{speed up the process}?
\pause
\vfill
\textbf{Alternative}: \textbf{Mini-batch} $k$-means. At each iteration
\begin{itemize}
	\item Choose a subset of points
	\item Apply a $k$-means iteration
\end{itemize}
\end{frame}

\begin{frame}{Number of clusters}
In some applications, you know how many clusters you want. In this case, $k$ is \textbf{easy to set}.
\vfill
In other applications, we don't know the optimal number of classes we want. Ideally, we would like $k$ to be \textbf{selected automatically}.
\vfill
There is always \textbf{some ambiguity} in selecting the \textit{optimal} number of clusters. This is normal: When doing unsupervised learning, there is necessarily some \textbf{inherent subjectivity} in the labeling process!
\end{frame}

\begin{frame}{Number of clusters}
That being said, it is possible to define some criterias to determine whether $k_1$ is a better number of clusters than $k_2$. We can use the sum of squared errors to the centroids:
\begin{equation*}
\text{SSE}(k) = \sum_{i = 1}^{k} \sum_{x \in S_i} \norm{x - \mu_i}_2^2
\end{equation*}
And apply the \textbf{Elbow method}.
\vfill
\pause
Note that this is not a miracle solution.
\end{frame}

\begin{frame}{The elbow rule/method}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{images/elbow.png}
\end{figure}
\end{frame}

\begin{frame}{$k$-means: concluding remarks}
Pros:
\begin{itemize}
	\item Fast, and can be faster with mini-batches
	\item Intuitive and easy to implement
\end{itemize}
\vfill
\pause
Cons:
\begin{itemize}
	\item Might not always give the same solution
	\item Number of clusters $k$
\end{itemize}
\vfill
\pause
We can use different metrics to link clusters and various linkage criteria. (More on this later.)
\end{frame}

\begin{frame}
	\center \Huge{Hierarchical clustering}
\end{frame}

\begin{frame}{Hierarchical clustering}
\textbf{Bottom-up} approach:
\begin{itemize}
	\item \textbf{Bottom}: Initially, each point is a cluster
	\item \textbf{Top}: Merge the 2 closest clusters until we have one cluster
\end{itemize}
\vfill
\pause
\textit{closest} has to be defined. We can use the \textbf{Euclidean norm}: $a$ and $b$ are closer than $a'$ and $b'$ if and only if $\norm{a - b} \leq \norm{a' - b'}$.
\pause
\vfill
(illustration on the clipboard)
\end{frame}

\begin{frame}{Hierarchical clustering: Dendograms}
\begin{tikzpicture}[sloped]
\node (a) at (-6,0) {a};
\node (b) at (-3,0) {b};
\node (c) at (-0.5,0) {c};
\node (d) at (0.5,0) {d};
\node (e) at (2,0) {e};
\node (ab) at (-4.5,3) {};
\node (cd) at (0,1) {};
\node (cde) at (1,2) {};
\node (all) at (-1.5,5) {};
\draw[->] (-7,0) -- node[above]{distance} (-7,6);

\pause
\draw  (c) |- (cd.center);
\draw  (d) |- (cd.center);
\pause
\draw  (e) |- (cde.center);
\draw  (cd.center) |- (cde.center);
\pause
\draw  (a) |- (ab.center);
\draw  (b) |- (ab.center);
\pause
\draw  (ab.center) |- (all.center);
\draw  (cde.center) |- (all.center);

\pause
\draw[dashed, thick, red] (-6.5,2.5) -- (+2.5,+2.5);
\pause
\draw[dashed, thick, blue] (-6.5,3.5) -- (+2.5,+3.5);
\pause
\draw[<-] (+3,0) -- node[below]{number of clusters} (+3,6);
\end{tikzpicture}
\vfill
\pause
Formally, this can be done with a \textbf{linkage criterion}.
\end{frame}

\begin{frame}{Hierarchical clustering: concluding remarks}
Pros:
\begin{itemize}
	\item No need to provide an initial number of classes
	\item Can split the clusters at different levels
\end{itemize}
\vfill
\pause
Cons:
\begin{itemize}
	\item Similarity matrix (need to compute the distances between all pairs of points in the dataset): Does not scale well with the number of points!
\end{itemize}
\vfill
\pause
We can use different metrics to link clusters and various linkage criteria.
\vfill
\pause
Top-down approach also possible.
\end{frame}

\begin{frame}{Conclusion}
Clustering methods create groups within a set of unlabeled points.
\vfill
\pause
They typically rely on:
\begin{itemize}
	\item A similarity (or dissimilarity) measure (\textit{e.g.} the Euclidian norm)
	\item A few parameters, \textit{e.g.}
	\begin{itemize}
		\item The number of clusters for $k$-means
		\item The dendogram cut (linkage) for hierarchical clustering
	\end{itemize}
\end{itemize}
\end{frame}

\begin{frame}
	\center
	\huge{Thank you! Questions?}
\end{frame}

\end{document}


text:
#---------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#---------------------------------------------------------------------------------------------

CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'

ENV_DEFAULT = 'AzureCloud'
ENV_US_GOVERNMENT = 'AzureUSGovernment'
ENV_CHINA = 'AzureChinaCloud'

COMMON_TENANT = 'common'

#ported from https://github.com/Azure/azure-xplat-cli/blob/dev/lib/util/profile/environment.js
class ENDPOINT_URLS: #pylint: disable=too-few-public-methods,old-style-class,no-init
    MANAGEMENT = 'management'
    ACTIVE_DIRECTORY_AUTHORITY = 'active_directory_authority'
    ACTIVE_DIRECTORY_GRAPH_RESOURCE_ID = 'active_directory_graph_resource_id'

_environments = {
    ENV_DEFAULT: {
        ENDPOINT_URLS.MANAGEMENT: 'https://management.core.windows.net/',
        ENDPOINT_URLS.ACTIVE_DIRECTORY_AUTHORITY : 'https://login.microsoftonline.com',
        ENDPOINT_URLS.ACTIVE_DIRECTORY_GRAPH_RESOURCE_ID: 'https://graph.windows.net/'
        },
    ENV_CHINA: {
        ENDPOINT_URLS.MANAGEMENT: 'https://management.core.chinacloudapi.cn/',
        ENDPOINT_URLS.ACTIVE_DIRECTORY_AUTHORITY: 'https://login.chinacloudapi.cn',
        ENDPOINT_URLS.ACTIVE_DIRECTORY_GRAPH_RESOURCE_ID: 'https://graph.chinacloudapi.cn/'
        },
    ENV_US_GOVERNMENT: {
        ENDPOINT_URLS.MANAGEMENT: 'https://management.core.usgovcloudapi.net/',
        ENDPOINT_URLS.ACTIVE_DIRECTORY_AUTHORITY: 'https://login.microsoftonline.com',
        ENDPOINT_URLS.ACTIVE_DIRECTORY_GRAPH_RESOURCE_ID: 'https://graph.windows.net/'
        }
}

def get_env(env_name=None):
    if env_name is None:
        env_name = ENV_DEFAULT
    elif env_name not in _environments:
        raise ValueError
    return _environments[env_name]

def get_authority_url(tenant=None, env_name=None):
    env = get_env(env_name)
    return env[ENDPOINT_URLS.ACTIVE_DIRECTORY_AUTHORITY] + '/' + (tenant or COMMON_TENANT)


text:
package org.mitre.synthea.modules;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class ImmunizationsTest {

  @Test
  public void testMaximumDoses() {
    // "52" is the Hep A Adult vaccine. It requires two doses.
    int doses = Immunizations.getMaximumDoses("52");
    assertEquals(2, doses);
  }

  @Test
  public void testMaximumDosesNothing() {
    int doses = Immunizations.getMaximumDoses(null);
    assertEquals(1, doses);
  }
}


text:
set :css_dir, 'css'
set :js_dir, 'js'
set :images_dir, 'images'

# Make Bower & NPM files available to Sprockets
sprockets.append_path File.join root, 'bower_components'
sprockets.append_path File.join root, 'node_modules'

# Configure Babel
require 'babel'
::Babel.options do |o|
  o[:loose] = %w{es6.classes es6.modules} # loose mode enables better IE <= 10 compatibility
end

# Output .js.es6 files as .js
def remap_es6_files(dir)
  Dir.foreach(File.join(config[:source], dir)) do |file|
    next if file == '.' or file == '..'

    path = File.join(dir, file)
    if File.directory?(File.join(config[:source], path))
      remap_es6_files(path)
    elsif file =~ /\.es6$/
      proxy path.gsub(/\.es6$/, ''), path
      ignore path
    elsif file =~ /\.es6\.erb$/
      proxy path.gsub(/\.es6\.erb$/, ''), path.gsub(/\.erb$/, '')
      ignore path
    end
  end
end

remap_es6_files(config[:js_dir])

# Dev-specific configuration
configure :development do
  set :debug_assets, true

  # reload page
  activate :livereload, :apply_js_live => true, :apply_css_live => true
  config[:file_watcher_ignore] << %r{\.idea\/}
end


text:
export const LINKS = {
  email: 'mailto:contact@valentinvoilean.com',
  linkedin: 'http://linkedin.com/in/vvoilean',
  github: 'https://github.com/valentinvoilean',
  stackOverflow: 'https://stackoverflow.com/users/2670603/vvm',
  twitter: 'https://twitter.com/ValentinVoilean',
  photo: 'https://500px.com/ValentinVoilean'
};


text:
@interface SRSettingTableViewController : UITableViewController<UITableViewDelegate>

@end


text:
package com.babel.core.data.utils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.util.ISO8601Utils;

public class CustomJsonDateDeserializer extends JsonDeserializer<Date>
{
    @Override
    public Date deserialize(JsonParser jsonparser,
            DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {

        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        String date = jsonparser.getText(); System.err.println("deserializer resulted in string date: " + date);

        Date d;
        try {//parse from the user format
        	d = format.parse(date);
        } catch (ParseException e) {
        	try {//parse from ISO-8601 format (jackson standard)
        		d = ISO8601Utils.parse(date);
			} catch (IllegalArgumentException e2) {
				throw new RuntimeException(e);
			}
        }

        return d;
    }
}


text:
/*

Round 1

Write a function lengths that accepts a single parameter as an argument, namely an array of strings. The function should return an array of numbers. Each number in the array should be the length of the corresponding string. To get you started, you'll need to loop through each string in the array and get the length of each one. Those lengths should be stored in a different array that you will return.

Remember that when building a function, you want to use declare a function with a name that accepts arguments. So for building our function called lengths that accepts an array of strings, it might look something like:

// declare function named "lengths"
// that accepts a arguments named "arrayOfStrings"
function lengths(arrayOfStrings) {

  // we can log out our "arrayOfStrings"
  console.log(arrayOfStrings);

  // now, we want to "return" something... but what?

  return whateverVariableYouWantToReturnHere;

}
Reference: MDN: String.length
var words = ["hello", "what", "is", "up", "dude"]
lengths(words)  # => [5, 4, 2, 2, 4]

*/

var lengths = function (arrayOfStrings){
  //console.log(arrayOfStrings);
  var arr=[]; //intialize array we will puts lengths in

  for(string in arrayOfStrings){ // for-loop to interate through arrayOfStrings and push each indiviual string's length property to arr.

    arr.push(arrayOfStrings[string].length);

  }

  //console.log(arr);// log out for debugging

  return arr;
}

//testing
var words=['hello','what','is','up','dude'];
console.log(lengths(words));//[5,4,2,2,4]

/*

Round 2

Write a Javascript function called transmogrifier This function should accept three arguments, which you can assume will be numbers. Your function should return the "transmogrified" result

The transmogrified result of three numbers is the product (numbers multiplied together) of the first two numbers, raised to the power (exponentially) of the third number.

For example, the transmogrified result of 5, 3, and 2 is (5 times 3) to the power of 2 is 225.

Use your function to find the following answers.

Reference: MDN: Math.pow() for Exponential numbers

transmogrifier(5, 4, 3)
transmogrifier(13, 12, 5)
transmogrifier(42, 13, 7)

*/

var transmogrified=function(num1,num2,num3){
  return Math.pow((num1*num2),num3);
}

console.log(transmogrified(5,3,2));//225

/*

Round 3

Write a function called toonify that takes two parameters, accent and sentence.

If accent is the string "daffy", return a modified version of sentence with all "s" replaced with "th".
If the accent is "elmer", replace all "r" with "w".
Feel free to add your own accents as well!
If the accent is not recognized, just return the sentence as-is.

Reference: MDN: String.replace()

toonify("daffy", "so you smell like sausage")
#=> "tho you thmell like thauthage"

*/

var toonify=function(accent,sentance){
    if(accent=='daffy'){
      sentance=sentance.replace(/s/g,'th');
    }

   if (accent=='elmer'){
      sentance=sentance.replace(/r/g,'w');
    }

    return sentance;
}

console.log(toonify('daffy','so you smell like sausage')) //'tho you thmell like thauthage'

/*

Round 4

Write a function wordReverse that accepts a single argument, a string. The method should return a string with the order of the words reversed. Don't worry about punctuation. You'll need to use String.split() to create an array of words (splitting them with a space or " ". Then you'll need to reverse the order of that array using array.reverse(). Finally, you'll loop through them to create a new string).

References:
MDN: String.split()
MDN: Array.reverse()
wordReverse("Now I know what a TV dinner feels like")
# => "like feels dinner TV a what know I Now"

*/

var wordReverse = function(string){
  arr=string.split(' ');
  arr.reverse();

  var reversed='';

  for (word in arr){
      reversed+=arr[word]+' ';
  }

  return reversed;
}

console.log(wordReverse("Now I know what a TV dinner feels like"));//"like feels dinner TV a what know I Now"

/*

Round 5

Write a function letterReverse that accepts a single argument, a string. The function should maintain the order of words in the string but reverse the letters in each word. Don't worry about punctuation. This will be very similar to round 4 except you won't need to split them with a space.

References:
MDN: String.split()
MDN: Array.reverse()
letterReverse("Now I know what a TV dinner feels like")
# => "woN I wonk tahw a VT rennid sleef ekil"
letterReverse("Put Hans back on the line")
# => "tuP snaH kcab no eht enil"

*/

var letterReverse=function(str){
  var arr=str.split(' ')
  //console.log(arr);
  var reversed='';

  for (word in arr){
        x=arr[word].split('');//convert from string to arr of letters
        x=x.reverse();//reverse and save the arr to x
        //console.log(x);
        reversed=x.join('');//convert back to string
      //  console.log(reversed,'type of reversed:',typeof(reversed));
        arr[word]=reversed;
    //    console.log(arr[word]);
  }
  //console.log(arr);
  str=arr.join(' ');
  //console.log(typeof(str));

  return str;
}

console.log(letterReverse('Now I know what a TV dinner feels like'));

/*

Round 6

Write a function longest that accepts a single argument, an array of strings. The method should return the longest word in the array. In case of a tie, the method should return the word that appears first in the array.

Reference: MDN: String.length
longest(["oh", "good", "grief"]) # => "grief"
longest(["Nothing" , "takes", "the", "taste", "out", "of", "peanut", "butter", "quite", "like", "unrequited", "love"])
# => "unrequited"

*/

function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);
}

var longest=function(arr){
  lengthOfWordsArr=lengths(arr);
  console.log(lengthOfWordsArr);

  return getMaxOfArray(lengthOfWordsArr);
}

console.log(longest(["Nothing" , "takes", "the", "taste", "out", "of", "peanut", "butter", "quite", "like", "unrequited", "love"]));

/*

Final Round

Write a function, called repMaster, that accepts two arguments, input and a function. Input should be able to be used with the function. The function used as an argument must return a string. repMaster should take the result of the string, passed as an argument to the argument function, and return this result with ' proves that I am the rep MASTER!' concatenated to it.

repMaster("Never give your heart to a blockhead", wordReverse) # =>
"blockhead a to heart your give never proves that I am the rep MASTER!"
repMaster("I finished this practice", toUpperCase);
"I FINISHED THIS PRACTICE proves that I am the rep MASTER!"
Note that a function can be used as an argument for a function! Inside of the function, you just need to call it. Example:

function logSomething(string) {

  console.log(string);

}

function doSomethingWithFunctions(yourFunction) {

  // you 'call' yourFunction
  var someString = "hey, you're going to log me";
  return yourFunction(someString);

}

doSomethingWithFunctions(logSomething);

*/

var repMaster = function (input,myFunction){
  return myFunction(input);
}

console.log(repMaster("Never give your heart to a blockhead", wordReverse));


text:
/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
 */
package org.hibernate.validator.tckrunner.securitymanager;

import java.util.Collections;
import java.util.List;

import org.hibernate.beanvalidation.tck.util.IntegrationTestsMethodSelector;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.xml.XmlMethodSelector;
import org.testng.xml.XmlPackage;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

/**
 * Main class for running the test suite programmatically, for debugging purposes.
 *

 * Specify the following arguments when running this test:
 *

 * -Dvalidation.provider=org.hibernate.validator.HibernateValidator
 * -Djava.security.manager=default
 * -Djava.security.policy=target/test-classes/test.policy
 * -Djava.security.debug=access
 * -DexcludeIntegrationTests=true
 * -Darquillian.protocol=LocalSecurityManagerTesting
 * -Dorg.jboss.testharness.spi.StandaloneContainers=org.hibernate.jsr303.tck.util.StandaloneContainersImpl
 *

 *
 * @author Gunnar Morling
 */
public class TckRunner {

	public static void main(String[] args) {
		XmlSuite suite = new XmlSuite();
		suite.setName( "JSR-349-TCK" );

		XmlTest test = new XmlTest(suite);
		test.setName( "JSR-349-TCK" );

		List<XmlPackage> packages = Collections.singletonList( new XmlPackage( "org.hibernate.beanvalidation.tck.tests" ) );
		test.setXmlPackages( packages );

		// Alternatively e.g. use this for running single tests
		// List<XmlClass> classes = Collections.singletonList( new XmlClass( ValidateTest.class ) );
		// test.setXmlClasses( classes );

		XmlMethodSelector selector = new XmlMethodSelector();
		selector.setClassName( IntegrationTestsMethodSelector.class.getName() );
		test.setMethodSelectors( Collections.singletonList( selector  ) );

		TestListenerAdapter tla = new TestListenerAdapter();
		TestNG testng = new TestNG();
		testng.setXmlSuites( Collections.singletonList( suite ) );
		testng.addListener( tla );
		testng.run();

		for ( ITestResult failure: tla.getConfigurationFailures() ) {
			System.out.println( "Failure: " + failure.getName() );
			failure.getThrowable().printStackTrace();
		}

		for ( ITestResult result : tla.getFailedTests() ) {
			System.out.println( "Failed:" + result.getName() );
			result.getThrowable().printStackTrace();
		}
	}
}


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,300italic,700' rel='stylesheet' type='text/css'>

    <link rel="stylesheet" href="../../css/normalize/normalize.css">

    <link rel="stylesheet" href="../../css/layout/bootstrap/bootstrap.css">

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

    <link rel="stylesheet" href="../../bower_components/responsive-nav/responsive-nav.css">

		<meta http-equiv="cache-control" content="max-age=0" />
		<meta http-equiv="cache-control" content="no-cache" />
		<meta http-equiv="expires" content="0" />
		<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
		<meta http-equiv="pragma" content="no-cache" />

		<link rel="stylesheet" href="../../styleguide/css/styleguide.css?1402940540" media="all">
		<link rel="stylesheet" href="../../styleguide/css/styleguide-specific.css?1402940540" media="all" />

        Home

        Blog

        About

        Projects

        Home

        Blog

        About

        Projects


text:
/*!
 *  Copyright (c) 2017 by Contributors
 * \file image_iter_common.h
 * \brief common types used by image data iterators
 */

#ifndef MXNET_IO_IMAGE_ITER_COMMON_H_
#define MXNET_IO_IMAGE_ITER_COMMON_H_

#include <mxnet/io.h>
#include <vector>
#include <unordered_map>
#include <string>

namespace mxnet {
namespace io {
/*! \brief data structure to hold labels for images */
class ImageLabelMap {
 public:
  /*!
   * \brief initialize the label list into memory
   * \param path_imglist path to the image list
   * \param label_width predefined label_width
   */
  explicit ImageLabelMap(const char *path_imglist,
                         index_t label_width,
                         bool silent) {
    this->label_width = label_width;
    image_index_.clear();
    label_.clear();
    idx2label_.clear();
    dmlc::InputSplit *fi = dmlc::InputSplit::Create
        (path_imglist, 0, 1, "text");
    dmlc::InputSplit::Blob rec;
    while (fi->NextRecord(&rec)) {
      // quick manual parsing
      char *p = reinterpret_cast<char*>(rec.dptr);
      char *end = p + rec.size;
      // skip space
      while (isspace(*p) && p != end) ++p;
      image_index_.push_back(static_cast<size_t>(atol(p)));
      for (index_t i = 0; i < label_width; ++i) {
        // skip till space
        while (!isspace(*p) && p != end) ++p;
        // skip space
        while (isspace(*p) && p != end) ++p;
        CHECK(p != end) << "Bad ImageList format";
        label_.push_back(static_cast<real_t>(atof(p)));
      }
    }
    delete fi;
    // be careful not to resize label_ afterwards
    idx2label_.reserve(image_index_.size());
    for (size_t i = 0; i < image_index_.size(); ++i) {
      idx2label_[image_index_[i]] = dmlc::BeginPtr(label_) + i * label_width;
    }
    if (!silent) {
      LOG(INFO) << "Loaded ImageList from " << path_imglist << ' '
                << image_index_.size() << " Image records";
    }
  }
  /*! \brief find a label for corresponding index */
  inline mshadow::Tensor<cpu, 1> Find(size_t imid) const {
    std::unordered_map<size_t, real_t*>::const_iterator it
        = idx2label_.find(imid);
    CHECK(it != idx2label_.end()) << "fail to find imagelabel for id " << imid;
    return mshadow::Tensor<cpu, 1>(it->second, mshadow::Shape1(label_width));
  }
  /*! \brief find a label for corresponding index, return vector as copy */
  inline std::vector<float> FindCopy(size_t imid) const {
    std::unordered_map<size_t, real_t*>::const_iterator it
        = idx2label_.find(imid);
    CHECK(it != idx2label_.end()) << "fail to find imagelabel for id " << imid;
    const real_t *ptr = it->second;
    return std::vector<float>(ptr, ptr + label_width);
  }

 private:
  // label with_
  mshadow::index_t label_width;
  // image index of each record
  std::vector<size_t> image_index_;
  // real label content
  std::vector<real_t> label_;
  // map index to label
  std::unordered_map<size_t, real_t*> idx2label_;
};

// Define image record parser parameters
struct ImageRecParserParam : public dmlc::Parameter<ImageRecParserParam> {
  /*! \brief path to image list */
  std::string path_imglist;
  /*! \brief path to image recordio */
  std::string path_imgrec;
  /*! \brief path to index file */
  std::string path_imgidx;
  /*! \brief a sequence of names of image augmenters, seperated by , */
  std::string aug_seq;
  /*! \brief label-width */
  int label_width;
  /*! \brief input shape */
  mxnet::TShape data_shape;
  /*! \brief number of threads */
  int preprocess_threads;
  /*! \brief whether to remain silent */
  bool verbose;
  /*! \brief partition the data into multiple parts */
  int num_parts;
  /*! \brief the index of the part will read */
  int part_index;
  /*! \brief device id used to create context for internal NDArray */
  int device_id;
  /*! \brief the size of a shuffle chunk */
  size_t shuffle_chunk_size;
  /*! \brief the seed for chunk shuffling */
  int shuffle_chunk_seed;
  /*! \brief random seed for augmentations */
  dmlc::optional<int> seed_aug;

  // declare parameters
  DMLC_DECLARE_PARAMETER(ImageRecParserParam) {
    DMLC_DECLARE_FIELD(path_imglist).set_default("")
        .describe("Path to the image list (.lst) file. Generally created with tools/im2rec.py. "\
                  "Format (Tab separated): "\
                  "<index of record>\t<one or more labels>\t<relative path from root folder>.");
    DMLC_DECLARE_FIELD(path_imgrec).set_default("")
        .describe("Path to the image RecordIO (.rec) file or a directory path. "\
                  "Created with tools/im2rec.py.");
    DMLC_DECLARE_FIELD(path_imgidx).set_default("")
        .describe("Path to the image RecordIO index (.idx) file. "\
                  "Created with tools/im2rec.py.");
    DMLC_DECLARE_FIELD(aug_seq).set_default("aug_default")
        .describe("The augmenter names to represent"\
                  " sequence of augmenters to be applied, seperated by comma." \
                  " Additional keyword parameters will be seen by these augmenters.");
    DMLC_DECLARE_FIELD(label_width).set_lower_bound(1).set_default(1)
        .describe("The number of labels per image.");
    DMLC_DECLARE_FIELD(data_shape)
        .set_expect_ndim(3).enforce_nonzero()
        .describe("The shape of one output image in (channels, height, width) format.");
    DMLC_DECLARE_FIELD(preprocess_threads).set_lower_bound(1).set_default(4)
        .describe("The number of threads to do preprocessing.");
    DMLC_DECLARE_FIELD(verbose).set_default(true)
        .describe("If or not output verbose information.");
    DMLC_DECLARE_FIELD(num_parts).set_default(1)
        .describe("Virtually partition the data into these many parts.");
    DMLC_DECLARE_FIELD(part_index).set_default(0)
        .describe("The *i*-th virtual partition to be read.");
    DMLC_DECLARE_FIELD(device_id).set_default(0)
        .describe("The device id used to create context for internal NDArray. "\
                  "Setting device_id to -1 will create Context::CPU(0). Setting "
                  "device_id to valid positive device id will create "
                  "Context::CPUPinned(device_id). Default is 0.");
    DMLC_DECLARE_FIELD(shuffle_chunk_size).set_default(0)
        .describe("The data shuffle buffer size in MB. Only valid if shuffle is true.");
    DMLC_DECLARE_FIELD(shuffle_chunk_seed).set_default(0)
        .describe("The random seed for shuffling");
    DMLC_DECLARE_FIELD(seed_aug).set_default(dmlc::optional<int>())
        .describe("Random seed for augmentations.");
  }
};

// Batch parameters
struct BatchParam : public dmlc::Parameter<BatchParam> {
  /*! \brief label width */
  uint32_t batch_size;
  /*! \brief use round roubin to handle overflow batch */
  bool round_batch;
  // declare parameters
  DMLC_DECLARE_PARAMETER(BatchParam) {
    DMLC_DECLARE_FIELD(batch_size)
        .describe("Batch size.");
    DMLC_DECLARE_FIELD(round_batch).set_default(true)
        .describe("Whether to use round robin to handle overflow batch or not.");
  }
};

// Batch Sampler parameters
struct BatchSamplerParam : public dmlc::Parameter<BatchSamplerParam> {
  /*! \brief Last batch behavior type */
  enum LastBatchType {
    /*! \brief Keep not fully filled last batch */
    kKeep = 0,
    /*! \brief Roll over the remaining batch to next epoch */
    kRollOver,
    /*! \brief Discard not fully filled last batch */
    kDiscard
  };  // enum LastBatchType
  /*! \brief batch size */
  uint32_t batch_size;
  /*! \brief last batch behavior */
  int last_batch;
  // declare parameters
  DMLC_DECLARE_PARAMETER(BatchSamplerParam) {
    DMLC_DECLARE_FIELD(batch_size)
        .describe("Batch size.");
    DMLC_DECLARE_FIELD(last_batch).set_default(kKeep)
        .add_enum("keep", kKeep)
        .add_enum("rollover", kRollOver)
        .add_enum("discard", kDiscard)
        .describe("Specifies how the last batch is handled if batch_size does not evenly "
                  "divide sequence length. "
                  "If 'keep', the last batch will be returned directly, but will contain "
                  "less element than `batch_size` requires. "
                  "If 'discard', the last batch will be discarded. "
                  "If 'rollover', the remaining elements will be rolled over to the next "
                  "iteration. Note: legacy batch param with round_batch will always round data "
                  "in order to always provide full batchs. Rollover behavior will instead result "
                  "in different iteration sizes for each epoch.");
  }
};

// Define image record parameters
struct ImageRecordParam: public dmlc::Parameter<ImageRecordParam> {
  /*! \brief whether to do shuffle */
  bool shuffle;
  /*! \brief random seed */
  int seed;
  /*! \brief whether to remain silent */
  bool verbose;
  // declare parameters
  DMLC_DECLARE_PARAMETER(ImageRecordParam) {
    DMLC_DECLARE_FIELD(shuffle).set_default(false)
        .describe("Whether to shuffle data randomly or not.");
    DMLC_DECLARE_FIELD(seed).set_default(0)
        .describe("The random seed.");
    DMLC_DECLARE_FIELD(verbose).set_default(true)
        .describe("Whether to output verbose information or not.");
  }
};

// normalize parameters
struct ImageNormalizeParam :  public dmlc::Parameter<ImageNormalizeParam> {
  /*! \brief random seed */
  int seed;
  /*! \brief whether to mirror the image */
  bool mirror;
  /*! \brief whether to perform rand mirror the image */
  bool rand_mirror;
  /*! \brief mean file string */
  std::string mean_img;
  /*! \brief mean value for r channel */
  float mean_r;
  /*! \brief mean value for g channel */
  float mean_g;
  /*! \brief mean value for b channel */
  float mean_b;
  /*! \brief mean value for alpha channel */
  float mean_a;
  /*! \brief standard deviation for r channel */
  float std_r;
  /*! \brief standard deviation for g channel */
  float std_g;
  /*! \brief standard deviation for b channel */
  float std_b;
  /*! \brief standard deviation for alpha channel */
  float std_a;
  /*! \brief scale on color space */
  float scale;
  /*! \brief maximum ratio of contrast variation */
  float max_random_contrast;
  /*! \brief maximum value of illumination variation */
  float max_random_illumination;
  /*! \brief silent */
  bool verbose;
  // declare parameters
  DMLC_DECLARE_PARAMETER(ImageNormalizeParam) {
    DMLC_DECLARE_FIELD(seed).set_default(0)
        .describe("The random seed.");
    DMLC_DECLARE_FIELD(mirror).set_default(false)
        .describe("Whether to mirror the image or not. If true, images are "\
                  "flipped along the horizontal axis.");
    DMLC_DECLARE_FIELD(rand_mirror).set_default(false)
        .describe("Whether to randomly mirror images or not. If true, 50% of "\
                  "the images will be randomly mirrored (flipped along the "\
                  "horizontal axis)");
    DMLC_DECLARE_FIELD(mean_img).set_default("")
        .describe("Filename of the mean image.");
    DMLC_DECLARE_FIELD(mean_r).set_default(0.0f)
        .describe("The mean value to be subtracted on the R channel");
    DMLC_DECLARE_FIELD(mean_g).set_default(0.0f)
        .describe("The mean value to be subtracted on the G channel");
    DMLC_DECLARE_FIELD(mean_b).set_default(0.0f)
        .describe("The mean value to be subtracted on the B channel");
    DMLC_DECLARE_FIELD(mean_a).set_default(0.0f)
        .describe("The mean value to be subtracted on the alpha channel");
    DMLC_DECLARE_FIELD(std_r).set_default(1.0f)
        .describe("Augmentation Param: Standard deviation on R channel.");
    DMLC_DECLARE_FIELD(std_g).set_default(1.0f)
        .describe("Augmentation Param: Standard deviation on G channel.");
    DMLC_DECLARE_FIELD(std_b).set_default(1.0f)
        .describe("Augmentation Param: Standard deviation on B channel.");
    DMLC_DECLARE_FIELD(std_a).set_default(1.0f)
        .describe("Augmentation Param: Standard deviation on Alpha channel.");
    DMLC_DECLARE_FIELD(scale).set_default(1.0f)
        .describe("Multiply the image with a scale value.");
    DMLC_DECLARE_FIELD(max_random_contrast).set_default(0.0f)
        .describe("Change the contrast with a value randomly chosen from "
                  "``[-max_random_contrast, max_random_contrast]``");
    DMLC_DECLARE_FIELD(max_random_illumination).set_default(0.0f)
        .describe("Change the illumination with a value randomly chosen from "
                  "``[-max_random_illumination, max_random_illumination]``");
    DMLC_DECLARE_FIELD(verbose).set_default(true)
        .describe("If or not output verbose information.");
  }
};

// normalize det parameters
struct ImageDetNormalizeParam :  public dmlc::Parameter<ImageDetNormalizeParam> {
  /*! \brief random seed */
  int seed;
  /*! \brief mean file string */
  std::string mean_img;
  /*! \brief mean value for r channel */
  float mean_r;
  /*! \brief mean value for g channel */
  float mean_g;
  /*! \brief mean value for b channel */
  float mean_b;
  /*! \brief mean value for alpha channel */
  float mean_a;
  /*! \brief standard deviation for r channel */
  float std_r;
  /*! \brief standard deviation for g channel */
  float std_g;
  /*! \brief standard deviation for b channel */
  float std_b;
  /*! \brief standard deviation for alpha channel */
  float std_a;
  /*! \brief scale on color space */
  float scale;
  /*! \brief silent */
  bool verbose;
  // declare parameters
  DMLC_DECLARE_PARAMETER(ImageDetNormalizeParam) {
    DMLC_DECLARE_FIELD(seed).set_default(0)
        .describe("Augmentation Param: Random Seed.");
    DMLC_DECLARE_FIELD(mean_img).set_default("")
        .describe("Augmentation Param: Mean Image to be subtracted.");
    DMLC_DECLARE_FIELD(mean_r).set_default(0.0f)
        .describe("Augmentation Param: Mean value on R channel.");
    DMLC_DECLARE_FIELD(mean_g).set_default(0.0f)
        .describe("Augmentation Param: Mean value on G channel.");
    DMLC_DECLARE_FIELD(mean_b).set_default(0.0f)
        .describe("Augmentation Param: Mean value on B channel.");
    DMLC_DECLARE_FIELD(mean_a).set_default(0.0f)
        .describe("Augmentation Param: Mean value on Alpha channel.");
    DMLC_DECLARE_FIELD(std_r).set_default(0.0f)
        .describe("Augmentation Param: Standard deviation on R channel.");
    DMLC_DECLARE_FIELD(std_g).set_default(0.0f)
        .describe("Augmentation Param: Standard deviation on G channel.");
    DMLC_DECLARE_FIELD(std_b).set_default(0.0f)
        .describe("Augmentation Param: Standard deviation on B channel.");
    DMLC_DECLARE_FIELD(std_a).set_default(0.0f)
        .describe("Augmentation Param: Standard deviation on Alpha channel.");
    DMLC_DECLARE_FIELD(scale).set_default(1.0f)
        .describe("Augmentation Param: Scale in color space.");
    DMLC_DECLARE_FIELD(verbose).set_default(true)
        .describe("Augmentation Param: Whether to print augmentor info.");
  }
};

// Define prefetcher parameters
struct PrefetcherParam : public dmlc::Parameter<PrefetcherParam> {
  enum CtxType { kGPU = 0, kCPU, kCPUPinned, kCPUShared};
  /*! \brief number of prefetched batches */
  size_t prefetch_buffer;

  /*! \brief Context data loader optimized for */
  int ctx;
  int device_id;
  /*! \brief data type */
  dmlc::optional<int> dtype;

  // declare parameters
  DMLC_DECLARE_PARAMETER(PrefetcherParam) {
    DMLC_DECLARE_FIELD(prefetch_buffer).set_default(4)
        .describe("Maximum number of batches to prefetch.");
    DMLC_DECLARE_FIELD(ctx).set_default(kGPU)
        .add_enum("cpu", kCPU)
        .add_enum("gpu", kGPU)
        .add_enum("cpu_pinned", kCPUPinned)
        .describe("Context data loader optimized for. "
                  "Note that it only indicates the optimization strategy for devices, "
                  "by no means the prefetcher will load data to GPUs. "
                  "If ctx is 'cpu_pinned' and device_id is not -1, "
                  "it will use cpu_pinned(device_id) as ctx");
    DMLC_DECLARE_FIELD(device_id).set_default(-1)
        .describe("The default device id for context. -1 indicate it's on default device");
    DMLC_DECLARE_FIELD(dtype)
      .add_enum("float32", mshadow::kFloat32)
      .add_enum("float64", mshadow::kFloat64)
      .add_enum("float16", mshadow::kFloat16)
      .add_enum("bfloat16", mshadow::kBfloat16)
      .add_enum("int64", mshadow::kInt64)
      .add_enum("int32", mshadow::kInt32)
      .add_enum("uint8", mshadow::kUint8)
      .add_enum("int8", mshadow::kInt8)
      .set_default(dmlc::optional<int>())
      .describe("Output data type. ``None`` means no change.");
  }
};

}  // namespace io
}  // namespace mxnet

#endif  // MXNET_IO_IMAGE_ITER_COMMON_H_


text:
package bytestore

import (
	"net/http"
	"os"
	"path"
	"strings"
)

// to be expanded
var MimeList = map[string]string{
	".js":  "text/javascript",
	".css": "text/css",
}

func GetFileInfo(fpath string) (map[string]interface{}, error) {
	// try and get uploaded file mime type
	tmpfile, err := os.Open(fpath)
	if err != nil {
		return nil, err
	}
	defer tmpfile.Close()

	// read 1024 bytes to enable mime type retrieval
	mimebuffer := make([]byte, 1024)
	_, err = tmpfile.Read(mimebuffer)
	if err != nil {
		return nil, err
	}
	mime := http.DetectContentType(mimebuffer)

	// if mime is 'text/plain' try and get exact mime from file extension
	prefix := "text/plain;"
	if strings.HasPrefix(mime, prefix) {
		ext := path.Ext(fpath)
		mval, exists := MimeList[ext]
		if exists {
			mime = strings.Replace(mime, prefix, mval, 1)
		}
	}

	// get total file size
	f_info, _ := tmpfile.Stat()
	size := f_info.Size()

	val := make(map[string]interface{})
	val["mime"] = mime
	val["size"] = size
	return val, nil
}


text:
package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"os/signal"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/coreos/clair/api/v1"
	"github.com/coreos/clair/utils/types"
	"github.com/fatih/color"
	"github.com/kr/text"
)

const (
	postLayerURI        = "/v1/layers"
	getLayerFeaturesURI = "/v1/layers/%s?vulnerabilities"
	httpPort            = 9279
)

var (
	flagEndpoint        = flag.String("endpoint", "http://127.0.0.1:6060", "Address to Clair API")
	flagMyAddress       = flag.String("my-address", "127.0.0.1", "Address from the point of view of Clair")
	flagMinimumSeverity = flag.String("minimum-severity", "Negligible", "Minimum severity of vulnerabilities to show (Unknown, Negligible, Low, Medium, High, Critical, Defcon1)")
	flagColorMode       = flag.String("color", "auto", "Colorize the output (always, auto, never)")
)

type vulnerabilityInfo struct {
	vulnerability v1.Vulnerability
	feature       v1.Feature
	severity      types.Priority
}

type By func(v1, v2 vulnerabilityInfo) bool

func (by By) Sort(vulnerabilities []vulnerabilityInfo) {
	ps := &sorter{
		vulnerabilities: vulnerabilities,
		by:              by,
	}
	sort.Sort(ps)
}

type sorter struct {
	vulnerabilities []vulnerabilityInfo
	by              func(v1, v2 vulnerabilityInfo) bool
}

func (s *sorter) Len() int {
	return len(s.vulnerabilities)
}

func (s *sorter) Swap(i, j int) {
	s.vulnerabilities[i], s.vulnerabilities[j] = s.vulnerabilities[j], s.vulnerabilities[i]
}

func (s *sorter) Less(i, j int) bool {
	return s.by(s.vulnerabilities[i], s.vulnerabilities[j])
}

func main() {
	os.Exit(intMain())
}

func intMain() int {
	// Parse command-line arguments.
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options] image-id\n\nOptions:\n", os.Args[0])
		flag.PrintDefaults()
	}
	flag.Parse()

	if len(flag.Args()) != 1 {
		flag.Usage()
		return 1
	}
	imageName := flag.Args()[0]

	minSeverity := types.Priority(*flagMinimumSeverity)
	if !minSeverity.IsValid() {
		flag.Usage()
		return 1
	}

	if *flagColorMode == "never" {
		color.NoColor = true
	} else if *flagColorMode == "always" {
		color.NoColor = false
	}

	// Create a temporary folder.
	tmpPath, err := ioutil.TempDir("", "analyze-local-image-")
	if err != nil {
		log.Fatalf("Could not create temporary folder: %s", err)
	}
	defer os.RemoveAll(tmpPath)

	// Intercept SIGINT / SIGKILl signals.
	interrupt := make(chan os.Signal)
	signal.Notify(interrupt, os.Interrupt, os.Kill)

	// Analyze the image.
	analyzeCh := make(chan error, 1)
	go func() {
		analyzeCh <- AnalyzeLocalImage(imageName, minSeverity, *flagEndpoint, *flagMyAddress, tmpPath)
	}()

	select {
	case <-interrupt:
		return 130
	case err := <-analyzeCh:
		if err != nil {
			log.Print(err)
			return 1
		}
	}
	return 0
}

func AnalyzeLocalImage(imageName string, minSeverity types.Priority, endpoint, myAddress, tmpPath string) error {
	// Save image.
	log.Printf("Saving %s to local disk (this may take some time)", imageName)
	err := save(imageName, tmpPath)
	if err != nil {
		return fmt.Errorf("Could not save image: %s", err)
	}

	// Retrieve history.
	log.Println("Retrieving image history")
	layerIDs, err := historyFromManifest(tmpPath)
	if err != nil {
		layerIDs, err = historyFromCommand(imageName)
	}
	if err != nil || len(layerIDs) == 0 {
		return fmt.Errorf("Could not get image's history: %s", err)
	}

	// Setup a simple HTTP server if Clair is not local.
	if !strings.Contains(endpoint, "127.0.0.1") && !strings.Contains(endpoint, "localhost") {
		allowedHost := strings.TrimPrefix(endpoint, "http://")
		portIndex := strings.Index(allowedHost, ":")
		if portIndex >= 0 {
			allowedHost = allowedHost[:portIndex]
		}

		log.Printf("Setting up HTTP server (allowing: %s)\n", allowedHost)

		ch := make(chan error)
		go listenHTTP(tmpPath, allowedHost, ch)
		select {
		case err := <-ch:
			return fmt.Errorf("An error occured when starting HTTP server: %s", err)
		case <-time.After(100 * time.Millisecond):
			break
		}

		tmpPath = "http://" + myAddress + ":" + strconv.Itoa(httpPort)
	}

	// Analyze layers.
	log.Printf("Analyzing %d layers... \n", len(layerIDs))
	for i := 0; i < len(layerIDs); i++ {
		log.Printf("Analyzing %s\n", layerIDs[i])

		if i > 0 {
			err = analyzeLayer(endpoint, tmpPath+"/"+layerIDs[i]+"/layer.tar", layerIDs[i], layerIDs[i-1])
		} else {
			err = analyzeLayer(endpoint, tmpPath+"/"+layerIDs[i]+"/layer.tar", layerIDs[i], "")
		}
		if err != nil {
			return fmt.Errorf("Could not analyze layer: %s", err)
		}
	}

	// Get vulnerabilities.
	log.Println("Retrieving image's vulnerabilities")
	layer, err := getLayer(endpoint, layerIDs[len(layerIDs)-1])
	if err != nil {
		return fmt.Errorf("Could not get layer information: %s", err)
	}

	// Print report.
	fmt.Printf("Clair report for image %s (%s)\n", imageName, time.Now().UTC())

	if len(layer.Features) == 0 {
		fmt.Printf("%s No features have been detected in the image. This usually means that the image isn't supported by Clair.\n", color.YellowString("NOTE:"))
		return nil
	}

	isSafe := true
	hasVisibleVulnerabilities := false

	var vulnerabilities = make([]vulnerabilityInfo, 0)
	for _, feature := range layer.Features {
		if len(feature.Vulnerabilities) > 0 {
			for _, vulnerability := range feature.Vulnerabilities {
				severity := types.Priority(vulnerability.Severity)
				isSafe = false

				if minSeverity.Compare(severity) > 0 {
					continue
				}

				hasVisibleVulnerabilities = true
				vulnerabilities = append(vulnerabilities, vulnerabilityInfo{vulnerability, feature, severity})
			}
		}
	}

	// Sort vulnerabilitiy by severity.
	priority := func(v1, v2 vulnerabilityInfo) bool {
		return v1.severity.Compare(v2.severity) >= 0
	}

	By(priority).Sort(vulnerabilities)

	for _, vulnerabilityInfo := range vulnerabilities {
		vulnerability := vulnerabilityInfo.vulnerability
		feature := vulnerabilityInfo.feature
		severity := vulnerabilityInfo.severity

		fmt.Printf("%s (%s)\n", vulnerability.Name, coloredSeverity(severity))

		if vulnerability.Description != "" {
			fmt.Printf("%s\n\n", text.Indent(text.Wrap(vulnerability.Description, 80), "\t"))
		}

		fmt.Printf("\tPackage:       %s @ %s\n", feature.Name, feature.Version)

		if vulnerability.FixedBy != "" {
			fmt.Printf("\tFixed version: %s\n", vulnerability.FixedBy)
		}

		if vulnerability.Link != "" {
			fmt.Printf("\tLink:          %s\n", vulnerability.Link)
		}

		fmt.Printf("\tLayer:         %s\n", feature.AddedBy)
		fmt.Println("")
	}

	if isSafe {
		fmt.Printf("%s No vulnerabilities were detected in your image\n", color.GreenString("Success!"))
	} else if !hasVisibleVulnerabilities {
		fmt.Printf("%s No vulnerabilities matching the minimum severity level were detected in your image\n", color.YellowString("NOTE:"))
	}

	return nil
}

func save(imageName, path string) error {
	var stderr bytes.Buffer
	save := exec.Command("docker", "save", imageName)
	save.Stderr = &stderr
	extract := exec.Command("tar", "xf", "-", "-C"+path)
	extract.Stderr = &stderr
	pipe, err := extract.StdinPipe()
	if err != nil {
		return err
	}
	save.Stdout = pipe

	err = extract.Start()
	if err != nil {
		return errors.New(stderr.String())
	}
	err = save.Run()
	if err != nil {
		return errors.New(stderr.String())
	}
	err = pipe.Close()
	if err != nil {
		return err
	}
	err = extract.Wait()
	if err != nil {
		return errors.New(stderr.String())
	}

	return nil
}

func historyFromManifest(path string) ([]string, error) {
	mf, err := os.Open(path + "/manifest.json")
	if err != nil {
		return nil, err
	}
	defer mf.Close()

	// https://github.com/docker/docker/blob/master/image/tarexport/tarexport.go#L17
	type manifestItem struct {
		Config   string
		RepoTags []string
		Layers   []string
	}

	var manifest []manifestItem
	if err = json.NewDecoder(mf).Decode(&manifest); err != nil {
		return nil, err
	} else if len(manifest) != 1 {
		return nil, err
	}
	var layers []string
	for _, layer := range manifest[0].Layers {
		layers = append(layers, strings.TrimSuffix(layer, "/layer.tar"))
	}
	return layers, nil
}

func historyFromCommand(imageName string) ([]string, error) {
	var stderr bytes.Buffer
	cmd := exec.Command("docker", "history", "-q", "--no-trunc", imageName)
	cmd.Stderr = &stderr
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return []string{}, err
	}

	err = cmd.Start()
	if err != nil {
		return []string{}, errors.New(stderr.String())
	}

	var layers []string
	scanner := bufio.NewScanner(stdout)
	for scanner.Scan() {
		layers = append(layers, scanner.Text())
	}

	for i := len(layers)/2 - 1; i >= 0; i-- {
		opp := len(layers) - 1 - i
		layers[i], layers[opp] = layers[opp], layers[i]
	}

	return layers, nil
}

func listenHTTP(path, allowedHost string, ch chan error) {
	restrictedFileServer := func(path, allowedHost string) http.Handler {
		fc := func(w http.ResponseWriter, r *http.Request) {
			host, _, err := net.SplitHostPort(r.RemoteAddr)
			if err == nil && strings.EqualFold(host, allowedHost) {
				http.FileServer(http.Dir(path)).ServeHTTP(w, r)
				return
			}
			w.WriteHeader(403)
		}
		return http.HandlerFunc(fc)
	}

	ch <- http.ListenAndServe(":"+strconv.Itoa(httpPort), restrictedFileServer(path, allowedHost))
}

func analyzeLayer(endpoint, path, layerName, parentLayerName string) error {
	payload := v1.LayerEnvelope{
		Layer: &v1.Layer{
			Name:       layerName,
			Path:       path,
			ParentName: parentLayerName,
			Format:     "Docker",
		},
	}

	jsonPayload, err := json.Marshal(payload)
	if err != nil {
		return err
	}

	request, err := http.NewRequest("POST", endpoint+postLayerURI, bytes.NewBuffer(jsonPayload))
	if err != nil {
		return err
	}
	request.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	response, err := client.Do(request)
	if err != nil {
		return err
	}
	defer response.Body.Close()

	if response.StatusCode != 201 {
		body, _ := ioutil.ReadAll(response.Body)
		return fmt.Errorf("Got response %d with message %s", response.StatusCode, string(body))
	}

	return nil
}

func getLayer(endpoint, layerID string) (v1.Layer, error) {
	response, err := http.Get(endpoint + fmt.Sprintf(getLayerFeaturesURI, layerID))
	if err != nil {
		return v1.Layer{}, err
	}
	defer response.Body.Close()

	if response.StatusCode != 200 {
		body, _ := ioutil.ReadAll(response.Body)
		err := fmt.Errorf("Got response %d with message %s", response.StatusCode, string(body))
		return v1.Layer{}, err
	}

	var apiResponse v1.LayerEnvelope
	if err = json.NewDecoder(response.Body).Decode(&apiResponse); err != nil {
		return v1.Layer{}, err
	} else if apiResponse.Error != nil {
		return v1.Layer{}, errors.New(apiResponse.Error.Message)
	}

	return *apiResponse.Layer, nil
}

func coloredSeverity(severity types.Priority) string {
	red := color.New(color.FgRed).SprintFunc()
	yellow := color.New(color.FgYellow).SprintFunc()
	white := color.New(color.FgWhite).SprintFunc()

	switch severity {
	case types.High, types.Critical:
		return red(severity)
	case types.Medium:
		return yellow(severity)
	default:
		return white(severity)
	}
}


text:
A C# IRC bot.

## NetIRC2

This repository contains a modified version of Zer's [NetIRC2](http://www.zer7.com/software/netirc)
to add support for whois command.

## License

[MIT](LICENSE)


text:
import attempt from './utility/attempt';
import callback from './utility/callback';
import constant from './utility/constant';
import identity from './utility/identity';
import iteratee from './utility/iteratee';
import matches from './utility/matches';
import matchesProperty from './utility/matchesProperty';
import mixin from './utility/mixin';
import noop from './utility/noop';
import property from './utility/property';
import propertyOf from './utility/propertyOf';
import range from './utility/range';
import times from './utility/times';
import uniqueId from './utility/uniqueId';

export default {
  'attempt': attempt,
  'callback': callback,
  'constant': constant,
  'identity': identity,
  'iteratee': iteratee,
  'matches': matches,
  'matchesProperty': matchesProperty,
  'mixin': mixin,
  'noop': noop,
  'property': property,
  'propertyOf': propertyOf,
  'range': range,
  'times': times,
  'uniqueId': uniqueId
};


text:
import pyowm

#KEY = "c613d7fb9e2ec98823f8bbb8de7befc4" # default
KEY = "de79ebae33363e14394e058db03512aa" # nanny

class Weather:

    def __init__(self, location="Quebec,ca"):
        self.owm = pyowm.OWM(KEY)
        self.location = location
        self.get_obs()

    def get_obs(self):
        self.obs = self.owm.weather_at_place(self.location)

    def get_temp(self, update=False):
        if update:
            self.get_obs()
        w = self.obs.get_weather()
        return w.get_temperature("celsius")["temp"], w.get_status()

    def get_id(self):
        self.reg = self.owm.city_id_registry()
        print self.reg.ids_for(self.location)
        print self.reg.locations_for(self.location)

if __name__ == "__main__":
    w = Weather()
    print "Temp.: {} C".format(w.get_temp())
    we = w.obs.get_weather()
    print we.get_temperature()
    w.get_id()


text:
export enum DeployTarget {
    live = "live",
    staging = "staging",
    hans = "hans",
    playfair = "playfair",
    jefferson = "jefferson",
    nightingale = "nightingale",
    explorer = "explorer",
    exemplars = "exemplars",
    tufte = "tufte",
    roser = "roser",
    snow = "snow",
    halley = "halley",
    neurath = "neurath",
}

export const ProdTarget = "live"