<s> import csv , StringIO , sys , urllib <EOL> class Logger : <EOL> def __init__ ( self , filename , buf = None ) : <EOL> self . log = open ( filename , '<STR_LIT:w>' ) <EOL> self . buf = buf <EOL> def flush ( self ) : <EOL> self . log . flush ( ) <EOL> if self . buf is not None : <EOL> self . buf . flush ( ) <EOL> def write ( self , message ) : <EOL> self . log . write ( message ) <EOL> self . log . flush ( ) <EOL> if self . buf is not None : <EOL> self . buf . write ( message ) <EOL> self . buf . flush ( ) <EOL> class Reader : <EOL> def __init__ ( self , buf , filename = None ) : <EOL> self . buf = buf <EOL> if filename is not None : <EOL> self . log = open ( filename , '<STR_LIT:w>' ) <EOL> else : <EOL> self . log = None <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return self . readline ( ) <EOL> def readline ( self ) : <EOL> line = self . buf . readline ( ) <EOL> if not line : <EOL> raise StopIteration <EOL> if self . log is not None : <EOL> self . log . write ( line ) <EOL> self . log . flush ( ) <EOL> return line <EOL> def output_results ( results , mvdelim = '<STR_LIT:\n>' , output = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> fields = set ( ) <EOL> for result in results : <EOL> for key in result . keys ( ) : <EOL> if ( isinstance ( result [ key ] , list ) ) : <EOL> result [ '<STR_LIT>' + key ] = encode_mv ( result [ key ] ) <EOL> result [ key ] = mvdelim . join ( result [ key ] ) <EOL> fields . update ( result . keys ( ) ) <EOL> fields = sorted ( list ( fields ) ) <EOL> writer = csv . DictWriter ( output , fields ) <EOL> writer . writerow ( dict ( zip ( fields , fields ) ) ) <EOL> writer . writerows ( results ) <EOL> def read_input ( buf , has_header = True ) : <EOL> """<STR_LIT>""" <EOL> if buf is None : <EOL> buf = sys . stdin <EOL> header = { } <EOL> if has_header : <EOL> last_attr = None <EOL> while True : <EOL> line = buf . readline ( ) <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> if len ( line ) == <NUM_LIT:0> : <EOL> break <EOL> colon = line . find ( '<STR_LIT::>' ) <EOL> if colon < <NUM_LIT:0> : <EOL> if last_attr : <EOL> header [ last_attr ] = header [ last_attr ] + '<STR_LIT:\n>' + urllib . unquote ( line ) <EOL> else : <EOL> continue <EOL> last_attr = attr = line [ : colon ] <EOL> val = urllib . unquote ( line [ colon + <NUM_LIT:1> : ] ) <EOL> header [ attr ] = val <EOL> return buf , header <EOL> def encode_mv ( vals ) : <EOL> """<STR_LIT>""" <EOL> s = "<STR_LIT>" <EOL> for val in vals : <EOL> val = val . replace ( '<STR_LIT:$>' , '<STR_LIT>' ) <EOL> if len ( s ) > <NUM_LIT:0> : <EOL> s += '<STR_LIT:;>' <EOL> s += '<STR_LIT:$>' + val + '<STR_LIT:$>' <EOL> return s <EOL> def main ( argv ) : <EOL> stdin_wrapper = Reader ( sys . stdin ) <EOL> buf , settings = read_input ( stdin_wrapper , has_header = True ) <EOL> events = csv . DictReader ( buf ) <EOL> results = [ ] <EOL> for event in events : <EOL> raw = StringIO . StringIO ( event [ "<STR_LIT>" ] ) <EOL> top_output = csv . DictReader ( raw , delimiter = '<STR_LIT:U+0020>' , skipinitialspace = True ) <EOL> usercounts = { } <EOL> for row in top_output : <EOL> user = row [ "<STR_LIT>" ] <EOL> user = user if not user . startswith ( '<STR_LIT:_>' ) else user [ <NUM_LIT:1> : ] <EOL> usercount = <NUM_LIT:0> <EOL> if usercounts . has_key ( user ) : <EOL> usercount = usercounts [ user ] <EOL> usercount += <NUM_LIT:1> <EOL> usercounts [ user ] = usercount <EOL> results . append ( usercounts ) <EOL> output_results ( results ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> try : <EOL> main ( sys . argv ) <EOL> except Exception : <EOL> import traceback <EOL> traceback . print_exc ( file = sys . stdout ) </s>
