<s> from __future__ import division , print_function , absolute_import <EOL> from petl . compat import next , string_types <EOL> from petl . util . base import iterpeek , ValuesView , Table <EOL> from petl . util . materialise import columns <EOL> def infer_dtype ( table ) : <EOL> import numpy as np <EOL> it = iter ( table ) <EOL> hdr = next ( it ) <EOL> flds = list ( map ( str , hdr ) ) <EOL> rows = tuple ( it ) <EOL> dtype = np . rec . array ( rows ) . dtype <EOL> dtype . names = flds <EOL> return dtype <EOL> def construct_dtype ( flds , peek , dtype ) : <EOL> import numpy as np <EOL> if dtype is None : <EOL> dtype = infer_dtype ( peek ) <EOL> elif isinstance ( dtype , string_types ) : <EOL> typestrings = [ s . strip ( ) for s in dtype . split ( '<STR_LIT:U+002C>' ) ] <EOL> dtype = [ ( f , t ) for f , t in zip ( flds , typestrings ) ] <EOL> elif ( isinstance ( dtype , dict ) and <EOL> ( '<STR_LIT>' not in dtype or '<STR_LIT>' not in dtype ) ) : <EOL> cols = columns ( peek ) <EOL> newdtype = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> for f in flds : <EOL> newdtype [ '<STR_LIT>' ] . append ( f ) <EOL> if f in dtype and isinstance ( dtype [ f ] , tuple ) : <EOL> newdtype [ '<STR_LIT>' ] . append ( dtype [ f ] [ <NUM_LIT:0> ] ) <EOL> elif f not in dtype : <EOL> a = np . array ( cols [ f ] ) <EOL> newdtype [ '<STR_LIT>' ] . append ( a . dtype ) <EOL> else : <EOL> newdtype [ '<STR_LIT>' ] . append ( dtype [ f ] ) <EOL> dtype = newdtype <EOL> return dtype <EOL> def toarray ( table , dtype = None , count = - <NUM_LIT:1> , sample = <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> import numpy as np <EOL> it = iter ( table ) <EOL> peek , it = iterpeek ( it , sample ) <EOL> hdr = next ( it ) <EOL> flds = list ( map ( str , hdr ) ) <EOL> dtype = construct_dtype ( flds , peek , dtype ) <EOL> it = ( tuple ( row ) for row in it ) <EOL> sa = np . fromiter ( it , dtype = dtype , count = count ) <EOL> return sa <EOL> Table . toarray = toarray <EOL> def torecarray ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> import numpy as np <EOL> return toarray ( * args , ** kwargs ) . view ( np . recarray ) <EOL> Table . torecarray = torecarray <EOL> def fromarray ( a ) : <EOL> """<STR_LIT>""" <EOL> return ArrayView ( a ) <EOL> class ArrayView ( Table ) : <EOL> def __init__ ( self , a ) : <EOL> self . a = a <EOL> def __iter__ ( self ) : <EOL> yield tuple ( self . a . dtype . names ) <EOL> for row in self . a : <EOL> yield tuple ( row ) <EOL> def valuestoarray ( vals , dtype = None , count = - <NUM_LIT:1> , sample = <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> import numpy as np <EOL> it = iter ( vals ) <EOL> if dtype is None : <EOL> peek , it = iterpeek ( it , sample ) <EOL> dtype = np . array ( peek ) . dtype <EOL> a = np . fromiter ( it , dtype = dtype , count = count ) <EOL> return a <EOL> ValuesView . toarray = valuestoarray <EOL> ValuesView . array = valuestoarray </s>
