-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_helper.py
51 lines (41 loc) · 1.03 KB
/
db_helper.py
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
#!/usr/bin/env python
import json
import redis
from actualapp import app
REDIS_URL = app.config['REDIS_URL']
REDIS_PORT = app.config['REDIS_PORT']
REDIS_PASSWORD = app.config['REDIS_PASSWORD']
db = redis.StrictRedis(host=REDIS_URL, port=REDIS_PORT, password=REDIS_PASSWORD, db=0)
def initDbList(key):
thingy = db.get(key)
if thingy is None:
db.set(key, json.dumps([]))
return True
return False
def getDbObj(key):
thingy = db.get(key)
if thingy is None:
return None
obj = json.loads(thingy)
return obj
def saveDbObj(key, obj):
thingy = json.dumps(obj)
db.set(key, thingy)
def trimDbList(key, length):
thingy = db.get(key)
listy = json.loads(thingy)
listy = listy[-length:]
thingy = json.dumps(listy)
db.set(key, thingy)
def addToDbList(key, newItem):
thingy = db.get(key)
listy = json.loads(thingy)
listy.append(newItem)
thingy = json.dumps(listy)
db.set(key, thingy)
def popFromDbList(key, popIndex):
thingy = db.get(key)
listy = json.loads(thingy)
listy.pop(popIndex)
thingy = json.dumps(listy)
db.set(key, thingy)