示例#1
0
def answers_with_max_upvotes(limit=20):
  answers = Answer.select().order_by(Answer.upvotes.desc()).limit(limit)

  print('Top %d Most upvoted answers - ' % limit)
  total_views = 0
  total_upvotes = 0
  writer = {}
  for answer in answers:
    u = ANSWER_URL.format(answer.question, answer.writer_uname).encode("UTF-8")
    print("{0} ({1}, {2}, {3})".format(u, answer.upvotes,
          answer.views, answer.views / answer.upvotes))
    total_upvotes += answer.upvotes
    total_views += answer.views
    writer[answer.writer_uname] = writer.get(answer.writer_uname, 0) + 1

  print("Total Views = {0}".format(total_views))
  print("Total Upvotes = {0}".format(total_upvotes))
  print("Average Views = {0}".format(total_views / limit))
  print("Average Upvotes = {0}".format(total_upvotes / limit))
  avg_up = (float(total_upvotes) / float(total_views)) * 100
  print("On an average %.2f viewers upvoted the answer" % avg_up)

  # Writer Stat
  with open('top_writers_2016.json', 'r') as fstream:
    writer_list = json.load(fstream)
  notw = 0
  for w in writer_list:
    if w['uname'] in writer:
      notw += 1
  print("{0} People on this list are Top Writers(2016)".format(notw))
  sorted_writer = sorted(writer.items(), key=operator.itemgetter(1),
                         reverse=True)
  print("Total number of unique writers is {0}".format(len(sorted_writer)))
  total_followers = 0
  total_answers = 0
  for tup in sorted_writer:
    profile = Profile.get(Profile.uname == tup[0])
    total_followers += int(profile.followers)
    total_answers += int(profile.total_answers)
  print("Average number of followers of each {0}".format(
    total_followers / len(sorted_writer)))
  print("Average number of answers written by each is {}".format(
    total_answers / len(sorted_writer)))

  # Plotting Graph
  figure = mpplt.figure(figsize=(10, 10))
  plt = figure.add_subplot(1, 1, 1)
  plt.set_title("Views vs Upvote")
  plt.plot([answer.views for answer in answers],
           [answer.upvotes for answer in answers],
           '.', color='green')
  plt.set_xlabel('Views')
  plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
  plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
  plt.set_xlim([0, 1500000])
  plt.set_ylim([10000, 25000])
  plt.set_ylabel('Upvotes')
  figure.savefig('view_upvote.png', facecolor='white', edgecolor='black')
示例#2
0
def gateway():
    if is_logged_in():
        return redirect(url_for("index"))
    err = ""
    if request.method == "POST":
        try:
            email = fix(request.form["email"], "@\.")
            username = fix(request.form["username"])
            password = fix(request.form["password"])
            confirm = fix(request.form["confirm"])
            if validate_email(email):
                if not Profile.already_exists(email=email):
                    if validate_username(username):
                        if not Profile.already_exists(username=username):
                            if password == confirm and password != "":
                                Profile.insert_profile(email, username,
                                                       hash_password(password))
                                return render_template("gateway.html",
                                                       title="Gateway")
                            else:
                                err = "Invalid Passwords Entered"
                        else:
                            err = "Username Already Exists"
                    else:
                        err = "Invalid Username Entered"
                else:
                    err = "Email Already Exists"
            else:
                err = "Invalid Email Entered"
        except Exception as e:
            email = fix(request.form["email"], "@\.")
            password = fix(request.form["password"])
            if email is not None and password is not None:
                if Profile.already_exists(email=email):
                    expected_pass = Profile.get_password_hash(email)
                    salt, pass_hash = str(expected_pass).split("$")
                    if expected_pass == str(hash_password(password, salt)):
                        session["email"] = email
                        session["username"] = Profile.get("username",
                                                          email=email)
                        return redirect(url_for("index"))
                    else:
                        err = "Incorrect Email or Password"
                else:
                    err = "Incorrect Email"
            else:
                err = "Fill in all blanks"

    reg = True if request.args.get("reg") == "true" else False
    return render_template("gateway.html",
                           title="Gateway",
                           indent=False,
                           reg=reg,
                           err=err)
示例#3
0
def answers_with_max_upvotes(limit=20):
    answers = Answer.select().order_by(Answer.upvotes.desc()).limit(limit)

    print("Top %d Most upvoted answers - " % limit)
    total_views = 0
    total_upvotes = 0
    writer = {}
    for answer in answers:
        u = ANSWER_URL.format(answer.question, answer.writer_uname).encode("UTF-8")
        print("{0} ({1}, {2}, {3})".format(u, answer.upvotes, answer.views, answer.views / answer.upvotes))
        total_upvotes += answer.upvotes
        total_views += answer.views
        writer[answer.writer_uname] = writer.get(answer.writer_uname, 0) + 1

    print("Total Views = {0}".format(total_views))
    print("Total Upvotes = {0}".format(total_upvotes))
    print("Average Views = {0}".format(total_views / limit))
    print("Average Upvotes = {0}".format(total_upvotes / limit))
    avg_up = (float(total_upvotes) / float(total_views)) * 100
    print("On an average %.2f viewers upvoted the answer" % avg_up)

    # Writer Stat
    with open("top_writers_2016.json", "r") as fstream:
        writer_list = json.load(fstream)
    notw = 0
    for w in writer_list:
        if w["uname"] in writer:
            notw += 1
    print("{0} People on this list are Top Writers(2016)".format(notw))
    sorted_writer = sorted(writer.items(), key=operator.itemgetter(1), reverse=True)
    print("Total number of unique writers is {0}".format(len(sorted_writer)))
    total_followers = 0
    total_answers = 0
    for tup in sorted_writer:
        profile = Profile.get(Profile.uname == tup[0])
        total_followers += int(profile.followers)
        total_answers += int(profile.total_answers)
    print("Average number of followers of each {0}".format(total_followers / len(sorted_writer)))
    print("Average number of answers written by each is {}".format(total_answers / len(sorted_writer)))

    # Plotting Graph
    figure = mpplt.figure(figsize=(10, 10))
    plt = figure.add_subplot(1, 1, 1)
    plt.set_title("Views vs Upvote")
    plt.plot([answer.views for answer in answers], [answer.upvotes for answer in answers], ".", color="green")
    plt.set_xlabel("Views")
    plt.ticklabel_format(style="sci", axis="x", scilimits=(0, 0))
    plt.ticklabel_format(style="sci", axis="y", scilimits=(0, 0))
    plt.set_xlim([0, 1500000])
    plt.set_ylim([10000, 25000])
    plt.set_ylabel("Upvotes")
    figure.savefig("view_upvote.png", facecolor="white", edgecolor="black")
def answers_with_max_upvotes(limit=20):
  answers = Answer.select().order_by(Answer.upvotes.desc()).limit(limit)

  print('Top %d Most upvoted answers - ' % limit)
  total_views = 0
  total_upvotes = 0
  writer = {}
  max_views = 0
  for answer in answers:
    u = ANSWER_URL.format(answer.question, answer.writer_uname).encode("UTF-8")
    print("{0} ({1}, {2}, {3})".format(u, answer.upvotes,
          answer.views, answer.views / answer.upvotes))
    total_upvotes += answer.upvotes
    total_views += answer.views
    writer[answer.writer_uname] = writer.get(answer.writer_uname, 0) + 1
    if answer.views > max_views :
      max_views = answer.views
      print "Max - ", u, answer.views

  print("Total Views = {0}".format(total_views))
  print("Total Upvotes = {0}".format(total_upvotes))
  print("Average Views = {0}".format(total_views / limit))
  print("Average Upvotes = {0}".format(total_upvotes / limit))
  avg_up = (float(total_upvotes) / float(total_views)) * 100
  print("On an average %.2f viewers upvoted the answer" % avg_up)

  # Writer Stat
  with open('top_writers_2016.json', 'r') as fstream:
    writer_list = json.load(fstream)
  notw = 0
  for w in writer_list:
    if w['uname'] in writer:
      notw += 1
  print("{0} People on this list are Top Writers(2016)".format(notw))
  sorted_writer = sorted(writer.items(), key=operator.itemgetter(1),
                         reverse=True)
  print sorted_writer[:10]
  print("Total number of unique writers is {0}".format(len(sorted_writer)))
  total_followers = 0
  total_answers = 0
  for tup in sorted_writer:
    profile = Profile.get(Profile.uname == tup[0])
    total_followers += int(profile.followers)
    total_answers += int(profile.total_answers)
  print("Average number of followers of each {0}".format(
    total_followers / len(sorted_writer)))
  print("Average number of answers written by each is {}".format(
    total_answers / len(sorted_writer)))

  # Plotting Graph
  figure = plt.figure(figsize=(10, 10))
  splt = figure.add_subplot(1, 1, 1)
  splt.set_title("Views vs Upvote")
  splt.plot([answer.views for answer in answers],
           [answer.upvotes for answer in answers],
           '.', color='green')
  splt.set_xlabel('Views')
  splt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
  splt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
  splt.set_xlim([0, 1500000])
  splt.set_ylim([10000, 25000])
  splt.set_ylabel('Upvotes')
  figure.tight_layout()
  figure.savefig('view_upvote.png', facecolor='white', edgecolor='black')