Skip to content

Commit

Permalink
Complete Exercise 6-29: CouchDB
Browse files Browse the repository at this point in the history
  • Loading branch information
schedutron committed Nov 9, 2017
1 parent 36b52c9 commit de3b974
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions Chap6/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SQLAlchemy==1.1.14
SQLObject==3.4.0
mysql-connector==2.1.6
couchdb
89 changes: 89 additions & 0 deletions Chap6/ushuffle_couch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env python3

from distutils.log import warn as printf
from random import randrange as rand
import couchdb
from ushuffle_dbU import randName, FIELDS, tformat, cformat

DBNAME = 'test_users'

class CouchTest(object):
def __init__(self):
try:
self.couch = couchdb.Server()
del self.couch[DBNAME]
except couchdb.http.ResourceNotFound:
try:
self.users = self.couch.create(DBNAME)
except couchdb.http.ResourceNotFound:
raise RuntimeError()
else:
self.users = self.couch.create(DBNAME)


def insert(self):
for who, uid in randName():
self.users.save(
dict(login=who, userid=uid, projid=rand(1, 5))
)

def update(self):
fr = rand(1, 5)
to = rand(1, 5)
i = -1
for uid in self.users:
user = self.users[uid]
if user['projid'] == fr:
user['projid'] = to
self.users.save(user)
i += 1
return fr, to, i+1

def delete(self):
rm = rand(1, 5)
i = -1
for uid in self.users:
user = self.users[uid]
if user['projid'] == rm:
self.users.delete(user)
i += 1
return rm, i+1

def dbDump(self):
printf("\n%s" % "".join(map(cformat, FIELDS)))
for user in self.users:
printf("".join(map(tformat, (self.users[user][k] for k in FIELDS))))

def finish(self):
del self.couch # Can't find a proper method to disconnect.


def main():
printf("*** Connect to %r database" % DBNAME)
try:
couch = CouchTest()
except RuntimeError:
printf("\nERROR: MongoDB server unreachable, exit")
return

printf("\n*** Insert names into table")
couch.insert()
couch.dbDump()

printf("\n*** Move users to a random group")
fr, to, num = couch.update()
printf("\t(%d users moved) from (%d) to (%d)" % (num, fr, to))
couch.dbDump()

printf("\n*** Randomly delete group")
rm, num = couch.delete()
printf("\t(group #%d; %d users removed)" % (rm, num))
couch.dbDump()

printf("\n*** Drop users table")
del couch.couch[DBNAME]
printf("\n*** Close cxns")
couch.finish()

if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
* Exercise 6-26: Storm ORM - tried installing storm, in vain
* Exercise 6-27: NoSQL ([for_nosql.md][6-27])
* Exercise 6-28: NoSQL ([nosql_types.md][6-28])
* Exercise 6-29: CouchDB ([ushuffle_couch.py][6-29])

[req2]: /requirements.txt
[chap4]: /Chap4
Expand Down Expand Up @@ -173,4 +174,5 @@
[6-23]: /Chap6/ushuffle_sad.py
[6-24]: /Chap6/ushuffle_sae.py
[6-27]: /Chap6/for_nosql.md
[6-28]: /Chap6/nosql_types.md
[6-28]: /Chap6/nosql_types.md
[6-29]: /Chap6/ushuffle_couch.py

0 comments on commit de3b974

Please sign in to comment.