Skip to content

Instantly share code, notes, and snippets.

@gustavopinto
Created November 19, 2018 23:03
Show Gist options
  • Save gustavopinto/b06b44ec2d706f1760ac65312c8f11ef to your computer and use it in GitHub Desktop.
Save gustavopinto/b06b44ec2d706f1760ac65312c8f11ef to your computer and use it in GitHub Desktop.
sotorrent exercise
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The goal of this exercise is to understand how do the size of a SO code snipet changes
# during its own evolution, and in terms of the programming language used and the popularity
# of the SO question
# In case you do not have MySQLdb instaled:
# More details at: https://askubuntu.com/questions/989965
import MySQLdb as db
HOST = "200.239.79.1"
PORT = 3306
USER = "sotorrent"
PASSWORD = "12345"
DB = "sotorrent18_09"
def test_connection():
con = None
try:
con = db.Connection(host=HOST, port=PORT, user=USER, passwd=PASSWORD, db=DB)
con.query("SELECT VERSION()")
result = con.use_result()
print("MYSQL version: %s" % result.fetch_row()[0])
except Exception as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
finally:
con.close()
def size_of_code_blocks(question_id=21691202):
con = None
try:
con = db.Connection(host=HOST, port=PORT, user=USER, passwd=PASSWORD, db=DB)
cursor = con.cursor()
sql = """SELECT PostHistoryId, PostId, PostBlockTypeId, LineCount, Length, Content
FROM PostBlockVersion
WHERE PostId=21691202 and
PostBlockTypeId = {str1}
ORDER BY PostHistoryId ASC, LocalId ASC
LIMIT 100""".format(str1=question_id)
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
except Exception as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
finally:
con.close()
def selecting_questions_per_pl(pl):
con = None
try:
con = db.Connection(host=HOST, port=PORT, user=USER, passwd=PASSWORD, db=DB)
cursor = con.cursor()
sql = """SELECT Id #, AcceptedAnswerId, CreationDate, ClosedDate, Title, Body, Score, AnswerCount, Tags
FROM Posts
WHERE PostTypeId=1
AND tags like '%<java>%'
AND AcceptedAnswerId>0
ORDER BY Id
LIMIT 100"""
cursor.execute(sql)
ids = [item[0] for item in cursor.fetchall()]
print(ids)
except Exception as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
finally:
con.close()
selecting_questions_per_pl("java")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment