old_similarity_tables.py 2.81 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from sqlalchemy import create_engine, Column, String, Integer, DateTime, Float, ForeignKey, func
from sqlalchemy.orm import sessionmaker, relationship, configure_mappers, joinedload
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.ext.declarative import AbstractConcreteBase, ConcreteBase
from transform_frame import TransformationRules

TTDB_ADDRESS = 'postgresql://khelleb:haslo@localhost/selprefs'

TTBase = declarative_base()

class AbstractTransformationTable(AbstractConcreteBase, TTBase):
    __phantom__ = None

def make_table(name):
    class TransformationTable(AbstractTransformationTable):
        __tablename__ = name
        __mapper_args__ = { 'polymorphic_identity': name, 'concrete':True}

        id = Column(Integer, primary_key=True)
        frame1 = Column(Integer, index=True)
        signature1 = Column(String(250))
        frame2 = Column(Integer)
        signature2 = Column(String(250))
        similarity = Column(Float, index=True)

        def __init__(self, frame1, signature1, frame2, signature2, similarity):
            self.frame1 = frame1
            self.signature1 = signature1
            self.frame2 = frame2
            self.signature2 = signature2
            self.similarity = similarity

    return TransformationTable


def transformation_tables_and_session():
    rules = TransformationRules.get_rules()
    TT_dict = {str(rule): make_table('similarity_{}'.format(rule.sign())) for rule in rules}
    engine = create_engine(TTDB_ADDRESS)
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    return (TT_dict, session)

def _create_model():
    rules = TransformationRules.get_rules()
    TT_list = [make_table('similarity_{}'.format(rule.sign())) for rule in rules]
    engine = create_engine(TTDB_ADDRESS)
    TTBase.metadata.create_all(engine)

def _insert_data():
    rules = TransformationRules.get_rules()
    TT_list = [make_table('similarity_{}'.format(rule.sign())) for rule in rules]
    engine = create_engine(TTDB_ADDRESS)
    DBSession = sessionmaker(bind=engine)
    session = DBSession()

    i = 0
    for Table in TT_list: 
        session.add(Table(1, 'T1/M1', 2, 'T1/M1', i*1.0/10))
        i += 1

    session.commit()

def _test():
    rules = TransformationRules.get_rules()
    TT_list = [make_table('similarity_{}'.format(rule.sign())) for rule in rules]
    engine = create_engine(TTDB_ADDRESS)
    TTBase.metadata.create_all(engine)

    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    
    i = 0
    for Table in TT_list: 
        session.add(Table(1, 'T1/M1', 2, 'T1/M1', i*1.0/10))
        i += 1
        session.add(Table(1, 'T1/M2', 2, 'T1/M1', i*1.0/10))
        i += 1

    session.commit()
    
    
if __name__ == '__main__':
    _create_model()
    # _insert_data()
    # _test()