<s> from __future__ import absolute_import , print_function , division <EOL> from petl . compat import pickle , next <EOL> from petl . util . base import Table <EOL> from petl . io . sources import read_source_from_arg , write_source_from_arg <EOL> def frompickle ( source = None ) : <EOL> """<STR_LIT>""" <EOL> source = read_source_from_arg ( source ) <EOL> return PickleView ( source ) <EOL> class PickleView ( Table ) : <EOL> def __init__ ( self , source ) : <EOL> self . source = source <EOL> def __iter__ ( self ) : <EOL> with self . source . open ( '<STR_LIT:rb>' ) as f : <EOL> try : <EOL> while True : <EOL> yield tuple ( pickle . load ( f ) ) <EOL> except EOFError : <EOL> pass <EOL> def topickle ( table , source = None , protocol = - <NUM_LIT:1> , write_header = True ) : <EOL> """<STR_LIT>""" <EOL> _writepickle ( table , source = source , mode = '<STR_LIT:wb>' , protocol = protocol , <EOL> write_header = write_header ) <EOL> Table . topickle = topickle <EOL> def appendpickle ( table , source = None , protocol = - <NUM_LIT:1> , write_header = False ) : <EOL> """<STR_LIT>""" <EOL> _writepickle ( table , source = source , mode = '<STR_LIT>' , protocol = protocol , <EOL> write_header = write_header ) <EOL> Table . appendpickle = appendpickle <EOL> def _writepickle ( table , source , mode , protocol , write_header ) : <EOL> source = write_source_from_arg ( source ) <EOL> with source . open ( mode ) as f : <EOL> it = iter ( table ) <EOL> hdr = next ( it ) <EOL> if write_header : <EOL> pickle . dump ( hdr , f , protocol ) <EOL> for row in it : <EOL> pickle . dump ( row , f , protocol ) <EOL> def teepickle ( table , source = None , protocol = - <NUM_LIT:1> , write_header = True ) : <EOL> """<STR_LIT>""" <EOL> return TeePickleView ( table , source = source , protocol = protocol , <EOL> write_header = write_header ) <EOL> Table . teepickle = teepickle <EOL> class TeePickleView ( Table ) : <EOL> def __init__ ( self , table , source = None , protocol = - <NUM_LIT:1> , write_header = True ) : <EOL> self . table = table <EOL> self . source = source <EOL> self . protocol = protocol <EOL> self . write_header = write_header <EOL> def __iter__ ( self ) : <EOL> protocol = self . protocol <EOL> source = write_source_from_arg ( self . source ) <EOL> with source . open ( '<STR_LIT:wb>' ) as f : <EOL> it = iter ( self . table ) <EOL> hdr = next ( it ) <EOL> if self . write_header : <EOL> pickle . dump ( hdr , f , protocol ) <EOL> yield tuple ( hdr ) <EOL> for row in it : <EOL> pickle . dump ( row , f , protocol ) <EOL> yield tuple ( row ) </s>
