library.py 2.17 KB
Newer Older
1
2
3
4
5
6
7
'''
MySQL connectivity library.

@author: Anze Vavpetic <anze.vavpetic@ijs.si>
'''
import mysql.connector as sql
from context import DBConnection, DBContext
Anze Vavpetic's avatar
Anze Vavpetic committed
8
from converters import RSD_Converter, Aleph_Converter, Orange_Converter, TreeLikerConverter
9
10
11
12
13
14
15
16
17
18
19
20
21
22

def mysql_connect(input_dict):
    user = str(input_dict['user'])
    password = str(input_dict['password'])
    host = str(input_dict['host'])
    db = str(input_dict['database'])
    con = DBConnection(user, password, host, db)
    return {'connection' : con}

def mysql_db_context(input_dict):
    return {'context' : None}

def mysql_db_context_finished(postdata, input_dict, output_dict):
    con = input_dict['connection']
23
24
    find_con = input_dict['find_connections'] == 'true'
    context = DBContext(con, find_connections=find_con)
25
    context.update(postdata)
26
27
28
    return {'context' : context}

def mysql_rsd_converter(input_dict):
29
    dump = input_dict['dump'] == 'true'
30
    rsd = RSD_Converter(input_dict['context'], discr_intervals=input_dict['discr_intervals'] or {}, dump=dump)
31
32
33
    return {'examples' : rsd.all_examples(), 'bk' : rsd.background_knowledge()}

def mysql_aleph_converter(input_dict):
34
    dump = input_dict['dump'] == 'true'
35
36
37
38
    target_att_val = input_dict['target_att_val']
    if not target_att_val:
        raise Exception('Please specify a target attribute value.')
    aleph = Aleph_Converter(input_dict['context'], target_att_val=target_att_val, discr_intervals=input_dict['discr_intervals'] or {}, dump=dump)
39
40
    return {'pos_examples' : aleph.positive_examples(), 'neg_examples' : aleph.negative_examples(), 'bk' : aleph.background_knowledge()}

Anze Vavpetic's avatar
Anze Vavpetic committed
41
42
43
44
45
46
def mysql_treeliker_converter(input_dict):
    treeliker = TreeLikerConverter(input_dict['context'], 
                                   discr_intervals=input_dict['discr_intervals'] or {})
    return {'dataset': treeliker.dataset(), 
            'template': treeliker.default_template()}

47
48
49
50
51
52
def mysql_query_to_odt(input_dict):
    return {'dataset' : None}

def mysql_orange_converter(input_dict):
    context = input_dict['context']
    orange = Orange_Converter(context)
Matic Perovšek's avatar
Matic Perovšek committed
53
    return {'target_table_dataset' : orange.target_Orange_table(),'other_table_datasets': orange.other_Orange_tables()}