<s> from __future__ import absolute_import , print_function , division <EOL> from petl . compat import Counter , string_types , maketrans <EOL> from petl . util . base import values , Table , data , wrap <EOL> def nrows ( table ) : <EOL> """<STR_LIT>""" <EOL> return sum ( <NUM_LIT:1> for _ in data ( table ) ) <EOL> Table . nrows = nrows <EOL> def valuecount ( table , field , value , missing = None ) : <EOL> """<STR_LIT>""" <EOL> total = <NUM_LIT:0> <EOL> vs = <NUM_LIT:0> <EOL> for v in values ( table , field , missing = missing ) : <EOL> total += <NUM_LIT:1> <EOL> if v == value : <EOL> vs += <NUM_LIT:1> <EOL> return vs , float ( vs ) / total <EOL> Table . valuecount = valuecount <EOL> def valuecounter ( table , * field , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> missing = kwargs . get ( '<STR_LIT>' , None ) <EOL> counter = Counter ( ) <EOL> for v in values ( table , field , missing = missing ) : <EOL> try : <EOL> counter [ v ] += <NUM_LIT:1> <EOL> except IndexError : <EOL> pass <EOL> return counter <EOL> Table . valuecounter = valuecounter <EOL> def valuecounts ( table , * field , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return ValueCountsView ( table , field , ** kwargs ) <EOL> Table . valuecounts = valuecounts <EOL> class ValueCountsView ( Table ) : <EOL> def __init__ ( self , table , field , missing = None ) : <EOL> self . table = table <EOL> self . field = field <EOL> self . missing = missing <EOL> def __iter__ ( self ) : <EOL> if isinstance ( self . field , ( tuple , list ) ) : <EOL> outhdr = tuple ( self . field ) + ( '<STR_LIT:count>' , '<STR_LIT>' ) <EOL> else : <EOL> outhdr = ( self . field , '<STR_LIT:count>' , '<STR_LIT>' ) <EOL> yield outhdr <EOL> counter = valuecounter ( self . table , * self . field , missing = self . missing ) <EOL> counts = counter . most_common ( ) <EOL> total = sum ( c [ <NUM_LIT:1> ] for c in counts ) <EOL> if len ( self . field ) > <NUM_LIT:1> : <EOL> for c in counts : <EOL> yield tuple ( c [ <NUM_LIT:0> ] ) + ( c [ <NUM_LIT:1> ] , float ( c [ <NUM_LIT:1> ] ) / total ) <EOL> else : <EOL> for c in counts : <EOL> yield ( c [ <NUM_LIT:0> ] , c [ <NUM_LIT:1> ] , float ( c [ <NUM_LIT:1> ] ) / total ) <EOL> def parsecounter ( table , field , parsers = ( ( '<STR_LIT:int>' , int ) , ( '<STR_LIT:float>' , float ) ) ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( parsers , ( list , tuple ) ) : <EOL> parsers = dict ( parsers ) <EOL> counter , errors = Counter ( ) , Counter ( ) <EOL> for n in parsers . keys ( ) : <EOL> counter [ n ] = <NUM_LIT:0> <EOL> errors [ n ] = <NUM_LIT:0> <EOL> for v in values ( table , field ) : <EOL> if isinstance ( v , string_types ) : <EOL> for name , parser in parsers . items ( ) : <EOL> try : <EOL> parser ( v ) <EOL> except : <EOL> errors [ name ] += <NUM_LIT:1> <EOL> else : <EOL> counter [ name ] += <NUM_LIT:1> <EOL> return counter , errors <EOL> Table . parsecounter = parsecounter <EOL> def parsecounts ( table , field , parsers = ( ( '<STR_LIT:int>' , int ) , ( '<STR_LIT:float>' , float ) ) ) : <EOL> """<STR_LIT>""" <EOL> return ParseCountsView ( table , field , parsers = parsers ) <EOL> Table . parsecounts = parsecounts <EOL> class ParseCountsView ( Table ) : <EOL> def __init__ ( self , table , field , parsers = ( ( '<STR_LIT:int>' , int ) , ( '<STR_LIT:float>' , float ) ) ) : <EOL> self . table = table <EOL> self . field = field <EOL> if isinstance ( parsers , ( list , tuple ) ) : <EOL> parsers = dict ( parsers ) <EOL> self . parsers = parsers <EOL> def __iter__ ( self ) : <EOL> counter , errors = parsecounter ( self . table , self . field , self . parsers ) <EOL> yield ( '<STR_LIT:type>' , '<STR_LIT:count>' , '<STR_LIT>' ) <EOL> for ( item , n ) in counter . most_common ( ) : <EOL> yield ( item , n , errors [ item ] ) <EOL> def typecounter ( table , field ) : <EOL> """<STR_LIT>""" <EOL> counter = Counter ( ) <EOL> for v in values ( table , field ) : <EOL> try : <EOL> counter [ v . __class__ . __name__ ] += <NUM_LIT:1> <EOL> except IndexError : <EOL> pass <EOL> return counter <EOL> Table . typecounter = typecounter <EOL> def typecounts ( table , field ) : <EOL> """<STR_LIT>""" <EOL> return TypeCountsView ( table , field ) <EOL> Table . typecounts = typecounts <EOL> class TypeCountsView ( Table ) : <EOL> def __init__ ( self , table , field ) : <EOL> self . table = table <EOL> self . field = field <EOL> def __iter__ ( self ) : <EOL> counter = typecounter ( self . table , self . field ) <EOL> yield ( '<STR_LIT:type>' , '<STR_LIT:count>' , '<STR_LIT>' ) <EOL> counts = counter . most_common ( ) <EOL> total = sum ( c [ <NUM_LIT:1> ] for c in counts ) <EOL> for c in counts : <EOL> yield ( c [ <NUM_LIT:0> ] , c [ <NUM_LIT:1> ] , float ( c [ <NUM_LIT:1> ] ) / total ) <EOL> def stringpatterncounter ( table , field ) : <EOL> """<STR_LIT>""" <EOL> trans = maketrans ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> counter = Counter ( ) <EOL> for v in values ( table , field ) : <EOL> p = str ( v ) . translate ( trans ) <EOL> counter [ p ] += <NUM_LIT:1> <EOL> return counter <EOL> Table . stringpatterncounter = stringpatterncounter <EOL> def stringpatterns ( table , field ) : <EOL> """<STR_LIT>""" <EOL> counter = stringpatterncounter ( table , field ) <EOL> output = [ ( '<STR_LIT>' , '<STR_LIT:count>' , '<STR_LIT>' ) ] <EOL> counter = counter . most_common ( ) <EOL> total = sum ( c [ <NUM_LIT:1> ] for c in counter ) <EOL> cnts = [ ( c [ <NUM_LIT:0> ] , c [ <NUM_LIT:1> ] , float ( c [ <NUM_LIT:1> ] ) / total ) for c in counter ] <EOL> output . extend ( cnts ) <EOL> return wrap ( output ) <EOL> Table . stringpatterns = stringpatterns <EOL> def rowlengths ( table ) : <EOL> """<STR_LIT>""" <EOL> counter = Counter ( ) <EOL> for row in data ( table ) : <EOL> counter [ len ( row ) ] += <NUM_LIT:1> <EOL> output = [ ( '<STR_LIT>' , '<STR_LIT:count>' ) ] <EOL> output . extend ( counter . most_common ( ) ) <EOL> return wrap ( output ) <EOL> Table . rowlengths = rowlengths </s>
