-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood.py
39 lines (29 loc) · 841 Bytes
/
food.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
import random
from pymongo import Connection
class Food(object):
def __init__(self):
self.db = Connection()["food"]["choices"]
def add(self, name):
name = str(name).lower()
self.db.update({'name':name}, {'name':name}, upsert=True)
def remove(self, name):
self.db.remove({'name':name})
def get_all(self):
retVal = []
try:
cur = self.db.find()
for i in cur:
retVal.append(i['name'])
except:
pass
return retVal
def choose(self):
try:
cur = self.db.find()
tmp = []
for i in cur:
tmp.append(i['name'])
index = random.randrange(len(tmp))
return tmp[index]
except:
return "unknown"