old_similarity_tables.py
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /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()