aboutsummaryrefslogtreecommitdiff
path: root/wqflask/maintenance/dataset/utilities.py
blob: d389e67273707e04cbfb3c9705a33da70d3b3f32 (plain)
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
import MySQLdb
import re
import ConfigParser

def get_cursor():
    host = 'localhost'
    user = 'webqtl'
    passwd = 'webqtl'
    db = 'db_webqtl'
    con = MySQLdb.Connect(db=db, host=host, user=user, passwd=passwd)
    cursor = con.cursor()
    return cursor, con

def clearspaces(s, default=None):
    if s:
        s = re.sub('\s+', ' ', s)
        s = s.strip()
        return s
    else:
        return default
        
def to_dic(keys, values):
    dic = {}
    for i in range(len(keys)):
        key = keys[i]
        value = values[i]
        dic[key] = value
    return dic

def overlap(dic1, dic2):
    keys = []
    values1 = []
    values2 = []
    for key in dic1.keys():
        if key in dic2:
            value1 = dic1[key]
            value2 = dic2[key]
            if value1 and value2:
                keys.append(key)
                values1.append(value1)
                values2.append(value2)
    return keys, values1, values2

def to_db_string(s, default):
    if s:
        s = s.strip()
        if len(s) == 0:
            return default
        elif s == 'x':
            return default
        else:
            return s
    else:
        return default

def to_db_float(s, default):
    if s:
        s = s.strip()
        if len(s) == 0:
            return default
        elif s == 'x':
            return default
        else:
            try:
                return float(s)
            except:
                return default
    else:
        return default
        
def to_db_int(s, default):
    if s:
        s = s.strip()
        if len(s) == 0:
            return default
        elif s == 'x':
            return default
        else:
            try:
                return int(s)
            except:
                return default
    else:
        return default

def get_config(configfile):
    config = ConfigParser.ConfigParser()
    config.read(configfile)
    return config