Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 40,
  6. "metadata": {
  7. "collapsed": false
  8. },
  9. "outputs": [],
  10. "source": [
  11. "import pymysql\n",
  12. "import datetime\n",
  13. "import pywikibot\n",
  14. "from ipywidgets import interact"
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 41,
  20. "metadata": {
  21. "collapsed": true
  22. },
  23. "outputs": [],
  24. "source": [
  25. "conn = pymysql.connect(host='paws-db', user='cscw', password='cscw')"
  26. ]
  27. },
  28. {
  29. "cell_type": "code",
  30. "execution_count": 31,
  31. "metadata": {
  32. "collapsed": false
  33. },
  34. "outputs": [],
  35. "source": [
  36. "def calculate_edit_hours(user, cursor):\n",
  37. " starttime = datetime.datetime.now()\n",
  38. " qstring = u'''SELECT rev_timestamp FROM enwiki_p.revision_userindex WHERE rev_user_text like \"'''+ user + u'''\";'''\n",
  39. " cursor.execute(qstring)\n",
  40. " results = cursor.fetchall()\n",
  41. " clean_results = map(lambda t: t[0], results)\n",
  42. " str_clean = [res.decode(\"utf-8\") for res in clean_results]\n",
  43. " timestamps = [pywikibot.Timestamp.fromtimestampformat(x) for x in str_clean]\n",
  44. " #print len(timestamps) \n",
  45. " edit_sessions = []\n",
  46. " curr_edit_session = []\n",
  47. "\n",
  48. " prev_timestamp = datetime.datetime(year=2001, month=1, day=1)\n",
  49. "\n",
  50. "\n",
  51. " for contrib in timestamps:\n",
  52. " curr_timestamp = contrib\n",
  53. "\n",
  54. " #if curr_timestamp > snapshot_timestamp: \n",
  55. " # break \n",
  56. " \n",
  57. " if curr_timestamp-prev_timestamp < datetime.timedelta(hours=1):\n",
  58. " curr_edit_session.append(curr_timestamp)\n",
  59. " prev_timestamp = curr_timestamp\n",
  60. "\n",
  61. " else:\n",
  62. " if curr_edit_session:\n",
  63. " edit_sessions.append(curr_edit_session)\n",
  64. " curr_edit_session = [curr_timestamp]\n",
  65. " prev_timestamp = curr_timestamp\n",
  66. "\n",
  67. " #finally have to add the curr_edit_session to list \n",
  68. " if curr_edit_session:\n",
  69. " print('hea')\n",
  70. " edit_sessions.append(curr_edit_session)\n",
  71. "\n",
  72. "\n",
  73. " #print len(edit_sessions) \n",
  74. " def session_length(edit_session):\n",
  75. " avg_time = datetime.timedelta(minutes=4, seconds=30)\n",
  76. " last = edit_session[-1]\n",
  77. " first = edit_session[0]\n",
  78. " span = last - first\n",
  79. " total = span + avg_time\n",
  80. " return total\n",
  81. "\n",
  82. " session_lengths = map(session_length, edit_sessions)\n",
  83. " second_lens = map(lambda td: td.total_seconds(), session_lengths)\n",
  84. " total_time = sum(second_lens)\n",
  85. "\n",
  86. " took = datetime.datetime.now() - starttime\n",
  87. " tooksecs = took.total_seconds()\n",
  88. " print('timestamps per second: ', len(timestamps)/float(tooksecs))\n",
  89. " #returning total hours \n",
  90. " return total_time / float(3600)\n"
  91. ]
  92. },
  93. {
  94. "cell_type": "code",
  95. "execution_count": 45,
  96. "metadata": {
  97. "collapsed": false
  98. },
  99. "outputs": [
  100. {
  101. "name": "stdout",
  102. "output_type": "stream",
  103. "text": [
  104. "hea\n",
  105. "timestamps per second: 21443.30095127309\n"
  106. ]
  107. }
  108. ],
  109. "source": [
  110. "@interact\n",
  111. "def calc(username=''):\n",
  112. " conn.ping(True)\n",
  113. " calculate_edit_hours(username, conn.cursor())"
  114. ]
  115. }
  116. ],
  117. "metadata": {
  118. "kernelspec": {
  119. "display_name": "Python 3",
  120. "language": "python",
  121. "name": "python3"
  122. },
  123. "language_info": {
  124. "codemirror_mode": {
  125. "name": "ipython",
  126. "version": 3
  127. },
  128. "file_extension": ".py",
  129. "mimetype": "text/x-python",
  130. "name": "python",
  131. "nbconvert_exporter": "python",
  132. "pygments_lexer": "ipython3",
  133. "version": "3.4.2"
  134. }
  135. },
  136. "nbformat": 4,
  137. "nbformat_minor": 0
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement