text:
package internal

import (
	"bytes"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"net/url"
	"os"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	netcontext "context"

	"github.com/golang/protobuf/proto"

	basepb "google.golang.org/appengine/internal/base"
	logpb "google.golang.org/appengine/internal/log"
	remotepb "google.golang.org/appengine/internal/remote_api"
)

const (
	apiPath = "/rpc_http"
)

var (
	// Incoming headers.
	ticketHeader       = http.CanonicalHeaderKey("X-AppEngine-API-Ticket")
	dapperHeader       = http.CanonicalHeaderKey("X-Google-DapperTraceInfo")
	traceHeader        = http.CanonicalHeaderKey("X-Cloud-Trace-Context")
	curNamespaceHeader = http.CanonicalHeaderKey("X-AppEngine-Current-Namespace")
	userIPHeader       = http.CanonicalHeaderKey("X-AppEngine-User-IP")
	remoteAddrHeader   = http.CanonicalHeaderKey("X-AppEngine-Remote-Addr")
	devRequestIdHeader = http.CanonicalHeaderKey("X-Appengine-Dev-Request-Id")

	// Outgoing headers.
	apiEndpointHeader      = http.CanonicalHeaderKey("X-Google-RPC-Service-Endpoint")
	apiEndpointHeaderValue = []string{"app-engine-apis"}
	apiMethodHeader        = http.CanonicalHeaderKey("X-Google-RPC-Service-Method")
	apiMethodHeaderValue   = []string{"/VMRemoteAPI.CallRemoteAPI"}
	apiDeadlineHeader      = http.CanonicalHeaderKey("X-Google-RPC-Service-Deadline")
	apiContentType         = http.CanonicalHeaderKey("Content-Type")
	apiContentTypeValue    = []string{"application/octet-stream"}
	logFlushHeader         = http.CanonicalHeaderKey("X-AppEngine-Log-Flush-Count")

	apiHTTPClient = &http.Client{
		Transport: &http.Transport{
			Proxy:               http.ProxyFromEnvironment,
			Dial:                limitDial,
			MaxIdleConns:        1000,
			MaxIdleConnsPerHost: 10000,
			IdleConnTimeout:     90 * time.Second,
		},
	}
)

func apiURL(ctx netcontext.Context) *url.URL {
	host, port := "appengine.googleapis.internal", "10001"
	if h := os.Getenv("API_HOST"); h != "" {
		host = h
	}
	if hostOverride := ctx.Value(apiHostOverrideKey); hostOverride != nil {
		host = hostOverride.(string)
	}
	if p := os.Getenv("API_PORT"); p != "" {
		port = p
	}
	if portOverride := ctx.Value(apiPortOverrideKey); portOverride != nil {
		port = portOverride.(string)
	}
	return &url.URL{
		Scheme: "http",
		Host:   host + ":" + port,
		Path:   apiPath,
	}
}

// Middleware wraps an http handler so that it can make GAE API calls
func Middleware(next http.Handler) http.Handler {
	return handleHTTPMiddleware(executeRequestSafelyMiddleware(next))
}

func handleHTTPMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		c := &context{
			req:       r,
			outHeader: w.Header(),
		}
		r = r.WithContext(withContext(r.Context(), c))
		c.req = r

		stopFlushing := make(chan int)

		// Patch up RemoteAddr so it looks reasonable.
		if addr := r.Header.Get(userIPHeader); addr != "" {
			r.RemoteAddr = addr
		} else if addr = r.Header.Get(remoteAddrHeader); addr != "" {
			r.RemoteAddr = addr
		} else {
			// Should not normally reach here, but pick a sensible default anyway.
			r.RemoteAddr = "127.0.0.1"
		}
		// The address in the headers will most likely be of these forms:
		//	123.123.123.123
		//	2001:db8::1
		// net/http.Request.RemoteAddr is specified to be in "IP:port" form.
		if _, _, err := net.SplitHostPort(r.RemoteAddr); err != nil {
			// Assume the remote address is only a host; add a default port.
			r.RemoteAddr = net.JoinHostPort(r.RemoteAddr, "80")
		}

		if logToLogservice() {
			// Start goroutine responsible for flushing app logs.
			// This is done after adding c to ctx.m (and stopped before removing it)
			// because flushing logs requires making an API call.
			go c.logFlusher(stopFlushing)
		}

		next.ServeHTTP(c, r)
		c.outHeader = nil // make sure header changes aren't respected any more

		flushed := make(chan struct{})
		if logToLogservice() {
			stopFlushing <- 1 // any logging beyond this point will be dropped

			// Flush any pending logs asynchronously.
			c.pendingLogs.Lock()
			flushes := c.pendingLogs.flushes
			if len(c.pendingLogs.lines) > 0 {
				flushes++
			}
			c.pendingLogs.Unlock()
			go func() {
				defer close(flushed)
				// Force a log flush, because with very short requests we
				// may not ever flush logs.
				c.flushLog(true)
			}()
			w.Header().Set(logFlushHeader, strconv.Itoa(flushes))
		}

		// Avoid nil Write call if c.Write is never called.
		if c.outCode != 0 {
			w.WriteHeader(c.outCode)
		}
		if c.outBody != nil {
			w.Write(c.outBody)
		}
		if logToLogservice() {
			// Wait for the last flush to complete before returning,
			// otherwise the security ticket will not be valid.
			<-flushed
		}
	})
}

func executeRequestSafelyMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if x := recover(); x != nil {
				c := w.(*context)
				logf(c, 4, "%s", renderPanic(x)) // 4 == critical
				c.outCode = 500
			}
		}()

		next.ServeHTTP(w, r)
	})
}

func renderPanic(x interface{}) string {
	buf := make([]byte, 16<<10) // 16 KB should be plenty
	buf = buf[:runtime.Stack(buf, false)]

	// Remove the first few stack frames:
	//   this func
	//   the recover closure in the caller
	// That will root the stack trace at the site of the panic.
	const (
		skipStart  = "internal.renderPanic"
		skipFrames = 2
	)
	start := bytes.Index(buf, []byte(skipStart))
	p := start
	for i := 0; i < skipFrames*2 && p+1 < len(buf); i++ {
		p = bytes.IndexByte(buf[p+1:], '\n') + p + 1
		if p < 0 {
			break
		}
	}
	if p >= 0 {
		// buf[start:p+1] is the block to remove.
		// Copy buf[p+1:] over buf[start:] and shrink buf.
		copy(buf[start:], buf[p+1:])
		buf = buf[:len(buf)-(p+1-start)]
	}

	// Add panic heading.
	head := fmt.Sprintf("panic: %v\n\n", x)
	if len(head) > len(buf) {
		// Extremely unlikely to happen.
		return head
	}
	copy(buf[len(head):], buf)
	copy(buf, head)

	return string(buf)
}

// context represents the context of an in-flight HTTP request.
// It implements the appengine.Context and http.ResponseWriter interfaces.
type context struct {
	req *http.Request

	outCode   int
	outHeader http.Header
	outBody   []byte

	pendingLogs struct {
		sync.Mutex
		lines   []*logpb.UserAppLogLine
		flushes int
	}
}

var contextKey = "holds a *context"

// jointContext joins two contexts in a superficial way.
// It takes values and timeouts from a base context, and only values from another context.
type jointContext struct {
	base       netcontext.Context
	valuesOnly netcontext.Context
}

func (c jointContext) Deadline() (time.Time, bool) {
	return c.base.Deadline()
}

func (c jointContext) Done() <-chan struct{} {
	return c.base.Done()
}

func (c jointContext) Err() error {
	return c.base.Err()
}

func (c jointContext) Value(key interface{}) interface{} {
	if val := c.base.Value(key); val != nil {
		return val
	}
	return c.valuesOnly.Value(key)
}

// fromContext returns the App Engine context or nil if ctx is not
// derived from an App Engine context.
func fromContext(ctx netcontext.Context) *context {
	c, _ := ctx.Value(&contextKey).(*context)
	return c
}

func withContext(parent netcontext.Context, c *context) netcontext.Context {
	ctx := netcontext.WithValue(parent, &contextKey, c)
	if ns := c.req.Header.Get(curNamespaceHeader); ns != "" {
		ctx = withNamespace(ctx, ns)
	}
	return ctx
}

func toContext(c *context) netcontext.Context {
	return withContext(netcontext.Background(), c)
}

func IncomingHeaders(ctx netcontext.Context) http.Header {
	if c := fromContext(ctx); c != nil {
		return c.req.Header
	}
	return nil
}

func ReqContext(req *http.Request) netcontext.Context {
	return req.Context()
}

func WithContext(parent netcontext.Context, req *http.Request) netcontext.Context {
	return jointContext{
		base:       parent,
		valuesOnly: req.Context(),
	}
}

// RegisterTestRequest registers the HTTP request req for testing, such that
// any API calls are sent to the provided URL.
// It should only be used by aetest package.
func RegisterTestRequest(req *http.Request, apiURL *url.URL, appID string) *http.Request {
	ctx := req.Context()
	ctx = withAPIHostOverride(ctx, apiURL.Hostname())
	ctx = withAPIPortOverride(ctx, apiURL.Port())
	ctx = WithAppIDOverride(ctx, appID)

	// use the unregistered request as a placeholder so that withContext can read the headers
	c := &context{req: req}
	c.req = req.WithContext(withContext(ctx, c))
	return c.req
}

var errTimeout = &CallError{
	Detail:  "Deadline exceeded",
	Code:    int32(remotepb.RpcError_CANCELLED),
	Timeout: true,
}

func (c *context) Header() http.Header { return c.outHeader }

// Copied from $GOROOT/src/pkg/net/http/transfer.go. Some response status
// codes do not permit a response body (nor response entity headers such as
// Content-Length, Content-Type, etc).
func bodyAllowedForStatus(status int) bool {
	switch {
	case status >= 100 && status <= 199:
		return false
	case status == 204:
		return false
	case status == 304:
		return false
	}
	return true
}

func (c *context) Write(b []byte) (int, error) {
	if c.outCode == 0 {
		c.WriteHeader(http.StatusOK)
	}
	if len(b) > 0 && !bodyAllowedForStatus(c.outCode) {
		return 0, http.ErrBodyNotAllowed
	}
	c.outBody = append(c.outBody, b...)
	return len(b), nil
}

func (c *context) WriteHeader(code int) {
	if c.outCode != 0 {
		logf(c, 3, "WriteHeader called multiple times on request.") // error level
		return
	}
	c.outCode = code
}

func post(ctx netcontext.Context, body []byte, timeout time.Duration) (b []byte, err error) {
	apiURL := apiURL(ctx)
	hreq := &http.Request{
		Method: "POST",
		URL:    apiURL,
		Header: http.Header{
			apiEndpointHeader: apiEndpointHeaderValue,
			apiMethodHeader:   apiMethodHeaderValue,
			apiContentType:    apiContentTypeValue,
			apiDeadlineHeader: []string{strconv.FormatFloat(timeout.Seconds(), 'f', -1, 64)},
		},
		Body:          ioutil.NopCloser(bytes.NewReader(body)),
		ContentLength: int64(len(body)),
		Host:          apiURL.Host,
	}
	c := fromContext(ctx)
	if c != nil {
		if info := c.req.Header.Get(dapperHeader); info != "" {
			hreq.Header.Set(dapperHeader, info)
		}
		if info := c.req.Header.Get(traceHeader); info != "" {
			hreq.Header.Set(traceHeader, info)
		}
	}

	tr := apiHTTPClient.Transport.(*http.Transport)

	var timedOut int32 // atomic; set to 1 if timed out
	t := time.AfterFunc(timeout, func() {
		atomic.StoreInt32(&timedOut, 1)
		tr.CancelRequest(hreq)
	})
	defer t.Stop()
	defer func() {
		// Check if timeout was exceeded.
		if atomic.LoadInt32(&timedOut) != 0 {
			err = errTimeout
		}
	}()

	hresp, err := apiHTTPClient.Do(hreq)
	if err != nil {
		return nil, &CallError{
			Detail: fmt.Sprintf("service bridge HTTP failed: %v", err),
			Code:   int32(remotepb.RpcError_UNKNOWN),
		}
	}
	defer hresp.Body.Close()
	hrespBody, err := ioutil.ReadAll(hresp.Body)
	if hresp.StatusCode != 200 {
		return nil, &CallError{
			Detail: fmt.Sprintf("service bridge returned HTTP %d (%q)", hresp.StatusCode, hrespBody),
			Code:   int32(remotepb.RpcError_UNKNOWN),
		}
	}
	if err != nil {
		return nil, &CallError{
			Detail: fmt.Sprintf("service bridge response bad: %v", err),
			Code:   int32(remotepb.RpcError_UNKNOWN),
		}
	}
	return hrespBody, nil
}

func Call(ctx netcontext.Context, service, method string, in, out proto.Message) error {
	if ns := NamespaceFromContext(ctx); ns != "" {
		if fn, ok := NamespaceMods[service]; ok {
			fn(in, ns)
		}
	}

	if f, ctx, ok := callOverrideFromContext(ctx); ok {
		return f(ctx, service, method, in, out)
	}

	// Handle already-done contexts quickly.
	select {
	case <-ctx.Done():
		return ctx.Err()
	default:
	}

	c := fromContext(ctx)

	// Apply transaction modifications if we're in a transaction.
	if t := transactionFromContext(ctx); t != nil {
		if t.finished {
			return errors.New("transaction context has expired")
		}
		applyTransaction(in, &t.transaction)
	}

	// Default RPC timeout is 60s.
	timeout := 60 * time.Second
	if deadline, ok := ctx.Deadline(); ok {
		timeout = deadline.Sub(time.Now())
	}

	data, err := proto.Marshal(in)
	if err != nil {
		return err
	}

	ticket := ""
	if c != nil {
		ticket = c.req.Header.Get(ticketHeader)
		if dri := c.req.Header.Get(devRequestIdHeader); IsDevAppServer() && dri != "" {
			ticket = dri
		}
	}
	req := &remotepb.Request{
		ServiceName: &service,
		Method:      &method,
		Request:     data,
		RequestId:   &ticket,
	}
	hreqBody, err := proto.Marshal(req)
	if err != nil {
		return err
	}

	hrespBody, err := post(ctx, hreqBody, timeout)
	if err != nil {
		return err
	}

	res := &remotepb.Response{}
	if err := proto.Unmarshal(hrespBody, res); err != nil {
		return err
	}
	if res.RpcError != nil {
		ce := &CallError{
			Detail: res.RpcError.GetDetail(),
			Code:   *res.RpcError.Code,
		}
		switch remotepb.RpcError_ErrorCode(ce.Code) {
		case remotepb.RpcError_CANCELLED, remotepb.RpcError_DEADLINE_EXCEEDED:
			ce.Timeout = true
		}
		return ce
	}
	if res.ApplicationError != nil {
		return &APIError{
			Service: *req.ServiceName,
			Detail:  res.ApplicationError.GetDetail(),
			Code:    *res.ApplicationError.Code,
		}
	}
	if res.Exception != nil || res.JavaException != nil {
		// This shouldn't happen, but let's be defensive.
		return &CallError{
			Detail: "service bridge returned exception",
			Code:   int32(remotepb.RpcError_UNKNOWN),
		}
	}
	return proto.Unmarshal(res.Response, out)
}

func (c *context) Request() *http.Request {
	return c.req
}

func (c *context) addLogLine(ll *logpb.UserAppLogLine) {
	// Truncate long log lines.
	// TODO(dsymonds): Check if this is still necessary.
	const lim = 8 << 10
	if len(*ll.Message) > lim {
		suffix := fmt.Sprintf("...(length %d)", len(*ll.Message))
		ll.Message = proto.String((*ll.Message)[:lim-len(suffix)] + suffix)
	}

	c.pendingLogs.Lock()
	c.pendingLogs.lines = append(c.pendingLogs.lines, ll)
	c.pendingLogs.Unlock()
}

var logLevelName = map[int64]string{
	0: "DEBUG",
	1: "INFO",
	2: "WARNING",
	3: "ERROR",
	4: "CRITICAL",
}

func logf(c *context, level int64, format string, args ...interface{}) {
	if c == nil {
		panic("not an App Engine context")
	}
	s := fmt.Sprintf(format, args...)
	s = strings.TrimRight(s, "\n") // Remove any trailing newline characters.
	if logToLogservice() {
		c.addLogLine(&logpb.UserAppLogLine{
			TimestampUsec: proto.Int64(time.Now().UnixNano() / 1e3),
			Level:         &level,
			Message:       &s,
		})
	}
	// Log to stdout if not deployed
	if !IsSecondGen() {
		log.Print(logLevelName[level] + ": " + s)
	}
}

// flushLog attempts to flush any pending logs to the appserver.
// It should not be called concurrently.
func (c *context) flushLog(force bool) (flushed bool) {
	c.pendingLogs.Lock()
	// Grab up to 30 MB. We can get away with up to 32 MB, but let's be cautious.
	n, rem := 0, 30<<20
	for ; n < len(c.pendingLogs.lines); n++ {
		ll := c.pendingLogs.lines[n]
		// Each log line will require about 3 bytes of overhead.
		nb := proto.Size(ll) + 3
		if nb > rem {
			break
		}
		rem -= nb
	}
	lines := c.pendingLogs.lines[:n]
	c.pendingLogs.lines = c.pendingLogs.lines[n:]
	c.pendingLogs.Unlock()

	if len(lines) == 0 && !force {
		// Nothing to flush.
		return false
	}

	rescueLogs := false
	defer func() {
		if rescueLogs {
			c.pendingLogs.Lock()
			c.pendingLogs.lines = append(lines, c.pendingLogs.lines...)
			c.pendingLogs.Unlock()
		}
	}()

	buf, err := proto.Marshal(&logpb.UserAppLogGroup{
		LogLine: lines,
	})
	if err != nil {
		log.Printf("internal.flushLog: marshaling UserAppLogGroup: %v", err)
		rescueLogs = true
		return false
	}

	req := &logpb.FlushRequest{
		Logs: buf,
	}
	res := &basepb.VoidProto{}
	c.pendingLogs.Lock()
	c.pendingLogs.flushes++
	c.pendingLogs.Unlock()
	if err := Call(toContext(c), "logservice", "Flush", req, res); err != nil {
		log.Printf("internal.flushLog: Flush RPC: %v", err)
		rescueLogs = true
		return false
	}
	return true
}

const (
	// Log flushing parameters.
	flushInterval      = 1 * time.Second
	forceFlushInterval = 60 * time.Second
)

func (c *context) logFlusher(stop <-chan int) {
	lastFlush := time.Now()
	tick := time.NewTicker(flushInterval)
	for {
		select {
		case <-stop:
			// Request finished.
			tick.Stop()
			return
		case <-tick.C:
			force := time.Now().Sub(lastFlush) > forceFlushInterval
			if c.flushLog(force) {
				lastFlush = time.Now()
			}
		}
	}
}

func ContextForTesting(req *http.Request) netcontext.Context {
	return toContext(&context{req: req})
}

func logToLogservice() bool {
	// TODO: replace logservice with json structured logs to $LOG_DIR/app.log.json
	// where $LOG_DIR is /var/log in prod and some tmpdir in dev
	return os.Getenv("LOG_TO_LOGSERVICE") != "0"
}


text:
namespace Bit.BlazorUI;

/// <summary>
/// An interface that, if implemented by a <see cref="BitDataGridColumnBase{TGridItem}"/> subclass, allows a <see cref="BitDataGrid{TGridItem}"/>
/// to understand the sorting rules associated with that column.
///
/// If a <see cref="BitDataGridColumnBase{TGridItem}"/> subclass does not implement this, that column can still be marked as sortable and can
/// be the current sort column, but its sorting logic cannot be applied to the data queries automatically. The developer would be
/// responsible for implementing that sorting logic separately inside their <see cref="BitDataGridItemsProvider{TGridItem}"/>.
/// </summary>
/// <typeparam name="TGridItem">The type of data represented by each row in the grid.</typeparam>
public interface IBitDataGridSortBuilderColumn<TGridItem>
{
    /// <summary>
    /// Gets the sorting rules associated with the column.
    /// </summary>
    public BitDataGridSort<TGridItem>? SortBuilder { get; }
}

redpajama_set_name: RedPajamaGithub


text:
package org.apache.pig.test;

import org.apache.pig.ExecType;
import org.apache.pig.backend.hadoop.executionengine.Launcher;
import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MRConfiguration;
import org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher;

public class MapReduceMiniCluster extends YarnMiniCluster {

  public MapReduceMiniCluster(int dataNodeCount, int nodeManagerCount) {
      super(dataNodeCount, nodeManagerCount);
  }

    @Override
  public ExecType getExecType() {
    return ExecType.MAPREDUCE;
  }

  static public Launcher getLauncher() {
    return new MapReduceLauncher();
  }

  @Override
  protected void setConfigOverrides() {
    m_mr_conf.setInt(MRConfiguration.SUMIT_REPLICATION, 2);
    m_mr_conf.setInt(MRConfiguration.MAP_MAX_ATTEMPTS, 2);
    m_mr_conf.setInt(MRConfiguration.REDUCE_MAX_ATTEMPTS, 2);
    m_mr_conf.setInt("pig.jobcontrol.sleep", 100);
  }
}


text:
import copy
from cStringIO import StringIO
from fnmatch import fnmatch
import gzip
import hashlib
import mimetypes
import os

from ftplib import FTP

import app_config

GZIP_FILE_TYPES = ['.html', '.js', '.json', '.css', '.xml']

class FakeTime:
    def time(self):
        return 1261130520.0

def checkExists (ftp, path):
    return path in ftp.nlst()

def uploadThis(ftp, path):
    files = os.listdir(path)
    os.chdir(path)
    for f in files:
        p = os.path.join(path, f)
        if os.path.isfile(p):
            fh = open(f, 'rb')
            # print "IS FILE %s" % p
            ftp.storbinary('STOR %s' % f, fh)
            fh.close()
        elif os.path.isdir(p):
            # print "IS DIR %s" % p
            if not checkExists(ftp, f):
                ftp.mkd(f)
            ftp.cwd(f)
            uploadThis(ftp, p)
    ftp.cwd('..')
    os.chdir('..')

# Hack to override gzip's time implementation
# See: http://stackoverflow.com/questions/264224/setting-the-gzip-timestamp-from-python
gzip.time = FakeTime()

def deploy_file(connection, src, dst, headers={}):
    """
    Deploy a single file to S3, if the local version is different.
    """

def deploy_folder(src, dst, headers={}, ignore=[]):
    """
    Deploy a folder to ContentFTP
    """
    print '\nDeploying...'
    ftp = FTP(app_config.FTP_URL)
    ftp.login(app_config.FTP_USER, app_config.FTP_PASS)
    ftp.cwd(app_config.FTP_PATH)
    if not checkExists(ftp, dst):
        ftp.mkd(dst)
    ftp.cwd(dst)
    uploadThis(ftp, os.path.join(src, 'build'))

    # for src, dst in to_deploy:
        # deploy_file(ftp, src, dst, headers)

def delete_folder(dst):
    """
    Delete a folder from S3.
    """
    s3 = boto.connect_s3()

    bucket = s3.get_bucket(app_config.S3_BUCKET['bucket_name'])

    for key in bucket.list(prefix='%s/' % dst):
        print 'Deleting %s' % (key.key)

        key.delete()


text:
from __future__ import division

import unittest

import numpy as np

from wyrm.io import convert_mushu_data

CHANNELS = 5
SAMPLES = 3
FS = 10

class TestConvertMushuData(unittest.TestCase):

    def setUp(self):
        xv, yv = np.meshgrid(list(range(CHANNELS)), list(range(SAMPLES)))
        self.data = xv * 10 + yv
        self.channels = ['ch %i' % i for i in range(CHANNELS)]
        self.time = np.linspace(0, SAMPLES / FS * 1000, SAMPLES, endpoint=False)
        self.markers = [[-10, 'a'], [0, 'b'], [1e100000, 'c']]

    def test_convert_mushu_data(self):
        """Convert mushu data."""
        cnt = convert_mushu_data(self.data, self.markers, FS, self.channels)
        # data
        np.testing.assert_array_equal(cnt.data, self.data)
        self.assertEqual(cnt.data.shape, (SAMPLES, CHANNELS))
        # axes
        timeaxis = np.linspace(0, SAMPLES / FS * 1000, SAMPLES, endpoint=False)
        np.testing.assert_array_equal(cnt.axes[0], timeaxis)
        np.testing.assert_array_equal(cnt.axes[1], self.channels)
        # names and units
        self.assertEqual(cnt.names, ['time', 'channel'])
        self.assertEqual(cnt.units, ['uV', '#'])
        # fs
        self.assertEqual(cnt.fs, FS)
        # markers
        self.assertEqual(cnt.markers, self.markers)

    def test_convert_mushu_data_copy(self):
        """convert_mushu_data should make a copy of its arguments."""
        data = self.data.copy()
        channels = self.channels[:]
        markers = self.markers[:]
        cnt = convert_mushu_data(data, self.markers, FS, channels)
        # data
        data[0, 0] = -1
        np.testing.assert_array_equal(cnt.data, self.data)
        # axes
        channels[0] = 'FOO'
        np.testing.assert_array_equal(cnt.axes[-1], self.channels)
        # markers
        markers[0][0] = markers[0][0] - 1
        self.assertEqual(cnt.markers, self.markers)

if __name__ == '__main__':
    unittest.main()


text:
@interface RedExampleSectionGroup : BGRecursiveTableViewDataSourceSectionGroup

@end


text:
sheaf::implicit_index_space_iterator::
~implicit_index_space_iterator()
{
  // Preconditions:

  // Body:

  // Nothing to do;

  // Postconditions:

  // Exit:

  return;
}

// PROTECTED MEMBER FUNCTIONS

sheaf::implicit_index_space_iterator::
implicit_index_space_iterator()
  : _host(0),
    _local_id(invalid_pod_index())
{
  // Preconditions:

  // Body:

  // Postconditions:

  ensure(invariant());

  // Exit:

  return;
}

sheaf::implicit_index_space_iterator::
implicit_index_space_iterator(const implicit_index_space_iterator& xother)
{
  // Preconditions:

  // Body:

  *this = xother;

  // Postconditions:

  ensure(host() == xother.host());
  ensure(index() == xother.index());

  // Exit:

  return;
}

sheaf::implicit_index_space_iterator&
sheaf::implicit_index_space_iterator::
operator=(const implicit_index_space_iterator& xother)
{
  // Preconditions:

  // Body:

  if(xother.is_attached())
  {
    _host = xother._host;
    _local_id = xother._local_id;

    (void) index_space_iterator::operator=(xother);
  }

  // Postconditions:

  ensure(invariant());
  ensure((*this) == xother);

  // Exit

  return *this;
}

// PRIVATE MEMBER FUNCTIONS

// ===========================================================
// INDEX_SPACE_ITERATOR FACET
// ===========================================================

// PUBLIC MEMBER FUNCTIONS

sheaf::implicit_index_space_iterator&
sheaf::implicit_index_space_iterator::
operator=(const index_space_iterator& xother)
{
  // Preconditions:

  require(is_ancestor_of(&xother));

  // Body:

  const implicit_index_space_iterator& lother =
    dynamic_cast<const implicit_index_space_iterator&>(xother);

  (void) this->operator=(lother);

  // Postconditions:

  ensure(invariant());
  ensure((*this) == xother);

  // Exit

  return *this;
}

bool
sheaf::implicit_index_space_iterator::
operator==(const index_space_iterator& xother) const
{
  // Preconditions:

  require(is_ancestor_of(&xother));

  // Body:

  bool result = index_space_iterator::operator==(xother);
  if(result && is_attached())
  {
    const implicit_index_space_iterator& lother =
      dynamic_cast<const implicit_index_space_iterator&>(xother);

    result = result && (_host == lother._host);
    result = result && (_local_id == lother._local_id);
  }

  // Postconditions:

  ensure(is_basic_query);

  // Exit:

  return result;
}

sheaf::implicit_index_space_iterator*
sheaf::implicit_index_space_iterator::
clone() const
{
  implicit_index_space_iterator* result;

  // Preconditions:

  // Body:

  result = 0; // Just to silence compiler warnings.

  is_abstract();

  // Postconditions:

  ensure(result != 0);
  ensure(is_same_type(result));
  ensure(*result == *this);

  // Exit:

  return result;
}

// PROTECTED MEMBER FUNCTIONS

// PRIVATE MEMBER FUNCTIONS

// ===========================================================
// HANDLE FACET
// ===========================================================

// PUBLIC MEMBER FUNCTIONS

const sheaf::index_space_collection&
sheaf::implicit_index_space_iterator::
host() const
{
  // Preconditions:

  require(is_attached());

  // Body:

  const index_space_collection& result = *_host;

  // Postconditions:

  ensure(is_basic_query);

  // Exit:

  return result;
}

sheaf::index_space_iterator::pod_type
sheaf::implicit_index_space_iterator::
index() const
{
  // Preconditions:

  require(is_attached());

  // Body:

  pod_type result = _host->family_scope(_local_id);

  // Postconditions:

  ensure(id_spaces().contains(result));

  // Exit:

  return result;
}

bool
sheaf::implicit_index_space_iterator::
is_attached() const
{
  // Preconditions:

  // Body:

  bool result = is_valid(_local_id);

  // Postconditions:

  ensure(is_basic_query);

  // Exit:

  return result;
}

void
sheaf::implicit_index_space_iterator::
attach_to(pod_type xindex)
{
  // Preconditions:

  require(is_attached());
  require(id_spaces().contains(xindex));
  require(conforms_to_state(xindex));

  // Body:

  define_old_variable(const index_space_family& old_id_spaces = id_spaces());

  const index_space_collection* lhost =
    _host->contains(xindex) ? _host : id_spaces().collection(xindex);

  attach_to(*lhost, lhost->local_scope(xindex));

  // Postconditions:

  ensure(invariant());
  ensure(is_attached());
  ensure(&id_spaces() == &old_id_spaces);
  ensure(index() == xindex);

  // Exit:

  return;
}

void
sheaf::implicit_index_space_iterator::
detach()
{
  // Preconditions:

  // Body:

  _local_id = invalid_pod_index();

  // Postconditions:

  ensure(!is_attached());

  // Exit:

  return;
}

// PROTECTED MEMBER FUNCTIONS

// PRIVATE MEMBER FUNCTIONS

// ===========================================================
// INDEX_SPACE_ITERATOR FACET
// ===========================================================

// PUBLIC MEMBER FUNCTIONS

// PROTECTED MEMBER FUNCTIONS

// PRIVATE MEMBER FUNCTIONS

// ===========================================================
// ANY FACET
// ===========================================================

// PUBLIC MEMBER FUNCTIONS

bool
sheaf::implicit_index_space_iterator::
is_ancestor_of(const any *other) const
{
  // Preconditions:

  require(other != 0);

  // Body:

  // True if other conforms to this

  bool result = dynamic_cast<const implicit_index_space_iterator*>(other) != 0;

  // Postconditions:

  // Exit:

  return result;
}

bool
sheaf::implicit_index_space_iterator::
invariant() const
{
  bool result = true;

  if(invariant_check())
  {
    // Must satisfy base class invariant

    invariance(index_space_iterator::invariant());

    // Prevent recursive calls to invariant

    disable_invariant_check();

    // Invariances for this class:

    // Finished, turn invariant checking back on.

    enable_invariant_check();
  }

  // Exit

  return result;
}

// PROTECTED MEMBER FUNCTIONS

// PRIVATE MEMBER FUNCTIONS

// ===========================================================
//  NON-MEMBER FUNCTIONS
// ===========================================================


text:
import importlib
import os

import pytest
from model_metadata.scripting import cp

# from .conftest import INPUT_FILE, Bmi

def load_component(entry_point):
    module_name, cls_name = entry_point.split(":")

    component = None
    try:
        module = importlib.import_module(module_name)
    except ImportError:
        raise
    else:
        try:
            component = module.__dict__[cls_name]
        except KeyError:
            raise ImportError(cls_name)

    return component

try:
    class_to_test = os.environ["BMITEST_CLASS"]
except KeyError:
    Bmi = None
else:
    Bmi = load_component(class_to_test)

INPUT_FILE = os.environ.get("BMITEST_INPUT_FILE", None)

@pytest.mark.dependency()
def test_has_initialize():
    """Test component has an initialize method."""
    bmi = Bmi()
    assert hasattr(bmi, "initialize")

@pytest.mark.dependency()
def test_has_finalize():
    """Test component has a finalize method."""
    bmi = Bmi()
    assert hasattr(bmi, "finalize")

@pytest.mark.dependency(
    depends=["has_initialize", "has_finalize"], name="initialize_works"
)
def test_initialize(tmpdir):
    """Test component can initialize itself."""
    infile = os.environ.get("BMITEST_INPUT_FILE", None)
    manifest = os.environ.get("BMITEST_MANIFEST", infile or "").splitlines()

    with tmpdir.as_cwd() as prev:
        for file_ in [fname.strip() for fname in manifest]:
            if file_:
                cp(os.path.join(str(prev), file_), tmpdir / file_, create_dirs=True)

        bmi = Bmi()
        assert bmi.initialize(INPUT_FILE) is None
        bmi.finalize()

@pytest.mark.dependency(depends=["initialize_works"])
def test_update(tmpdir):
    """Test component can update itself."""
    infile = os.environ.get("BMITEST_INPUT_FILE", None)
    manifest = os.environ.get("BMITEST_MANIFEST", infile or "").splitlines()

    with tmpdir.as_cwd() as prev:
        for file_ in [fname.strip() for fname in manifest]:
            if file_:
                cp(os.path.join(str(prev), file_), tmpdir / file_, create_dirs=True)

        bmi = Bmi()
        bmi.initialize(INPUT_FILE)
        assert bmi.update() is None
        bmi.finalize()


text:
package aQute.tester.test.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.namespace.ExecutionEnvironmentNamespace;

import aQute.launchpad.BundleBuilder;
import aQute.launchpad.BundleSpecBuilder;
import aQute.launchpad.Launchpad;
import aQute.lib.exceptions.Exceptions;

/**
 * Utility class for reflectively accessing class definitions inside a bundle.
 * <p>
 * Testing the tester requires test classes to feed it. The tests in this
 * project have the feeder test classes in the src directory and they are
 * accessible on the classpath at runtime, but in order to create a true test
 * they are copied into the framework-under-test rather than imported from the
 * framework classloader. This utility class allows you to reflectively access
 * the class objects inside the framework.
 *
 * @author Fr Jeremy Krieg (fr.jkrieg@greekwelfaresa.org.au)
 */
public class TestBundler {

	final Launchpad			lp;

	Map<Class<?>, Class<?>>	classMap	= new HashMap<>();
	Map<Class<?>, Bundle>	bundleMap	= new HashMap<>();

	public TestBundler(Launchpad lp) {
		this.lp = lp;
	}

	public Bundle startTestBundle(Class<?>... testClasses) {
		return buildTestBundle(testClasses).start();
	}

	public Bundle installTestBundle(Class<?>... testClasses) throws Exception {
		return buildTestBundle(testClasses).install();
	}

	public BundleSpecBuilder buildTestBundle(Class<?>... testClasses) {
		BundleBuilder underlying = lp.bundle();
		BundleSpecBuilder bb = new BundleSpecBuilder() {
			@Override
			public BundleBuilder x() {
				return underlying;
			}

			@Override
			public Bundle install() throws Exception {
				Bundle retval = BundleSpecBuilder.super.install();
				Stream.of(testClasses)
					.forEach(x -> bundleMap.put(x, retval));
				return retval;
			}
		};
		final StringBuilder sb = new StringBuilder(128);
		boolean first = true;
		for (Class<?> testClass : testClasses) {
			if (first) {
				first = false;
			} else {
				sb.append(',');
			}
			sb.append(testClass.getName());
			bb.addResourceWithCopy(testClass);
		}
		System.err.println("Creating bundle: " + bb + ", " + sb);
		withEE(bb.header("Test-Cases", sb.toString()));
		// Don't chain the return value of withEE in this case - we need to
		// return actual custom instance above
		return bb;
	}

	public static BundleSpecBuilder withEE(BundleSpecBuilder bb) {
		return bb.requireCapability(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE)
			.filter("(&(osgi.ee=JavaSE)(version=1.8))");
	}

	public BundleSpecBuilder bundleWithEE() {
		return withEE(lp.bundle());

	}

	public Bundle getBundleOf(Class<?> testClass) {
		return bundleMap.get(testClass);
	}

	public Class<?> getBundleClass(Class<?> testClass) {
		Bundle b = bundleMap.get(testClass);
		try {
			return b.loadClass(testClass.getName());
		} catch (ClassNotFoundException e) {
			throw Exceptions.duck(e);
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T getStatic(Class<?> testClass, String field) {
		try {
			Field f = getBundleClass(testClass).getField(field);
			f.setAccessible(true);
			return (T) f.get(null);
		} catch (Exception e) {
			throw Exceptions.duck(e);
		}
	}

	/**
	 * Convenience version that allows you to avoid explicit cast (neater for
	 * chaining).
	 */
	@SuppressWarnings("unchecked")
	public <T> T getStatic(Class<?> testClass, Class<T> fieldType, String field) {
		try {
			Field f = getBundleClass(testClass).getField(field);
			f.setAccessible(true);
			return (T) f.get(null);
		} catch (Exception e) {
			throw Exceptions.duck(e);
		}
	}

	// Convenience wrappers for common fields in the test classes
	@SuppressWarnings("unchecked")
	public BundleContext getBundleContext(Class<?> testClass) {
		return ((AtomicReference<BundleContext>) getStatic(testClass, "bundleContext")).get();
	}

	@SuppressWarnings("unchecked")
	public BundleContext getActualBundleContext(Class<?> testClass) {
		return ((AtomicReference<BundleContext>) getStatic(testClass, "actualBundleContext")).get();
	}

	@SuppressWarnings("unchecked")
	public Thread getCurrentThread(Class<?> testClass) {
		return ((AtomicReference<Thread>) getStatic(testClass, "currentThread")).get();
	}

	public int getInteger(String fieldName, Class<?> testClass) {
		return ((AtomicInteger) getStatic(testClass, fieldName)).get();
	}

	public boolean getFlag(Class<?> testClass, String flag) {
		return ((AtomicBoolean) getStatic(testClass, flag)).get();
	}
}


text:
<?php

declare(strict_types=1);

namespace BEAR\Resource\Annotation;

use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

/**
 * @Annotation
 * @Target("METHOD")
 * @NamedArgumentConstructor
 */
#[Attribute(Attribute::TARGET_METHOD)]
final class JsonSchema
{
    /**
     * Json schema body key name
     *
     * @var string
     */
    public $key = '';

    /** @var string */
    public $schema;

    /**
     * Input parameter validation schema
     *
     * @var string
     */
    public $params;

    /**
     * @Enum({"view", "body"})
     * @var string
     */
    public $target;

    public function __construct(string $schema = '', string $key = '', string $params = '', string $target = 'body')
    {
        $this->key = $key;
        $this->schema = $schema;
        $this->params = $params;
        $this->target = $target;
    }
}


text:
package org.pantsbuild.tools.junit.impl;

import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;

import org.pantsbuild.junit.annotations.TestSerial;

@TestSerial
public class ConsoleRunnerTestHelper {

  @Before
  public void setUp() {
    ConsoleRunnerImpl.setCallSystemExitOnFinish(false);
    ConsoleRunnerImpl.setExitStatus(0);
    TestRegistry.reset();
  }

  @After
  public void tearDown() {
    ConsoleRunnerImpl.setCallSystemExitOnFinish(true);
    ConsoleRunnerImpl.setExitStatus(0);
  }

  protected void assertContainsTestOutput(String output) {
    Assert.assertThat(output, CoreMatchers.containsString("test41"));
    Assert.assertThat(output, CoreMatchers.containsString("start test42"));
    Assert.assertThat(output, CoreMatchers.containsString("end test42"));
  }

  protected String[] asArgsArray(String cmdLine) {
    String[] args = cmdLine.split(" ");
    for (int i = 0; i < args.length; i++) {
      if (args[i].contains("Test")) {
        args[i] = getClass().getPackage().getName() + '.' + args[i];
      }
    }
    return args;
  }
}


text:
package org.apache.lens.regression.core.helpers;

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import javax.xml.bind.JAXBException;

import org.apache.lens.api.StringList;
import org.apache.lens.regression.core.constants.MetastoreURL;
import org.apache.lens.regression.core.type.MapBuilder;
import org.apache.lens.regression.util.AssertUtil;
import org.apache.lens.server.api.error.LensException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MetastoreHelper extends ServiceManagerHelper {

  private WebTarget servLens = ServiceManagerHelper.getServerLens();
  private String sessionHandleString = ServiceManagerHelper.getSessionHandle();

  public MetastoreHelper() {
  }

  public MetastoreHelper(String envFileName) {
    super(envFileName);
  }

  /**
   * Set Current Database for a Session
   *
   * @param sessionHandleString
   * @param currentDBName
   */

  public void setCurrentDatabase(String sessionHandleString, String currentDBName) throws JAXBException, LensException {
    MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
    Response response = this.exec("put", MetastoreURL.METASTORE_DATABASES_CURRENT_URL, servLens, null, query,
        MediaType.APPLICATION_XML_TYPE, null, currentDBName);
    AssertUtil.assertSucceededResponse(response);
    response = this.exec("get", MetastoreURL.METASTORE_DATABASES_CURRENT_URL, servLens, null, query);
    String responseString = response.readEntity(String.class);
    AssertUtil.assertSucceededResponse(response);
    log.info("Response: {}", responseString.trim());
    if (!responseString.trim().equals(currentDBName)) {
      throw new LensException("Could not set database");
    }
    log.info("Set Current database to {}", currentDBName);
  }

  public void setCurrentDatabase(String currentDBName) throws JAXBException, LensException {
    setCurrentDatabase(sessionHandleString, currentDBName);
  }

  /**
   * Get Current Database for a Session
   *
   * @param sessionHandleString
   * @return the current DB Name
   */

  public String getCurrentDatabase(String sessionHandleString) throws JAXBException, LensException {
    MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
    Response response = this.exec("get", MetastoreURL.METASTORE_DATABASES_CURRENT_URL, servLens, null, query,
        MediaType.APPLICATION_XML_TYPE, null);
    AssertUtil.assertSucceededResponse(response);
    return response.readEntity(String.class);
  }

  public String getCurrentDatabase() throws JAXBException, LensException {
    return getCurrentDatabase(sessionHandleString);
  }

  /**
   * Get list of databases
   *
   * @param sessionHandleString
   * @return List of all the Databases
   */

  public StringList listDatabases(String sessionHandleString) throws JAXBException, LensException {
    MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
    Response response = this
        .exec("get", MetastoreURL.METASTORE_DATABASES_URL, servLens, null, query, MediaType.APPLICATION_XML_TYPE, null);
    AssertUtil.assertSucceededResponse(response);
    StringList responseString = response.readEntity(StringList.class);
    return responseString;
  }

  public StringList listDatabases() throws JAXBException, LensException {
    return listDatabases(sessionHandleString);
  }

  /**
   * Create a database
   *
   * @param dbName
   * @param sessionHandleString
   */

  public void createDatabase(String dbName, String sessionHandleString) throws JAXBException, LensException {
    MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
    Response response = this
        .exec("post", MetastoreURL.METASTORE_DATABASES_URL, servLens, null, query, MediaType.APPLICATION_XML_TYPE, null,
            dbName);
    AssertUtil.assertSucceeded(response);
  }

  public void createDatabase(String dbName) throws JAXBException, LensException {
    createDatabase(dbName, sessionHandleString);
  }

  /**
   * Drop a DB
   *
   * @param dbName
   * @param sessionHandleString
   */
  public void dropDatabase(String dbName, String sessionHandleString) throws JAXBException, LensException {
    MapBuilder query = new MapBuilder("sessionid", sessionHandleString);
    query.put("cascade", "true");
    Response response = this.exec("delete", MetastoreURL.METASTORE_DATABASES_URL + "/" + dbName, servLens, null, query,
        MediaType.APPLICATION_XML_TYPE, null);
    AssertUtil.assertSucceeded(response);
  }

  public void dropDatabase(String dbName) throws JAXBException, LensException {
    dropDatabase(dbName, sessionHandleString);
  }

}