aboutsummaryrefslogtreecommitdiffstats
path: root/python/common.py
blob: 05deab261b269bd0f9aabbce712d599fc3b1cd1a (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
import redis
import logging

def sanitize_float(intex_float):
    try:
        intex_float = intex_float.replace(",", "")
        if " " in intex_float: #case of combo notes
            return float(intex_float.split(" ")[0])
        if "(" in intex_float:
            return - float(intex_float[1:-1])
        else:
            return float(intex_float)
    except (AttributeError, ValueError):
        return intex_float

def get_redis_queue():
    q = redis.Redis(unix_socket_path='/run/redis/redis.sock')
    try:
        q.ping()
    except redis.ConnectionError:
        try:
            q = redis.Redis(os.environ['REDIS_HOST'])
        except KeyError:
            logging.error("Please set redis host in REDIS_HOST")
            sys.exit(1)
    return q