Skip to content
This repository has been archived by the owner on Jan 19, 2018. It is now read-only.

Bugs with "like" stats in posts-by-user.py #13

Open
sbonds opened this issue Sep 17, 2015 · 0 comments
Open

Bugs with "like" stats in posts-by-user.py #13

sbonds opened this issue Sep 17, 2015 · 0 comments

Comments

@sbonds
Copy link

sbonds commented Sep 17, 2015

Here's a patch to fix some issues where users who have ONLY done likes and have not posted messages create problems for the stat script:

With the current script we get KeyError: u'<id>', but when that was fixed some of the division by zero errors came out. This uses a small function to allow anything divided by zero to be zero-- probably close enough for this purpose.


diff --git a/stat/posts-by-user.py b/stat/posts-by-user.py
index b0bf462..054153e 100644
--- a/stat/posts-by-user.py
+++ b/stat/posts-by-user.py
@@ -6,6 +6,11 @@ sys.setdefaultencoding("utf-8")
 import json
 import datetime

+def divideWhereDivZeroIsZero(dividend,divisor):
+    try:
+        return dividend/divisor
+    except ZeroDivisionError:
+        return 0

 def main():
     """Usage: posts-by-user.py filename.json
@@ -45,18 +50,22 @@ Assumes filename.json is a JSON GroupMe transcript.

     }
     for id, stats in counts.items():
-        name = names[id]
+       try:
+               name = names[id]
+       except KeyError:
+               names[id] = 'UID ' + str(id)
+               name = names[id]
         count = stats['messages']
         like_given_count = stats['likes_given']
         like_received_count = stats['likes_received']
         output['messages'].append(u'{name}: messages: {count} ({msg_pct:.1f}%)'.format(
-            name=name, count=count, msg_pct=count/float(totalMessages) * 100,
+            name=name, count=count, msg_pct=divideWhereDivZeroIsZero(count,float(totalMessages) * 100),
         ))
         output['likes_received'].append(u'{name}: likes received: {like_count} ({like_pct:.1f} per message)'.format(
-            name=name, like_count=like_received_count, like_pct=like_received_count/float(count),
+            name=name, like_count=like_received_count, like_pct=divideWhereDivZeroIsZero(like_received_count,float(count)),
         ))
         output['likes_given'].append(u'{name}: likes given: {like_count} ({like_pct:.1f}%)'.format(
-            name=name, like_count=like_given_count, like_pct=like_given_count/float(totalLikes) * 100
+            name=name, like_count=like_given_count, like_pct=divideWhereDivZeroIsZero(like_given_count,float(totalLikes) * 100)
         ))
     for category, values in output.items():
         print '\n'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant