<s> """<STR_LIT>""" <EOL> import sys <EOL> import json <EOL> import time <EOL> import itertools <EOL> import subprocess <EOL> from collections import defaultdict <EOL> def oldestAllowedContribution ( max_buckets ) : <EOL> return makeBucketId ( time . time ( ) ) - ( max_buckets * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> ) <EOL> def makeBucketId ( timestamp ) : <EOL> tm = time . gmtime ( timestamp ) <EOL> week_day = ( tm . tm_wday + <NUM_LIT:1> ) % <NUM_LIT:7> <EOL> timestamp -= week_day * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> timestamp = int ( timestamp ) <EOL> timestamp -= tm . tm_hour * <NUM_LIT> * <NUM_LIT> <EOL> timestamp -= tm . tm_min * <NUM_LIT> <EOL> timestamp -= tm . tm_sec <EOL> return timestamp + <NUM_LIT:7> * <NUM_LIT> * <NUM_LIT> + <NUM_LIT:1> <EOL> def sanitize ( instream ) : <EOL> """<STR_LIT>""" <EOL> for line in instream : <EOL> line = line . strip ( ) <EOL> if not line : <EOL> continue <EOL> parts = line . split ( ) <EOL> yield int ( parts [ <NUM_LIT:0> ] ) , int ( parts [ <NUM_LIT:1> ] ) , "<STR_LIT:U+0020>" . join ( parts [ <NUM_LIT:2> : ] ) <EOL> def make_impact_data ( contributions , max_buckets = None ) : <EOL> """<STR_LIT>""" <EOL> authors = { } <EOL> author_starts = { } <EOL> author_ends = { } <EOL> author_ids = { } <EOL> author_id_gen = itertools . count ( ) <EOL> buckets = defaultdict ( lambda : [ ] ) <EOL> contributions = sorted ( contributions ) <EOL> if contributions and max_buckets is not None : <EOL> oldest_allowed = oldestAllowedContribution ( max_buckets ) <EOL> else : <EOL> oldest_allowed = float ( '<STR_LIT>' ) <EOL> for date , size , author in contributions : <EOL> if author not in author_ids : <EOL> author_id = next ( author_id_gen ) <EOL> author_ids [ author ] = author_id <EOL> authors [ author_id ] = { <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT:name>" : author } <EOL> else : <EOL> author_id = author_ids [ author ] <EOL> bucket_id = makeBucketId ( date ) <EOL> if author_id not in author_starts : <EOL> author_starts [ author_id ] = bucket_id <EOL> author_ends [ author_id ] = bucket_id <EOL> if date < oldest_allowed : <EOL> continue <EOL> author = authors [ author_id ] <EOL> author [ "<STR_LIT>" ] += size <EOL> buckets [ bucket_id ] . append ( ( date , author_id , size ) ) <EOL> fixed_buckets = [ ] <EOL> for bucket_id in sorted ( buckets . keys ( ) ) : <EOL> bucket = buckets [ bucket_id ] <EOL> bucket . sort ( ) <EOL> bucket_date = bucket_id <EOL> contributions = defaultdict ( lambda : <NUM_LIT:0> ) <EOL> bucket_size = <NUM_LIT:0> <EOL> for _ , author_id , size in bucket : <EOL> contributions [ author_id ] += size <EOL> bucket_size += size <EOL> contributions_sorted = sorted ( <EOL> ( ( size , author_id ) <EOL> for author_id , size in contributions . iteritems ( ) ) , <EOL> reverse = True ) <EOL> for author_id in authors . iterkeys ( ) : <EOL> if author_id not in contributions and ( <EOL> author_starts [ author_id ] <= bucket_id <= <EOL> author_ends [ author_id ] ) : <EOL> contributions_sorted . append ( ( <NUM_LIT:0> , author_id ) ) <EOL> fixed_buckets . append ( { <EOL> "<STR_LIT:date>" : bucket_date , <EOL> "<STR_LIT>" : <EOL> [ { "<STR_LIT>" : author_id , "<STR_LIT:size>" : size } <EOL> for size , author_id in contributions_sorted ] } ) <EOL> fixed_authors = [ ] <EOL> for author_id , data in sorted ( <EOL> authors . iteritems ( ) , key = lambda item : item [ <NUM_LIT:1> ] [ "<STR_LIT:name>" ] . lower ( ) ) : <EOL> if data [ "<STR_LIT>" ] == <NUM_LIT:0> : <EOL> continue <EOL> fixed_authors . append ( { <EOL> "<STR_LIT>" : author_id , <EOL> "<STR_LIT:name>" : data [ "<STR_LIT:name>" ] , <EOL> "<STR_LIT>" : data [ "<STR_LIT>" ] } ) <EOL> return { "<STR_LIT>" : fixed_buckets , <EOL> "<STR_LIT>" : fixed_authors } <EOL> def main ( ) : <EOL> max_buckets = None <EOL> js_var_name = "<STR_LIT>" <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> js_var_name = sys . argv [ <NUM_LIT:1> ] <EOL> if len ( sys . argv ) > <NUM_LIT:2> : <EOL> max_buckets = int ( sys . argv [ <NUM_LIT:2> ] ) <EOL> print "<STR_LIT>" % js_var_name , json . dumps ( <EOL> make_impact_data ( <EOL> sanitize ( sys . stdin ) , <EOL> max_buckets = max_buckets ) , <EOL> indent = <NUM_LIT:2> ) , "<STR_LIT:;>" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
