stackexchange-gilles

shortest_answers

Feb 9th, 2014
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env python
  2. import json, sys
  3.  
  4. def load_posts(filename):
  5.     dump = open(filename)
  6.     posts = json.load(dump)
  7.     dump.close()
  8.     return posts
  9.  
  10. def answers_by_length(posts):
  11.     answers = filter(lambda m: m['post_type']=='answer', posts)
  12.     answers.sort(key=lambda m: len(m['body']))
  13.     return answers
  14.  
  15. def format_answers(site_name, answers):
  16.     return [' * http://%s.stackexchange.com/a/%d (%d) [%d%s]' %
  17.             (site_name,
  18.              a['post_id'],
  19.              len(a['body']),
  20.              a['score'],
  21.              '*' * len(a['comments']) if a.has_key('comments') else '')
  22.             for a in answers]
  23.  
  24. if __name__ == '__main__':
  25.     site_name = sys.argv[1]
  26.     if site_name.endswith('.json'): site_name = site_name[:-5]
  27.     answers = answers_by_length(load_posts(sys.argv[1]))
  28.     json.dump(answers, open(site_name + '-answers.json', 'w'))
  29.     print '\n'.join(format_answers(site_name, answers))
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×