text:
import os
import json
import time
import pickle
import argparse
import dateutil.parser
from random import shuffle, randrange, uniform

import numpy as np
from sqlite3 import dbapi2 as sqlite3
from hashlib import md5
from flask import Flask, request, session, url_for, redirect, \
     render_template, abort, g, flash, _app_ctx_stack
from flask_limiter import Limiter
from werkzeug import check_password_hash, generate_password_hash
import pymongo

from utils import safe_pickle_dump, strip_version, isvalidid, Config

# various globals
# -----------------------------------------------------------------------------

# database configuration
if os.path.isfile('secret_key.txt'):
  SECRET_KEY = open('secret_key.txt', 'r').read()
else:
  SECRET_KEY = 'devkey, should be in a file'
app = Flask(__name__)
app.config.from_object(__name__)
#limiter = Limiter(app, global_limits=["100 per hour", "20 per minute"])
limiter = Limiter(app, default_limits=["100 per hour", "20 per minute"])
#limiter = Limiter(app, application_limits=["100 per hour", "20 per minute"])

# -----------------------------------------------------------------------------
# utilities for database interactions
# -----------------------------------------------------------------------------
# to initialize the database: sqlite3 as.db < schema.sql
def connect_db():
  sqlite_db = sqlite3.connect(Config.database_path)
  sqlite_db.row_factory = sqlite3.Row # to return dicts rather than tuples
  return sqlite_db

def query_db(query, args=(), one=False):
  """Queries the database and returns a list of dictionaries."""
  cur = g.db.execute(query, args)
  rv = cur.fetchall()
  return (rv[0] if rv else None) if one else rv

def get_user_id(username):
  """Convenience method to look up the id for a username."""
  rv = query_db('select user_id from user where username = ?',
                [username], one=True)
  return rv[0] if rv else None

def get_username(user_id):
  """Convenience method to look up the username for a user."""
  rv = query_db('select username from user where user_id = ?',
                [user_id], one=True)
  return rv[0] if rv else None

# -----------------------------------------------------------------------------
# connection handlers
# -----------------------------------------------------------------------------

@app.before_request
def before_request():
  # this will always request database connection, even if we dont end up using it ;\
  g.db = connect_db()
  # retrieve user object from the database if user_id is set
  g.user = None
  if 'user_id' in session:
    g.user = query_db('select * from user where user_id = ?',
                      [session['user_id']], one=True)

@app.teardown_request
def teardown_request(exception):
  db = getattr(g, 'db', None)
  if db is not None:
    db.close()

# -----------------------------------------------------------------------------
# search/sort functionality
# -----------------------------------------------------------------------------

def papers_search(qraw):
  qparts = qraw.lower().strip().split() # split by spaces
  # e.g. if qraw = "localization in space"
  # then qparts = ['localization', 'in', 'space']
  # use reverse index and accumulate scores
  print(qparts)
  scores = []
  if isvalidid(qraw) is not None:
    # now qraw should be like 1611.00675v4
    pid = qraw.split('v')[0]
    # pid is like 1611.00675
    if pid in db.keys():
      scores.append((1, db[pid]))

  qparts = qraw.lower().strip().split() # split by spaces
  for pid,p in db.items():
    score = sum(SEARCH_DICT[pid].get(q,0) for q in qparts)
    #for q in qparts:
    #  print(SEARCH_DICT[pid].get(q,0))
    # for any given paper, get the contribution of each word
    # in qraw to that paper, equivalent to calculating the correlation
    if score == 0:
      continue # no match whatsoever, dont include
    # give a small boost to more recent papers
    score += 0.0001*p['tscore']
    scores.append((score, p))
  scores.sort(reverse=True, key=lambda x: x[0]) # descending
  out = [x[1] for x in scores if x[0] > 0]
  return out

def papers_similar(pid):
  rawpid = strip_version(pid)

  # check if we have this paper at all, otherwise return empty list
  if not rawpid in db:
    return []

  # check if we have distances to this specific version of paper id (includes version)
  if pid in sim_dict:
    # good, simplest case: lets return the papers
    return [db[strip_version(k)] for k in sim_dict[pid]]
  else:
    # ok we don't have this specific version. could be a stale URL that points to,
    # e.g. v1 of a paper, but due to an updated version of it we only have v2 on file
    # now. We want to use v2 in that case.
    # lets try to retrieve the most recent version of this paper we do have
    kok = [k for k in sim_dict if rawpid in k]
    if kok:
      # ok we have at least one different version of this paper, lets use it instead
      id_use_instead = kok[0]
      return [db[strip_version(k)] for k in sim_dict[id_use_instead]]
    else:
      # return just the paper. we dont have similarities for it for some reason
      return [db[rawpid]]

def papers_from_library():
  #print("calling papers_from_library")
  out = []
  if g.user:
    # user is logged in, lets fetch their saved library data
    uid = session['user_id']
    user_library = query_db('''select * from library where user_id = ?''', [uid])
    libids = [strip_version(x['paper_id']) for x in user_library]
    out = [db[x] for x in libids]
    out = sorted(out, key=lambda k: k['updated'], reverse=True)
  return out

def papers_from_svm(recent_days=None):
  out = []
  if g.user:

    uid = session['user_id']
    if not uid in user_sim:
      return []

    # we want to exclude papers that are already in user library from the result, so fetch them.
    user_library = query_db('''select * from library where user_id = ?''', [uid])
    libids = {strip_version(x['paper_id']) for x in user_library}

    plist = user_sim[uid]
    out = [db[x] for x in plist if not x in libids]

    if recent_days is not None:
      # filter as well to only most recent papers
      curtime = int(time.time()) # in seconds
      out = [x for x in out if curtime - x['time_published'] < recent_days*24*60*60]

  return out

def papers_filter_version(papers, v):
  if v != '1':
    return papers # noop
  intv = int(v)
  filtered = [p for p in papers if p['_version'] == intv]
  return filtered

def encode_json(ps, n=10, send_images=True, send_abstracts=True):

  libids = set()
  if g.user:
    # user is logged in, lets fetch their saved library data
    uid = session['user_id']
    user_library = query_db('''select * from library where user_id = ?''', [uid])
    libids = {strip_version(x['paper_id']) for x in user_library}

  ret = []
  for i in range(min(len(ps),n)):
    p = ps[i]
    idvv = '%sv%d' % (p['_rawid'], p['_version'])
    struct = {}
    struct['title'] = p['title']
    struct['pid'] = idvv
    struct['rawpid'] = p['_rawid']
    struct['category'] = p['arxiv_primary_category']['term']
    struct['authors'] = [a['name'] for a in p['authors']]
    struct['link'] = p['link']
    struct['in_library'] = 1 if p['_rawid'] in libids else 0
    if send_abstracts:
      struct['abstract'] = p['summary']
    if send_images:
      struct['img'] = '/static/thumbs/' + idvv + '.pdf.jpg'
    struct['tags'] = [t['term'] for t in p['tags']]

    # render time information nicely
    timestruct = dateutil.parser.parse(p['updated'])
    struct['published_time'] = '%s/%s/%s' % (timestruct.month, timestruct.day, timestruct.year)
    timestruct = dateutil.parser.parse(p['published'])
    struct['originally_published_time'] = '%s/%s/%s' % (timestruct.month, timestruct.day, timestruct.year)

    # fetch amount of discussion on this paper
    struct['num_discussion'] = comments.count({ 'pid': p['_rawid'] })

    # arxiv comments from the authors (when they submit the paper)
    cc = p.get('arxiv_comment', '')
    if len(cc) > 100:
      cc = cc[:100] + '...' # crop very long comments
    struct['comment'] = cc

    ret.append(struct)
  return ret

# -----------------------------------------------------------------------------
# flask request handling
# -----------------------------------------------------------------------------

def default_context(papers, **kws):
  top_papers = encode_json(papers, args.num_results)

  # prompt logic
  show_prompt = 'no'
  try:
    if Config.beg_for_hosting_money and g.user and uniform(0,1) < 0.05:
      uid = session['user_id']
      entry = goaway_collection.find_one({ 'uid':uid })
      if not entry:
        lib_count = query_db('''select count(*) from library where user_id = ?''', [uid], one=True)
        lib_count = lib_count['count(*)']
        if lib_count > 0: # user has some items in their library too
          show_prompt = 'yes'
  except Exception as e:
    print(e)

  ans = dict(papers=top_papers, numresults=len(papers), totpapers=len(db), tweets=[], msg='', show_prompt=show_prompt, pid_to_users={})
  ans.update(kws)
  return ans

@app.route('/goaway', methods=['POST'])
def goaway():
  if not g.user: return # weird
  uid = session['user_id']
  entry = goaway_collection.find_one({ 'uid':uid })
  if not entry: # ok record this user wanting it to stop
    username = get_username(session['user_id'])
    print('adding', uid, username, 'to goaway.')
    goaway_collection.insert_one({ 'uid':uid, 'time':int(time.time()) })
  return 'OK'

@app.route("/")
def intmain():
  vstr = request.args.get('vfilter', 'all')
  papers = [db[pid] for pid in DATE_SORTED_PIDS] # precomputed
  papers = papers_filter_version(papers, vstr)
  ctx = default_context(papers, render_format='recent',
                        msg='Showing most recent Arxiv papers:')
  return render_template('main.html', **ctx)

@app.route("/<request_pid>")
def rank(request_pid=None):
  if not isvalidid(request_pid):
    return '' # these are requests for icons, things like robots.txt, etc
  papers = papers_similar(request_pid)
  ctx = default_context(papers, render_format='paper')
  return render_template('main.html', **ctx)

@app.route('/discuss', methods=['GET'])
def discuss():
  """ return discussion related to a paper """
  pid = request.args.get('id', '') # paper id of paper we wish to discuss
  papers = [db[pid]] if pid in db else []

  # fetch the comments
  comms_cursor = comments.find({ 'pid':pid }).sort([('time_posted', pymongo.DESCENDING)])
  comms = list(comms_cursor)
  for c in comms:
    c['_id'] = str(c['_id']) # have to convert these to strs from ObjectId, and backwards later http://api.mongodb.com/python/current/tutorial.html

  # fetch the counts for all tags
  tag_counts = []
  for c in comms:
    cc = [tags_collection.count({ 'comment_id':c['_id'], 'tag_name':t }) for t in TAGS]
    tag_counts.append(cc);

  # and render
  ctx = default_context(papers, render_format='default', comments=comms, gpid=pid, tags=TAGS, tag_counts=tag_counts)
  return render_template('discuss.html', **ctx)

@app.route('/comment', methods=['POST'])
def comment():
  """ user wants to post a comment """
  anon = int(request.form['anon'])

  if g.user and (not anon):
    username = get_username(session['user_id'])
  else:
    # generate a unique username if user wants to be anon, or user not logged in.
    username = 'anon-%s-%s' % (str(int(time.time())), str(randrange(1000)))

  # process the raw pid and validate it, etc
  try:
    pid = request.form['pid']
    if not pid in db: raise Exception("invalid pid")
    version = db[pid]['_version'] # most recent version of this paper
  except Exception as e:
    print(e)
    return 'bad pid. This is most likely Andrej\'s fault.'

  # create the entry
  entry = {
    'user': username,
    'pid': pid, # raw pid with no version, for search convenience
    'version': version, # version as int, again as convenience
    'conf': request.form['conf'],
    'anon': anon,
    'time_posted': time.time(),
    'text': request.form['text'],
  }

  # enter into database
  print(entry)
  comments.insert_one(entry)
  return 'OK'

@app.route("/discussions", methods=['GET'])
def discussions():
  # return most recently discussed papers
  comms_cursor = comments.find().sort([('time_posted', pymongo.DESCENDING)]).limit(100)

  # get the (unique) set of papers.
  papers = []
  have = set()
  for e in comms_cursor:
    pid = e['pid']
    if pid in db and not pid in have:
      have.add(pid)
      papers.append(db[pid])

  ctx = default_context(papers, render_format="discussions")
  return render_template('main.html', **ctx)

@app.route('/toggletag', methods=['POST'])
def toggletag():

  if not g.user:
    return 'You have to be logged in to tag. Sorry - otherwise things could get out of hand FAST.'

  # get the tag and validate it as an allowed tag
  tag_name = request.form['tag_name']
  if not tag_name in TAGS:
    print('tag name %s is not in allowed tags.' % (tag_name, ))
    return "Bad tag name. This is most likely Andrej's fault."

  pid = request.form['pid']
  comment_id = request.form['comment_id']
  username = get_username(session['user_id'])
  time_toggled = time.time()
  entry = {
    'username': username,
    'pid': pid,
    'comment_id': comment_id,
    'tag_name': tag_name,
    'time': time_toggled,
  }

  # remove any existing entries for this user/comment/tag
  result = tags_collection.delete_one({ 'username':username, 'comment_id':comment_id, 'tag_name':tag_name })
  if result.deleted_count > 0:
    print('cleared an existing entry from database')
  else:
    print('no entry existed, so this is a toggle ON. inserting:')
    print(entry)
    tags_collection.insert_one(entry)

  return 'OK'

@app.route("/search", methods=['GET'])
def search():
  q = request.args.get('q', '') # get the search request
  # pass what is typed in html's searchbox to a new variable q
  papers = papers_search(q) # perform the query and get sorted documents
  ctx = default_context(papers, render_format="search")
  return render_template('main.html', **ctx)

@app.route('/recommend', methods=['GET'])
def recommend():
  """ return user's svm sorted list """
  ttstr = request.args.get('timefilter', 'week') # default is week
  vstr = request.args.get('vfilter', 'all') # default is all (no filter)
  legend = {'day':1, '3days':3, 'week':7, 'month':30, 'year':365}
  tt = legend.get(ttstr, None)
  papers = papers_from_svm(recent_days=tt)
  papers = papers_filter_version(papers, vstr)
  ctx = default_context(papers, render_format='recommend',
                        msg='Recommended papers: (based on SVM trained on tfidf of papers in your library, refreshed every day or so)' if g.user else 'You must be logged in and have some papers saved in your library.')
  return render_template('main.html', **ctx)

@app.route('/top', methods=['GET'])
def top():
  """ return top papers """
  ttstr = request.args.get('timefilter', 'week') # default is week
  vstr = request.args.get('vfilter', 'all') # default is all (no filter)
  legend = {'day':1, '3days':3, 'week':7, 'month':30, 'year':365, 'alltime':10000}
  tt = legend.get(ttstr, 7)
  curtime = int(time.time()) # in seconds
  top_sorted_papers = [db[p] for p in TOP_SORTED_PIDS]
  papers = [p for p in top_sorted_papers if curtime - p['time_published'] < tt*24*60*60]
  papers = papers_filter_version(papers, vstr)
  ctx = default_context(papers, render_format='top',
                        msg='Top papers based on people\'s libraries:')
  return render_template('main.html', **ctx)

@app.route('/toptwtr', methods=['GET'])
def toptwtr():
  """ return top papers """
  ttstr = request.args.get('timefilter', 'day') # default is day
  tweets_top = {'day':tweets_top1, 'week':tweets_top7, 'month':tweets_top30}[ttstr]
  cursor = tweets_top.find().sort([('vote', pymongo.DESCENDING)]).limit(100)
  papers, tweets = [], []
  for rec in cursor:
    if rec['pid'] in db:
      papers.append(db[rec['pid']])
      tweet = {k:v for k,v in rec.items() if k != '_id'}
      tweets.append(tweet)
  ctx = default_context(papers, render_format='toptwtr', tweets=tweets,
                        msg='Top papers mentioned on Twitter over last ' + ttstr + ':')
  return render_template('main.html', **ctx)

@app.route('/library')
def library():
  """ render user's library """
  papers = papers_from_library()
  ret = encode_json(papers, 500) # cap at 500 papers in someone's library. that's a lot!
  if g.user:
    msg = '%d papers in your library:' % (len(ret), )
  else:
    msg = 'You must be logged in. Once you are, you can save papers to your library (with the save icon on the right of each paper) and they will show up here.'
  ctx = default_context(papers, render_format='library', msg=msg)
  return render_template('main.html', **ctx)

"""
@app.route('/testpage', methods=['POST'])
def test_func():
  if not g.user:
    return 'NO'
  uid = session['user_id']
  passed_message = request.form['test_message']
  print(passed_message)
  return 'OFF'
"""

@app.route('/titlepage',methods=['POST'])
def title_page():
  pdf_path = request.form['titleRe']
  os.system('open '+pdf_path)
  return 'OK'

@app.route('/libtoggle', methods=['POST'])
def review():
  """ user wants to toggle a paper in his library """

  # make sure user is logged in
  if not g.user:
    return 'NO' # fail... (not logged in). JS should prevent from us getting here.

  idvv = request.form['pid'] # includes version
  # correspond to $.post("/libtoggle", {pid: pid}) line in .js
  if not isvalidid(idvv):
    return 'NO' # fail, malformed id. weird.
  pid = strip_version(idvv)
  if not pid in db:
    return 'NO' # we don't know this paper. wat

  uid = session['user_id'] # id of logged in user

  # check this user already has this paper in library
  record = query_db('''select * from library where
          user_id = ? and paper_id = ?''', [uid, pid], one=True)
  print(record)

  ret = 'NO'
  if record:
    # record exists, erase it.
    g.db.execute('''delete from library where user_id = ? and paper_id = ?''', [uid, pid])
    g.db.commit()
    #print('removed %s for %s' % (pid, uid))
    ret = 'OFF'
  else:
    # record does not exist, add it.
    rawpid = strip_version(pid)
    g.db.execute('''insert into library (paper_id, user_id, update_time) values (?, ?, ?)''',
        [rawpid, uid, int(time.time())])
    g.db.commit()
    #print('added %s for %s' % (pid, uid))
    #do not return a template html here, so creating a null page to handle library
    ret = 'ON'

  return ret

@app.route('/friends', methods=['GET'])
def friends():

    ttstr = request.args.get('timefilter', 'week') # default is week
    legend = {'day':1, '3days':3, 'week':7, 'month':30, 'year':365}
    tt = legend.get(ttstr, 7)

    papers = []
    pid_to_users = {}
    if g.user:
        # gather all the people we are following
        username = get_username(session['user_id'])
        edges = list(follow_collection.find({ 'who':username }))
        # fetch all papers in all of their libraries, and count the top ones
        counts = {}
        for edict in edges:
            whom = edict['whom']
            uid = get_user_id(whom)
            user_library = query_db('''select * from library where user_id = ?''', [uid])
            libids = [strip_version(x['paper_id']) for x in user_library]
            for lid in libids:
                if not lid in counts:
                    counts[lid] = []
                counts[lid].append(whom)

        keys = list(counts.keys())
        keys.sort(key=lambda k: len(counts[k]), reverse=True) # descending by count
        papers = [db[x] for x in keys]
        # finally filter by date
        curtime = int(time.time()) # in seconds
        papers = [x for x in papers if curtime - x['time_published'] < tt*24*60*60]
        # trim at like 100
        if len(papers) > 100: papers = papers[:100]
        # trim counts as well correspondingly
        pid_to_users = { p['_rawid'] : counts.get(p['_rawid'], []) for p in papers }

    if not g.user:
        msg = "You must be logged in and follow some people to enjoy this tab."
    else:
        if len(papers) == 0:
            msg = "No friend papers present. Try to extend the time range, or add friends by clicking on your account name (top, right)"
        else:
            msg = "Papers in your friend's libraries:"

    ctx = default_context(papers, render_format='friends', pid_to_users=pid_to_users, msg=msg)
    return render_template('main.html', **ctx)

@app.route('/account')
def account():
    ctx = { 'totpapers':len(db) }

    followers = []
    following = []
    # fetch all followers/following of the logged in user
    if g.user:
        username = get_username(session['user_id'])

        following_db = list(follow_collection.find({ 'who':username }))
        for e in following_db:
            following.append({ 'user':e['whom'], 'active':e['active'] })

        followers_db = list(follow_collection.find({ 'whom':username }))
        for e in followers_db:
            followers.append({ 'user':e['who'], 'active':e['active'] })

    ctx['followers'] = followers
    ctx['following'] = following
    return render_template('account.html', **ctx)

@app.route('/requestfollow', methods=['POST'])
def requestfollow():
    if request.form['newf'] and g.user:
        # add an entry: this user is requesting to follow a second user
        who = get_username(session['user_id'])
        whom = request.form['newf']
        # make sure whom exists in our database
        whom_id = get_user_id(whom)
        if whom_id is not None:
            e = { 'who':who, 'whom':whom, 'active':0, 'time_request':int(time.time()) }
            print('adding request follow:')
            print(e)
            follow_collection.insert_one(e)

    return redirect(url_for('account'))

@app.route('/removefollow', methods=['POST'])
def removefollow():
    user = request.form['user']
    lst = request.form['lst']
    if user and lst:
        username = get_username(session['user_id'])
        if lst == 'followers':
            # user clicked "X" in their followers list. Erase the follower of this user
            who = user
            whom = username
        elif lst == 'following':
            # user clicked "X" in their following list. Stop following this user.
            who = username
            whom = user
        else:
            return 'NOTOK'

        delq = { 'who':who, 'whom':whom }
        print('deleting from follow collection:', delq)
        follow_collection.delete_one(delq)
        return 'OK'
    else:
        return 'NOTOK'

@app.route('/addfollow', methods=['POST'])
def addfollow():
    user = request.form['user']
    lst = request.form['lst']
    if user and lst:
        username = get_username(session['user_id'])
        if lst == 'followers':
            # user clicked "OK" in the followers list, wants to approve some follower. make active.
            who = user
            whom = username
            delq = { 'who':who, 'whom':whom }
            print('making active in follow collection:', delq)
            follow_collection.update_one(delq, {'$set':{'active':1}})
            return 'OK'

    return 'NOTOK'

@app.route('/login', methods=['POST'])
def login():
  """ logs in the user. if the username doesn't exist creates the account """

  if not request.form['username']:
    flash('You have to enter a username')
  elif not request.form['password']:
    flash('You have to enter a password')
  elif get_user_id(request.form['username']) is not None:
    # username already exists, fetch all of its attributes
    user = query_db('''select * from user where
          username = ?''', [request.form['username']], one=True)
    if check_password_hash(user['pw_hash'], request.form['password']):
      # password is correct, log in the user
      session['user_id'] = get_user_id(request.form['username'])
      flash('User ' + request.form['username'] + ' logged in.')
    else:
      # incorrect password
      flash('User ' + request.form['username'] + ' already exists, wrong password.')
  else:
    # create account and log in
    creation_time = int(time.time())
    g.db.execute('''insert into user (username, pw_hash, creation_time) values (?, ?, ?)''',
      [request.form['username'],
      generate_password_hash(request.form['password']),
      creation_time])
    user_id = g.db.execute('select last_insert_rowid()').fetchall()[0][0]
    g.db.commit()

    session['user_id'] = user_id
    flash('New account %s created' % (request.form['username'], ))

  return redirect(url_for('intmain'))

@app.route('/logout')
def logout():
  session.pop('user_id', None)
  flash('You were logged out')
  return redirect(url_for('intmain'))

# -----------------------------------------------------------------------------
# int main
# -----------------------------------------------------------------------------
if __name__ == "__main__":

  parser = argparse.ArgumentParser()
  parser.add_argument('-p', '--prod', dest='prod', action='store_true', help='run in prod?')
  parser.add_argument('-r', '--num_results', dest='num_results', type=int, default=200, help='number of results to return per query')
  parser.add_argument('--port', dest='port', type=int, default=5000, help='port to serve on')
  args = parser.parse_args()
  print(args)

  if not os.path.isfile(Config.database_path):
    print('did not find as.db, trying to create an empty database from schema.sql...')
    print('this needs sqlite3 to be installed!')
    os.system('sqlite3 as.db < schema.sql')

  print('loading the paper database', Config.db_serve_path)
  db = pickle.load(open(Config.db_serve_path, 'rb'))

  print('loading tfidf_meta', Config.meta_path)
  meta = pickle.load(open(Config.meta_path, "rb"))
  vocab = meta['vocab']
  idf = meta['idf']

  print('loading paper similarities', Config.sim_path)
  sim_dict = pickle.load(open(Config.sim_path, "rb"))

  print('loading user recommendations', Config.user_sim_path)
  user_sim = {}
  if os.path.isfile(Config.user_sim_path):
    user_sim = pickle.load(open(Config.user_sim_path, 'rb'))

  print('loading serve cache...', Config.serve_cache_path)
  cache = pickle.load(open(Config.serve_cache_path, "rb"))
  DATE_SORTED_PIDS = cache['date_sorted_pids']
  TOP_SORTED_PIDS = cache['top_sorted_pids']
  SEARCH_DICT = cache['search_dict']

  print('connecting to mongodb...')
  client = pymongo.MongoClient()
  mdb = client.arxiv
  tweets_top1 = mdb.tweets_top1
  tweets_top7 = mdb.tweets_top7
  tweets_top30 = mdb.tweets_top30
  comments = mdb.comments
  tags_collection = mdb.tags
  goaway_collection = mdb.goaway
  follow_collection = mdb.follow
  print('mongodb tweets_top1 collection size:', tweets_top1.count())
  print('mongodb tweets_top7 collection size:', tweets_top7.count())
  print('mongodb tweets_top30 collection size:', tweets_top30.count())
  print('mongodb comments collection size:', comments.count())
  print('mongodb tags collection size:', tags_collection.count())
  print('mongodb goaway collection size:', goaway_collection.count())
  print('mongodb follow collection size:', follow_collection.count())

  TAGS = ['insightful!', 'thank you', 'agree', 'disagree', 'not constructive', 'troll', 'spam']
  # start
  if args.prod:
    # run on Tornado instead, since running raw Flask in prod is not recommended
    print('starting tornado!')
    from tornado.wsgi import WSGIContainer
    from tornado.httpserver import HTTPServer
    from tornado.ioloop import IOLoop
    from tornado.log import enable_pretty_logging
    enable_pretty_logging()
    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(args.port)
    IOLoop.instance().start()
  else:
    print('starting flask!')
    app.debug = False
    app.run(port=args.port, host='0.0.0.0')


text:
package agent

import (
	"github.com/cloudinsight/cloudinsight-agent/common/config"
	"github.com/cloudinsight/cloudinsight-agent/common/metric"
)

// NewAggregator XXX
func NewAggregator(
	metrics chan metric.Metric,
	conf *config.Config,
) metric.Aggregator {
	return metric.NewAggregator(metrics, 1, conf.GetHostname(), formatter, nil, nil, 0)
}

// Format metrics coming from the MetricsAggregator. Will look like:
// (metric, timestamp, value, {"tags": ["tag1", "tag2"], ...})
func formatter(m metric.Metric) interface{} {
	var ret []interface{}
	ret = append(ret, m.Name)
	ret = append(ret, m.Timestamp)
	ret = append(ret, m.Value)

	attributes := make(map[string]interface{})
	if len(m.Tags) > 0 {
		attributes["tags"] = m.Tags
	}
	if m.Hostname != "" {
		attributes["hostname"] = m.Hostname
	}
	if m.DeviceName != "" {
		attributes["device_name"] = m.DeviceName
	}
	if m.Type != "" {
		attributes["type"] = m.Type
	}
	if len(attributes) > 0 {
		ret = append(ret, attributes)
	}

	return ret
}

redpajama_set_name: RedPajamaGithub


text:
/**
 * Encoder and decoder which compresses and decompresses {@link io.netty.buffer.ByteBuf}s
 * in a compression format such as zlib,
 * gzip, and
 * Snappy.
 */
package io.netty.handler.codec.compression;
// TODO Implement bzip2 and lzma handlers


text:
using namespace Glyph3;
//--------------------------------------------------------------------------------
BufferDX11::BufferDX11()
{
	ZeroMemory( &m_DesiredDesc, sizeof( D3D11_BUFFER_DESC ) );
	ZeroMemory( &m_ActualDesc, sizeof( D3D11_BUFFER_DESC ) );
}
//--------------------------------------------------------------------------------
BufferDX11::~BufferDX11()
{
}
//--------------------------------------------------------------------------------
D3D11_BUFFER_DESC BufferDX11::GetActualDescription()
{
	ZeroMemory( &m_ActualDesc, sizeof( D3D11_BUFFER_DESC ) );

	if ( m_pBuffer )
		m_pBuffer->GetDesc( &m_ActualDesc );

	return( m_ActualDesc );
}
//--------------------------------------------------------------------------------
D3D11_BUFFER_DESC BufferDX11::GetDesiredDescription()
{
	return( m_DesiredDesc );
}
//--------------------------------------------------------------------------------
void BufferDX11::SetDesiredDescription( D3D11_BUFFER_DESC desc )
{
	m_DesiredDesc = desc;
}
//--------------------------------------------------------------------------------
UINT BufferDX11::GetByteWidth()
{
	D3D11_BUFFER_DESC description = GetActualDescription();

	return( description.ByteWidth );
}
//--------------------------------------------------------------------------------
D3D11_USAGE BufferDX11::GetUsage()
{
	D3D11_BUFFER_DESC description = GetActualDescription();

	return( description.Usage );
}
//--------------------------------------------------------------------------------
UINT BufferDX11::GetBindFlags()
{
	D3D11_BUFFER_DESC description = GetActualDescription();

	return( description.BindFlags );
}
//--------------------------------------------------------------------------------
UINT BufferDX11::GetCPUAccessFlags()
{
	D3D11_BUFFER_DESC description = GetActualDescription();

	return( description.CPUAccessFlags );
}
//--------------------------------------------------------------------------------
UINT BufferDX11::GetMiscFlags()
{
	D3D11_BUFFER_DESC description = GetActualDescription();

	return( description.MiscFlags );
}
//--------------------------------------------------------------------------------
UINT BufferDX11::GetStructureByteStride()
{
	D3D11_BUFFER_DESC description = GetActualDescription();

	return( description.StructureByteStride );
}
//--------------------------------------------------------------------------------
void BufferDX11::SetByteWidth( UINT width )
{
	m_DesiredDesc.ByteWidth = width;
}
//--------------------------------------------------------------------------------
void BufferDX11::SetUsage( D3D11_USAGE usage )
{
	m_DesiredDesc.Usage = usage;
}
//--------------------------------------------------------------------------------
void BufferDX11::SetBindFlags( UINT flags )
{
	m_DesiredDesc.BindFlags = flags;
}
//--------------------------------------------------------------------------------
void BufferDX11::SetCPUAccessFlags( UINT flags )
{
	m_DesiredDesc.CPUAccessFlags = flags;
}
//--------------------------------------------------------------------------------
void BufferDX11::SetMiscFlags( UINT flags )
{
	m_DesiredDesc.MiscFlags = flags;
}
//--------------------------------------------------------------------------------
void BufferDX11::SetStructureByteStride( UINT stride )
{
	m_DesiredDesc.StructureByteStride = stride;
}
//--------------------------------------------------------------------------------
void* BufferDX11::Map()
{
	return( 0 );
}
//--------------------------------------------------------------------------------
void BufferDX11::UnMap()
{
}
//--------------------------------------------------------------------------------
ID3D11Resource*	BufferDX11::GetResource()
{
	return( m_pBuffer.Get() );
}
//--------------------------------------------------------------------------------
UINT BufferDX11::GetEvictionPriority()
{
	UINT priority = 0;

	if ( m_pBuffer )
		priority = m_pBuffer->GetEvictionPriority();

	return( priority );
}
//--------------------------------------------------------------------------------
void BufferDX11::SetEvictionPriority( UINT EvictionPriority )
{
	if ( m_pBuffer )
		m_pBuffer->SetEvictionPriority( EvictionPriority );
}
//--------------------------------------------------------------------------------


text:
<?php
namespace Application\Form\Type;

use Symfony\Component\Form\FormBuilderInterface;
use Application\Form\Extension\MoneyToLocalizedStringTransformer;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

use Symfony\Component\Form\Extension\Core\Type\MoneyType as BaseType;

class MoneyType extends BaseType
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $attr = $builder->getAttribute('sonata_admin');
        if (isset($attr['class'])){
            $attr['class'] .= ' money';
            $builder->setAttribute('sonata_admin', $attr);
        }

        $builder->addViewTransformer(new MoneyToLocalizedStringTransformer(
            $options['precision'],
            $options['grouping'],
            null,
            $options['divisor']
        ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'decorator' => null,
            'precision' => 2,
            'grouping'  => true,
            'divisor'   => 1,
            'currency'  => 'EUR',
            'compound'  => false,
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        parent::buildView($view, $form, $options);
        if (isset($options['decorator'])){
            $view->vars['money_pattern'] = $options['decorator']($view->vars['money_pattern']);
        }
    }

}


text:
package org.sagebionetworks.web.client.widget.doi;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import java.util.List;
import java.util.Optional;
import org.gwtbootstrap3.client.ui.Button;
import org.gwtbootstrap3.client.ui.FormGroup;
import org.gwtbootstrap3.client.ui.Heading;
import org.gwtbootstrap3.client.ui.IntegerBox;
import org.gwtbootstrap3.client.ui.ListBox;
import org.gwtbootstrap3.client.ui.Modal;
import org.gwtbootstrap3.client.ui.TextArea;
import org.gwtbootstrap3.client.ui.html.Div;
import org.sagebionetworks.repo.model.Entity;
import org.sagebionetworks.repo.model.EntityTypeUtils;
import org.sagebionetworks.repo.model.VersionInfo;
import org.sagebionetworks.repo.model.Versionable;
import org.sagebionetworks.repo.model.doi.v2.DoiResourceTypeGeneral;
import org.sagebionetworks.repo.model.table.Table;
import org.sagebionetworks.web.client.widget.HelpWidget;

public class CreateOrUpdateDoiModalViewImpl
  implements CreateOrUpdateDoiModalView {

  public interface CreateOrUpdateDoiModalViewImplUiBinder
    extends UiBinder<Widget, CreateOrUpdateDoiModalViewImpl> {}

  private CreateOrUpdateDoiModalView.Presenter presenter;
  private Widget widget;
  private Entity entity;

  // A select box can't have a null value, so we use -1 to represent selecting a null version
  private static final long NO_VERSION = -1L;

  @UiField
  Button mintDoiButton;

  @UiField
  Div doiEditorDiv;

  @UiField
  TextArea creatorsField;

  @UiField
  TextArea titlesField;

  @UiField
  ListBox resourceTypeGeneralSelect;

  @UiField
  IntegerBox publicationYearField;

  @UiField
  Modal doiModal;

  @UiField
  Heading title;

  @UiField
  Button cancelButton;

  @UiField
  Div jobTrackingWidget;

  @UiField
  Div synAlert;

  @UiField
  Div doiOverwriteWarning;

  @UiField
  FormGroup versionForm;

  @UiField
  ListBox versionSelection;

  @UiField
  HelpWidget versionHelpBox;

  @Inject
  public CreateOrUpdateDoiModalViewImpl(
    CreateOrUpdateDoiModalViewImplUiBinder binder
  ) {
    this.widget = binder.createAndBindUi(this);
    jobTrackingWidget.setVisible(false);
    mintDoiButton.addClickHandler(event -> presenter.onSaveDoi());
    mintDoiButton.setEnabled(true);
    cancelButton.addClickHandler(event -> doiModal.hide());
    versionSelection.addChangeHandler(event -> {
      Long version = Long.valueOf(versionSelection.getSelectedValue());
      if (version == NO_VERSION) {
        presenter.onVersionChange(Optional.empty());
      } else {
        presenter.onVersionChange(Optional.of(version));
      }
    });

    initializeResourceTypeGeneralSelect();
  }

  private void initializeResourceTypeGeneralSelect() {
    // SWC-4445
    // initialize the resource type general select field by adding typical types to the top and adding a
    // separator.
    resourceTypeGeneralSelect.addItem(DoiResourceTypeGeneral.Dataset.name());
    resourceTypeGeneralSelect.addItem(DoiResourceTypeGeneral.Collection.name());
    resourceTypeGeneralSelect.addItem("────────────────────");
    resourceTypeGeneralSelect
      .getElement()
      .getElementsByTagName("option")
      .getItem(2)
      .setAttribute("disabled", "disabled");
    for (DoiResourceTypeGeneral rtg : DoiResourceTypeGeneral.values()) {
      if (
        !rtg.equals(DoiResourceTypeGeneral.Dataset) &&
        !rtg.equals(DoiResourceTypeGeneral.Collection)
      ) {
        resourceTypeGeneralSelect.addItem(rtg.name());
      }
    }
  }

  @Override
  public void reset() {
    this.setIsLoading(true);
    creatorsField.clear();
    titlesField.clear();
    resourceTypeGeneralSelect.setTitle(DoiResourceTypeGeneral.Dataset.name());
    versionForm.setVisible(this.entity instanceof Versionable);
    String entityTypeDisplay = EntityTypeUtils.getDisplayName(
      EntityTypeUtils.getEntityTypeForClass(this.entity.getClass())
    );
    String versionHelpMarkdown =
      "A DOI can be associated with a specific version of this " +
      entityTypeDisplay +
      ".\n\n" +
      "Versioned DOIs will link to the specified version of the " +
      entityTypeDisplay +
      ".\n\n" +
      "A DOI without a version will always link to the newest version of this " +
      entityTypeDisplay +
      ", so the data that someone retrieves using the DOI may change over time.";
    if (entity instanceof Table) {
      versionHelpMarkdown +=
        "\n\nTo create a DOI that will always link to the current set of data in the " +
        entityTypeDisplay +
        ", create a new version and mint a DOI for that version.";
    }
    versionHelpBox.setHelpMarkdown(versionHelpMarkdown);
    publicationYearField.reset();
    mintDoiButton.setEnabled(true);
  }

  @Override
  public void setIsLoading(boolean isLoading) {
    jobTrackingWidget.setVisible(isLoading);
    mintDoiButton.setEnabled(!isLoading);
    versionSelection.setEnabled(!isLoading);
    creatorsField.setEnabled(!isLoading);
    resourceTypeGeneralSelect.setEnabled(!isLoading);
    titlesField.setEnabled(!isLoading);
    publicationYearField.setEnabled(!isLoading);
  }

  @Override
  public void setVersions(
    List<VersionInfo> versions,
    Optional<Long> selectedVersion
  ) {
    versionSelection.clear();
    versionSelection.addItem("No version", String.valueOf(NO_VERSION));
    int selectedIndex = 0;
    for (int i = 0; i < versions.size(); i++) {
      VersionInfo version = versions.get(i);
      String label = "Version " + version.getVersionNumber();
      if (!version.getVersionLabel().isEmpty()) {
        label += " / " + version.getVersionLabel();
      }
      versionSelection.addItem(
        label,
        String.valueOf(version.getVersionNumber())
      );
      if (
        selectedVersion.isPresent() &&
        selectedVersion.get().equals(version.getVersionNumber())
      ) {
        selectedIndex = i + 1; // add one to offset the "Unversioned" entry before we iterate
      }
    }
    versionSelection.setSelectedIndex(selectedIndex);
  }

  @Override
  public void setPresenter(Presenter presenter) {
    this.presenter = presenter;
  }

  @Override
  public String getCreators() {
    return creatorsField.getText();
  }

  @Override
  public void setCreators(String creators) {
    creatorsField.setText(creators);
  }

  @Override
  public String getTitles() {
    return titlesField.getText();
  }

  @Override
  public void setTitles(String titles) {
    titlesField.setText(titles);
  }

  @Override
  public String getResourceTypeGeneral() {
    return resourceTypeGeneralSelect.getSelectedValue();
  }

  @Override
  public void setResourceTypeGeneral(String resourceTypeGeneral) {
    for (int i = 0; i < resourceTypeGeneralSelect.getItemCount(); i++) {
      if (resourceTypeGeneral.equals(resourceTypeGeneralSelect.getValue(i))) {
        resourceTypeGeneralSelect.setSelectedIndex(i);
        break;
      }
    }
  }

  @Override
  public Long getPublicationYear() {
    return Long.valueOf(publicationYearField.getText());
  }

  @Override
  public void setPublicationYear(Long publicationYear) {
    publicationYearField.setText(publicationYear.toString());
  }

  @Override
  public Widget asWidget() {
    return widget;
  }

  @Override
  public void setOverwriteWarningVisible(boolean visible) {
    doiOverwriteWarning.setVisible(visible);
  }

  @Override
  public void setEntity(Entity entity) {
    this.entity = entity;
  }

  @Override
  public void show() {
    doiModal.show();
  }

  @Override
  public void hide() {
    doiModal.hide();
  }

  @Override
  public void setModalTitle(String newTitle) {
    title.setText(newTitle);
  }

  @Override
  public void setJobTrackingWidget(IsWidget w) {
    jobTrackingWidget.clear();
    jobTrackingWidget.add(w);
  }

  @Override
  public void setSynAlert(IsWidget w) {
    synAlert.clear();
    synAlert.add(w);
  }
}


text:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Dirichlet Process on A Hugo website by Sang</title>
    <link>https://ecosang.github.io/tags/dirichlet-process/</link>
    <description>Recent content in Dirichlet Process on A Hugo website by Sang</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <managingEditor>ham9@purdue.edu (Sang woo Ham)</managingEditor>
    <webMaster>ham9@purdue.edu (Sang woo Ham)</webMaster>
    <lastBuildDate>Fri, 14 Jul 2017 19:14:12 -0400</lastBuildDate>

	<atom:link href="https://ecosang.github.io/tags/dirichlet-process/index.xml" rel="self" type="application/rss+xml" />

    <item>
      <title>Dirichlet process with Stan</title>
      <link>https://ecosang.github.io/blog/study/dirichlet-process-with-stan/</link>
      <pubDate>Fri, 14 Jul 2017 19:14:12 -0400</pubDate>
      <author>ham9@purdue.edu (Sang woo Ham)</author>
      <guid>https://ecosang.github.io/blog/study/dirichlet-process-with-stan/</guid>
      <description>In this posting, we will mimic Dirichlet process based Gaussian mixture model using Stan. Since Stan doesn't provide the Dirichlet Process prior, we mimic it via finite mixture model. The stick-breaking process is achived inside of the stan code, and in my personal view, this method is faily similar to one that we can find in PyMC3 manual link.
First, I will generate data which consists of 3 mixture of normal distributions.</description>
    </item>

  </channel>
</rss>


text:
//SHW080926

#include "stdafx.h"
#include "OEM.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////////
void debug_printf(char *fmt,...)
{
	static char szLine[512] = "";

    va_list ap;
    char szTemp[256];
    va_start(ap, fmt);
    vsprintf(szTemp, fmt, ap);
    strcat(szLine, szTemp);

	int len = strlen(szLine);
	if (len >= 2 && strcmp(szLine + len - 2, "\r\n") == 0)
	{
		szLine[len - 2] = 0;
		OutputDebugStringA(szLine);
		szLine[0] = 0;
	}

    va_end(ap);
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
class CCommSerial
{
public:
	CCommSerial();
	virtual ~CCommSerial();

	BOOL Open(int nPort, DWORD dwBaudrate);
	void Close();

	int SendData(BYTE *pBuf, int nSize, DWORD dwTimeOut);
	int RecvData(BYTE *pBuf, int nSize, DWORD dwTimeOut);

private:
	HANDLE m_hComm;
};

CCommSerial::CCommSerial()
{
	m_hComm = INVALID_HANDLE_VALUE;
}

CCommSerial::~CCommSerial()
{
	Close();
}

BOOL CCommSerial::Open(int nPort, DWORD dwBaudrate)
{
	Close();

	char szComName[20];
	sprintf( szComName, "COM%d", nPort );

	m_hComm = CreateFileA( szComName, GENERIC_READ | GENERIC_WRITE,0,
		NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL );
	if(m_hComm == INVALID_HANDLE_VALUE)
		return FALSE;

	PurgeComm(m_hComm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ;

	//
	DCB dcb;
	memset(&dcb, 0, sizeof(DCB));

	dcb.DCBlength = sizeof(DCB) ;

	dcb.BaudRate = dwBaudrate;
	dcb.fBinary = TRUE;
	dcb.fParity = FALSE;

	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;

	dcb.fAbortOnError = TRUE;

	if(!SetCommState( m_hComm, &dcb ))
		return FALSE;

	//
	COMMTIMEOUTS to;
	to.ReadIntervalTimeout = 0;
	to.ReadTotalTimeoutMultiplier = 0;
	to.ReadTotalTimeoutConstant = 16;
	to.WriteTotalTimeoutMultiplier = 0;
	to.WriteTotalTimeoutConstant = 16;

	if(!SetCommTimeouts(m_hComm, &to))
		return FALSE;

	return TRUE;
}

void CCommSerial::Close()
{
	if ( m_hComm != INVALID_HANDLE_VALUE )
	{
		CloseHandle(m_hComm) ;
		m_hComm = INVALID_HANDLE_VALUE;
	}
}

int CCommSerial::SendData(BYTE *pBuf, int nSize, DWORD dwTimeOut)
{
	int nReqSize = nSize;
	int nWrittenSize = 0;

	DWORD dwStartTime = GetTickCount();
	while (nSize && GetTickCount() - dwStartTime < dwTimeOut)
	{
		DWORD nCurrentWrittenSize;
		BOOL bWrite = WriteFile(m_hComm, pBuf, nSize, &nCurrentWrittenSize, NULL);

		if (!bWrite)
			break;
		FlushFileBuffers( m_hComm );
		pBuf += nCurrentWrittenSize;
		nSize -= nCurrentWrittenSize;
		nWrittenSize += nCurrentWrittenSize;

		if (nCurrentWrittenSize)
			dwStartTime = GetTickCount();

		comm_percent = (double)nWrittenSize / (double)nReqSize * 100.0;
		ui_polling();
	}

	return nWrittenSize;
}

int CCommSerial::RecvData(BYTE *pBuf, int nSize, DWORD dwTimeOut)
{
	int nReqSize = nSize;
	int nReadSize = 0;

	DWORD dwStartTime = GetTickCount();
	while (nSize && GetTickCount() - dwStartTime < dwTimeOut)
	{
		DWORD nCurrentReadSize;
		BOOL bRead = ReadFile(m_hComm, pBuf, nSize, &nCurrentReadSize, NULL);

		if (!bRead)
			break;
		pBuf += nCurrentReadSize;
		nSize -= nCurrentReadSize;
		nReadSize += nCurrentReadSize;

		if (nCurrentReadSize)
			dwStartTime = GetTickCount();

		comm_percent = (double)nReadSize / (double)nReqSize * 100.0;
		ui_polling();
	}

	return nReadSize;
}

/************************************************************************/
/*      CCommUSB definition                                             */
/************************************************************************/
CCommUSB::CCommUSB()
{
	m_hDevice = INVALID_HANDLE_VALUE;
}

CCommUSB::~CCommUSB()
{
	Close();
}

//////////////////////////////////////////////////////////////////////////
#define USB_DEVICE_MARK 0x55
BOOL _commex_check_usb()
{
	if( oem_usb_internal_check() < 0 )
		return FALSE;

	if(gwLastAck == NACK_INFO)
		return FALSE;

	return USB_DEVICE_MARK == gwLastAckParam;
}

BOOL CCommUSB::SCSI_Open()
{
	TCHAR dosDrive[8] = _T("C:");
	TCHAR ddkDrive[8] = _T("\\\\.\\C:");
	UINT DriveType;

// 	TCHAR szVolume[256];

	for (TCHAR Drive=_T('C');Drive<=_T('Z');Drive++)
	{
		dosDrive[0] = Drive;
		DriveType= GetDriveType(dosDrive);

		if(DriveType==DRIVE_REMOVABLE || DriveType==DRIVE_CDROM)//
		{
			ddkDrive[4] = Drive;
// 			if( !GetVolumeInformation(dosDrive, szVolume, 255, NULL,
// 				NULL, NULL, NULL, 0 ) ){
// 				continue;
// 			}

			m_hDevice = CreateFile(ddkDrive,
				GENERIC_WRITE | GENERIC_READ,
				FILE_SHARE_READ | FILE_SHARE_WRITE,
				NULL,
				OPEN_EXISTING,
				0,
				NULL);

			if(m_hDevice==INVALID_HANDLE_VALUE) continue;
			if(_commex_check_usb())
			{
				return TRUE;
			}
		}
	}

	return FALSE;
}

void CCommUSB::SCSI_Close()
{
	if (m_hDevice != INVALID_HANDLE_VALUE)
		CloseHandle(m_hDevice);
	m_hDevice = INVALID_HANDLE_VALUE;
}

#define USB_BLOCK_SIZE 65536

int CCommUSB::SCSI_Operation(BYTE *pBuf, int nSize, DWORD dwTimeOut, BOOL bRead)
{
	BYTE* pDataTemp = (BYTE*)pBuf;
	int nSizeTemp = nSize, nSizeReal;

	while(nSizeTemp)
	{
		nSizeReal = nSizeTemp;
		if (nSizeReal > USB_BLOCK_SIZE)
			nSizeReal = USB_BLOCK_SIZE;

		if (!OperationInternal(pDataTemp, nSizeReal, dwTimeOut, bRead) == nSizeReal)
			break;

		pDataTemp += nSizeReal;
		nSizeTemp -= nSizeReal;
	}

	if (nSizeTemp == 0)
		return nSize;
	return 0;
}

int CCommUSB::OperationInternal(BYTE *pBuf, int nSize, DWORD dwTimeOut, BOOL bRead)
{
	if (m_hDevice == INVALID_HANDLE_VALUE)
		return FALSE;

    SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER sptdwb;
    ZeroMemory(&sptdwb, sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER));

    sptdwb.sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
    sptdwb.sptd.PathId = 0;
    sptdwb.sptd.TargetId = 1;
    sptdwb.sptd.Lun = 0;
    sptdwb.sptd.CdbLength = CDB10GENERIC_LENGTH;
    sptdwb.sptd.SenseInfoLength = 0;
    sptdwb.sptd.DataIn = bRead ? SCSI_IOCTL_DATA_IN : SCSI_IOCTL_DATA_OUT;
    sptdwb.sptd.DataTransferLength = nSize;

    sptdwb.sptd.TimeOutValue = dwTimeOut / 1000;
    sptdwb.sptd.DataBuffer = pBuf;
    sptdwb.sptd.SenseInfoOffset = offsetof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER,ucSenseBuf);

	sptdwb.sptd.Cdb[0] = 0xEF;
	sptdwb.sptd.Cdb[1] = bRead ? 0xFF : 0xFE;

    DWORD length = sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER);
	DWORD returned = 0;

	if (!DeviceIoControl(m_hDevice,
		IOCTL_SCSI_PASS_THROUGH_DIRECT,
		&sptdwb,
		length,
		&sptdwb,
		length,
		&returned,
		NULL) ||
		returned < offsetof(SCSI_PASS_THROUGH_DIRECT, ScsiStatus) + sizeof (sptdwb.sptd.ScsiStatus) ||
		sptdwb.sptd.ScsiStatus != 0 /*||
		returned < offsetof(SCSI_PASS_THROUGH_DIRECT, DataTransferLength) + sizeof (sptdwb.sptd.DataTransferLength)*/)
		return 0;

	return sptdwb.sptd.DataTransferLength;
}

//////////////////////////////////////////////////////////////////////////
BOOL CCommUSB::Open()
{
	return SCSI_Open();
}

void CCommUSB::Close()
{
	SCSI_Close();
}

int CCommUSB::SendData(BYTE *pBuf, int nSize, DWORD dwTimeOut)
{
	return SCSI_Operation(pBuf, nSize, dwTimeOut, FALSE);
}

int CCommUSB::RecvData(BYTE *pBuf, int nSize, DWORD dwTimeOut)
{
	return SCSI_Operation(pBuf, nSize, dwTimeOut, TRUE);
}

/************************************************************************/
/*       global function definitions                                    */
/************************************************************************/
CCommSerial comm_serial;
CCommUSB	comm_usb;
double		comm_percent = 0.0;
int			gn_comm_type = 0;

int comm_send(BYTE* pbuf, int nsize, int ntimeout)
{
	if( gn_comm_type == COMM_MODE_SERIAL )
		return comm_serial.SendData(pbuf, nsize, ntimeout);
	else if( gn_comm_type == COMM_MODE_USB )
		return comm_usb.SendData( pbuf, nsize, ntimeout );
	return 0;
}

int comm_recv(BYTE* pbuf, int nsize, int ntimeout)
{
	if( gn_comm_type == COMM_MODE_SERIAL )
		return comm_serial.RecvData(pbuf, nsize, ntimeout);
	else if( gn_comm_type == COMM_MODE_USB )
		return comm_usb.RecvData( pbuf, nsize, ntimeout );
	return 0;
}

BOOL comm_open_serial(int nComNumber, int nComBaudRate)
{
	comm_close();

	gn_comm_type = COMM_MODE_SERIAL;
	if(!comm_serial.Open(nComNumber, nComBaudRate))
	{
		comm_close();
		return FALSE;
	}

	return TRUE;
}

BOOL comm_open_usb(){
	comm_close();
	gn_comm_type = COMM_MODE_USB;
	return comm_usb.Open();
}

void comm_close()
{
	if( gn_comm_type == COMM_MODE_SERIAL )
		comm_serial.Close();
	else if( gn_comm_type == COMM_MODE_USB )
		comm_usb.Close();
}


text:
using System;
using System.Diagnostics.Contracts;

/// <averagemary>
///   Extensions for Tuples
/// </averagemary>
public static partial class TupleExtensions
{
  public static double? Average<T1>(this Tuple<T1> value, Func<object, int?> func)
  {
    Contract.Requires<ArgumentNullException>(value != null);
    Contract.Requires<ArgumentNullException>(func != null);

    return func(value.Item1);
  }

  public static double? Average<T1, T2>(this Tuple<T1, T2> value, Func<object, int?> func)
  {
    Contract.Requires<ArgumentNullException>(value != null);
    Contract.Requires<ArgumentNullException>(func != null);

    double? average = value.Sum(func)/(double?) value.Count();
    return average;
  }

  public static double? Average<T1, T2, T3>(this Tuple<T1, T2, T3> value, Func<object, int?> func)
  {
    Contract.Requires<ArgumentNullException>(value != null);
    Contract.Requires<ArgumentNullException>(func != null);

    double? average = value.Sum(func)/(double?) value.Count();
    return average;
  }

  public static double? Average<T1, T2, T3, T4>(this Tuple<T1, T2, T3, T4> value, Func<object, int?> func)
  {
    Contract.Requires<ArgumentNullException>(value != null);
    Contract.Requires<ArgumentNullException>(func != null);

    double? average = value.Sum(func)/(double?) value.Count();
    return average;
  }

  public static double? Average<T1, T2, T3, T4, T5>(this Tuple<T1, T2, T3, T4, T5> value, Func<object, int?> func)
  {
    Contract.Requires<ArgumentNullException>(value != null);
    Contract.Requires<ArgumentNullException>(func != null);

    double? average = value.Sum(func)/(double?) value.Count();
    return average;
  }

  public static double? Average<T1, T2, T3, T4, T5, T6>(this Tuple<T1, T2, T3, T4, T5, T6> value,
    Func<object, int?> func)
  {
    Contract.Requires<ArgumentNullException>(value != null);
    Contract.Requires<ArgumentNullException>(func != null);

    double? average = value.Sum(func)/(double?) value.Count();
    return average;
  }

  public static double? Average<T1, T2, T3, T4, T5, T6, T7>(this Tuple<T1, T2, T3, T4, T5, T6, T7> value,
    Func<object, int?> func)
  {
    Contract.Requires<ArgumentNullException>(value != null);
    Contract.Requires<ArgumentNullException>(func != null);

    double? average = value.Sum(func)/(double?) value.Count();
    return average;
  }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@android:id/background"
    android:drawable="@drawable/seekbg"
    />
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/seekprogress"
        ></item>

</layer-list >


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>0.9.9 API documenation: std_based_type.hpp File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>

<link href="search/search.css" rel="stylesheet" type="text/css"/>

<link href="doxygen.css" rel="stylesheet" type="text/css" />

0.9.9 API documenation

Main Page

Modules

Files

          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)"
               onblur="searchBox.OnSearchFieldFocus(false)"
               onkeyup="searchBox.OnSearchFieldChange(event)"/>

File List

G:

Source

G-Truc

glm

glm

gtx

Typedefs

std_based_type.hpp File Reference

GLM_GTX_std_based_type
More...

Go to the source code of this file.

Typedefs

typedef tvec1< std::size_t, defaultp > 	size1

 	Vector type based of one std::size_t component.  More...

typedef tvec1< std::size_t, defaultp > 	size1_t

 	Vector type based of one std::size_t component.  More...

typedef tvec2< std::size_t, defaultp > 	size2

 	Vector type based of two std::size_t components.  More...

typedef tvec2< std::size_t, defaultp > 	size2_t

 	Vector type based of two std::size_t components.  More...

typedef tvec3< std::size_t, defaultp > 	size3

 	Vector type based of three std::size_t components.  More...

typedef tvec3< std::size_t, defaultp > 	size3_t

 	Vector type based of three std::size_t components.  More...

typedef tvec4< std::size_t, defaultp > 	size4

 	Vector type based of four std::size_t components.  More...

typedef tvec4< std::size_t, defaultp > 	size4_t

 	Vector type based of four std::size_t components.  More...

Detailed Description

GLM_GTX_std_based_type

See also

GLM Core (dependence)

GLM_GTX_extented_min_max (dependence)

Definition in file std_based_type.hpp.

<address class="footer">
Generated by

 1.8.10
</address>


text:
#if !defined(_Logging_h_)
#define _Logging_h_

#include <string>
#include <assert.h>
#include <stdarg.h>

/*!
 * \brief Base logger class.
 *
 * There are two types of logging levels that are used by this class. First
 * there is the filter level. Any log message that is assigned a level
 * higher than the current filter level is discarded. Secondly there is the
 * current output level. Log messages that do not have their own level
 * use the current output level to determine if they should be shown or
 * not.
 *
 * The two methods setFilterLevel() and setOutputLevel() set the filter
 * and default output logging levels, respectively. There are corresponding
 * getter methods as well. Both the filter and output levels are
 * initialized to #INFO during object construction.
 *
 * Most use of the logger classes is expected to be through the Log
 * class. It provides static logging methods that call through to a global
 * singleton logger instance. There is also a Log::SetOutputLevel utility
 * class that makes it extremely easiy to temporarily change the default
 * output logging level.
 *
 * Of all the overloaded log() methods in this class, none of them are
 * really expected to be reimplemented by subclasses. Instead, there is
 * the single protected _log() method that takes a simple string pointer.
 * The other log methods all wind up calling _log(), so it provides a
 * single point to override. In fact, _log() is pure virtual, so subclasses
 * must implement it.
 *
 * \see Log
 */
class Logger
{
public:
	//! \brief Logging levels.
	enum log_level_t
	{
		URGENT = 0,	//!< The lowest level, for messages that must always be logged.
		ERROR,		//!< For fatal error messages.
		WARNING,	//!< For non-fatal warning messages.
		INFO,		//!< The normal log level, for status messages.
		INFO2,		//!< For verbose status messages.
		DEBUG,		//!< For internal reporting.
		DEBUG2		//!< Highest log level; verbose debug logging.
	};

public:
	//! \brief Default constructor.
	Logger() : m_filter(INFO), m_level(INFO) {}

	//! \brief Destructor.
	virtual ~Logger() {}

	//! \name Logging levels
	//@{
	//! \brief Changes the logging level to \a level.
	inline void setFilterLevel(log_level_t level) { m_filter = level; }

	//! \brief Returns the current logging filter level.
	inline log_level_t getFilterLevel() const { return m_filter; }

	//! \brief Changes the logging output level to \a level.
	inline void setOutputLevel(log_level_t level) { m_level = level; }

	//! \brief Returns the current logging output level.
	inline log_level_t getOutputLevel() const { return m_level; }
	//@}

	//! \name Logging
	//@{
	//! \brief Log with format.
	virtual void log(const char * fmt, ...);

	//! \brief Log a string object.
	virtual void log(const std::string & msg) { log(msg.c_str()); }

	//! \brief Log with format at a specific output level.
	virtual void log(log_level_t level, const char * fmt, ...);

	//! \brief Log a string output at a specific output level.
	virtual void log(log_level_t level, const std::string & msg) { log(level, msg.c_str()); }

	//! \brief Log with format using an argument list.
	virtual void log(const char * fmt, va_list args);

	//! \brief Log with format using an argument with a specific output level.
	virtual void log(log_level_t level, const char * fmt, va_list args);
	//@}

protected:
	log_level_t m_filter;	//!< The current logging filter level.
	log_level_t m_level;	//!< The current log output level.

protected:
	//! \brief The base pure virtual logging function implemented by subclasses.
	virtual void _log(const char * msg)=0;
};

/*!
 * \brief Wraps a set of static functions for easy global logging access.
 *
 * This class has a set of static methods that make it easy to access a global
 * logger instance without having to worry about extern symbols. It does this
 * by keeping a static member variable pointing at the singleton logger instance,
 * which is set with the setLogger() static method.
 *
 * There is also an inner utility class called SetOutputLevel that uses
 * C++ scoping rules to temporarily change the output logging level. When the
 * SetOutputLevel instance falls out of scope the output level is restored
 * to the previous value.
 */
class Log
{
public:
	//! \name Singleton logger access
	//@{
	//! \brief Returns the current global logger singleton.
	static inline Logger * getLogger() { return s_logger; }

	//! \brief Sets the global logger singleton instance.
	static inline void setLogger(Logger * logger) { s_logger = logger; }
	//@}

	//! \name Logging
	//@{
	//! \brief Log with format.
	static void log(const char * fmt, ...);

	//! \brief Log a string object.
	static void log(const std::string & msg);

	//! \brief Log with format at a specific output level.
	static void log(Logger::log_level_t level, const char * fmt, ...);

	//! \brief Log a string output at a specific output level.
	static void log(Logger::log_level_t level, const std::string & msg);
	//@}

protected:
	static Logger * s_logger;	//!< The single global logger instance.

public:
	/*!
	 * \brief Utility class to temporarily change the logging output level.
	 *
	 * This class will change the current logging output level of a given
	 * logger instance. Then when it falls out of scope it will set the
	 * level back to what it was originally.
	 *
	 * Use like this:
	 * \code
	 *		// output level is some value here
	 *		{
	 *			Log::SetOutputLevel leveler(Logger::DEBUG);
	 *			// now output level is DEBUG
	 *			Log::log("my debug message 1");
	 *			Log::log("my debug message 2");
	 *		}
	 *		// output level is restored to previous value
	 * \endcode
	 */
	class SetOutputLevel
	{
	public:
		//! \brief Default constructor.
		//!
		//! Saves the current logging output level of the global logger,
		//! as managed by the Log class, and sets the new level to \a level.
		SetOutputLevel(Logger::log_level_t level)
		:	m_logger(Log::getLogger()), m_saved(Logger::INFO)
		{
			assert(m_logger);
			m_saved = m_logger->getOutputLevel();
			m_logger->setOutputLevel(level);
		}

		//! \brief Constructor.
		//!
		//! Saves the current logging output level of \a logger and sets
		//! the new level to \a level.
		SetOutputLevel(Logger * logger, Logger::log_level_t level)
		:	m_logger(logger), m_saved(logger->getOutputLevel())
		{
			assert(m_logger);
			m_logger->setOutputLevel(level);
		}

		//! \brief Destructor.
		//!
		//! Restores the saved logging output level.
		~SetOutputLevel()
		{
			m_logger->setOutputLevel(m_saved);
		}

	protected:
		Logger * m_logger;	//!< The logger instance we're controlling.
		Logger::log_level_t m_saved;	//!< Original logging output level.
	};

};

/*!
 * \brief Simple logger that writes to stdout.
 */
class StdoutLogger : public Logger
{
protected:
	//! \brief Logs the message to stdout.
	virtual void _log(const char * msg);
};

#endif // _Logging_h_


text:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
	<string name="service">物流服务</string>
	<string name="company">物流企业</string>
	<string name="garden">园区</string>
	<string name="information">资讯</string>
	<string name="myLogistics">我的物流</string>

	<string name="openWindow">Open New Window</string>
	<string name="newWindow">New Window</string>
</resources>


text:
import { expect } from 'chai';
import { spec } from 'modules/adriverBidAdapter.js';
import { newBidder } from 'src/adapters/bidderFactory.js';
import * as bidderFactory from 'src/adapters/bidderFactory.js';
import { auctionManager } from 'src/auctionManager.js';
const ENDPOINT = 'https://pb.adriver.ru/cgi-bin/bid.cgi';

describe('adriverAdapter', function () {
  const adapter = newBidder(spec);

  describe('inherited functions', function () {
    it('exists and is a function', function () {
      expect(adapter.callBids).to.exist.and.to.be.a('function');
    });
  });

  describe('isBidRequestValid', function () {
    let bid = {
      'bidder': 'adriver',
      'params': {
        'placementId': '55:test_placement',
        'siteid': 'testSiteID'
      },
      'adUnitCode': 'adunit-code',
      'sizes': [[300, 250], [300, 600], [300, 250]],
      'bidId': '30b31c1838de1e',
      'bidderRequestId': '22edbae2733bf6',
      'auctionId': '1d1a030790a475',
    };

    it('should return true when required params found', function () {
      expect(spec.isBidRequestValid(bid)).to.equal(true);
    });
  });

  describe('buildRequests', function () {
    let getAdUnitsStub;
    const floor = 3;

    let bidRequests = [
      {
        'bidder': 'adriver',
        'params': {
          'placementId': '55:test_placement',
          'siteid': 'testSiteID',
          'dealid': 'dealidTest'
        },
        'adUnitCode': 'adunit-code',
        'sizes': [[300, 250], [300, 600], [300, 250]],
        'bidId': '30b31c1838de1e',
        'bidderRequestId': '22edbae2733bf6',
        'auctionId': '1d1a030790a475',
        'transactionId': '04f2659e-c005-4eb1-a57c-fa93145e3843'
      }
    ];

    let floorTestData = {
      'currency': 'USD',
      'floor': floor
    };
    bidRequests[0].getFloor = _ => {
      return floorTestData;
    };

    beforeEach(function() {
      getAdUnitsStub = sinon.stub(auctionManager, 'getAdUnits').callsFake(function() {
        return [];
      });
    });

    afterEach(function() {
      getAdUnitsStub.restore();
    });

    it('should exist currency', function () {
      const request = spec.buildRequests(bidRequests);
      const payload = JSON.parse(request.data);

      expect(payload.cur).to.exist;
    });

    it('should exist at', function () {
      const request = spec.buildRequests(bidRequests);
      const payload = JSON.parse(request.data);

      expect(payload.at).to.exist;
      expect(payload.at).to.deep.equal(1);
    });

    it('should parse imp', function () {
      const request = spec.buildRequests(bidRequests);
      const payload = JSON.parse(request.data);

      expect(payload.imp[0]).to.exist;
      expect(payload.imp[0].id).to.deep.equal('55:test_placement');

      expect(payload.imp[0].ext).to.exist;
      expect(payload.imp[0].ext.query).to.deep.equal('bn=15&custom=111=' + '30b31c1838de1e');

      expect(payload.imp[0].banner).to.exist;
      expect(payload.imp[0].banner.w).to.deep.equal(300);
      expect(payload.imp[0].banner.h).to.deep.equal(250);
    });

    it('should parse pmp', function () {
      const request = spec.buildRequests(bidRequests);
      const payload = JSON.parse(request.data);

      expect(payload.imp[0].pmp).to.exist;

      expect(payload.imp[0].pmp.deals).to.exist;

      expect(payload.imp[0].pmp.deals[0].bidfloor).to.exist;
      expect(payload.imp[0].pmp.deals[0].bidfloor).to.deep.equal(3);

      expect(payload.imp[0].pmp.deals[0].bidfloorcur).to.exist;
      expect(payload.imp[0].pmp.deals[0].bidfloorcur).to.deep.equal('RUB');
    });

    it('sends bid request to ENDPOINT via POST', function () {
      const request = spec.buildRequests(bidRequests);
      expect(request.url).to.equal(ENDPOINT);
      expect(request.method).to.equal('POST');
    });
  });

  describe('interpretResponse', function () {
    let bfStub;
    before(function() {
      bfStub = sinon.stub(bidderFactory, 'getIabSubCategory');
    });

    after(function() {
      bfStub.restore();
    });

    let response = {
      'id': '221594457-1615288400-1-46-',
      'bidid': 'D8JW8XU8-L5m7qFMNQGs7i1gcuPvYMEDOKsktw6e9uLy5Eebo9HftVXb0VpKj4R2dXa93i6QmRhjextJVM4y1SqodMAh5vFOb_eVkHA',
      'seatbid': [{
        'bid': [{
          'id': '1',
          'impid': '/19968336/header-bid-tag-0',
          'price': 4.29,
          'h': 250,
          'w': 300,
          'adid': '7121351',
          'adomain': ['http://ikea.com'],
          'nurl': 'https://ad.adriver.ru/cgi-bin/erle.cgi?expid=D8JW8XU8-L5m7qFMNQGs7i1gcuPvYMEDOKsktw6e9uLy5Eebo9HftVXb0VpKj4R2dXa93i6QmRhjextJVM4y1SqodMAh5vFOb_eVkHA&bid=7121351&wprc=4.29&tuid=-1&custom=207=/19968336/header-bid-tag-0',
          'cid': '717570',
          'ext': '2c262a7058758d'
        }]
      }, {
        'bid': [{
          'id': '1',
          'impid': '/19968336/header-bid-tag-0',
          'price': 17.67,
          'h': 600,
          'w': 300,
          'adid': '7121369',
          'adomain': ['http://ikea.com'],
          'nurl': 'https://ad.adriver.ru/cgi-bin/erle.cgi?expid=DdtToXX5cpTaMMxrJSEsOsUIXt3WmC3jOvuNI5DguDrY8edFG60Jg1M-iMkVNKQ4OiAdHSLPJLQQXMUXZfI9VbjMoGCb-zzOTPiMpshI&bid=7121369&wprc=17.67&tuid=-1&custom=207=/19968336/header-bid-tag-0',
          'cid': '717570',
          'ext': '2c262a7058758d'
        }]
      }],
      'cur': 'RUB'
    };

    it('should get correct bid response', function () {
      let expectedResponse = [
        {
          requestId: '2c262a7058758d',
          cpm: 4.29,
          width: 300,
          height: 250,
          creativeId: '/19968336/header-bid-tag-0',
          currency: 'RUB',
          netRevenue: true,
          ttl: 3000,
          meta: {
            advertiserDomains: ['http://ikea.com']
          },
          ad: '<IFRAME SRC="https://ad.adriver.ru/cgi-bin/erle.cgi?expid=D8JW8XU8-L5m7qFMNQGs7i1gcuPvYMEDOKsktw6e9uLy5Eebo9HftVXb0VpKj4R2dXa93i6QmRhjextJVM4y1SqodMAh5vFOb_eVkHA&bid=7121351&wprc=4.29&tuid=-1&custom=207=/19968336/header-bid-tag-0" FRAMEBORDER="0" SCROLLING="no" MARGINHEIGHT="0" MARGINWIDTH="0" TOPMARGIN="0" LEFTMARGIN="0" ALLOWTRANSPARENCY="true" STYLE ="WIDTH:300px; HEIGHT:250px"></IFRAME>'
        }
      ];
      let bidderRequest = {
        bids: [{
          bidId: '3db3773286ee59',
          adUnitCode: 'code'
        }]
      };
      let result = spec.interpretResponse({ body: response }, {bidderRequest});
      expect(Object.keys(result[0])).to.have.members(Object.keys(expectedResponse[0]));
    });

    it('handles nobid responses', function () {
      let response = {
        'version': '0.0.1',
        'tags': [{
          'uuid': '84ab500420319d',
          'tag_id': 5976557,
          'auction_id': '297492697822162468',
          'nobid': true
        }]
      };
      let bidderRequest;

      let result = spec.interpretResponse({ body: response }, {bidderRequest});
      expect(result.length).to.equal(0);
    });
  });

  describe('function _getFloor', function () {
    let bidRequests = [
      {
        bidder: 'adriver',
        params: {
          placementId: '55:test_placement',
          siteid: 'testSiteID',
          dealid: 'dealidTest',
        },
        adUnitCode: 'adunit-code',
        sizes: [[300, 250], [300, 600], [300, 250]],
        bidId: '30b31c1838de1e',
        bidderRequestId: '22edbae2733bf6',
        auctionId: '1d1a030790a475',
        transactionId: '04f2659e-c005-4eb1-a57c-fa93145e3843'
      }
    ];

    const floorTestData = {
      'currency': 'RUB',
      'floor': 1.50
    };

    const bitRequestStandard = JSON.parse(JSON.stringify(bidRequests));

    bitRequestStandard[0].getFloor = () => {
      return floorTestData;
    };

    it('valid BidRequests', function () {
      const request = spec.buildRequests(bitRequestStandard);
      const payload = JSON.parse(request.data);

      expect(typeof bitRequestStandard[0].getFloor).to.equal('function');
      expect(payload.imp[0].bidfloor).to.equal(1.50);
      expect(payload.imp[0].bidfloorcur).to.equal('RUB');
    });

    const bitRequestEmptyCurrency = JSON.parse(JSON.stringify(bidRequests));

    const floorTestDataEmptyCurrency = {
      'currency': 'RUB',
      'floor': 1.50
    };

    bitRequestEmptyCurrency[0].getFloor = () => {
      return floorTestDataEmptyCurrency;
    };

    it('empty currency', function () {
      const request = spec.buildRequests(bitRequestEmptyCurrency);
      const payload = JSON.parse(request.data);

      expect(payload.imp[0].bidfloor).to.equal(1.50);
      expect(payload.imp[0].bidfloorcur).to.equal('RUB');
    });

    const bitRequestFloorNull = JSON.parse(JSON.stringify(bidRequests));

    const floorTestDataFloorNull = {
      'currency': '',
      'floor': null
    };

    bitRequestFloorNull[0].getFloor = () => {
      return floorTestDataFloorNull;
    };

    it('empty floor', function () {
      const request = spec.buildRequests(bitRequestFloorNull);
      const payload = JSON.parse(request.data);

      expect(payload.imp[0].bidfloor).to.equal(0);
    });

    const bitRequestGetFloorNotFunction = JSON.parse(JSON.stringify(bidRequests));

    bitRequestGetFloorNotFunction[0].getFloor = 0;

    it('bid.getFloor is not a function', function () {
      const request = spec.buildRequests(bitRequestGetFloorNotFunction);
      const payload = JSON.parse(request.data);

      expect(payload.imp[0].bidfloor).to.equal(0);
      expect(payload.imp[0].bidfloorcur).to.equal('RUB');
    });

    const bitRequestGetFloorBySized = JSON.parse(JSON.stringify(bidRequests));

    bitRequestGetFloorBySized[0].getFloor = (requestParams = {currency: 'USD', mediaType: '*', size: '*'}) => {
      if (requestParams.size.length === 2 && requestParams.size[0] === 300 && requestParams.size[1] === 250) {
        return {
          'currency': 'RUB',
          'floor': 3.33
        }
      } else {
        return {}
      }
    };

    it('bid.getFloor get size', function () {
      const request = spec.buildRequests(bitRequestGetFloorBySized);
      const payload = JSON.parse(request.data);

      expect(payload.imp[0].bidfloor).to.equal(3.33);
      expect(payload.imp[0].bidfloorcur).to.equal('RUB');
      expect(payload.imp[0].bidfloorcur).to.equal('RUB');
    });
  });
});


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Ann. Mo. bot. Gdn 23: 129-150 (1936)

#### Original name
Candida desidiosa Cif. & Redaelli, 1935

### Remarks
null


text:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- content -->
    <item >
        <shape android:shape="rectangle" >
            <solid android:color="#fff" />

        </shape>
    </item>
    <!-- bottom shadow -->
    <item android:top="130px">
        <shape android:shape="rectangle" >
            <!-- from top to bottom -->
            <gradient
                android:angle="270"
                android:endColor="#fff"
                android:startColor="#fff" />
        </shape>
    </item>
</layer-list>


text:
from django.shortcuts import _get_queryset

__all__ = ('LinkResolver', 'ModelLinkResolver',)

class LinkResolver(object):
    def resolve(self, object_id):
        return RuntimeError('You have to implement this, use ModelLinkResolver for simple models')

    def handles(self, obj):
        return False

    def get_object_id(self, obj):
        raise NotImplementedError('get_object_id needs to be implemented by subclasses.')

class ModelLinkResolver(LinkResolver):
    def __init__(self, model_or_qs):
        self.queryset = _get_queryset(model_or_qs)
        self.model = self.queryset.model

    def resolve(self, object_id):
        return self.queryset.get(pk=object_id).get_absolute_url()

    def handles(self, obj):
        return isinstance(obj, self.model)

    def get_object_id(self, obj):
        return obj.pk


text:
{% trans %}Preview{% endtrans %}:

{% trans %}Economic Activities{% endtrans %}

            {% trans %}Rings{% endtrans %}


text:
package nl.esciencecenter.xenon;

/**
 * Signals that an adaptor is unable to connect to a resource due to version problems.
 *
 * @author Jason Maassen <J.Maassen@esciencecenter.nl>
 * @version 1.0
 * @since 1.0
 */
public class IncompatibleVersionException extends XenonException {

    private static final long serialVersionUID = 1L;

    public IncompatibleVersionException(String adaptorName, String message, Throwable t) {
        super(adaptorName, message, t);
    }

    public IncompatibleVersionException(String adaptorName, String message) {
        super(adaptorName, message);
    }
}


text:
var express = require('express');

// set up
// get all the tools we need
var session  = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
var favicon = require('serve-favicon');
// for password reset
var nodemailer = require('nodemailer');
var async = require('async');
var crypto = require('crypto');
var path = require('path');

var multer  = require('multer')
var upload = multer({ dest: 'public/assets/img/avatar/' })

var app = express();

var server = app.listen(process.env.PORT || 8090,function () {
  // body...
  console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
var io = require('socket.io').listen(server);

var mysql=require('mysql');
var bodyParser=require('body-parser');
var middlewareBodyParser=bodyParser.urlencoded({extended:false})
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'yala_netlob_development',
  multipleStatements: true
});

connection.connect();

// var port     = process.env.PORT || 8090;

var passport = require('passport');
var config = require('./config/auth.js');
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var GoogleStrategy = require('passport-google-oauth').Strategy;

var flash    = require('connect-flash');

var path = require('path');
var formidable = require('formidable');
var fs = require('fs');

// configuration

require('./config/passport')(passport);

var path = require('path'),
    fs = require('fs'),
    formidable = require('formidable'),
    readChunk = require('read-chunk'),
    fileType = require('file-type');

    app.use('/uploads', express.static('uploads'));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// app.use(express.bodyParser({uploadDir:'./uploads'}));

// required for passport
app.use(session({
  secret: 'twinkletwinklelittlestarhowiwonderwhatyouare',
  resave: true,
  saveUninitialized: true
 } )); // session secret
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

// routes
require('./routes/index.js')(app, passport);

var index = require('./routes/index');
var users = require('./routes/users');
var friends = require('./routes/friends');
var groups = require('./routes/groups');
var orders = require('./routes/orders');
var new_order = require('./routes/new_order');
var order_details = require('./routes/order_details');
var profile = require('./routes/profile');

app.use('/user',profile);
app.use('/home', index);
app.use('/users', users);
app.use('/groups', groups);
app.use('/friends', friends);
app.use('/orders', orders);
app.use('/new_order', new_order);
app.use('/order_details', order_details);

var currentConnections = [];
var sockets = [];
var rooms = {};
var flag = false;

io.on('connection', function(socket){
  console.log(socket.id);
  currentConnections.push({socket: [socket.id]});
  sockets.push(socket);
  console.log(currentConnections);
  socket.on('join',function(data){
      console.log("DATA.USER_ID ", data.user_id);
      console.log("currentConnections ", currentConnections);

      for (var i = currentConnections.length - 1; i >= 0; i--) {
        // console.log(data.user_id);
        // console.log(currentConnections[i].user_id);

        if (currentConnections[i].user_id == data.user_id) {
          // console.log("HIIIII")
          // console.log("FLAG BEFORE CHANGE ",flag);
          // currentConnections[i].socket = socket.id;
          console.log("test ", currentConnections[i].socket.length);
          currentConnections[i].socket[currentConnections[i].socket.length] = socket.id;
          flag = true;
          // console.log("FLAG AFTER CHANGE ",flag);
          break;
        }
      }

      console.log("FLAG AFTER FOR ",flag);
      if (flag != true) {
        // console.log("INSIDE IF FLAG != TRUE. FLAG = ", flag);
        currentConnections[currentConnections.length-1].user_id = data.user_id;
      }
      console.log("join***"+data.user_id);
      flag = false;
      //currentConnections[currentConnections.length-1].user_id = data.user_id;
      // console.log(currentConnections);
      // console.log(currentConnections[socket.id]);
      // users[data.user_id]=socket;
      // console.log("USERS LENGTH => ", users.length);
      // console.log(users[data.user_id]);
  });

  var room = "";

  socket.on('send notification',function(data){
    console.log("in notification send");
    room = data.room;
    console.log(data.owner_id);

    for (var i = currentConnections.length - 1; i >= 0; i--) {
      // for (var j = sockets.length - 1; j >= 0; j--) {
        // clients[j];
        console.log('HELLLLLLLLLLLLOOOOOOOOOOOOOOOO ITS MEEEEEEEEEEEEEEEEEEEEEEEEE\N\N\N');
        // console.log(data.user_id);
        // console.log(currentConnections[i].user_id);
        // console.log(sockets[j].id);
        if (currentConnections[i].user_id == data.owner_id) {
          console.log("HEHEHEHEHEHEHE");
          // console.log("HIIIII")
          // console.log("FLAG BEFORE CHANGE ",flag);
          // currentConnections[i].socket = socket.id;
          // console.log("BEFORE",socket.id);
          // socket.id = sockets[j].id;
          // console.log("AFTER",socket.id);
          // delete sockets[j];
          // console.log(sockets[j].id);
          // console.log(room);
          // socket.join(room);
          // console.log("FLAG AFTER CHANGE ",flag);
          rooms[data.room] = [currentConnections[i].socket];
          console.log(rooms);
          break;
        }
      // }
    }

    console.log("ROOOM NAAAAAAAAAME", room);
    // console.log(io.sockets.clients(room));
    // console.log(data.user_id);
    for (var i = currentConnections.length - 1; i >= 0; i--) {
      if(currentConnections[i].user_id == data.user_id) {
        console.log(currentConnections[i]);
        socket.broadcast.to(currentConnections[i].socket[currentConnections[i].socket.length - 1]).emit('notification',{owner_id:data.owner_id,owner_name:data.owner_name,msg:data.msg,room:data.room});
      }
    }

    // socket.join(room);

    // socket.on('join order', function (data) {
    //   io.sockets.in("room-"+2).emit('connectToRoom', "You are in room no. "+2);
    // });
  });

  socket.on('join order',function (data) {
    // body...
    console.log("JOIN ROOOOOOOOOM ", data.room);
    // for (var i = currentConnections.length - 1; i >= 0; i--) {
    //   if (currentConnections[i].user_id == data.user_id) {
    //     rooms[data.room][rooms[data.room].length] = (currentConnections[i].socket);
    //     console.log(rooms);
    //     for (var i = 0; i <= rooms[data.room].length - 1; i++) {
    //       console.log(rooms[data.room][i]);
    //       socket.broadcast.to(rooms[data.room][i]).emit('notification',{msg:data.msg,room:data.room});
    //       // socket.broadcast.to(rooms[data.room][i]).emit('friend joined',{msg:'your friend has joined your order'});
    //     }
    //   }
    var owner_id = data.owner_id;
    console.log(owner_id);
    var order_name = data.room;
    var user_id = data.user_id;
    var owner_name = data.owner_name;
    var user_name = data.user_name;
    var order_id = data.order_id;
    // var order_name = data.order_name;
    console.log("\n\nNOTIFIED ",user_id);
    console.log("NOTIFIER ",owner_name);
    console.log("\n\n\n\n\nMy order id is ",order_id);
    console.log("ORDER NAME ",order_name);

    // connection.query("select notifications.order_id AS order_id from notifications where notifier_name=? AND notified_id=? AND type=? AND order_name = ?",[owner_name,user_id,0,order_name],function(error,rows){
        // if(error){
          // console.log(error);
        // }else{
          // console.log(rows);
          // console.log(rows[0].order_name);
          // for(var i=0;i<req.body.user_id.length;i++){
            // console.log(req.body.user_id[i]);
    connection.query("insert into orders_users (order_id,user_id) values (?,?)",[order_id,user_id],function(error,rows){
              if(error){
                console.log(error);
                for (var i = currentConnections.length - 1; i >= 0; i--) {
                        console.log("INSIDE for",rows[0].owner_id);
                        if(currentConnections[i].user_id == rows[0].user_id) {
                          console.log("OWNER ID ", data.owner_id);
                          console.log(currentConnections[i]);
                          socket.broadcast.to(currentConnections[i].socket[currentConnections[i].socket.length - 1]).emit('invalid join',{user_name:data.user_name,user_id:data.user_id,owner_id:data.owner_id,msg:data.msg,room:data.room});
                        }
                      }
              }
              else{
                  connection.query("select owner_id, order_id, order_name from orders where order_id=?",[order_id],function(error,rows){
                    if(error){
                      console.log(error);
                    }else{
                      console.log("NOTIFY SELECT ",rows);
                      console.log(owner_name);
                      connection.query("INSERT INTO notifications (notifier_id, notified_id, order_id, notifier_name, type, order_name) values(?,?,?,?,?,?)",[user_id,rows[0].owner_id,rows[0].order_id,user_name,1,rows[0].order_name],function (err,rows) {
                        if (err) {
                          console.log(err);
                        }
                      else {
                        console.log("added");
                        console.log("TEST TEST");
                      }
                    });
                      for (var i = currentConnections.length - 1; i >= 0; i--) {
                        console.log("INSIDE for",rows[0].owner_id);
                        if(currentConnections[i].user_id == rows[0].owner_id) {
                          console.log("OWNER ID ", data.owner_id);
                          console.log(currentConnections[i]);
                          socket.broadcast.to(currentConnections[i].socket[currentConnections[i].socket.length - 1]).emit('friend joined',{user_name:data.user_name,user_id:data.user_id,owner_id:data.owner_id,msg:data.msg,room:data.room});
                        }
                      }
                    }
                  });
                }});
    // }});

    // socket.join(data.room);
    // io.of('/').in(data.room).clients(function(error, clients){
    //   if (error) throw error;
    //     console.log("\n\n\n\n\n\n\nCLIENTS ",clients,"\n\n\n\n\n\n\n\n\n"); // => [Anw2LatarvGVVXEIAAAD]
    //   });
    // console.log(io.sockets.clients(data.room));
    // io.sockets.in(data.room).emit('message', 'what is going on, party people?');
  });

  socket.on('cancel', function (data) {
    // body...
    console.log('inside cancel')
    var owner_id = data.owner_id;
    var order_id = data.order_id;
    var order_users = data.order_users;
    var owner_name = data.owner_name;
    console.log("\n\n\n\n\n\n\n\n\n\n\n\n",owner_id);
    console.log(order_id);
    console.log(order_users);
    console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n");

    var notify = [];

    for (var i = 0; i < order_users.length; i++) {
      notify.push([owner_id,order_users[i],order_id,owner_name,3]);
    }

    // console.log(rows[1]);
    for (var i = 0; i < order_users.length; i++) {
      for (var j = 0; j < currentConnections.length; j++) {
        if (order_users[i] == currentConnections[j].user_id) {
          console.log("FOUND ",currentConnections[j].user_id);
          socket.broadcast.to(currentConnections[i].socket[currentConnections[i].socket.length - 1]).emit('cancel',{owner_name:owner_name,order_id:order_id});
        }
      }
    }

    console.log(notify);

    // var query = "INSERT INTO notifications (notifier_id,notified_id,order_id,notifier_name,type) VALUES (?,?,?,?,?)"
    // connection.query(query,[notify],function (err,row) {
    //   // body...
    //   if (err) {
    //     console.log(err);
    //   }
    //   else {
    //     console.log("done");
    //   }
    // });

    // var query = "SELECT user_name FROM users WHERE user_id = ?;SELECT * FROM orders_users WHERE order_id = ?";
    // connection.query(query,[owner_id,order_id],function (err,rows) {
    //   // body...
    //   if(err) {
    //     console.log(err);
    //   }
    //   else {
    //     if (rows[1]) {
    //       console.log(rows[1]);
    //       for (var i = rows[1].length - 1; i >= 0; i--) {
    //         for (var j = currentConnections.length - 1; j >= 0; j--) {
    //           if (rows[1][i].user_id == currentConnections[j].user_id) {
    //             socket.broadcast.to(currentConnections[i].socket[currentConnections[i].socket.length - 1]).emit('cancel',{owner_name:rows[0].user_name,order_id:rows[1].order_id});
    //           }
    //         }
    //       }
    //     }
    //     else {
    //       console.log("empty")
    //     }
    //   }
    // });
  });

  console.log('connection')
  socket.on('groups', function (data) {
    console.log("GROUPS")
  });

  socket.on('disconnect', function() {
    delete currentConnections[socket.id];
  });
});

// catch 404 error and forward error status handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, error only being provided in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

// app.listen(port,function () {
//   console.log("App is running on port " + port);
// });


text:
var gulp = require('gulp'),                                     // Gulp JS
    gulpif = require('gulp-if'),                                // Gulp if module
    gutil = require('gulp-util'),                               // Gulp util module
    rename = require('gulp-rename'),
    cache = require('gulp-cached'),                             // Gulp cache module
    path = require('path'),
    notify = require('gulp-notify'),                            // Plugin for notify
    projectConfig = require('../../projectConfig');
    notifyConfig = projectConfig.notifyConfig,                  // Notify config
    modifyDate = require('../helpers/modifyDateFormatter');     // Date formatter for notify

// Move svg to dev directory
module.exports = function() {

    if (projectConfig.useSVG) {

        return gulp.src('./markup/static/images/svg/*.svg')
        .pipe(cache('move-svg'))
        .pipe(rename(function(path) {
            path.dirname = '';
        }))
        .on('error', notify.onError(function (error) {
            return notifyConfig.errorMessage(error);
        }))
        .pipe(gulp.dest('./dev/static/img/svg'))
        .pipe(
            gulpif(notifyConfig.useNotify,
                notify({
                    onLast: true,
                    sound: notifyConfig.sounds.onSuccess,
                    title: notifyConfig.title,
                    message: 'Svg\'ve been moved to dev\n'+ notifyConfig.taskFinishedText +'<%= options.date %>',
                    templateOptions: {
                        date: modifyDate.getTimeOfModify()
                    }
                })
            )
        );

    } else {
        gutil.log('!SVG is not used!');
        cb(null);
    }
};


text:
package org.dvare.test.dataobjects;

import java.util.Date;

public class BetweenOperation {
    public Integer Variable1;
    public Float Variable2;
    public Date Variable3;
    public Date Variable4;

    public Integer getVariable1() {
        return Variable1;
    }

    public void setVariable1(Integer variable1) {
        Variable1 = variable1;
    }

    public Float getVariable2() {
        return Variable2;
    }

    public void setVariable2(Float variable2) {
        Variable2 = variable2;
    }

    public Date getVariable3() {
        return Variable3;
    }

    public void setVariable3(Date variable3) {
        Variable3 = variable3;
    }

    public Date getVariable4() {
        return Variable4;
    }

    public void setVariable4(Date variable4) {
        Variable4 = variable4;
    }
}


text:
<?php

require_once "./common.php";

use Duck\Queue;

$queue = new Queue($json->token, $json->project_id, 'suck');
$queue->setTimeout(5);

$nextQueue = new Queue($json->token, $json->project_id, 'suckle');
$nextQueue->setTimeout(5);

do {

    list($duck, $id) = $queue->dequeue();

    if (!$duck) {
        sleep(1);
        continue;
    }

    try {
        $mustSucceed($duck, 'beSucked', 'was sucked');
        $queue->ack($id);
        $nextQueue->enqueue($duck);

    } catch (\Exception $e) {
        // Empty spaces, what are we living for?
    }

} while(true);


text:
package org.conqat.engine.core.driver.instance;

import org.conqat.lib.commons.clone.DeepCloneException;

/**
 * Interface for objects providing a value. In this context these are either
 * {@link InstanceOutput}s or {@link InstanceAttribute}s.
 *

 * To avoid interference of different consumers in their usage of the returned
 * value object, the value object is usually cloned before returning it.
 *

 * However, as this is potentially expensive, the cloning is only performed if
 * we know for sure that the value will be requested again. Thus, each potential
 * consumer has to register during its initialization, so the exact number of
 * {@link #consumeValue()} calls is known at execution time (and thus the
 * necessary number of clone operations).
 *
 * @author $Author: kinnen $
 * @version $Rev: 41751 $
 * @ConQAT.Rating GREEN Hash: 4C5AB0F97F3F486FB40E1EEBBDD52319
 */
public interface IValueProvider {

	/**
	 * Returns whether a value is available from this provider. False can mean
	 * either that the associated object has not yet been executed or that there
	 * were errors during execution preventing the generation of the value.
	 */
	public boolean hasValue();

	/**
	 * Increases the consumer counter by one. Each call to this method allows
	 * exactly one call to the {@link #consumeValue()} method later. This method
	 * must be called before the actual value was calculated.
	 */
	public void addConsumer();

	/**
	 * Decreases the consumer counter by one. This can be used instead of
	 * {@link #consumeValue()}.
	 */
	public void removeConsumer();

	/**
	 * Returns the value (or a clone of the value) available from this provider,
	 * or null if no value is present (also see {@link #hasValue()}). A null
	 * value could also indicate that the provided value is null (thus the
	 * underlying processor returned null for some reason). This method may only
	 * be called as often as {@link #addConsumer()} was called before (taking
	 * calls to {@link #removeConsumer()} into account, too).
	 *
	 * @throws DeepCloneException
	 *             if the cloning process caused any problems.
	 */
	public Object consumeValue() throws DeepCloneException;
}


text:
PermissionControl.deleteAllRoleValues = DELETE jforum_role_values \
	FROM jforum_role_values rv, jforum_roles r \
	WHERE r.role_id = rv.role_id \
	AND r.group_id = ?


text:
package com.scrumkin.api.exceptions;

/**
 * Created by Matija on 26.4.2014.
 */
public class UserStoryDoesNotExist extends Exception {
}


text:
class RemoveUserAssociationsFromDefenceRequests < ActiveRecord::Migration
  def change
    remove_index :defence_requests, column: :solicitor_id
    remove_column :defence_requests, :solicitor_id, :integer
    remove_column :defence_requests, :cco_id, :integer
  end
end