-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path03_storage_json.py
63 lines (47 loc) · 1.28 KB
/
03_storage_json.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from couchbase import Couchbase
from blessings import Terminal
from pprint import pprint
import json
import hashlib
t = Terminal()
os.system('clear')
print t.bold_red("--------------------------------------------------------------------------")
print t.bold_red("Couchbase JSON Document Storage Operations")
print t.bold_red("--------------------------------------------------------------------------")
print
# connect to the default bucket
cb = Couchbase.connect(bucket='default')
# Create a new dictionary
mydoc = \
{
"doctype": "test",
"name": "John Smith"
}
print t.bold("Set a Document")
print
# store a json doc (automatically converted from dictionary to JSON)
print cb.set("mydoc", mydoc)
print
print t.bold("Get a Document")
print
# retrieve, get type of value, and attempt parse of json if string
val = cb.get("mydoc")
print t.bold("Pretty print a document")
print
# pretty print the document
pprint(val.value, indent=4)
print
print t.bold("Print the document as JSON")
print
print json.dumps(val.value)
print
print t.bold("Delete the document")
print
# delete the test docs to repeat tests
cb.delete("mydoc")
print
print t.bold_red("--------------------------------------------------------------------------")
print