<s> import datetime <EOL> from sqlalchemy import orm <EOL> from dssg import db <EOL> class BaseModel : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> for k , v in kwargs . iteritems ( ) : <EOL> setattr ( self , k , v ) <EOL> @ classmethod <EOL> def by_id ( cls , id ) : <EOL> """<STR_LIT>""" <EOL> return db . session . query ( cls ) . get ( id ) <EOL> def create ( self ) : <EOL> """<STR_LIT>""" <EOL> db . session . add ( self ) <EOL> db . session . commit ( ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> db . session . delete ( self ) <EOL> db . session . commit ( ) <EOL> def as_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> _dict = { } <EOL> table = orm . class_mapper ( self . __class__ ) . mapped_table <EOL> for col in table . c : <EOL> val = getattr ( self , col . name ) <EOL> if isinstance ( val , datetime . date ) : <EOL> val = str ( val ) <EOL> if isinstance ( val , datetime . datetime ) : <EOL> val = val . isoformat ( ) <EOL> _dict [ col . name ] = val <EOL> return _dict <EOL> @ classmethod <EOL> def create_all ( cls , entries = [ ] ) : <EOL> """<STR_LIT>""" <EOL> for row in entries : <EOL> db . session . add ( row ) <EOL> if len ( entries ) > <NUM_LIT:0> : <EOL> db . session . commit ( ) </s>
