diff --git a/Chap6/requirements.txt b/Chap6/requirements.txt index c20dd96..0de935e 100644 --- a/Chap6/requirements.txt +++ b/Chap6/requirements.txt @@ -1,3 +1,4 @@ SQLAlchemy==1.1.14 SQLObject==3.4.0 mysql-connector==2.1.6 +couchdb diff --git a/Chap6/ushuffle_couch.py b/Chap6/ushuffle_couch.py new file mode 100644 index 0000000..cbe0e63 --- /dev/null +++ b/Chap6/ushuffle_couch.py @@ -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() diff --git a/README.md b/README.md index 754566b..24e2541 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 \ No newline at end of file +[6-28]: /Chap6/nosql_types.md +[6-29]: /Chap6/ushuffle_couch.py \ No newline at end of file