aboutsummaryrefslogtreecommitdiffstats
path: root/python/common.py
blob: 8f2748d94d7b58f8f4589e41ec6bcba301f1decf (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
import logging
import os
import redis
import sys

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