<s> """<STR_LIT>""" <EOL> from contextlib import contextmanager <EOL> from datetime import datetime <EOL> import sqlalchemy <EOL> @ contextmanager <EOL> def tables ( engine , * table_names ) : <EOL> """<STR_LIT>""" <EOL> with engine . connect ( ) as connection : <EOL> metadata = sqlalchemy . MetaData ( bind = connection ) <EOL> metadata . reflect ( ) <EOL> yield tuple ( [ connection ] + [ metadata . tables [ t ] for t in table_names ] ) <EOL> class CRUDError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , subject , message ) : <EOL> self . subject = subject <EOL> self . message = message <EOL> def order ( lst , ordering , key = None ) : <EOL> """<STR_LIT>""" <EOL> if key is None : <EOL> lookup = lambda x : x <EOL> elif isinstance ( key , basestring ) : <EOL> lookup = lambda x : x [ key ] <EOL> else : <EOL> lookup = key <EOL> ordering = { name : idx for idx , name in enumerate ( ordering ) } <EOL> lst . sort ( key = lambda x : ordering [ lookup ( x ) ] ) <EOL> return lst <EOL> def find ( iterable , pred ) : <EOL> """<STR_LIT>""" <EOL> for x in iterable : <EOL> if pred ( x ) : <EOL> return x <EOL> def pick ( obj , * keys ) : <EOL> """<STR_LIT>""" <EOL> return { key : obj [ key ] for key in keys } <EOL> def to_epoch ( dt ) : <EOL> """<STR_LIT>""" <EOL> epoch = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , tzinfo = dt . tzinfo ) <EOL> return ( dt - epoch ) . total_seconds ( ) </s>
