text:
import dbutils as db
import rpy2.robjects as R
import time

from pylab import *
from scipy import stats,array,log,linalg,ones,sqrt,zeros,repeat,\
    concatenate,dot,asarray

import DataMining as DM

pcolors=['b','g','r','c','m','y','k',
         'b-.','g-.','r-.','c-.','m-.','y-.','k-.',
         'b:','g:','r:','c:','m:','y:','k:',
         'b--','g--','r--','c--','m--','y--','k--',]
Inf = float('inf')

#### Standard Statistical Utility Functions ####

def E(data,weighted=False,alpha=0.05):
  """
  Given an array and an alpha, returns
    (E_bar,moe)
  where E_bar = sample mean of data, and
  moe = 1/2 size of the (1-alpha) Confidence Interval.

  If weighted is true, then E_bar is a weighted mean.
  In this case data should be an N-by-2 matrix, with
  each row of the form
    (data_value,weight).
  """

  if not weighted:
    E_bar = data.mean()
    moe = stats.t.ppf(1-alpha/2,len(data)-1) * data.std(ddof=1)/sqrt(len(data))

  else:
    w_total = data[:,1].sum()
    w_sum = dot( data[:,0],data[:,1] )
    E_bar = w_sum/w_total
    w_var = dot( (data[:,0]-E_bar)**2, data[:,1] ) / w_total
    w_sd = sqrt(w_var)
    moe = stats.t.ppf(1-alpha/2,len(data)-1) * w_sd / sqrt(len(data))
    print w_sd,w_total,moe,len(data)

  return E_bar,moe

def probability_make_transfer_sim(xdata1,wdata1,
                                  xdata2,wdata2,
                                  transfer_window, nsims = 100000):
  """
  Given R-Vectors xdata1, xdata2 representing instances of the first
  and second vehicle of the transfer, respectively, and R-vectors wdata1
  and wdata2 representing their respective weighted probabilities (need not
  sum to 1, can be None for equal probability), and the
  transfer window in seconds (that is, the scheduled time between
  the arrival of the first and second buses), calculates via simulation the
  probability that you will make that transfer.
  """
  total_made = 0
  total_num = 0

  print "Sampling..."
  rref1 = R.r['sample'](xdata1, nsims, replace = True,
                        prob = wdata1)
  rref2 = R.r['sample'](xdata2, nsims, replace = True,
                        prob=wdata2);
  samples = array(zip(rref1,rref2))

  print "Done."

  print "Simulating..."
  total_made = sum(samples[:,0] <= (samples[:,1]+transfer_window));

  print "Done."

  return float(total_made)/nsims

def p_make_transfer_vs_window(data1,data2=None,
                              windows=linspace(-300,900,16),weighted=True,
                              doplot=True):
  """
  Given optionally weighted lateness data and a set of transfer windows
  (in seconds), plots the likelihood of making a transfer against
  transfer window. Returns Nx2 array of results, where N is the number
  of windows; first column is the window, second column is the probability.

  data1 represents the lateness distribution of the first bus, while
  data2 represents that of the second bus. The transfer is made from
  the first bus to the second. If data2 is identical to data1, then
  pass in None for data2; this saves memory.
  """

  xdata2 = wdata1 = wdata2 = None

  xdata1 = R.FloatVector(data1[:,0])
  if weighted:
    wdata1 = R.FloatVector(data1[:,1])

  if(data2 is not None):
    xdata2 = R.FloatVector(data2[:,0])
    if weighted:
      wdata2 = R.FloatVector(data2[:,1])

  probs=[]

  for window in windows:
     prob = probability_make_transfer_sim(xdata1,wdata1,
                                          xdata2 or xdata1,wdata2 or wdata1,
                                          window)
     probs.append(prob)

  probs = array(probs)

  if doplot:
    figure()
    plot(windows,probs,'k-',label="P(make transfer)")
    #legend()
    xlabel("Transfer window (s)")
    ylabel("Probability of making transfer")
    title("Probability of making transfer vs transfer windows")

  return array(zip(windows,probs))

def expected_wait_time_random_arrival(xdata,wdata,headway,
                                      nsims = 5000000, ntrips=3,
                                      q_half=None):
  """
  Given R-Vector xdata representing instances and R-Vector wdata
  representing weighted probabilities of those instances (need not
  add to 1, can be None for equal probability), and the headway in
  seconds of the bus, calculates via simulation the average wait time
  for a person arriving at the stop at random.

  Returns (expected_wait, (lowerq,upperq))
  or just expected_wait, if q_half == None

  where expected_wait is the value as described above, and lowerq,upperq
  are the upper and lower q_half quantiles, respectively.

  This function requires R-Vectors because otherwise a memory leak
  in the rpy2 package causes major problems if you call this function
  multiple times.
  """
  total_waits = 0.0
  total_num = 0

  print "Sampling (r)..."
  samples_per_sim = 1+2*ntrips
  rref = R.r['sample'](xdata, nsims*samples_per_sim, replace = True,
                       prob = wdata)
  samples = asarray(rref)

  print "Done."

  print "Simulating (r)..."
  timer = time.time()

  arrival_x = amap(int, random(nsims) * (headway) ) - headway/2
  arrival_x.resize((nsims,1))
  samples.resize( nsims, samples_per_sim )
  samples += arange(-ntrips,ntrips+1)*headway
  samples -= arrival_x

  wait_times = where(samples >= 0, samples, float('inf')).min(1)
  valids = (wait_times != float('inf'))
  wait_times = wait_times[valids]
  total_waits = wait_times.sum()
  total_num = valids.sum()

  if q_half:
    print "Finding quantiles (r)..."
    del samples,valids,arrival_x,rref #make room if necessary
    w_ecdf,p_ecdf,e_ecdf = ecdf(wait_times,weighted=False,alpha=0.05)
    lowerq,p,j = find_quantile(q_half,w_ecdf,p_ecdf)
    upperq,p,j = find_quantile(1.0-q_half,w_ecdf,p_ecdf)

  timer = time.time() - timer
  print "Done (r):",timer

  if q_half:
    return (total_waits/total_num), (lowerq,upperq)
  return total_waits/total_num

def calc_expected_wait_time_for_random_arrival(data,headways,weighted=True):

  xdata = R.FloatVector(data[:,0])
  if weighted:
    wdata = R.FloatVector(data[:,1])
  else:
    wdata = None

  ret = {}
  for headway in headways:
    ew = expected_wait_time_random_arrival(xdata,wdata,headway)
    print headway,ew
    ret[headway]=ew

  return ret

def trip_plan_evaluation_sim( xfer_arrive_data, xfer_arrive_time,
                              xfer_depart_data, xfer_depart_times,
                              dest_arrive_data, dest_arrive_times,
                              nsims = 100000):
  """
  Given the lateness distributions for a transfer and arrival at
  the final destination, computes an ECDF for the actual arrival
  time at the final destination. If the ECDF does not accumulate
  fully to 100%, then add another xfer_depart_time or just accept
  that some percent of the time you never make it to your destination.

  xfer_depart_times is a SORTED (increasing) list of the scheduled
  departure times of the 2nd bus of the trip.

  dest_arrive_times should correspond to the scheduled arrival times
  of each of the scheduled 2nd bus trips.
  """

  xarrd,xarrw = map(R.FloatVector,xfer_arrive_data.transpose())
  xdepd,xdepw = map(R.FloatVector,xfer_depart_data.transpose())
  darrd,darrw = map(R.FloatVector,dest_arrive_data.transpose())

  print "Sampling..."
  rref1 = R.r['sample'](xarrd, nsims, replace=True, prob=xarrw)
  rref2 = R.r['sample'](xdepd, nsims*len(xfer_depart_times),
                        replace=True, prob=xdepw)
  rref3 = R.r['sample'](darrd, nsims, replace=True, prob=darrw)

  s1,s2,s3 = map(asarray, (rref1,rref2,rref3))
  s2.resize( nsims, len(xfer_depart_times) )

  print "Done."

  print "Simulating..."
  xfers = array([False]*nsims)
  s1 += xfer_arrive_time
  s2 += xfer_depart_times

  # Find which transfer we made in each sim
  for i in range(len(xfer_depart_times)):
    made_this_xfer = (~ xfers) * (s1 < s2[:,i])
    s3[made_this_xfer] += dest_arrive_times[i]
    xfers += made_this_xfer

  print "Done."
  print "%d transfers totally never made" % ( (~ xfers).sum() )
  return where(xfers,s3,Inf)

def expected_wait_time_simulation(xdata,wdata,arrival_x,headway,
                                  nsims = 500000, ntrips=3,
                                  q_half=None):
  """
  Given lateness data which is optionally weighted,
  the arrival time of one waiting for the bus, and the headway
  for the route (i.e. the expected time between buses), computes
  by simulation the expected time that one will wait until a
  bus arrives.

  Returns:  (expected_wait_time, (lower_q,upper_q))
  or just expected_wait_time if q_half == None.

  where expected_wait_time is the value as described above, and
  lower_q and upper_q are the lower and upper q_half quantiles,
  respectively.

  xdata and wdata should be R Vectors!!! This is due to a memory leak
  in rpy2.

  nsims = number of iterations of simulation
  ntrips = number of arrivals placed before and after central trip.
           i.e., total number of arrivals will be ntrips*2 + 1.
           arrival_x is relative to the central trip.
  """

  ## We will use the R library to sample from our data,
  ## with weighted probability distribution if provided.

  total_waits = 0.0
  total_num = 0

  print "Sampling..."
  samples_per_sim = 1 + 2*ntrips
  rref = R.r['sample'](xdata, nsims * samples_per_sim,replace = True,
                       prob = wdata)
  samples = asarray(rref)

  print " done."

  print "Simulating..."
  timer = time.time()

  samples.resize( nsims, samples_per_sim )
  samples += arange(-ntrips,ntrips+1)*headway - arrival_x

  wait_times = where(samples >= 0, samples, float('inf')).min(1)
  valids = (wait_times != float('inf'))
  wait_times = wait_times[valids]
  total_waits = wait_times.sum()
  total_num = valids.sum()

  if q_half:
    print "Finding quantiles..."
    del rref,samples,valids # make room if needed
    w_ecdf, p_ecdf, e_ecdf = ecdf(wait_times,weighted=False,alpha=0.05)
    q_bottom,p,j = find_quantile(q_half,w_ecdf,p_ecdf)
    q_top,p,j = find_quantile(1.0-q_half,w_ecdf,p_ecdf)

  timer = time.time()-timer
  print " done:",timer

  if q_half:
    return (total_waits / total_num) , (q_bottom,q_top)

  return total_waits / total_num

def expected_wait_vs_arrival_plot(data,headways,min_arrival,
                                  weighted=True,doplot=True,
                                  ofile="simplot.png",q_half=None):
  """
  Given an optionally weighted dataset, a set of headways (in seconds),
  and a minimum value for relative person arrival time (in seconds),
  returns a dict with entry for each provided headway:
    { headway : (arrivals, expected_waits, expected_wait_qbounds,
                 expected_wait_random, expected_wait_random_qbound) }

  where:
    arrivals is a list of rider arrival times
    expected_waits is a list of the corresponding average wait times
    expected_wait_qbounds is a list of the (upper,lower) quantiles of the wait times
    expected_wait_random is the average wait time for a random rider arrival time
    expected_wait_random_qbound is the (upper,lower) quantiles of the wait fime for a random rider arrival time.

  The quantiles returned are the upper and lower q_half quantiles.
  If q_half is None, then the return values are only
    { headway : (arrivals, expected_waits, expected_wait_random) }

  If doplot is True, then plots expected wait time for that person for
  each value of headway, for personal arrival times from min_arrival
  to min_arrival+headway (i.e. one period).
  """
  xdata = R.FloatVector(data[:,0])
  if weighted:
    wdata = R.FloatVector(data[:,1])
  else:
    wdata = None
  ret = {}
  f=figure()
  for i,headway in enumerate(headways):
    print "Computing plot for headway",headway,"..."
    ew = []
    eb = []
    arrivals=arange(min_arrival,min_arrival+headway+61,60)
    for arrival in arrivals:
      e_wait = expected_wait_time_simulation(xdata,wdata,arrival,
                                             headway,q_half=q_half)
      if q_half:
        e_wait, e_bounds = e_wait
        eb.append(e_bounds)

      ew.append(e_wait)

    ewrandom = expected_wait_time_random_arrival(xdata,wdata,headway,
                                                 q_half=q_half)
    if q_half:
      ewrandom, ewr_b = ewrandom
      ret[headway] = arrivals,ew,eb,ewrandom,ewr_b
    else:
      ret[headway] = arrivals,ew,ewrandom

  i=0
  if doplot and not q_half:
    for headway,(arrivals,ew,ewrandom) in ret.items():
      plot(arrivals,ew,pcolors[i],label="Headway="+str(headway))
      plot( (arrivals[0],arrivals[-1]), (ewrandom,ewrandom),
            pcolors[i]+"--",
            label="For random arrival")
      i+=1

    legend()
    xlabel("Your Arrival")
    ylabel("Expected Wait Time")
    title("Expected Wait Times vs Headway, Arrival")
    f.savefig(ofile)
  return ret

def ecdf(data,weighted=False,alpha=0.05):
  """
  Given an array and an alpha, returns
     (x,p,a_n)
  x and p are arrays, where x are values in the sample space and p
  is the corresponding cdf value.
  a_n is the margin of error according to the DKWM theorem using
  the supplied value of alpha. Interpreted, this means:

    P( {|cdf(x) - ecdf(x)| > a_n} ) <= alpha

  If weighted=True, then data should be a N-by-2 matrix,
  where each row contains
    (data_pt, weight)
  where weight is as defined in the Horvitz-Thompson estimate.
  """

  cdf = {}

  if not weighted:
    # give all elements weight of 1
    data = concatenate( (data.reshape(len(data),1),ones((len(data),1))), axis=1 )

  def helper((x,w)):
    cdf[x] = cdf.get(x,0.0) + w

  print "  Uniqueifying..."
  map(helper,data)

  # data now has unique values
  print "  Arraying..."
  data = array(cdf.items())

  print "  Weighting..."
  w_total = data[:,1].sum()

  print "  Sorting..."
  sort_order = data[:,0].argsort()
  sorted = data[sort_order]

  print "  Summing..."
  ret_x = repeat(sorted[:,0],2)
  ret_p = concatenate(( [0.0],
                        repeat(1./w_total * cumsum(sorted[:-1,1]),2),
                        [1.0] ))
  a_n = sqrt( 1./(2*w_total) * log(2./alpha) )

  return ret_x,ret_p,a_n

def find_quantile(q,x,p):
  """
  Given x and p as returned by the ecdf function above,
  finds the q-quantile. Specifically, returns
    (x_q,p_q,i)
  where x_q is the q-quantile, p_q is the cumulative probability
  for that x-value (should be very close to q), and i is the index
  at which (x_q,p_q) was found.
  """
  ixs = find(p>=q)
  if len(ixs) == 0:
    i = len(x)-1
  else:
    i = ixs[0]
  return x[i],p[i],i

def evaluate_ecdf(x0,x,p):
  """
  Given x and p as returned by the ecdf function above, finds the
  value of the ecdf evaluated at x0. Specifically, returns
    (x_v,p_v,i)
  where x_v is the closest found x-value, p_v is the value of the ecdf
  at that point, and i is the index in x,p of these points.
  """
  i = find(x == x0)
  if len(i) == 0:
    i = max( 0,find(x > x0)[0]-1 )
  else:
    i = i[-1]
  return (x[i],p[i],i)

def QEPlot(datasets,qs,weighted=True):
  """
  Given a dictionary mapping label strings to optionally weighted data sets,
  and a list of quantiles [q_1,q_2,...,q_n], plots the average and the given
  quantiles for each ECDF.

  Returns
   (Qs,Es)

  where Qs is a dict of the format

    { dataset : [ [q_1,q1_lower,q1_upper], ... ] }
  where q_1 in this case represents the actual quantile value of the
  dataset, _upper and _lower are the upper and lower 95% CI bounds on the
  quantile, and the keys of the dict are the same as those in the provided
  datasets parameter.

  Similarly Es is a dict of the format

    { dataset : [avg, moe] }

  where avg is the mean of the dataset, and moe is the 95% CI bounds
  on the mean.
  """

  Qs = {}
  Es = {}

  for name,data in datasets.items():
    x,p,a_n = ecdf(data,weighted=weighted)
    Q = []
    for q in qs:
      quantile = find_quantile(q,x,p)[0]
      lower_q = find_quantile(q,x,p+a_n)[0]
      upper_q = find_quantile(q,x,p-a_n)[0]
      Q.append( (quantile,lower_q,upper_q) )
    Qs[name] = array(Q)
    Es[name] = E(data,weighted=weighted)

  figure()
  for name in datasets.keys():
    Q = Qs[name]
    avg,moe = Es[name]

    plot( [name]*len(Q),Q[:,0], 'k-x' )
    plot( [name]*len(Q),Q[:,1], 'k_' )
    plot( [name]*len(Q),Q[:,2], 'k_' )
    plot( [name],[avg], 'kD')
    plot( [name]*2,[avg-moe,avg+moe], 'k_')

  return Qs,Es

def find_pred_interval(x,p,a_n,alpha=0.05):
  """
  Given x, p and margin of error as returned by the ecdf function,
  finds and returns the prediction interval
    ( (lower, upper),
      (lower-lower_moe,upper+upper_moe) )
  where lower and upper are inside the x-space and represent capturing
  (1-alpha) of the distribution's area, and lower_moe and upper_moe
  represent the margin of error that is introduced from the ecdf's
  margin of error a_n.
  """

  # Finds the lowest possible alpha/2 quantile
  x_lolo,p_lolo,i_lolo = find_quantile(alpha/2,x,p+a_n)

  # Finds the central lower interval bound
  x_lo,p_lo,i_lo = find_quantile(alpha/2,x,p)

  # Finds the highest possible 1-alpha/2 quantile
  x_hihi,p_hihi,i_hihi = find_quantile(1-alpha/2,x,p-a_n)

  # Finds the central upper interval bound
  x_hi,p_hi,i_hi = find_quantile(1-alpha/2,x,p)

  return ( (x_lo,x_hi),
           (x_lolo, x_hihi) )

def diffmeanCI(d1,d2,weighted=False,alpha=.05):
  """
  Given two arrays and an alpha value, returns the 1-alpha confidence
  interval for the difference in means between the two arrays.
  More specifically, the return value is:
    ( (lower_norm,upper_norm) , (lower_satt,upper_satt) )
  where (lower_norm,upper_norm) is the CI assuming a normal distribution
  for the datasets, and (lower_satt,upper_satt) is the CI without assuming
  any particular distribution (i.e. using the Satterthwaite degrees of
  freedom).
  """
  n1,n2=len(d1),len(d2)
  xbar1,xbar2=d1.mean(),d2.mean()
  s1,s2=d1.std(ddof=1),d2.std(ddof=1)

  center = xbar1-xbar2

  # Assuming equal variances
  s=sqrt( ((n1-1)*s1**2+(n2-1)*s2**2) / (n1+n2-2) )
  moe1 = stats.t.ppf(1-alpha/2,n1+n2-2)*s*sqrt(1./n1+1./n2)

  # Using Satterthwaite DOF
  w1,w2=d1.var(ddof=1)/n1,d2.var(ddof=1)/n2
  nu=((w1+w2)**2)/(w1**2/(n1-1)+w2**2/(n2-1))
  moe2 = stats.t.ppf(1-alpha/2,nu)*sqrt(s1**2/n1+s2**2/n2)

  return ( (center-moe1,center+moe1),
           (center-moe2,center+moe2) )

def compare_ecdfs(attrsplit,rows,
                  plot_CIs=False,
                  plot_Es=False,
                  plot_E_CIs=False,
                  col_name='lateness',
                  alpha=0.05):
  """
  Given a set of rows to split, and the attributes on which to split them,
  compares distributions and expectations on a plot (with optional
  confidence intervals).
  If rows are already split into a dict, then just put the partitioning
  column for attrsplit.
  """

  if isinstance(rows,dict):
    split = rows
  else:
    print "Splitting..."
    split = DM.split_on_attributes(attrsplit,rows)
    print "OK."

  figure()
  for i,key in enumerate(split.keys()):
    rows=array([(r[col_name],r['trip_stop_weight']) for r in split[key]])
    x,p,a_n = ecdf(rows,weighted=True,alpha=alpha/len(split)) #bonferroni
    plot(x,p,pcolors[i],label=str(key));
    print key,"ECDF MOE:",a_n

    if plot_CIs:
      plot(x,p-a_n,pcolors[i]+'--',label=None)
      plot(x,p+a_n,pcolors[i]+'--',label=None)

    E_bar,moe = E(rows,weighted=True,alpha=alpha/len(split)) #bonferroni
    print key,"E:",E_bar,", E MOE:",moe
    if plot_Es:
      plot((E_bar,E_bar),(0,1),pcolors[i],label=None)
    if plot_E_CIs:
      plot((E_bar-moe,E_bar-moe),(0,1),pcolors[i]+'--',label=None)
      plot((E_bar+moe,E_bar+moe),(0,1),pcolors[i]+'--',label=None)

  xlabel(col_name)
  ylabel("CDF("+col_name+")")
  title("ECDF of "+col_name+" partitioned by "+str(attrsplit))

#### Static Analyses ####

def compare_vehicle_type(rows=None):
  """Compare lateness distributions between vehicle types"""

  if rows is None:
    cur = db.get_cursor()
    sql = """
select vehicle_type,lateness,trip_stop_weight
from datamining_table natural join trip_stop_weights
where lateness is not null
"""
    print "Selecting..."
    db.SQLExec(cur,sql)
    print "Retrieving..."
    rows = cur.fetchall()
    cur.close()
    print len(rows),"rows retrieved."

  compare_ecdfs(('vehicle_type',),rows)

def compare_routenames(rows=None):
  """Compare lateness distributions between route ID's"""

  if rows is None:
    cur = db.get_cursor()
    sql = """
select route_name, lateness, trip_stop_weight
from datamining_table natural join trip_stop_weights
where lateness is not null
"""
    print "Selecting..."
    db.SQLExec(cur,sql)
    print "Retrieving..."
    rows = cur.fetchall()
    cur.close()
    print len(rows),"rows retrieved."

  compare_ecdfs(('route_name',),rows)

def compare_dows(rows=None):
  """Compares lateness distributions between days of the week"""

  if rows is None:
    cur = db.get_cursor()
    sql = """
select date_part('dow',trip_date) as wday, lateness, trip_stop_weight
from datamining_table
  natural join trip_stop_weights
  natural join gps_segments
where lateness is not null
"""
    print "Selecting..."
    db.SQLExec(cur,sql);
    print "Retrieving..."
    rows = cur.fetchall()
    cur.close();
    print len(rows),"rows retrieved."

  compare_ecdfs(('wday',),rows)
  return rows

def compare_hour_of_weekday(rows=None):
  """Compares lateness distributions between hours of the weekday"""

  if rows is None:
    cur = db.get_cursor()
    sql = """
select scheduled_hour_of_arrival as hoa, lateness, trip_stop_weight
from datamining_table natural join trip_stop_weights
where lateness is not null and service_id='1'
"""
    print "Selecting..."
    db.SQLExec(cur,sql)
    print "Retrieving..."
    rows = cur.fetchall()
    cur.close()
    print len(rows),"rows retrieved."

  compare_ecdfs(('hoa',),rows)

def compare_route_portion(rows=None):
  """Compares lateness distributions between portions of the route"""

  if rows is None:
    cur = db.get_cursor()
    print "Selecting..."
    sql = """
select stop_number, total_num_stops, total_num_stops-stop_number as stops_before_end, (100*stop_number::numeric/total_num_stops)::int as route_portion, lateness, trip_stop_weight
from datamining_table dm
  natural join trip_stop_weights
  natural join gps_segments
  inner join (select count(*) as total_num_stops, trip_id
              from gtf_stop_times
              group by trip_id) ns
    on ns.trip_id = dm.gtfs_trip_id
where lateness is not null
"""
    db.SQLExec(cur,sql)
    print "Retrieving..."
    rows = cur.fetchall()
    cur.close()
    print len(rows),'rows fetched.'

  # Plot ECDF comparisons
  stop_num_split = DM.split_on_attributes(('stop_number',),rows)
  end_num_split = DM.split_on_attributes(('stops_before_end',),rows)
  halfway_split = DM.split_on_attributes(('route_portion',),rows)

  cdf_dict = { "Second stop" : stop_num_split[(1,)],
               "Middle stop" : halfway_split[(50,)]+halfway_split[(51,)],
               "Next to last stop" : end_num_split[(1,)] }
  compare_ecdfs("Stop Position",cdf_dict);

  # Plot E vs stop number
  Es = []
  moes = []
  sns = array([k[0] for k in stop_num_split.keys()])
  sns.sort()
  for sn in sns:
    rowdata = array([(r['lateness'],r['trip_stop_weight']) for r in stop_num_split[(sn,)]])
    Eval,moe = E(rowdata,weighted=True)
    Es.append(Eval)
    moes.append(moe)
  Es = array(Es)
  moes = array(moes)

  figure()
  plot(sns,Es,'k-',label="Estimated expectation")
  plot(sns,Es+moes,'k--',label=None)
  plot(sns,Es-moes,'k--',label=None)
  #legend()
  xlabel("Stop Number")
  ylabel("Expected Latenes")
  title("Expected Lateness vs Stop Number")

def critical_mass_compare(diffrows,rows):
  """
  Given the rows returned by criticalmass_compare_rows(),
  compares ECDFs and CI's for mean difference.
  """
  compare_ecdfs(('is_cmass',),rows)
  diffdata = array([(r['ldiff'],r['trip_stop_weight']) for r in diffrows])
  Ediff,moe = E(diffdata,weighted=True)
  return Ediff,moe

def criticalmass_compare_rows():
  """
  Selects and pairs rows from the same trip where one is from
  March 27 and the others are not (this is a many-to-one pairing),
  and subtracts their latenesses as the column 'ldiff'.

  Also selects all latenesses with a column 'is_cmass' indicating
  0 for not-cmass days and 1 for cmass days, for an ecdf comparison
  split.
  """

  print "Selecting..."
  cur = db.get_cursor()
  db.SQLExec(cur,
             """
select (dm_cm.lateness - dm.lateness) as ldiff, trip_stop_weight
from
datamining_table dm_cm natural join trip_stop_weights
inner join gps_segments gs_cm on dm_cm.gps_segment_id=gs_cm.gps_segment_id
  and gs_cm.trip_date = '2009-03-27'
inner join datamining_table dm on dm.gtfs_trip_id=dm_cm.gtfs_trip_id
  and dm.stop_sequence=dm_cm.stop_sequence
inner join gps_segments gs on dm.gps_segment_id=gs.gps_segment_id
  and gs.trip_date != '2009-03-27'
where dm_cm.lateness is not null and dm.lateness is not null
and dm_cm.route_name in ('1','9','19')
             """);
  print "Retrieving..."
  diffrows=cur.fetchall();
  print len(diffrows),"rows retrieved."

  print "Selecting..."
  db.SQLExec(cur,
             """
select lateness, trip_stop_weight,
case when trip_date='2009-03-27' then 1
     else                             0
end as is_cmass
from datamining_table natural join gps_segments
  natural join trip_stop_weights
where lateness is not null and service_id='1'
""");
  print "Retrieving..."
  rows = cur.fetchall();
  print len(rows),'rows retrieved.'
  cur.close();

  return diffrows,rows

def independent_sampling_cdf(rows=None):

  if rows is None:
    cur = db.get_cursor();
    rows = []
    print "Selecting..."
    # For each gps_segment we want to randomly select a sample
    for i in range(58903):
      if (i+1)%(58903/100)==0:
        print " %d/%d"%(i+1,58903)
      if random() < 0.5:
        continue
      db.SQLExec(cur,
"""select lateness,trip_stop_weight
   from datamining_table dm natural join trip_stop_weights tsw
     where gps_segment_id=%(gseg)s and lateness is not null
     order by random() limit 1""",
                 {'gseg':i});
      if cur.rowcount > 0:
        srow = cur.fetchall()[0]; #should be just one
        rows.append(srow)
    cur.close()
    print len(rows),"rows retrieved."

  try:
    data = array([(r['lateness'],r['trip_stop_weight']) for r in rows])
    x,p,a_n = ecdf(data,weighted=True)

    figure()
    plot(x,p,'k',label="Independent")
    plot(x,p+a_n,'k--',label=None)
    plot(x,p-a_n,'k--',label=None)
    xlabel("lateness")
    ylabel("CDF(lateness)")
    title("CDF of Lateness for Independent Samples")
  except e:
    print e

  return rows

# scratch note, to run just one query for all the comparisons
comparison_sql = """
select service_id,date_part('dow',trip_date) as wday,
  scheduled_hour_of_arrival as hoa, stop_number,
  total_num_stops-stop_number as stops_before_end, total_num_stops,
  (100*stop_number::numeric/total_num_stops)::int as route_portion,
  vehicle_type,
  lateness_arrive, lateness_depart,
  trip_stop_weight_arrive, trip_stop_weight_depart
from datamining_table
  natural join trip_stop_weights
  natural join gps_segments
  inner join (select count(*) as total_num_stops, trip_id
              from gtf_stop_times
              group by trip_id) ns
    on ns.trip_id = datamining_table.gtfs_trip_id
where lateness_arrive is not null or lateness_depart is not null
--and stop_number > 0
"""

#### Dynamic Analyses ####

def depict_predinterval_calculation(rows=None,degsep=1,cond=60,alpha=0.05):
  """
  Creates a plot explaining how the prediction interval calculations
  work.
  """

  if rows is None:
    print "Selecting..."
    cur = db.get_cursor()
    db.SQLExec(cur,"""
select d2.lateness, trip_stop_weight
from datamining_table d2
natural join trip_stop_weights
inner join datamining_table d1
  on d1.gps_segment_id = d2.gps_segment_id
  and d2.stop_number-d1.stop_number=%(degsep)s
  and d1.lateness = %(cond)s
  and d2.lateness is not null and d1.lateness is not null
""",  {'degsep':degsep,'cond':cond});

    print "Retrieving..."
    rows = cur.fetchall();
    cur.close()
    print len(rows),"rows retrieved."

  figure()

  rowdata = array([(r['lateness'],r['trip_stop_weight']) for r in rows])
  x,p,a_n = ecdf(rowdata,weighted=True)

  plot(x,p,'k-',label="Conditional ECDF")
  plot(x,p+a_n,'k--',label="ECDF 95% CI")
  plot(x,p-a_n,'k--',label=None)

  (lower,upper),(lolo,upup) = find_pred_interval(x,p,a_n,alpha=alpha)

  plot( (lower,lower),(0,alpha/2), 'r-',label="Lower interval bound")
  plot( (-2000,lower),(alpha/2,alpha/2), 'r-',label=None)

  plot( (upper,upper),(0,1-alpha/2),'g-',label="Upper interval bound")
  plot( (-2000,upper),(1-alpha/2,1-alpha/2),'g-',label=None)

  plot( (lolo,lolo),(0,alpha/2), 'c-',label="Lower bound CI")
  #plot( (-2000,lolo),(alpha/2,alpha/2), 'c-',label=None)

  plot( (upup,upup),(0,1-alpha/2),'m-',label="Upper bound CI")
  #plot( (-2000,upup),(1-alpha/2,1-alpha/2),'m-',label=None)

  legend()
  xlabel("Lateness")
  ylabel("ECDF(Lateness)")
  title("Prediction Interval Calculation")

def conditional_lateness_prediction_intervals(rows=None,
                                              degrees_sep=(1,5,10,20,50),
                                              alpha=0.05):

  if rows is None:
    print "Selecting..."
    cur = db.get_cursor()
    db.SQLExec(cur,"""
select 30*(d1.lateness/30.0)::int as conditional, d2.stop_number-d1.stop_number as sepdegree,
  d2.lateness, trip_stop_weight
from datamining_table d2
natural join trip_stop_weights
inner join datamining_table d1
  on d1.gps_segment_id = d2.gps_segment_id
  and d2.stop_number-d1.stop_number in (""" + \
                 ",".join(map(str,degrees_sep)) + """)
  and d2.lateness is not null and d1.lateness is not null
""")
    print "Retrieving..."
    rows = cur.fetchall()
    cur.close()
    print len(rows),"rows retrieved."

  figure()

  sep_split = DM.split_on_attributes(('sepdegree',),rows)
  sds = array([k[0] for k in sep_split.keys()])
  sds.sort()
  for i,sd in enumerate(reversed(sds)):
    sdrows = sep_split[(sd,)]
    cond_split = DM.split_on_attributes(('conditional',),sdrows)
    conds = array([k[0] for k in cond_split.keys()])
    conds.sort()

    upper_preds = []
    lower_preds = []
    upup_preds = []
    lolo_preds = []
    for cond in conds:
      cond_rows = array([(r['lateness'],r['trip_stop_weight'])
                         for r in cond_split[(cond,)]])
      x,p,a_n = ecdf(cond_rows,weighted=True)
      (lower,upper),(lolo,hihi) = find_pred_interval(x,p,a_n,alpha=alpha)
      upper_preds.append(upper)
      lower_preds.append(lower)

      upup_preds.append(hihi)
      lolo_preds.append(lolo)

    #plot(conds,upper_preds,pcolors[i],label="D.o.S="+str(sd))
    #plot(conds,lower_preds,pcolors[i],label=None)
    plot(conds,upup_preds,pcolors[i]+'+-',label="D.o.S="+str(sd))
    plot(conds,lolo_preds,pcolors[i]+'+-',label=None)

  legend()
  xlabel("Conditional Lateness")
  ylabel("Lateness Prediction Interval")
  title("%d%% Prediction Intervals vs. Stop Separation, Prev Lateness"%(100*(1-alpha),))

def conditional_lateness_gained(rows=None,
                               degrees_sep=1,
                               conds=(-6,0,6,60,120)):

  if rows is None:
    print "Selecting..."
    cur = db.get_cursor()
    db.SQLExec(cur,"""select d1.lateness_gained as cond,d2.lateness_gained,
trip_stop_weight
from datamining_table d1 inner join datamining_table d2
  on d1.gps_segment_id=d2.gps_segment_id
  and d1.stop_number+%(deg_sep)s=d2.stop_number
  and d1.lateness_gained in (""" + ",".join(map(str,conds)) + """)
  and d2.lateness_gained is not null
inner join trip_stop_weights tsw on d2.gtfs_trip_id = tsw.gtfs_trip_id
  and d2.stop_id = tsw.stop_id
""",
               {'deg_sep':degrees_sep});
    print "Retrieving..."
    rows = cur.fetchall()
    cur.close()
    print len(rows),"rows retrieved."

  try:
    compare_ecdfs(('cond',),rows,col_name='lateness_gained',
                  plot_CIs=True,plot_Es=False,plot_E_CIs=False)
  except e:
    print e
  return rows

def conditional_lateness_plots(rows=None,
                               degrees_sep=1,
                               conds=(0,60,300,600,1200)):
  """
  Plots the (weighted) conditional lateness distribution as

    F( lateness at stop | lateness at Dth stop previous )

  where D = degrees_sep. This is plotted as conditioned on each of
  the latenesses provided in conds.
  """

  if rows is None:
    cur = db.get_cursor();
    print "Selecting..."
    db.SQLExec(cur,"""select d1.lateness as cond,d2.lateness,trip_stop_weight
from datamining_table d1 inner join datamining_table d2
  on d1.gps_segment_id=d2.gps_segment_id
  and d1.stop_number+%(deg_sep)s=d2.stop_number
  and d1.lateness in (""" + ",".join(map(str,conds)) + """)
  and d2.lateness is not null
inner join trip_stop_weights tsw on d2.gtfs_trip_id = tsw.gtfs_trip_id
  and d2.stop_id = tsw.stop_id
""",
               {'deg_sep':degrees_sep});
    print "Retrieving..."
    rows = cur.fetchall()
    cur.close()
    print len(rows),"rows retrieved."

  try:
    compare_ecdfs(('cond',),rows)
  except e:
    print e
  return rows

def measure_slowness_correlation(rows=None):
  """
  Measures the correlation of normalized travel time between sequential
  segments.
  """
  if rows is None:
    rows = DM.get_joined_rows(prev_attrs=("meannorm_ssls as prev_stds",),
                              degree_of_sep=1);

  max = rows.rowcount
  prev_stds = zeros(max)
  stds = zeros(max)
  n = 0

  print "running manually..."
  for k,row in enumerate(rows):
    if k%(max/100) == 0:
      print "%5.2f%% (%d/%d) Done (n=%d)..."%(100*float(k)/max,k,max,n)
    prev_stds[n] = row['prev_stds']
    stds[n] = row['meannorm_ssls']
    if row['prev_stds'] is not None and row['meannorm_ssls'] is not None:
      n += 1

  print "Calculating correlation..."
  Rpstds = R.FloatVector(prev_stds[:n])
  Rstds = R.FloatVector(stds[:n])
  corr = R.r['cor'](Rpstds,
                    Rstds)
  rows.close()
  corr_ret = array(corr)

  return corr_ret,prev_stds,stds

res_ewait = {300: ([225.021894,
        226.63841828442645,
        226.05250457054819,
        228.3526068549352,
        227.52869714505852,
        229.08866742950582,
        229.54039971558609,
        228.19271800000001,
        228.41869428441788,
        226.49241257053473,
        226.46926685495501,
        224.07704114499225,
        224.87847942944285,
        225.66833371556882,
        225.25969599999999],
       226.46945829389165),
 600: ([385.140468,
        374.56255285752536,
        365.04768171194866,
        358.9296485692945,
        357.87632143070329,
        360.36018628791663,
        367.98161714262318,
        381.11043000000001,
        397.0368248579847,
        407.10351971087545,
        409.37601456903712,
        406.00087543095492,
        395.48639228884571,
        382.66164714231309,
        370.62472200000002],
       381.24983859999998),
 900: ([443.70171800000003,
        423.08441943103395,
        423.1897228584528,
        439.55437228979952,
        474.85283770952577,
        528.21809914011851,
        585.96208657096315,
        620.23552800000004,
        630.41524742719344,
        614.67872686069791,
        578.78254028975527,
        535.39358370862351,
        488.64125514057406,
        448.75868456886911,
        423.332266],
       521.19431580000003),
 1200: ([466.89103999999998,
         456.435518,
         489.62153000000001,
         566.04258000000004,
         685.530348,
         801.310024,
         863.56086400000004,
         866.14851199999998,
         826.27934200000004,
         765.60573999999997,
         692.92793400000005,
         618.95193800000004,
         543.91690000000006,
         482.04592600000001,
         454.12408799999997],
        660.590912),
 1800: ([494.63929400000001,
         546.15524313991227,
         739.66406228518088,
         1061.2605794173724,
         1313.1383805745056,
         1382.8266737224997,
         1334.7329008517311,
         1240.502502,
         1128.4755371399333,
         1006.760060280792,
         881.22312542032478,
         754.15822057677781,
         632.1414637120331,
         527.2643308598856,
         499.08978200000001],
        945.41581040000005),
 2100: ([508.13229999999999,
         613.33199971145257,
         941.84975741918674,
         1400.4880651504243,
         1630.5615588434405,
         1625.13663856737,
         1531.9599182766408,
         1405.0440880000001,
         1264.3053337216706,
         1116.5285994189642,
         967.93784313748949,
         819.30576686193763,
         673.98771257434987,
         548.73005829090539,
         520.42697799999996],
        1090.6142248000001),
 3600: ([564.25454000000002,
         1235.3034685768512,
         2626.4430711490477,
         3007.0123396931949,
         2891.9512943035838,
         2675.3321028523633,
         2428.9744454425563,
         2176.7411499999998,
         1919.1412825627717,
         1659.0085411571963,
         1398.4152417226016,
         1140.3198062793674,
         884.07210886220957,
         644.73765742666831,
         619.26800400000002],
        1825.0092325999999)};


text:
class SprintDoneWork < ApplicationRecord
	belongs_to :sprint

  validates :committed_work, presence: true
end

redpajama_set_name: RedPajamaGithub


text:
/*!
 * Module dependencies.
 */

var util = require('util'),
	utils = require('keystone-utils'),
	super_ = require('../Type');

/**
 * Table FieldType Constructor
 * @extends {Field}
 * @api public
 */

function table(list, path, options) {
	this._nativeType = (options.schema)? [options.schema] : [];
        this._properties = ['columns'];
        this.columns = options.columns || []; //@todo implement validation etc.
        table.super_.call(this, list, path, options);
}

/*!
 * Inherit from Field
 */

util.inherits(table, super_);

/**
 * Updates the value for this field in the item from a data object
 * @param {Document} item
 * @param {Hash} data
 * @api public
 */

table.prototype.updateItem = function(item, data) {
	if ( data[this.path] === undefined ) {
		item.set(this.path, []);
	} else {
		item.set(this.path, data[this.path]);
	}
};

module.exports = table;


text:
export default class Opponent {

}


text:
var data = require('../dailies.json');

exports.view = function(req, res){
	console.log(data);
	res.render('dailies2', data);

};


text:
from suds.client import Client
import logging

logging.basicConfig(level=logging.INFO)
if __debug__:
    logging.getLogger('suds.client').setLevel(logging.DEBUG)
else:
    logging.getLogger('suds.client').setLevel(logging.CRITICAL)

URL = 'http://127.0.0.1:8080/helloworldws/bar?WSDL'
client = Client(URL)
result = client.service.getList()
assert(result == ["Table 1", "Table 2", "Table 3"])

result = client.service.getEcho("1234")
assert(result == "1234")


text:
"use strict";

var fs = require('fs');

var bbxml = require('../index');

var xmlUtil = bbxml.xmlUtil;
var component = bbxml.component;
var processor = bbxml.processor;

describe('readme', function () {
  var xmlfile = fs.readFileSync(__dirname + '/fixtures/file_readme.xml', 'utf-8');

  it('direct', function () {
    var doc = bbxml.xmlUtil.parse(xmlfile);
    var nodes = bbxml.xmlUtil.xpath(doc, "//element[@ready='true']/text()");
    var value = bbxml.processor.asString(nodes[0]);
    expect(value).toBe("82");
  });

  it('infrastructure', function () {
    var element = component.define("element");
    element.fields([
      ["value", "1..1", "text()", processor.asFloat],
      ["flag", "0..1", "@ready", processor.asBoolean]
    ]);

    var compA = component.define("compA");
    compA.fields([
      ["name", "0..1", "@name", processor.asString],
      ["element", "1..*", "element", element]
    ]);

    var root = component.define("root");
    root.fields([
      ["data", "1:1", "//document/root", compA]
    ]);

    var instance = root.run(xmlfile);
    var r = instance.toJSON();
    var e = {
      "data": {
        "name": "example",
        "element": [{
          "value": 82,
          "flag": true
        }, {
          "value": 16,
          "flag": false
        }]
      }
    };
    expect(r).toEqual(e);

    element.cleanupStep(function () {
      if (this.js && this.js.flag && this.js.value) {
        this.js.value = this.js.value + 10
      }
    });

    var instance2 = root.run(xmlfile);
    var r2 = instance2.toJSON();
    e.data.element[0].value = 92;
    expect(r2).toEqual(e);
  });
});


text:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{% block title %}Django-Phonegap{% endblock %}</title>
    <script src="{{ STATIC_URL }}phonegap/css/source-sans-pro.js"></script>
    <link href="{{ STATIC_URL }}phonegap/css/styles.css" rel="stylesheet" media="screen">
    <link href="{{ STATIC_URL }}phonegap/css/styles_small.css" rel="stylesheet" media="screen and (max-width: 480px)">

    {% block css %}{% endblock %}
</head>

<body>
    <div id="container">
        {% block content %}{% endblock %}
    </div>

    {% block js %}{% endblock %}
</body>
</html>


text:
<?php

namespace IngaLabs\Bundle\ImageBundle\Repository;

use Doctrine\Common\Persistence\ObjectRepository;
use IngaLabs\Bundle\ImageBundle\Model\Image;

/**
 * ImageRepositoryInterface.
 *
 * @author Antal Áron <antalaron@antalaron.hu>
 */
interface ImageRepositoryInterface extends ObjectRepository
{
    /**
     * Find one by hash.
     *
     * @param string $hash
     *
     * @return null|Image
     */
    public function findOneByHash($hash);
}


text:
#include "Precompiled.h"

#define XM_EMULATOR_CX  720
#define XM_EMULATOR_CY  1280

#include "platform.h"
#include "Manager/XMGlobalManager.h"
#include "Node/Scene/XMCompanyLogo.h"

COCOS2D_ENGINE_LAUNCH ( XM_SYS_LANDSCAPE );

KDbool AppDelegate::applicationDidFinishLaunching ( KDvoid )
{
	auto director = Director::getInstance ( );
	director->setOpenGLView ( EGLView::getInstance ( ) );

//	director->setDisplayStats (  );
	director->setAnimationInterval ( 1.0 / 60 );

	auto screenSize = EGLView::getInstance ( )->getFrameSize ( );

	auto designSize = Size ( 1280, 720 );

	auto pFileUtils = FileUtils::getInstance ( );
	std::vector<std::string> searchPaths;

	searchPaths.push_back ( "Image/Background" );
	searchPaths.push_back ( "Image/Ui/Stage" );
	searchPaths.push_back ( "Image/Ui/Main" );
	searchPaths.push_back ( "Image/DPad" );
	searchPaths.push_back ( "Image/Character/Player" );
	searchPaths.push_back ( "Image/Character/Enemy" );
	searchPaths.push_back ( "Image/Bullet/Player" );
	searchPaths.push_back ( "Image/Bullet/Enemy" );
	searchPaths.push_back ( "Image/Effect" );
	searchPaths.push_back ( "Font" );

	pFileUtils->setSearchPaths ( searchPaths );

	EGLView::getInstance ( )->setDesignResolutionSize ( designSize.width, designSize.height, ResolutionPolicy::NO_BORDER );

	GLOBAL->create ( );

	Director::getInstance ( )->runWithScene ( XMCompanyLogo::create ( ) );

    return true;
}

KDvoid AppDelegate::applicationWillTerminate ( KDvoid )
{

}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
KDvoid AppDelegate::applicationDidEnterBackground ( KDvoid )
{

}

// this function will be called when the app is active again
KDvoid AppDelegate::applicationWillEnterForeground ( KDvoid )
{

}


text:
#include "MODSERIAL.h"
#include "MACROS.h"

namespace AjK {

int
MODSERIAL::resizeBuffer(int size, IrqType type, bool memory_check)
{
    int rval = Ok;

    // If the requested size is the same as the current size there's nothing to do,
    // just continue to use the same buffer as it's fine as it is.
    if (buffer_size[type] == size) return rval;

    // Make sure the ISR cannot use the buffers while we are manipulating them.
    disableIrq();

    // If the requested buffer size is larger than the current size,
    // attempt to create a new buffer and use it.
    if (buffer_size[type] < size) {
        rval = upSizeBuffer(size, type, memory_check);
    }
    else if (buffer_size[type] > size) {
        rval = downSizeBuffer(size, type, memory_check);
    }

    // Start the ISR system again with the new buffers.
    enableIrq();

    return rval;
}

int
MODSERIAL::downSizeBuffer(int size, IrqType type, bool memory_check)
{
    if (size >= buffer_count[type]) {
        return BufferOversize;
    }

    char *s = (char *)malloc(size);

    if (s == (char *)NULL) {
        if (memory_check) {
            error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
        }
        return NoMemory;
    }

    int c, new_in = 0;

    do {
        c = __getc(false);
        if (c != -1) s[new_in++] = (char)c;
        if (new_in >= size) new_in = 0;
    }
    while (c != -1);

    free((char *)buffer[type]);
    buffer[type]      = s;
    buffer_in[type]   = new_in;
    buffer_out[type]  = 0;
    return Ok;
}

int
MODSERIAL::upSizeBuffer(int size, IrqType type, bool memory_check)
{
    char *s = (char *)malloc(size);

    if (s == (char *)NULL) {
        if (memory_check) {
            error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
        }
        return NoMemory;
    }

    if (buffer_count[type] == 0) { // Current buffer empty?
        free((char *)buffer[type]);
        buffer[type]      = s;
        buffer_in[type]   = 0;
        buffer_out[type]  = 0;
    }
    else { // Copy the current contents into the new buffer.
        int c, new_in = 0;
        do {
            c = __getc(false);
            if (c != -1) s[new_in++] = (char)c;
            if (new_in >= size) new_in = 0; // Shouldn't happen, but be sure.
        }
        while (c != -1);
        free((char *)buffer[type]);
        buffer[type]      = s;
        buffer_in[type]   = new_in;
        buffer_out[type]  = 0;
    }

    buffer_size[type] = size;
    return Ok;
}

}; // namespace AjK ends


text:
<?php

namespace Drupal\Tests\views_ui\Functional;

/**
 * Tests the boolean filter UI.
 *
 * @group views_ui
 * @see \Drupal\views\Plugin\views\filter\BooleanOperator
 */
class FilterBooleanWebTest extends UITestBase {

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = ['test_view'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests the filter boolean UI.
   */
  public function testFilterBooleanUI() {
    $this->drupalGet('admin/structure/views/nojs/add-handler/test_view/default/filter');
    $this->submitForm(['name[views_test_data.status]' => TRUE], 'Add and configure filter criteria');

    // Check the field widget label. 'title' should be used as a fallback.
    $result = $this->cssSelect('#edit-options-value--wrapper legend span');
    $this->assertEquals('Status', $result[0]->getHtml());

    // Ensure that the operator and the filter value are displayed using correct
    // layout.
    $this->assertSession()->elementExists('css', '.views-left-30 .form-item-options-operator');
    $this->assertSession()->elementExists('css', '.views-right-70 .form-item-options-value');

    $this->submitForm([], 'Expose filter');
    $this->submitForm([], 'Grouped filters');

    $edit = [];
    $edit['options[group_info][group_items][1][title]'] = 'Published';
    $edit['options[group_info][group_items][1][operator]'] = '=';
    $edit['options[group_info][group_items][1][value]'] = 1;
    $edit['options[group_info][group_items][2][title]'] = 'Not published';
    $edit['options[group_info][group_items][2][operator]'] = '=';
    $edit['options[group_info][group_items][2][value]'] = 0;
    $edit['options[group_info][group_items][3][title]'] = 'Not published2';
    $edit['options[group_info][group_items][3][operator]'] = '!=';
    $edit['options[group_info][group_items][3][value]'] = 1;

    $this->submitForm($edit, 'Apply');

    $this->drupalGet('admin/structure/views/nojs/handler/test_view/default/filter/status');

    $result = $this->xpath('//input[@name="options[group_info][group_items][1][value]"]');
    $this->assertEquals('checked', $result[1]->getAttribute('checked'));
    $result = $this->xpath('//input[@name="options[group_info][group_items][2][value]"]');
    $this->assertEquals('checked', $result[2]->getAttribute('checked'));
    $result = $this->xpath('//input[@name="options[group_info][group_items][3][value]"]');
    $this->assertEquals('checked', $result[1]->getAttribute('checked'));

    // Test that there is a remove link for each group.
    $this->assertCount(3, $this->cssSelect('a.views-remove-link'));

    // Test selecting a default and removing an item.
    $edit = [];
    $edit['options[group_info][default_group]'] = 2;
    $edit['options[group_info][group_items][3][remove]'] = 1;
    $this->submitForm($edit, 'Apply');
    $this->drupalGet('admin/structure/views/nojs/handler/test_view/default/filter/status');
    $this->assertSession()->fieldValueEquals('options[group_info][default_group]', 2);
    $this->assertSession()->fieldNotExists('options[group_info][group_items][3][remove]');
  }

}


text:
class AddColorsToWallpapers < ActiveRecord::Migration
  def change
    add_column :wallpapers, :colors, :text
  end
end


text:
#ifndef _MIMETYPE_IMPL_H
#define _MIMETYPE_IMPL_H

#include "wx/mimetype.h"

#if wxUSE_MIMETYPE

class wxMimeTypeCommands;

WX_DEFINE_ARRAY_PTR(wxMimeTypeCommands *, wxMimeCommandsArray);

// this is the real wxMimeTypesManager for Unix
class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl
{
public:
    // ctor and dtor
    wxMimeTypesManagerImpl();
    virtual ~wxMimeTypesManagerImpl();

    // load all data into memory - done when it is needed for the first time
    void Initialize(int mailcapStyles = wxMAILCAP_ALL,
                    const wxString& extraDir = wxEmptyString);

    // and delete the data here
    void ClearData();

    // implement containing class functions
    wxFileType *GetFileTypeFromExtension(const wxString& ext);
    wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);

    size_t EnumAllFileTypes(wxArrayString& mimetypes);

    void AddFallback(const wxFileTypeInfo& filetype);

    // add information about the given mimetype
    void AddMimeTypeInfo(const wxString& mimetype,
                         const wxString& extensions,
                         const wxString& description);
    void AddMailcapInfo(const wxString& strType,
                        const wxString& strOpenCmd,
                        const wxString& strPrintCmd,
                        const wxString& strTest,
                        const wxString& strDesc);

    // add a new record to the user .mailcap/.mime.types files
    wxFileType *Associate(const wxFileTypeInfo& ftInfo);
    // remove association
    bool Unassociate(wxFileType *ft);

    // accessors
        // get the string containing space separated extensions for the given
        // file type
    wxString GetExtension(size_t index) { return m_aExtensions[index]; }

protected:
    void InitIfNeeded();

    wxArrayString m_aTypes,         // MIME types
                  m_aDescriptions,  // descriptions (just some text)
                  m_aExtensions,    // space separated list of extensions
                  m_aIcons;         // Icon filenames

    // verb=command pairs for this file type
    wxMimeCommandsArray m_aEntries;

    // are we initialized?
    bool m_initialized;

    wxString GetCommand(const wxString &verb, size_t nIndex) const;

    // Read XDG *.desktop file
    void LoadXDGApp(const wxString& filename);
    // Scan XDG directory
    void LoadXDGAppsFilesFromDir(const wxString& dirname);

    // Load XDG globs files
    void LoadXDGGlobs(const wxString& filename);

    // functions used to do associations
    virtual int AddToMimeData(const wxString& strType,
                      const wxString& strIcon,
                      wxMimeTypeCommands *entry,
                      const wxArrayString& strExtensions,
                      const wxString& strDesc,
                      bool replaceExisting = true);
    virtual bool DoAssociation(const wxString& strType,
                       const wxString& strIcon,
                       wxMimeTypeCommands *entry,
                       const wxArrayString& strExtensions,
                       const wxString& strDesc);

    // give it access to m_aXXX variables
    friend class WXDLLIMPEXP_FWD_BASE wxFileTypeImpl;
};

class WXDLLIMPEXP_BASE wxFileTypeImpl
{
public:
    // initialization functions
    // this is used to construct a list of mimetypes which match;
    // if built with GetFileTypeFromMimetype index 0 has the exact match and
    // index 1 the type / * match
    // if built with GetFileTypeFromExtension, index 0 has the mimetype for
    // the first extension found, index 1 for the second and so on

    void Init(wxMimeTypesManagerImpl *manager, size_t index)
        { m_manager = manager; m_index.Add(index); }

    // accessors
    bool GetExtensions(wxArrayString& extensions);
    bool GetMimeType(wxString *mimeType) const
        { *mimeType = m_manager->m_aTypes[m_index[0]]; return true; }
    bool GetMimeTypes(wxArrayString& mimeTypes) const;
    bool GetIcon(wxIconLocation *iconLoc) const;

    bool GetDescription(wxString *desc) const
        { *desc = m_manager->m_aDescriptions[m_index[0]]; return true; }

    bool GetOpenCommand(wxString *openCmd,
                        const wxFileType::MessageParameters& params) const
    {
        *openCmd = GetExpandedCommand(wxT("open"), params);
        return (! openCmd -> IsEmpty() );
    }

    bool GetPrintCommand(wxString *printCmd,
                         const wxFileType::MessageParameters& params) const
    {
        *printCmd = GetExpandedCommand(wxT("print"), params);
        return (! printCmd -> IsEmpty() );
    }

        // return the number of commands defined for this file type, 0 if none
    size_t GetAllCommands(wxArrayString *verbs, wxArrayString *commands,
                          const wxFileType::MessageParameters& params) const;

    // remove the record for this file type
    // probably a mistake to come here, use wxMimeTypesManager.Unassociate (ft) instead
    bool Unassociate(wxFileType *ft)
    {
        return m_manager->Unassociate(ft);
    }

    // set an arbitrary command, ask confirmation if it already exists and
    // overwriteprompt is TRUE
    bool SetCommand(const wxString& cmd, const wxString& verb, bool overwriteprompt = true);
    bool SetDefaultIcon(const wxString& strIcon = wxEmptyString, int index = 0);

private:
    wxString
    GetExpandedCommand(const wxString & verb,
                       const wxFileType::MessageParameters& params) const;

    wxMimeTypesManagerImpl *m_manager;
    wxArrayInt              m_index; // in the wxMimeTypesManagerImpl arrays
};

#endif // wxUSE_MIMETYPE

#endif // _MIMETYPE_IMPL_H


text:
<?php

namespace App\File;

use RegexIterator;
//use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
 * @class PatternIterator
 * @see RegexIterator
 *
 * @package App\File
 * @version $Id$
 * @author iwyg <mail@thomas-appel.com>
 */
class PatternIterator extends RegexIterator
{
    /**
     * Constructor.
     *
     * @param string $path
     * @param string $pattern
     * @param int    $depth
     * @param int    $limit
     * @param int    $flags
     * @param int    $dirFlags
     */
    public function __construct($path, $pattern, $depth = null, $limit = -1, $flags = self::MATCH, $dirFlags = null)
    {
        $this->limit = $limit;
        parent::__construct($this->createInnerIterator($path, $depth, $dirFlags), $pattern, $flags);
    }

    /**
     * setMaxDepth
     *
     * @param int $depth
     *
     * @return void
     */
    public function setMaxDepth($depth = -1)
    {
        $this->getInnerIterator->setMaxDept((int)$depth);
    }

    /**
     * getMaxDepth
     *
     * @return int
     */
    public function getMaxDepth()
    {
        return $this->getInnerIterator->getMaxDept();
    }

    /**
     * {@inheritdoc}
     */
    public function valid()
    {
        if ($this->limit > -1 && $this->max === $this->limit) {
            return false;
        }

        return parent::valid();
    }

    /**
     * {@inheritdoc}
     */
    public function next()
    {
        $this->max++;

        return parent::next();
    }

    /**
     * {@inheritdoc}
     */
    public function rewind()
    {
        $this->max = 0;
        parent::rewind();
    }

    /**
     * getRecursiveIterator
     *
     * @param string $path
     * @param int    $depth
     * @param int    $flags
     *
     * @return RecursiveIteratorIterator
     */
    private function createInnerIterator($path, $depth = null, $flags = null)
    {
        $rcit = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path, $flags)
        );

        if (0 > (int)$depth) {
            $rcit->setMaxDepth($depth);
        }

        return $rcit;
    }
}


text:
# SymDep

Often I execute a lot of  similar commands for update my local project, thats why this project was created.

[![Latest Stable Version](https://poser.pugx.org/therat/symdep/v/stable.svg)](https://packagist.org/packages/therat/symdep)
[![Total Downloads](https://poser.pugx.org/therat/symdep/downloads.svg)](https://packagist.org/packages/therat/symdep)
[![Latest Unstable Version](https://poser.pugx.org/therat/symdep/v/unstable.svg)](https://packagist.org/packages/therat/symdep)
[![License](https://poser.pugx.org/therat/symdep/license.svg)](https://packagist.org/packages/therat/symdep)

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/38683099-7e9e-4323-8b41-b0be255e7dc9/big.png)](https://insight.sensiolabs.com/projects/38683099-7e9e-4323-8b41-b0be255e7dc9)

## Requirements

Any **symfony** project.

## Install

```bash
composer require therat/symdep ~3.0
```

Create deploy.php file into your project

```bash
cp vendor/therat/symdep/deploy.php.example deploy.php
```

Add file `symdep.lock` to your `.gitignore`

## Extend tasks

### Modify properties

Example

```
/**
 * modify properties
 */
task(
    'env',
    function () {
        switch (get('build_type')) {
            case BuildType::TYPE_DEV:
                break;
            case BuildType::TYPE_TEST:
                break;
            case BuildType::TYPE_PROD:
                break;
        }
    }
);
after('properties', 'env');
```

### Delete useless branch folder from test

```bash
./bin/project-drop-branches-from-test
```

## Build new version of geggs

* Create and push tag
* Create phar
```
./bin/box-build
```
* Go to github and upload new `symdep.phar` into new release
* Publish new manifest
```
./bin/generate-manifest
```


text:
<?php
namespace inklabs\kommerce\ActionHandler\CartPriceRule;

use inklabs\kommerce\Action\CartPriceRule\DeleteCartPriceRuleItemCommand;
use inklabs\kommerce\Entity\AbstractCartPriceRuleItem;
use inklabs\kommerce\Entity\CartPriceRule;
use inklabs\kommerce\Entity\Product;
use inklabs\kommerce\Exception\EntityNotFoundException;
use inklabs\kommerce\tests\Helper\TestCase\ActionTestCase;

class DeleteCartPriceRuleItemHandlerTest extends ActionTestCase
{
    protected $metaDataClassNames = [
        CartPriceRule::class,
        AbstractCartPriceRuleItem::class,
        Product::class,
    ];

    public function testHandle()
    {
        $cartPriceRule = $this->dummyData->getCartPriceRule();
        $product = $this->dummyData->getProduct();
        $cartPriceRuleItem = $this->dummyData->getCartPriceRuleProductItem($product, 1);
        $cartPriceRuleItem->setCartPriceRule($cartPriceRule);
        $this->persistEntityAndFlushClear([
            $cartPriceRule,
            $product,
            $cartPriceRuleItem,
        ]);
        $command = new DeleteCartPriceRuleItemCommand(
            $cartPriceRuleItem->getId()->getHex()
        );

        $this->dispatchCommand($command);

        $this->expectException(EntityNotFoundException::class);
        $this->getRepositoryFactory()->getCartPriceRuleItemRepository()->findOneById(
            $command->getCartPriceRuleItemId()
        );
    }
}


text:
#include "ArExport.h"
#include "ariaOSDef.h"
#include "ArSocket.h"
#include "ArLog.h"
#include <errno.h>
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "ArFunctor.h"
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

/// We're always initialized in Linux
bool ArSocket::ourInitialized=true;

/**
   In Windows, the networking subsystem needs to be initialized and shutdown
   individyaly by each program. So when a program starts they will need to
   call the static function ArSocket::init() and call ArSocket::shutdown()
   when it exits. For programs that use Aria::init() and Aria::uninit()
   calling the ArSocket::init() and ArSocket::shutdown() is unnecessary. The
   Aria initialization functions take care of this. These functions do nothing
   in Linux.
*/
bool ArSocket::init()
{
  return(true);
}

/**
   In Windows, the networking subsystem needs to be initialized and shutdown
   individyaly by each program. So when a program starts they will need to
   call the static function ArSocket::init() and call ArSocket::shutdown()
   when it exits. For programs that use Aria::init() and Aria::uninit()
   calling the ArSocket::init() and ArSocket::shutdown() is unnecessary. The
   Aria initialization functions take care of this. These functions do nothing
   in Linux.
*/
void ArSocket::shutdown()
{
}

ArSocket::ArSocket() :
  myType(Unknown),
  myError(NoErr),
  myErrorStr(),
  myDoClose(true),
  myFD(-1),
  myNonBlocking(false),
  mySin()
{
  internalInit();
}

/**
   Constructs the socket and connects it to the given host.
   @param host hostname of the server to connect to
   @param port port number of the server to connect to
   @param type protocol type to use
*/
ArSocket::ArSocket(const char *host, int port, Type type) :
  myType(type),
  myError(NoErr),
  myErrorStr(),
  myDoClose(true),
  myFD(-1),
  myNonBlocking(false),
  mySin()
{
  internalInit();
  connect(host, port, type);
}

/**
   Constructs the socket and opens it as a server port.
   @param port port number to bind the socket to
   @param doClose automaticaly close the port if the socket is destructed
   @param type protocol type to use
*/
ArSocket::ArSocket(int port, bool doClose, Type type) :
  myType(type),
  myError(NoErr),
  myErrorStr(),
  myDoClose(doClose),
  myFD(-1),
  myNonBlocking(false),
  mySin()
{
  internalInit();
  open(port, type);
}

ArSocket::~ArSocket()
{
  close();
}

bool ArSocket::hostAddr(const char *host, struct in_addr &addr)
{
  struct hostent *hp;
  if (!(hp=gethostbyname(host)))
  {
    perror("gethostbyname");
    memset(&addr, 0, sizeof(in_addr));
    return(false);
  }
  else
  {
    bcopy(hp->h_addr, &addr, hp->h_length);
    return(true);
  }
}

bool ArSocket::addrHost(struct in_addr &addr, char *host)
{
  struct hostent *hp;

  hp=gethostbyaddr((char*)&addr.s_addr, sizeof(addr.s_addr), AF_INET);
  if (hp)
    strcpy(host, hp->h_name);
  else
    strcpy(host, inet_ntoa(addr));

  return(true);
}

std::string ArSocket::getHostName()
{
  char localhost[100];  // maxHostNameLen()];

  if (gethostname(localhost, sizeof(localhost)) == 1)
    return("");
  else
    return(localhost);
}

bool ArSocket::connect(const char *host, int port, Type type)
{
  char localhost[100];  // maxHostNameLen()];

  if (!host)
  {
    if (gethostname(localhost, sizeof(localhost)) == 1)
    {
      myError=ConBadHost;
      myErrorStr="Failure to locate host '";
      myErrorStr+=localhost;
      myErrorStr+="'";
      perror("gethostname");
      return(false);
    }
    host=localhost;
  }

  bzero(&mySin, sizeof(mySin));
  // MPL taking out this next code line from the if since it makes
  // everything we can't resolve try to connect to localhost
  // &&  !hostAddr("localhost", mySin.sin_addr))
  if (!hostAddr(host, mySin.sin_addr))
    return(false);
  setIPString();
  mySin.sin_family=AF_INET;
  mySin.sin_port=hostToNetOrder(port);

  if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
  {
    myError=NetFail;
    myErrorStr="Failure to make TCP socket";
    perror("socket");
    return(false);
  }
  else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
  {
    myError=NetFail;
    myErrorStr="Failure to make UDP socket";
    perror("socket");
    return(false);
  }

  myType=type;

  if (::connect(myFD, (struct sockaddr *)&mySin,
		sizeof(struct sockaddr_in)) < 0)
  {
    myErrorStr="Failure to connect socket";
    switch (errno)
    {
    case ECONNREFUSED:
      myError=ConRefused;
      myErrorStr+="; Connection refused";
      break;
    case ENETUNREACH:
      myError=ConNoRoute;
      myErrorStr+="; No route to host";
      break;
    default:
      myError=NetFail;
      break;
    }
    //perror("connect");
    ::close(myFD);
    myFD = -1;
    return(false);
  }

  return(true);
}

bool ArSocket::open(int port, Type type, const char *openOnIP)
{
  int ret;
  char localhost[100];  // maxHostNameLen()];

  if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
  {
    myErrorStr="Failure to make TCP socket";
    perror("socket");
    return(false);
  }
  else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
  {
    myErrorStr="Failure to make UDP socket";
    perror("socket");
    return(false);
  }

  myType=type;

  /* MPL removed this since with what I Took out down below months ago
  if (gethostname(localhost, sizeof(localhost)) == 1)
  {
    myErrorStr="Failure to locate localhost";
    perror("gethostname");
    return(false);
  }
  */
  bzero(&mySin, sizeof(mySin));
  /* MPL took this out since it was just overriding it with the
     INADDR_ANY anyways and it could cause slowdowns if a machine wasn't
     configured so lookups are quick
  if (!hostAddr(localhost, mySin.sin_addr) &&
      !hostAddr("localhost", mySin.sin_addr))
    return(false); */

  if (openOnIP != NULL)
  {

    if (!hostAddr(openOnIP, mySin.sin_addr))
    {
      ArLog::log(ArLog::Normal, "Couldn't find ip of %s to open on", openOnIP);
      return(false);
    }
    else
    {
      //printf("Opening on %s\n", openOnIP);
    }
  }
  else
  {
    mySin.sin_addr.s_addr=htonl(INADDR_ANY);
  }

  setIPString();
  mySin.sin_family=AF_INET;
  mySin.sin_port=hostToNetOrder(port);

  if ((ret=bind(myFD, (struct sockaddr *)&mySin, sizeof(mySin))) < 0)
  {
    myErrorStr="Failure to bind socket to port ";
    sprintf(localhost, "%d", port);
    myErrorStr+=localhost;
    perror("socket");
    return(false);
  }

  if ((type == TCP) && (listen(myFD, 5) < 0))
  {
    myErrorStr="Failure to listen on socket";
    perror("listen");
    return(false);
  }

  return(true);
}

bool ArSocket::create(Type type)
{
  if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
  {
    myErrorStr="Failure to make TCP socket";
    perror("socket");
    return(false);
  }
  else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
  {
    myErrorStr="Failure to make UDP socket";
    perror("socket");
    return(false);
  }

  myType=type;

  if (getSockName())
    return(true);
  else
    return(false);
}

bool ArSocket::findValidPort(int startPort, const char *openOnIP)
{
//  char localhost[100];  // maxHostNameLen()];

  /*
  if (gethostname(localhost, sizeof(localhost)) == 1)
  {
    myErrorStr="Failure to locate localhost";
    perror("gethostname");
    return(false);
  }
  */

  for (int i=0; i+startPort < 65000; ++i)
  {
    bzero(&mySin, sizeof(mySin));
    /*
    if (!hostAddr(localhost, mySin.sin_addr) &&
	!hostAddr("localhost", mySin.sin_addr))
      return(false);
    */
    setIPString();

    if (openOnIP != NULL)
    {

      if (!hostAddr(openOnIP, mySin.sin_addr))
      {
	ArLog::log(ArLog::Normal, "Couldn't find ip of %s to open udp on", openOnIP);
	return(false);
      }
      else
      {
	//printf("Opening on %s\n", openOnIP);
      }
    }
    else
    {
      mySin.sin_addr.s_addr=htonl(INADDR_ANY);
    }

    mySin.sin_family=AF_INET;
    mySin.sin_port=hostToNetOrder(startPort+i);

    if (bind(myFD, (struct sockaddr *)&mySin, sizeof(mySin)) == 0)
      break;
  }

  return(true);
}

bool ArSocket::connectTo(const char *host, int port)
{
  char localhost[100];  // maxHostNameLen()];

  if (myFD < 0)
    return(false);

  if (!host)
  {
    if (gethostname(localhost, sizeof(localhost)) == 1)
    {
      myErrorStr="Failure to locate host '";
      myErrorStr+=localhost;
      myErrorStr+="'";
      perror("gethostname");
      return(false);
    }
    host=localhost;
  }

  bzero(&mySin, sizeof(mySin));
  if (!hostAddr(host, mySin.sin_addr))
    return(false);
  setIPString();
  mySin.sin_family=AF_INET;
  mySin.sin_port=hostToNetOrder(port);

  return(connectTo(&mySin));
}

bool ArSocket::connectTo(struct sockaddr_in *sin)
{
  if (::connect(myFD, (struct sockaddr *)sin,
		sizeof(struct sockaddr_in)) < 0)
  {
    myErrorStr="Failure to connect socket";
    perror("connect");
    return(0);
  }

  return(1);
}

bool ArSocket::close()
{
  if (myFD != -1)
    ArLog::log(ArLog::Verbose, "Closing socket");
  if (myCloseFunctor != NULL)
    myCloseFunctor->invoke();
  if (myDoClose && ::close(myFD))
  {
    myFD=-1;
    return(false);
  }
  else
  {
    myFD=-1;
    return(true);
  }
}

bool ArSocket::setLinger(int time)
{
  struct linger lin;

  if (time)
  {
    lin.l_onoff=1;
    lin.l_linger=time;
  }
  else
  {
    lin.l_onoff=0;
    lin.l_linger=time;
  }

  if (setsockopt(myFD, SOL_SOCKET, SO_LINGER, &lin, sizeof(lin)) != 0)
  {
    myErrorStr="Failure to setsockopt LINGER";
    perror("setsockopt");
    return(false);
  }
  else
    return(true);
}

bool ArSocket::setBroadcast()
{
  if (setsockopt(myFD, SOL_SOCKET, SO_BROADCAST, NULL, 0) != 0)
  {
    myErrorStr="Failure to setsockopt BROADCAST";
    perror("setsockopt");
    return(false);
  }
  else
    return(true);
}

bool ArSocket::setReuseAddress()
{
  int opt=1;

  if (setsockopt(myFD, SOL_SOCKET, SO_REUSEADDR,
		 (char*)&opt, sizeof(opt)) != 0)
  {
    myErrorStr="Failure to setsockopt REUSEADDR";
    perror("setsockopt");
    return(false);
  }
  else
    return(true);
}

bool ArSocket::setNonBlock()
{
  if (fcntl(myFD, F_SETFL, O_NONBLOCK) != 0)
  {
    myErrorStr="Failure to fcntl O_NONBLOCK";
    perror("fcntl");
    return(false);
  }
  else
  {
    myNonBlocking = true;
    return(true);
  }
}

/**
   Copy socket structures. Copy from one Socket to another will still have
   the first socket close the file descripter when it is destructed.
*/
bool ArSocket::copy(int fd, bool doclose)
{
  socklen_t len;

  myFD=fd;
  myDoClose=doclose;
  myType=Unknown;

  len=sizeof(struct sockaddr_in);
  if (getsockname(myFD, (struct sockaddr*)&mySin, &len))
  {
    myErrorStr="Failed to getsockname on fd ";
    perror("setsockopt");
    return(false);
  }
  else
    return(true);
}

/**
   @return true if there are no errors, false if there are
   errors... not that if you're in non-blocking mode and there is no
   socket to connect that is NOT an error, you'll want to check the
   getFD on the sock you pass in to see if it is actually a valid
   socket.
 **/
bool ArSocket::accept(ArSocket *sock)
{
  socklen_t len;
  unsigned char *bytes;

  len=sizeof(struct sockaddr_in);
  sock->myFD=::accept(myFD, (struct sockaddr*)&(sock->mySin), &len);
  sock->myType=myType;
  bytes = (unsigned char *)sock->inAddr();
  sprintf(sock->myIPString, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2],
	  bytes[3]);
  if ((sock->myFD < 0 && !myNonBlocking) ||
      (sock->myFD < 0 && errno != EWOULDBLOCK && myNonBlocking))
  {
    myErrorStr="Failed to accept on socket";
    perror("accept");
    return(false);
  }

  return(true);
}

void ArSocket::inToA(struct in_addr *addr, char *buff)
{
  strcpy(buff, inet_ntoa(*addr));
}

bool ArSocket::getSockName()
{
  socklen_t size;

  if (myFD < 0)
  {
    myErrorStr="Trying to get socket name on an unopened socket";
    printf("%s",myErrorStr.c_str());
    return(false);
  }

  size=sizeof(mySin);
  if (getsockname(myFD, (struct sockaddr *)&mySin, &size) != 0)
  {
    myErrorStr="Error getting socket name";
    perror(myErrorStr.c_str());
    return(false);
  }

  return(true);
}

unsigned int ArSocket::hostToNetOrder(int i)
{
  return(htons(i));
}

unsigned int ArSocket::netToHostOrder(int i)
{
  return(ntohs(i));
}

/** If this socket is a TCP socket, then set the TCP_NODELAY flag,
 *  to disable the use of the Nagle algorithm (which waits until enough
 *  data is ready to send to fill a TCP frame, rather then sending the
 *  packet immediately).
 *  @param flag true to turn on NoDelay, false to turn it off.
 *  @return true of the flag was successfully set, false if there was an
 *    error or this socket is not a TCP socket.
 */
bool ArSocket::setNoDelay(bool flag)
{
  if(myType != TCP) return false;
  int f = flag?1:0;
  int r = setsockopt(myFD, IPPROTO_TCP, TCP_NODELAY, (char*)&f, sizeof(f));
  return (r != -1);
}


text:
'use strict';

// Resultsannounces controller
angular.module('resultsannounces').controller('ResultsannouncesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Resultsannounces',
	function($scope, $stateParams, $location, Authentication, Resultsannounces) {
		$scope.authentication = Authentication;

		// Create new Resultsannounce
		$scope.create = function() {
			// Create new Resultsannounce object
			var resultsannounce = new Resultsannounces ({
				name: this.name
			});

			// Redirect after save
			resultsannounce.$save(function(response) {
				$location.path('resultsannounces/' + response._id);

				// Clear form fields
				$scope.name = '';
			}, function(errorResponse) {
				$scope.error = errorResponse.data.message;
			});
		};

		// Remove existing Resultsannounce
		$scope.remove = function(resultsannounce) {
			if ( resultsannounce ) {
				resultsannounce.$remove();

				for (var i in $scope.resultsannounces) {
					if ($scope.resultsannounces [i] === resultsannounce) {
						$scope.resultsannounces.splice(i, 1);
					}
				}
			} else {
				$scope.resultsannounce.$remove(function() {
					$location.path('resultsannounces');
				});
			}
		};

		// Update existing Resultsannounce
		$scope.update = function() {
			var resultsannounce = $scope.resultsannounce;

			resultsannounce.$update(function() {
				$location.path('resultsannounces/' + resultsannounce._id);
			}, function(errorResponse) {
				$scope.error = errorResponse.data.message;
			});
		};

		// Find a list of Resultsannounces
		$scope.find = function() {
			$scope.resultsannounces = Resultsannounces.query();
		};

		// Find existing Resultsannounce
		$scope.findOne = function() {
			$scope.resultsannounce = Resultsannounces.get({
				resultsannounceId: $stateParams.resultsannounceId
			});
		};
	}
]);


text:
from test_driver.driver import Driver
from test_driver.vlan import VLan
from test_driver.machine import Machine
from test_driver.logger import Logger
from typing import Callable, Iterator, ContextManager, Optional, List, Dict, Any, Union
from typing_extensions import Protocol
from pathlib import Path

class RetryProtocol(Protocol):
    def __call__(self, fn: Callable, timeout: int = 900) -> None:
        raise Exception("This is just type information for the Nix test driver")

class PollingConditionProtocol(Protocol):
    def __call__(
        self,
        fun_: Optional[Callable] = None,
        *,
        seconds_interval: float = 2.0,
        description: Optional[str] = None,
    ) -> Union[Callable[[Callable], ContextManager], ContextManager]:
        raise Exception("This is just type information for the Nix test driver")

start_all: Callable[[], None]
subtest: Callable[[str], ContextManager[None]]
retry: RetryProtocol
test_script: Callable[[], None]
machines: List[Machine]
vlans: List[VLan]
driver: Driver
log: Logger
create_machine: Callable[[Dict[str, Any]], Machine]
run_tests: Callable[[], None]
join_all: Callable[[], None]
serial_stdout_off: Callable[[], None]
serial_stdout_on: Callable[[], None]
polling_condition: PollingConditionProtocol


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zaradai</groupId>
    <artifactId>fastlog</artifactId>
    <version>0.1-SNAPSHOT</version>

    <name>${project.groupId}:${project.artifactId}</name>
    <description>Scala logger for low latency applications</description>
    <packaging>pom</packaging>
    <inceptionYear>2015</inceptionYear>
    <url>https://github.com/zaradai/diamond</url>

    <developers>
        <developer>
            <id>Zaradai</id>
            <name>Stephen Hough</name>
            <email>stephen.c.hough@gmail.com</email>
            <organization>Zaradai</organization>
            <organizationUrl>https://github.com/zaradai</organizationUrl>
            <timezone>+8</timezone>
            <roles>
                <role>developer</role>
            </roles>
        </developer>
    </developers>

    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
            <comments>A business-friendly OSS license</comments>
        </license>
    </licenses>

    <organization>
        <name>Zaradai</name>
        <url>https://github.com/zaradai</url>
    </organization>

    <issueManagement>
        <url>https://github.com/zaradai/fastlog/issues</url>
        <system>github</system>
    </issueManagement>

    <scm>
        <connection>scm:git:git@github.com/zaradai/fastlog.git</connection>
        <developerConnection>scm:git:git@github.com:zaradai/fastlog.git</developerConnection>
        <url>https://github.com/zaradai/fastlog</url>
        <tag>HEAD</tag>
    </scm>

    <prerequisites>
        <maven>3.0.4</maven>
    </prerequisites>

    <properties>
        <javac.target>1.7</javac.target>
        <scala.version>2.10.4</scala.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Project dependencies -->
        <scalalogging.version>2.1.2</scalalogging.version>
        <logback.version>1.1.2</logback.version>
        <slf4j.version>1.7.7</slf4j.version>
        <disruptor.version>3.3.2</disruptor.version>
        <!--<jmh.version>1.9.3</jmh.version>-->
        <jmh.version>1.9.3</jmh.version>
        <jmh.generator>default</jmh.generator>
        <uberjar.name>fastlog</uberjar.name>
        <!-- Test -->
        <scalatest.version>2.3.0-SNAP2</scalatest.version>
        <mockito.version>1.9.5</mockito.version>
        <!-- version properties for plugins -->
        <scala.plugin.version>3.1.6</scala.plugin.version>
        <scalatest.plugin.version>1.0</scalatest.plugin.version>
        <compiler.plugin.version>3.1</compiler.plugin.version>
        <surefire.version>2.7</surefire.version>
        <build.plugin.version>1.8</build.plugin.version>
        <exec.plugin.version>1.2.1</exec.plugin.version>
        <shade.plugin.version>2.2</shade.plugin.version>
    </properties>

    <modules>
        <module>logging</module>
        <module>performance</module>
    </modules>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>${build.plugin.version}</version>
                    <executions>
                        <execution>
                            <id>add-source</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>add-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>${project.basedir}/src/main/scala</source>
                                    <source>${project.basedir}/target/generated-sources/jmh</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>${scala.plugin.version}</version>
                    <configuration>
                        <recompileMode>incremental</recompileMode>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>${exec.plugin.version}</version>
                    <executions>
                        <execution>
                            <phase>process-sources</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <includePluginDependencies>true</includePluginDependencies>
                                <mainClass>org.openjdk.jmh.generators.bytecode.JmhBytecodeGenerator</mainClass>
                                <arguments>
                                    <argument>${project.basedir}/target/classes/</argument>
                                    <argument>${project.basedir}/target/generated-sources/jmh/</argument>
                                    <argument>${project.basedir}/target/classes/</argument>
                                    <argument>${jmh.generator}</argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.openjdk.jmh</groupId>
                            <artifactId>jmh-generator-bytecode</artifactId>
                            <version>${jmh.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${compiler.plugin.version}</version>
                    <configuration>
                        <compilerVersion>${javac.target}</compilerVersion>
                        <source>${javac.target}</source>
                        <target>${javac.target}</target>
                        <compilerArgument>-proc:none</compilerArgument>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>${shade.plugin.version}</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <finalName>${uberjar.name}</finalName>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>org.openjdk.jmh.Main</mainClass>
                                    </transformer>
                                </transformers>
                                <filters>
                                    <filter>
                                        <artifact>*:*</artifact>
                                        <excludes>
                                            <exclude>META-INF/*.SF</exclude>
                                            <exclude>META-INF/*.DSA</exclude>
                                            <exclude>META-INF/*.RSA</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${surefire.version}</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.scalatest</groupId>
                    <artifactId>scalatest-maven-plugin</artifactId>
                    <version>${scalatest.plugin.version}</version>
                    <configuration>
                        <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                        <junitxml>.</junitxml>
                    </configuration>
                    <executions>
                        <execution>
                            <id>scala-test</id>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>
</project>


text:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.anarres.gradle.plugin.jarjar;

import groovy.lang.Closure;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.ClosureBackedAction;
import static org.bouncycastle.asn1.x500.style.RFC4519Style.c;
import static org.gradle.internal.Transformers.name;

/**
 *
 * @author shevek
 */
public class JarjarPlugin implements Plugin<Project> {

    @Override
    public void apply(final Project project) {
        project.getLogger().info("Applying " + this);
        // project.getExtensions().getExtraProperties().set("Jarjar", JarjarTask.class);
        /*
         project.getExtensions().getExtraProperties().set("jarjarDependency", new Closure<FileCollection>(JarjarPlugin.this) {
         @Override
         public FileCollection call(Object... args) {
         JarjarTask jarjar = project.getTasks().create(
         name,
         JarjarTask.class,
         new ClosureBackedAction<JarjarTask>(c));
         return jarjar.getOutputs().getFiles();
         }
         });
         */
        project.getExtensions().create("jarjar", JarjarController.class, project);
    }

}


text:
<!DOCTYPE html>
<meta charset="utf-8">
<title>The sheet property should work even with an empty style element</title>
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<link rel="help" href="https://html.spec.whatwg.org/multipage/semantics.html#update-a-style-block">
<link rel="help" href="https://drafts.csswg.org/cssom/#the-linkstyle-interface">

<style></style>

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
"use strict";

setup({ single_test: true });

assert_not_equals(document.querySelector("style").sheet, null);
assert_true(document.querySelector("style").sheet instanceof CSSStyleSheet);

done();
</script>


text:
package com.example;

import java.util.stream.Stream;

import reactor.Environment;
import reactor.rx.broadcast.Broadcaster;

public class Main {

    private static final String[] NAMES;
    static {
        NAMES = new String[] { "Alice", "Bob", "Charles" };
    }

    private static void helloWorld() {
        System.out.println("\n--- helloWorld ---\n");

        Environment.initialize();
        Broadcaster<String> sink = Broadcaster.create(Environment.get());
        sink.dispatchOn(Environment.cachedDispatcher()).map(String::toUpperCase).consume(name -> {
            System.out.println("Hello " + name + "!");
        });

        Stream.of(NAMES).forEach(name -> sink.onNext(name));
        sink.onComplete();
        sink.join();
    }

    public static void main(String... args) {
        helloWorld();
    }

}