-
Notifications
You must be signed in to change notification settings - Fork 51
/
ushuffle_so.py
104 lines (82 loc) · 2.65 KB
/
ushuffle_so.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
#!/usr/bin/env python
from distutils.log import warn as printf
from os.path import dirname
from random import randrange as rand
from sqlobject import *
from ushuffle_dbU import DBNAME, NAMELEN, randName, FIELDS, tformat, cformat,\
setup
from ushuffle_sad import DSNs # Instead of redefining here.
class Users(SQLObject):
login = StringCol(length=NAMELEN)
userid = IntCol()
projid = IntCol()
def __str__(self):
return ''.join(map(tformat, (self.login, self.userid, self.projid)))
class SQLObjectTest(object):
def __init__(self, dsn):
try:
cxn = connectionForURI(dsn)
except ImportError:
raise RuntimeError()
try:
cxn.releaseConnection(cxn.getConnection())
except dberrors.OperationalError:
cxn = connectionForURI(dirname(dsn))
cxn.query("CREATE DATABASE %s" % DBNAME)
cxn = connectionForURI(dsn)
self.cxn = sqlhub.processConnection = cxn
def insert(self):
for who, userid in randName():
Users(login=who, userid=userid, projid=rand(1, 5))
def update(self):
fr = rand(1, 5)
to = rand(1, 5)
i = -1
users = Users.selectBy(projid=fr)
for i, user in enumerate(users):
user.projid = to
return fr, to, i+1
def delete(self):
rm = rand(1, 5)
users = Users.selectBy(projid=rm)
i = -1
for i, user in enumerate(users):
user.destroySelf()
return rm, i+1
def dbDump(self):
printf("\n%s" % ''.join(map(cformat, FIELDS)))
for user in Users.select():
printf(user)
def finish(self):
self.cxn.close()
def main():
printf("*** Connect to %r database" % DBNAME)
db = setup()
if db not in DSNs:
printf("\nERROR: %r not supported, exit" % db)
return
try:
orm = SQLObjectTest(DSNs[db])
except RuntimeError:
printf("\nERROR: %r not supported, exit" % db)
return
printf("\n*** Create users table (drop old one if appl.)")
Users.dropTable(True)
Users.createTable()
printf("\n*** Insert names into table")
orm.insert()
orm.dbDump()
printf("\n*** Move users to a random group")
fr, to, num = orm.update()
printf("\t(%d users moved) from (%d) to (%d)" % (num, fr, to))
orm.dbDump()
printf("\n*** Randomly delete group")
rm, num = orm.delete()
printf("\t(group #%d; %d users removed)" % (rm, num))
orm.dbDump()
printf("\n*** Drop users table")
Users.dropTable()
printf("\n*** Close cxns")
orm.finish()
if __name__ == "__main__":
main()