-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_test.py
107 lines (91 loc) · 3.14 KB
/
mongo_test.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# super simple pymongo contact list program
# pretty useless, except to help me remember how to
# run some simple Mongo code
# Spring 2018
# by JM
import pymongo
import easygui as g
import sys
client = pymongo.MongoClient("--your mongo connection string goes here--")
db = client.cool_db
def make_query(prompt="Enter a parameter and a value to find an item"):
query = g.enterbox(prompt)
if query:
q = query.split("=")
try:
# example contacts = db.contacts.find({'home':'Sunnyvale'})
qdict = {q[0]:q[1]}
contacts = db.contacts.find(qdict)
return list(contacts), qdict
except:
g.msgbox("Malformed query")
return None
g.msgbox("Welcome to Mongo Contact list program")
contacts = []
while True:
prompt = """
ALL) All Contacts
ADD) Add a Contact
SEL) Select a contact
DEL) Delete selected contact
QUIT) Quit
"""
selection = g.enterbox(prompt)
if selection is None or 'QUIT' in selection.upper():
g.msgbox("Thanks for using our program")
sys.exit(0)
selection = selection.upper()
if selection == 'ALL':
lim = g.enterbox("Enter number to see or blank/cancel for all")
try:
lim = int(lim)
except:
pass
if isinstance(lim, int):
lim = int(lim)
contacts = db.contacts.find().limit(lim)
else:
contacts = db.contacts.find()
if selection == 'SEL':
contacts, _ = make_query()
if contacts is None:
continue
if selection == 'DEL':
contacts, query = make_query("DELETE contacts identified by item=value")
if contacts:
buf = []
for item in contacts:
buf.append(str(item))
resp = g.codebox(msg="click item to delete, then OK", title="Query Results", text="\n".join(buf))
if resp:
db.contacts.delete_many(query
)
g.msgbox("delete completed.")
continue
else:
g.msgbox("Cancelled")
continue
if selection == "ADD":
items = ['firstname', 'lastname', 'home', 'email', 'age']
this_contact = {}
for item in items:
item_data = g.enterbox('What is the {}'.format(item))
if item_data:
this_contact[item] = item_data
while True:
resp = g.enterbox("Enter any additional tuples with item=value format.")
if resp:
try:
item, item_data = resp.split("=")
this_contact[item] = item_data
except:
g.msgbox("That wasn't recognized")
else:
break
db.contacts.insert(this_contact)
g.msgbox("Inserted {}".format(this_contact))
continue
buf = []
for item in contacts:
buf.append(str(item))
g.codebox(msg="Your Query Results", title="Query Results", text="\n".join(buf))