<s> """<STR_LIT>""" <EOL> import sqlite3 <EOL> import warnings <EOL> from collections import MutableMapping , Mapping <EOL> from operator import itemgetter <EOL> import numpy <EOL> import six <EOL> from six . moves import cPickle , map <EOL> from blocks . config import config <EOL> from . log import TrainingLogBase <EOL> ANCESTORS_QUERY = """<STR_LIT>""" <EOL> LARGE_BLOB_WARNING = """<STR_LIT>""" <EOL> def adapt_obj ( obj ) : <EOL> """<STR_LIT>""" <EOL> blob = sqlite3 . Binary ( cPickle . dumps ( obj ) ) <EOL> if len ( blob ) > config . max_blob_size : <EOL> warnings . warn ( '<STR_LIT>' + <EOL> LARGE_BLOB_WARNING . format ( type ( obj ) , len ( blob ) ) ) <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , '<STR_LIT>' ) <EOL> return blob <EOL> def adapt_ndarray ( obj ) : <EOL> """<STR_LIT>""" <EOL> if obj . ndim == <NUM_LIT:0> : <EOL> return float ( obj ) <EOL> else : <EOL> return adapt_obj ( obj ) <EOL> def _get_row ( row , key ) : <EOL> """<STR_LIT>""" <EOL> if row is not None : <EOL> value = row [ <NUM_LIT:0> ] <EOL> if ( isinstance ( value , ( sqlite3 . Binary , bytes ) ) and <EOL> key != '<STR_LIT>' ) : <EOL> value = cPickle . loads ( bytes ( value ) ) <EOL> return value <EOL> raise KeyError ( key ) <EOL> def _register_adapter ( value , key ) : <EOL> """<STR_LIT>""" <EOL> if ( not isinstance ( value , ( type ( None ) , int , float , six . string_types , <EOL> bytes , numpy . ndarray ) ) and <EOL> key != '<STR_LIT>' ) : <EOL> sqlite3 . register_adapter ( type ( value ) , adapt_obj ) <EOL> class SQLiteLog ( TrainingLogBase , Mapping ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , database = None , ** kwargs ) : <EOL> if database is None : <EOL> database = config . sqlite_database <EOL> self . database = database <EOL> self . conn = sqlite3 . connect ( database ) <EOL> sqlite3 . register_adapter ( numpy . ndarray , adapt_ndarray ) <EOL> with self . conn : <EOL> self . conn . execute ( """<STR_LIT>""" ) <EOL> self . conn . execute ( """<STR_LIT>""" ) <EOL> self . status = SQLiteStatus ( self ) <EOL> super ( SQLiteLog , self ) . __init__ ( ** kwargs ) <EOL> @ property <EOL> def conn ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _conn = sqlite3 . connect ( self . database ) <EOL> return self . _conn <EOL> @ conn . setter <EOL> def conn ( self , value ) : <EOL> self . _conn = value <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = self . __dict__ . copy ( ) <EOL> if '<STR_LIT>' in state : <EOL> del state [ '<STR_LIT>' ] <EOL> self . resume ( ) <EOL> return state <EOL> def __getitem__ ( self , time ) : <EOL> self . _check_time ( time ) <EOL> return SQLiteEntry ( self , time ) <EOL> def __iter__ ( self ) : <EOL> return map ( itemgetter ( <NUM_LIT:0> ) , self . conn . execute ( <EOL> ANCESTORS_QUERY + "<STR_LIT>" <EOL> "<STR_LIT>" , ( self . h_uuid , ) <EOL> ) ) <EOL> def __len__ ( self ) : <EOL> return self . conn . execute ( <EOL> ANCESTORS_QUERY + "<STR_LIT>" <EOL> "<STR_LIT>" , ( self . h_uuid , ) <EOL> ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> class SQLiteStatus ( MutableMapping ) : <EOL> def __init__ ( self , log ) : <EOL> self . log = log <EOL> def __getitem__ ( self , key ) : <EOL> row = self . log . conn . execute ( <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , key ) <EOL> ) . fetchone ( ) <EOL> return _get_row ( row , key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> _register_adapter ( value , key ) <EOL> with self . log . conn : <EOL> self . log . conn . execute ( <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , key , value ) <EOL> ) <EOL> def __delitem__ ( self , key ) : <EOL> with self . log . conn : <EOL> self . log . conn . execute ( <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , key ) <EOL> ) <EOL> def __len__ ( self ) : <EOL> return self . log . conn . execute ( <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , ) <EOL> ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> def __iter__ ( self ) : <EOL> return map ( itemgetter ( <NUM_LIT:0> ) , self . log . conn . execute ( <EOL> "<STR_LIT>" , ( self . log . h_uuid , ) <EOL> ) ) <EOL> class SQLiteEntry ( MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , log , time ) : <EOL> self . log = log <EOL> self . time = time <EOL> def __getitem__ ( self , key ) : <EOL> row = self . log . conn . execute ( <EOL> ANCESTORS_QUERY + "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , self . time , key ) <EOL> ) . fetchone ( ) <EOL> return _get_row ( row , key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> _register_adapter ( value , key ) <EOL> with self . log . conn : <EOL> self . log . conn . execute ( <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , self . time , key , value ) <EOL> ) <EOL> def __delitem__ ( self , key ) : <EOL> with self . log . conn : <EOL> self . log . conn . execute ( <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , self . time , key ) <EOL> ) <EOL> def __len__ ( self ) : <EOL> return self . log . conn . execute ( <EOL> ANCESTORS_QUERY + "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , self . time , ) <EOL> ) . fetchone ( ) [ <NUM_LIT:0> ] <EOL> def __iter__ ( self ) : <EOL> return map ( itemgetter ( <NUM_LIT:0> ) , self . log . conn . execute ( <EOL> ANCESTORS_QUERY + "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ( self . log . h_uuid , self . time , ) <EOL> ) ) </s>
