<s> """<STR_LIT>""" <EOL> import sys <EOL> import locale <EOL> import logging <EOL> from textwrap import dedent <EOL> from itertools import imap <EOL> from optparse import OptionParser <EOL> from prettytable import PrettyTable <EOL> from _config import interpolate_config , AttrDict <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> locale . setlocale ( locale . LC_ALL , "<STR_LIT>" ) <EOL> def arith_mean ( values ) : <EOL> """<STR_LIT>""" <EOL> return sum ( values , <NUM_LIT:0.0> ) / len ( values ) <EOL> def sumstat_parse_args ( ) : <EOL> usage = dedent ( """<STR_LIT>""" ) <EOL> parser = OptionParser ( usage = usage ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> options , args = parser . parse_args ( ) <EOL> options . delimiter = interpolate_config ( options . delimiter , options . profile , '<STR_LIT>' , default = '<STR_LIT:U+0020>' ) <EOL> options . reverse = interpolate_config ( options . reverse , options . profile , '<STR_LIT>' , type = bool , default = False ) <EOL> return AttrDict ( options . __dict__ ) , args <EOL> def sumstat ( fh , delimiter , reverse = False , ** kwargs ) : <EOL> counts = [ ] <EOL> N , M = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for line in imap ( lambda x : x . strip ( ) , fh ) : <EOL> try : <EOL> row = line . split ( delimiter , <NUM_LIT:1> ) <EOL> count = row [ <NUM_LIT:0> ] <EOL> except ValueError : <EOL> logging . exception ( "<STR_LIT>" , line ) <EOL> else : <EOL> count = int ( count ) <EOL> counts . append ( count ) <EOL> M += <NUM_LIT:1> <EOL> N += count <EOL> if reverse is True : <EOL> logging . info ( "<STR_LIT>" ) <EOL> counts . reverse ( ) <EOL> avg = arith_mean ( counts ) <EOL> minv , maxv = min ( counts ) , max ( counts ) <EOL> percentiles_idx = [ M / <NUM_LIT:10> , M / <NUM_LIT:4> , M / <NUM_LIT:2> , <NUM_LIT:3> * M / <NUM_LIT:4> , <NUM_LIT:9> * M / <NUM_LIT:10> , <NUM_LIT> * M / <NUM_LIT:100> , <NUM_LIT> * M / <NUM_LIT:100> , <NUM_LIT> * M / <NUM_LIT:1000> ] <EOL> percentiles = map ( lambda x : "<STR_LIT>" % <EOL> ( counts [ x ] , locale . format ( '<STR_LIT>' , x , True ) ) , <EOL> percentiles_idx ) <EOL> S10th , S25th , S40th , S50th , S75th , S90th = None , None , None , None , None , None <EOL> accum = <NUM_LIT:0.> <EOL> for idx , c in enumerate ( reversed ( counts ) ) : <EOL> accum += c <EOL> if not S10th and accum / N >= <NUM_LIT:0.1> : <EOL> S10th = idx + <NUM_LIT:1> <EOL> elif not S25th and accum / N >= <NUM_LIT> : <EOL> S25th = idx + <NUM_LIT:1> <EOL> elif not S40th and accum / N >= <NUM_LIT> : <EOL> S40th = idx + <NUM_LIT:1> <EOL> elif not S50th and accum / N >= <NUM_LIT:0.5> : <EOL> S50th = idx + <NUM_LIT:1> <EOL> elif not S75th and accum / N >= <NUM_LIT> : <EOL> S75th = idx + <NUM_LIT:1> <EOL> elif not S90th and accum / N >= <NUM_LIT> : <EOL> S90th = idx + <NUM_LIT:1> <EOL> return { <EOL> "<STR_LIT:M>" : M , <EOL> "<STR_LIT:N>" : N , <EOL> "<STR_LIT>" : avg , <EOL> "<STR_LIT>" : minv , <EOL> "<STR_LIT>" : maxv , <EOL> "<STR_LIT>" : percentiles , <EOL> "<STR_LIT>" : [ S10th , S25th , S40th , S50th , S75th , S90th ] <EOL> } <EOL> def sumstat_main ( ) : <EOL> """<STR_LIT>""" <EOL> options , args = sumstat_parse_args ( ) <EOL> stat_dict = sumstat ( fh = sys . stdin , * args , ** options ) <EOL> table = PrettyTable ( [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> table . add_row ( <EOL> map ( lambda x : locale . format ( '<STR_LIT>' , x , True ) , [ stat_dict [ '<STR_LIT:N>' ] , stat_dict [ '<STR_LIT:M>' ] ] ) + <EOL> [ stat_dict [ '<STR_LIT>' ] , stat_dict [ '<STR_LIT>' ] , stat_dict [ '<STR_LIT>' ] ] + stat_dict [ '<STR_LIT>' ] <EOL> ) <EOL> print table <EOL> S10th , S25th , S40th , S50th , S75th , S90th = stat_dict [ '<STR_LIT>' ] <EOL> M = stat_dict [ '<STR_LIT:M>' ] <EOL> print ( "<STR_LIT>" % <EOL> ( locale . format ( "<STR_LIT>" , S10th , True ) , <NUM_LIT> * S10th / M ) ) <EOL> print ( "<STR_LIT>" % <EOL> ( locale . format ( "<STR_LIT>" , S25th , True ) , <NUM_LIT> * S25th / M ) ) <EOL> print ( "<STR_LIT>" % <EOL> ( locale . format ( "<STR_LIT>" , S40th , True ) , <NUM_LIT> * S40th / M ) ) <EOL> print ( "<STR_LIT>" % <EOL> ( locale . format ( "<STR_LIT>" , S50th , True ) , <NUM_LIT> * S50th / M ) ) <EOL> print ( "<STR_LIT>" % <EOL> ( locale . format ( "<STR_LIT>" , S75th , True ) , <NUM_LIT> * S75th / M ) ) <EOL> print ( "<STR_LIT>" % <EOL> ( locale . format ( "<STR_LIT>" , S90th , True ) , <NUM_LIT> * S90th / M ) ) <EOL> return <NUM_LIT:0> </s>
