groupme-tools
groupme-tools copied to clipboard
Bugs with "like" stats in posts-by-user.py
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'