<s> """<STR_LIT>""" <EOL> import gzip <EOL> import json <EOL> import pickle <EOL> import bayeslite . core as core <EOL> from bayeslite . sqlite3_util import sqlite3_quote_name <EOL> from bayeslite . util import casefold <EOL> from bayeslite . util import cursor_value <EOL> renamed_column_stattypes = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> allowed_column_stattypes = { <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:ignore>' , <EOL> '<STR_LIT:key>' , <EOL> '<STR_LIT>' , <EOL> } <EOL> def bayesdb_load_legacy_models ( bdb , generator , table , metamodel , pathname , <EOL> create = False , ifnotexists = False , gzipped = None ) : <EOL> """<STR_LIT>""" <EOL> if metamodel != '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not create : <EOL> if ifnotexists : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> pickled = None <EOL> with open ( pathname , '<STR_LIT:rb>' ) as f : <EOL> if gzipped or ( gzipped is None and pathname . endswith ( '<STR_LIT>' ) ) : <EOL> with gzip . GzipFile ( fileobj = f ) as gzf : <EOL> pickled = pickle . load ( gzf ) <EOL> else : <EOL> pickled = pickle . load ( f ) <EOL> if '<STR_LIT>' not in pickled : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in pickled : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> schema = pickled [ '<STR_LIT>' ] <EOL> models = pickled [ '<STR_LIT>' ] <EOL> if not isinstance ( schema , dict ) : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> for column_name in schema : <EOL> column_schema = schema [ column_name ] <EOL> if not isinstance ( column_schema , dict ) : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> if not '<STR_LIT>' in column_schema : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> if column_schema [ '<STR_LIT>' ] in renamed_column_stattypes : <EOL> column_schema [ '<STR_LIT>' ] = renamed_column_stattypes [ column_schema [ '<STR_LIT>' ] ] <EOL> if column_schema [ '<STR_LIT>' ] not in allowed_column_stattypes : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> column_stattypes = dict ( ( casefold ( column_name ) , <EOL> casefold ( schema [ column_name ] [ '<STR_LIT>' ] ) ) <EOL> for column_name in schema ) <EOL> with bdb . savepoint ( ) : <EOL> if not core . bayesdb_has_table ( bdb , table ) : <EOL> raise ValueError ( '<STR_LIT>' % ( repr ( table ) , ) ) <EOL> if core . bayesdb_has_generator ( bdb , generator ) : <EOL> if create and not ifnotexists : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( repr ( generator ) , ) ) <EOL> generator_id = core . bayesdb_get_generator ( bdb , generator ) <EOL> generator_table = core . bayesdb_generator_table ( bdb , generator_id ) <EOL> if casefold ( table ) != generator_table : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> ( generator , generator_table , table ) ) <EOL> generator_id = core . bayesdb_get_generator ( bdb , generator ) <EOL> old_types = bayesdb_generator_column_stattypes ( bdb , generator_id ) <EOL> if column_stattypes != old_types : <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , ( generator_id , ) ) <EOL> if <NUM_LIT:0> < cursor_value ( cursor ) : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( repr ( generator ) , ) ) <EOL> qg = sqlite3_quote_name ( generator ) <EOL> bdb . execute ( '<STR_LIT>' % ( qg , ) ) <EOL> bayesdb_create_legacy_generator ( bdb , generator , table , <EOL> column_stattypes ) <EOL> elif create : <EOL> bayesdb_create_legacy_generator ( bdb , generator , table , <EOL> column_stattypes ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( repr ( generator ) , ) ) <EOL> for modelno in models : <EOL> theta = models [ modelno ] <EOL> if '<STR_LIT>' not in theta : <EOL> raise IOError ( '<STR_LIT>' % <EOL> ( modelno , ) ) <EOL> X_L = theta [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' not in X_L : <EOL> raise IOError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( modelno , ) ) <EOL> for viewno , view_state in enumerate ( X_L [ '<STR_LIT>' ] ) : <EOL> if '<STR_LIT>' not in view_state : <EOL> raise IOError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( viewno , modelno ) ) <EOL> view_column_names = view_state [ '<STR_LIT>' ] <EOL> if not isinstance ( view_column_names , list ) : <EOL> raise IOError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( viewno , modelno ) ) <EOL> for i in range ( len ( view_column_names ) ) : <EOL> name = view_column_names [ i ] <EOL> if not core . bayesdb_table_has_column ( bdb , table , name ) : <EOL> raise IOError ( '<STR_LIT>' % <EOL> ( repr ( table ) , repr ( name ) ) ) <EOL> colno = core . bayesdb_table_column_number ( bdb , table , name ) <EOL> name = core . bayesdb_table_column_name ( bdb , table , colno ) <EOL> view_column_names [ i ] = name <EOL> generator_id = core . bayesdb_get_generator ( bdb , generator ) <EOL> modelno_max_sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( modelno_max_sql , ( generator_id , ) ) <EOL> modelno_max = cursor_value ( cursor ) <EOL> modelno_start = <NUM_LIT:0> if modelno_max is None else modelno_max + <NUM_LIT:1> <EOL> insert_model_sql = '''<STR_LIT>''' <EOL> insert_theta_json_sql = '''<STR_LIT>''' <EOL> for i , modelno_ext in enumerate ( sorted ( models . keys ( ) ) ) : <EOL> modelno = modelno_start + i <EOL> theta = models [ modelno_ext ] <EOL> iterations = <NUM_LIT:0> <EOL> if '<STR_LIT>' in theta and isinstance ( theta [ '<STR_LIT>' ] , int ) : <EOL> iterations = theta [ '<STR_LIT>' ] <EOL> bdb . sql_execute ( insert_model_sql , { <EOL> '<STR_LIT>' : generator_id , <EOL> '<STR_LIT>' : modelno , <EOL> '<STR_LIT>' : iterations , <EOL> } ) <EOL> bdb . sql_execute ( insert_theta_json_sql , { <EOL> '<STR_LIT>' : generator_id , <EOL> '<STR_LIT>' : modelno , <EOL> '<STR_LIT>' : json . dumps ( theta ) , <EOL> } ) <EOL> def bayesdb_generator_column_stattypes ( bdb , generator_id ) : <EOL> column_stattypes = { } <EOL> for name in core . bayesdb_generator_column_names ( bdb , generator_id ) : <EOL> stattype = core . bayesdb_generator_column_stattype ( bdb , generator_id , <EOL> name ) <EOL> column_stattypes [ casefold ( name ) ] = casefold ( stattype ) <EOL> return column_stattypes <EOL> def bayesdb_create_legacy_generator ( bdb , generator , table , column_stattypes ) : <EOL> column_names = core . bayesdb_table_column_names ( bdb , table ) <EOL> qcns = map ( sqlite3_quote_name , column_names ) <EOL> assert all ( column_stattypes [ name ] in allowed_column_stattypes <EOL> for name in column_stattypes ) <EOL> column_name_set = set ( casefold ( name ) for name in column_names ) <EOL> for name in column_stattypes : <EOL> if name not in column_name_set : <EOL> raise IOError ( '<STR_LIT>' % <EOL> ( repr ( table ) , repr ( name ) ) ) <EOL> schema = '<STR_LIT:U+002C>' . join ( '<STR_LIT>' % ( qcn , column_stattypes [ casefold ( name ) ] ) <EOL> for name , qcn in zip ( column_names , qcns ) ) <EOL> qg = sqlite3_quote_name ( generator ) <EOL> qt = sqlite3_quote_name ( table ) <EOL> qmm = '<STR_LIT>' <EOL> bdb . execute ( '<STR_LIT>' % <EOL> ( qg , qt , qmm , schema ) ) </s>
