def grindESC (xd) : strs = [ "{ '123': 'illegal because starts with number' }", "{ 'a\"': 'illegal because of quote in name' }", "{ 'a<': 'illegal because has <' }", "{ 'a>': 'illegal because has >' }", "{ 'a&': 'illegal because has &' }", "{ 'a\\'': 'illegal because has \\'' }", "{ '': 'empty'}", "{ 'ok': 'special chars: < > & \" \\' '}", "{ 'ok': ['special chars: < > & \" \\' ', '\x11']}", # Note that special unprintable characters gets passed as is ONLY weirdness is escape chars "{ '__attrs__': { 'a':1, 'b':2 }, '__content__': 'something' }", # handle special __content__ "{ 'book': [ {'__attrs__':{'da':1}, '__content__':'junk' } ] }", "{ 'ns:book': { 'ns:a1':5 } }", # ya, we need better namespace support ] for s in strs: v = eval(s) sys.stdout.write("As normal:") pretty(v) print print "As XML:" try : xd.XMLDumpKeyValue("root",v) except Exception, e : sys.stdout.write("Saw Error:") print e print " ... continuing..." print
def retrieveOrderByAccepted(cur, con, accepted): try: query = "SELECT * FROM ORDER_REQUEST WHERE Accepted='%d'" % (accepted) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve failed") print(">>>>>>>>>>>>>", e) return
def retrievePlasmaByUsed(cur, con, used): try: query = "SELECT * FROM PLASMA WHERE Used='%d'" % (used) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve failed") print(">>>>>>>>>>>>>", e) return
def retrievePlasmaByBloodType(cur, con, bloodtype): try: query = "SELECT * FROM PLASMA WHERE Blood_type='%s'" % (bloodtype) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve failed") print(">>>>>>>>>>>>>", e) return
def retrieveDepenByStaffId(cur, con, staffid): try: query = "SELECT * FROM DEPENDENT WHERE Staff_id='%s'" % (staffpid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve failed") print(">>>>>>>>>>>>>", e) return
def retrieveDeptById(cur, con, deptid): try: query = "SELECT * FROM DEPARTMENT WHERE Department_id='%s'" % (deptid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve by ID failed") print(">>>>>>>>>>>>>", e) return
def retrievePlasmaById(cur, con, donorid): try: query = "SELECT * FROM PLASMA WHERE Donor_id='%s'" % (donorid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve by ID failed") print(">>>>>>>>>>>>>", e) return
def retrieveOrderById(cur, con, orderid): try: query = "SELECT * FROM ORDER_REQUEST WHERE Order_id='%s'" % (orderid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve by ID failed") print(">>>>>>>>>>>>>", e) return
def displayUserDetails(cur, con, loginid): try: query = "SELECT * FROM USER WHERE Login_id='%s'" % (loginid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Display Details failed") print(">>>>>>>>>>>>>", e) return
def showAllInv(cur, con): try: query = "SELECT * FROM PLASMA_INVENTORY" if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Show All failed") print(">>>>>>>>>>>>>", e) return
def showAllHospital(cur, con): try: query = "SELECT HOSPITAL.*,Contact,Address FROM HOSPITAL NATURAL JOIN USER" if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Show All failed") print(">>>>>>>>>>>>>", e) return
def showAllOrder(cur, con): try: query = "SELECT * FROM ORDER_REQUEST" if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Show All failed") print(">>>>>>>>>>>>>", e) return
def retrieveDonorByBloodType(cur, con, bloodtype): try: query = "SELECT DONOR.*,Contact,Address FROM DONOR NATURAL JOIN USER WHERE Blood_type='%s'" % ( bloodtype) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve failed") print(">>>>>>>>>>>>>", e) return
def retrieveDonorById(cur, con, donorid): try: query = "SELECT DONOR.*,Contact,Address FROM DONOR NATURAL JOIN USER WHERE Donor_id='%s'" % ( donorid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve by ID failed") print(">>>>>>>>>>>>>", e) return
def retrieveHospitalById(cur, con, hospid): try: query = "SELECT HOSPITAL.*,Contact,Address FROM HOSPITAL NATURAL JOIN USER WHERE Hospital_id='%s'" % ( hospid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve by ID failed") print(">>>>>>>>>>>>>", e) return
def retrieveInvById(cur, con, invid): try: query = "SELECT * FROM PLASMA_INVENTORY WHERE Inventory_id='%s'" % ( invid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Retrieve by ID failed") print(">>>>>>>>>>>>>", e) return
def donorMaxDons(cur, con): try: query = "SELECT * FROM DONOR WHERE Number_of_donations IN (SELECT MAX(Number_of_donations) FROM DONOR) " if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Failed to find donors with max donations") print(">>>>>>>>>>>>>", e) return
def avgSal(cur, con): try: query = "SELECT AVG(Salary) FROM STAFF" if cur.execute(query): averagesal = cur.fetchall() pretty(averagesal) con.commit() except Exception as e: con.rollback() print("Failed to find avg salary") print(">>>>>>>>>>>>>", e) return
def donorGrtrX(cur, con): try: x = int(input("Enter x: ")) query = "SELECT * FROM DONOR WHERE Number_of_donations > '%d' " % (x) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Failed to perform projection") print(">>>>>>>>>>>>>", e) return
def helpMe (typecode, xd) : cc = helpMeCore(typecode, xd) v = { } v["root"] = cc sys.stdout.write("As normal:") pretty(v) print print "As XML:" xd.XMLDumpKeyValue("up",v) print return cc
def getVehicle(cur, con): try: distmin = int(input("Enter lower limit distance: ")) distmax = int(input("Enter upper limit distance: ")) query = "SELECT Vehicle_type FROM LOGISTICS WHERE distance >= '%d' AND distance <= '%d'" % ( distmin, distmax) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print("Failed to perform projection") print(">>>>>>>>>>>>>", e) return
def getDonorOrder(cur, con): try: oid = input("Enter order_id: ") query = "SELECT DONOR.* FROM DONOR INNER JOIN ORDER_REQUEST ON DONOR.Donor_id = ORDER_REQUEST.Donor_id WHERE ORDER_REQUEST.Order_id LIKE '%s'" % ( oid) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print( "Failed to find donors whose blood samples have been used for the entered order ID" ) print(">>>>>>>>>>>>>", e) return
def getOrderVehicle(cur, con): try: vtype = input("Enter vehicle type: ") query = "SELECT ORDER_REQUEST.* FROM ORDER_REQUEST INNER JOIN LOGISTICS ON ORDER_REQUEST.Vehicle_id = LOGISTICS.Vehicle_id WHERE ORDER_REQUEST.Accepted = 1 AND LOGISTICS.Vehicle_type LIKE '%s'" % ( vtype) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print( "Failed to get orders that have been fulfilled by entered vehicle type" ) print(">>>>>>>>>>>>>", e) return
def grindAttrs (xd) : strs = [ "{ 'book': 1, '__attrs__': { 'date':100, 'time':'May 16th, 2010' } }", "{ 'simple': 100, 'complex':{ 'a':1 }, '_plug':'in', '__attrs__': { 'date':100, 'time':'May 16th, 2010' } }", ] for s in strs : v = eval(s) sys.stdout.write("As normal:") pretty(v) print print "As XML:" xd.XMLDumpKeyValue("top",v) print
def getDonAge(cur, con): try: mini = int(input("Enter minimum age: ")) maxi = int(input("Enter maximum age: ")) query = "SELECT DISTINCT DONOR.* FROM DONOR NATURAL JOIN PLASMA WHERE Age>=%d AND Age<=%d AND Used=1" % ( mini, maxi) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print( "Failed to get donors in the given range whose plasma sample has been used" ) print(">>>>>>>>>>>>>", e) return
def retrieveStaffById(cur, con, staffid): try: query = "SELECT STAFF.*,Contact,Address FROM STAFF NATURAL JOIN USER WHERE Staff_id='%s'" % ( staffid) if cur.execute(query): pretty(cur.fetchall()) con.commit() print("Skills:") query = "SELECT * FROM STAFF_SKILLS WHERE Staff_id='%s'" % (staffid) if cur.execute(query): prettySkills(cur.fetchall()) con.commit() print("") except Exception as e: con.rollback() print("Retrieve by ID failed") print(">>>>>>>>>>>>>", e) return
def grindPOD (xd) : helpMe('1', xd) helpMe('b', xd) helpMe('s', xd) helpMe('w', xd) v1 = helpMe('i', xd) helpMe('u', xd) helpMe('l', xd) # helpMe('l', xd) helpMe('f', xd) v2 = helpMe('d', xd) helpMe('F', xd) helpMe('D', xd) t = { } t["int_4"] = v1 t["real_4"] = v2 vv = { } vv["data"] = t sys.stdout.write("As normal:") pretty(vv) print print "As XML:" xd.XMLDumpKeyValue("top",vv) print # make sure we can put in lists a = [ ] a.append(v1); a.append(v2); v = { } v["data"] = a sys.stdout.write("As normal:") pretty(v) print print "As XML:" xd.XMLDumpKeyValue("top",v) print vvv = { 'top':{ 'int_4':[ array([1,2,3], 'i'), array([1.0,2.0,3.0], 'd') ] } } sys.stdout.write("As normal:") pretty(vvv) print pretty(vvv) print "As XML:" xd.XMLDumpValue(vvv) print
def displayStaffDetails(cur, con, loginid): try: query = "SELECT * FROM STAFF NATURAL JOIN WORKS_FOR WHERE Login_id='%s'" % ( loginid) if cur.execute(query): pretty(cur.fetchall()) con.commit() staffid = loginid[5:len(loginid)] print("Skills:") query = "SELECT * FROM STAFF_SKILLS WHERE Staff_id='%s'" % (staffid) if cur.execute(query): prettySkills(cur.fetchall()) con.commit() print("") except Exception as e: con.rollback() print("Display Details failed") print(">>>>>>>>>>>>>", e) return
def retrieveVehiById(cur, con, vehid): try: query = "SELECT * FROM LOGISTICS WHERE Vehicle_id='%s'" % (vehid) if cur.execute(query): row = cur.fetchall() pretty(row) for key, value in row.items(): if key == "Vehicle_type": query = "SELECT * FROM VEHICLE_DETAILS WHERE Vehicle_type='%s'" % ( value) if cur.execute(query): pretty(cur.fetchall()) con.commit() con.commit() except Exception as e: con.rollback() print("Retrieve by ID failed") print(">>>>>>>>>>>>>", e) return
def TryOptions (xd) : strs = [ "{ 'a':'' }", "{ 'a':'123' }", "{ 'a':123 }", "{ 'a': ' 123'} ", "{ 'a': (1+2j) }", "{ 'a': '(1+2j)' }", ] for s in strs : v = eval(s); sys.stdout.write("As normal:") pretty(v) print print "As XML:" xd.XMLDumpKeyValue("top",v); print
def avgOrders(cur, con): try: year = int( input( "Enter year for which you want to see average number of orders placed per month: " )) query = "SELECT AVG(num) AS AvgNumOrder FROM (SELECT COUNT(Order_id) AS num,MONTH(Order_date) FROM ORDER_REQUEST WHERE YEAR(Order_date) = '%d' GROUP BY MONTH(Order_date)) AS TAB" % ( year) if cur.execute(query): pretty(cur.fetchall()) con.commit() except Exception as e: con.rollback() print( "Failed to find average number of orders placed per month for given year" ) print(">>>>>>>>>>>>>", e) return
def helpMeCore (typecode, xd) : t = xlate_typecodes(typecode) v = array([0]*10, t) for ii in xrange(0,10) : r = float(float(ii)*123.456789012345678-500.0) th = array([int(0)], t) if typecoding(th) in type_codes['Integer']: r = int(r) if typecoding(th) in type_codes['UnsignedInteger'] : r = int(r) #unfiged? v[ii] = r; sys.stdout.write("As normal:") pretty(v) print print "As XML:" xd.XMLDumpKeyValue("top",v) print return v;
def grindNoAttrs (xd) : strs = [ "None", "1", "1.0", "1L", "(1.0-1.0j)", # Primitive types first "{ }", "{ 'html': 1 }", "{ 'book': { 'date':'2001', 'time':100, 'chapter':['chap1', 'chap2']}}", "{ 'book': { 'date':'2001', 'time':100, 'chapter':[{'p1':{'a':1, 'b':2, 'text':'chapter1'}}, 'chap2']}}", "{ 'book': { 'date':'2001', 'time':100, 'chapter':[{'p1':{'a':1, 'b':2, 'text':'chapter1'}}, {'text':[1,2,3]}]}}", "{ 'book': { 'date':'2001', 'time':100, 'chapter':[{'p1':{'a':1, 'b':2, 'text':'chapter1'}}, [1,2,3]]}}", "[1,2,3]", "[{'a':1}]", "{ 'empty': None, 'alsoempty': {} }", "[ [0,1,2], [2,3,4] ]", ] for s in strs : v = eval(s) sys.stdout.write("As normal:") pretty(v) print print "As XML:" xd.XMLDumpKeyValue("top",v); print
def completed_pretty(self): return pretty(getattr(self, "completed"))
def created_pretty(self): return pretty(getattr(self, "created"))
def req (xd, arr_disp) : if arr_disp==ARRAYDISPOSITION_AS_NUMERIC : from Numeric import array elif arr_disp==ARRAYDISPOSITION_AS_NUMPY : from numpy import array else : from simplearray import SimpleArray as array strs = [ "[]", "[1]", "[1,2]", "[ [] ] ", "[ [1] ] ", "[ [1,2] ] ", "[ {} ] ", "[ {'a':1} ] ", "[ {'a':1, 'b':2} ] ", "[ {'a':1, 'b':2}, {} ] ", "[ {'a':1, 'b':2}, {'a':1, 'b':2} ] ", "{ 'top': { 'duh' : [ { 'ds': 1, 'tcai': { 'stuff': 0 }}, {'ds': 2, 'tcai': { 'stuff': 666 }} ] } }", "[ { 'ds': 1, 'tcai': { 'stuff': 0 }}, {'ds': 2, 'tcai': { 'stuff': 666 } } ]", "[ { 'ds': [ [ { 'bd':1, 'bo':2 }, { 'bd':3, 'bo':4 } ] ], 'tcai': { 'stuff': 0 }}, {'ds': [ [ { 'bd':111, 'bo':2222 }, { 'bd':333, 'bo':444 } ] ], 'tcai': { 'stuff': 0 } } ]", "{ 'empty': [] }", "{ 'empty': [1] }", "{ 'empty': [1,2] }", "{ 'empty': [1,2, {}] }", "{ 'empty': [1,2, {'a':1}] }", "{ 'empty': [1,2, {'a':1, 'b':2}] }", "{ 'empty': [{}] }", "{ 'empty': [{}, 1] }", "{ 'empty': [{}, 1, 2] }", "{ 'empty': [{'a':1}] }", "{ 'empty': [{'a':1}, 1] }", "{ 'empty': [{'a':1}, 1, 2] }", "{ 'empty': [{'a':1, 'b':2}] }", "{ 'empty': {} }", "{ 'empty': [ 1 ] }", "{ 'empty': [ [ ] ] }", "{ 'empty': [ [1 ] ] }", "{ 'empty': [ [{}] ] }", "{ 'empty': [ [{'a':1}] ] }", "{ 'empty': [ [{'a':1, 'b':2}] ] }", "{ 'empty': [ [[]] ] }", "{ 'empty': [ [[{}]] ] }", "{ 'empty': [ [[{'a':1}]] ] }", "{ 'empty': [ [[{'a':1, 'b':2}]] ] }", "{ 'empty': [ [{},1] ] }", "{ 'empty': [ [{'a':1},1] ] }", "{ 'empty': [ [{'a':1, 'b':2},1] ] }", "{ 'empty': [ [{},1,2] ] }", "{ 'empty': [ [{'a':1},1,2] ] }", "{ 'empty': [ [{'a':1, 'b':2},1,2] ] }", "{ 'empty': [ [1,2] ] }", "{ 'empty': [ [1,2, {}] ] }", "{ 'empty': [ [1,2, {'a':1}] ] }", "{ 'empty': [ [1,2, {'a':1, 'b':2}] ] }", "{ 'empty': [ [{'a':1},2, {'a':1, 'b':2}] ] }", "{ 'empty': [ [{'a':1},2, {'a':1, 'b':2}] ] }", " [ [ { 'ds': [ [ { 'ts':1, 'td':2 }, { 'ts':4, 'td':5 } ] ], 'tcai': {'hey':1} } ] ]", "[ { 'ds': [ [ {'filename':'something', 'timedelta':1, 'empty':[], 'carl':[1] }, {'filename':'else', 'timedelta':2}] ], 'ot': {'a':1, 'b':2 } } ]", "{ 'a':1, 'SOMETHING': " "[ { 'ds': [ [ {'filename':'something', 'timedelta':1}, {'filename':'else', 'timedelta':2}], [ {'filename':'something', 'timedelta':1}, {'filename':'else', 'timedelta':2}]], 'ot': {'a':1, 'b':2 } } ]" " } ", "{ 'carl':1, 'empty':{} }", "{ 'carl':1, 'empty':[] }", "{ 'empty': array([], 'i') }", "{ 'empty': [array([], 'i')]}", "{ 'empty': [array([1], 'i')] }", "{ 'empty': [array([1,2,3], 'i')] }", "{ 'empty': [array([], 'i'),1]}", "{ 'empty': [array([1], 'i'),1] }", "{ 'empty': [array([1,2,3], 'i'),1] }", "{ 'empty': [array([], 'i'),1,2]}", "{ 'empty': [array([1], 'i'),1,2] }", "{ 'empty': [array([1,2,3], 'i'),1,2] }", "{ 'empty': [1, array([], 'i')] }", "{ 'empty': [1, array([1], 'i')] }", "{ 'empty': [1, array([1,2], 'i')] }", "{ 'empty': [1,2] }", "{ 'empty': [1,2, array([], 'i')] }", "{ 'empty': [1,2, array([1], 'i')] }", "{ 'empty': [1,2, array([1,2], 'i')] }", "{ 'empty': [1,2, {'a': array([], 'i')}] }", "{ 'empty': [1,2, {'a': array([1], 'i')}] }", "{ 'empty': [1,2, {'a': array([1,2,3], 'i')}] }", "{ 'empty': [1,2, {'a':array([],'i'), 'b':2}] }", "{ 'empty': [1,2, {'a':array([1],'i'), 'b':2}] }", "{ 'empty': [1,2, {'a':array([1,2,3],'i'), 'b':2}] }", "{ 'empty': [{'a':array([], 'i')}] }", "{ 'empty': [{'a':array([1], 'i')}] }", "{ 'empty': [{'a':array([1,2,3], 'i')}] }", "{ 'empty': [{'a':array([], 'i')},1] }", "{ 'empty': [{'a':array([1], 'i')},1] }", "{ 'empty': [{'a':array([1,2,3], 'i')},1] }", "{ 'empty': [{'a':array([], 'i')},1,2] }", "{ 'empty': [{'a':array([1], 'i')},1,2] }", "{ 'empty': [{'a':array([1,2,3], 'i')},1,2] }", "{ 'empty': [ [ ] ] }", "{ 'empty': [ [ array([],'i') ] ] }", "{ 'empty': [ [ array([1],'i') ] ] }", "{ 'empty': [ [ array([1,2,3],'i') ] ] }", "{ 'empty': [ [{}] ] }", "{ 'empty': [ [{'a':1}] ] }", "{ 'empty': [ [{'a':1, 'b':2}] ] }", "{ 'empty': [ [[array([],'i')]] ] }", "{ 'empty': [ [[array([1],'i')]] ] }", "{ 'empty': [ [[array([1,2,3],'i')]] ] }", "{ 'empty': [ [[array([1,2,3],'i')]] ] }", "{ 'data': array([(1+2j), (3+4j)], 'F') }", ] for s in strs : v = eval(s) print "-----------As normal:" pretty(v) print print "As XML:" xd.XMLDumpKeyValue("top",v); print os = StringIO.StringIO() ds = XMLDumper(os, XML_DUMP_PRETTY| XML_STRICT_HDR, arr_disp) ds.XMLDumpKeyValue("top",v) s = os.getvalue() iss = StringIO.StringIO(s) result = ReadFromXMLStream(iss, XML_LOAD_DROP_TOP_LEVEL | XML_LOAD_EVAL_CONTENT, arr_disp) pretty(result) if arr_disp==ARRAYDISPOSITION_AS_LIST : other_v = listify(v) else : other_v = v # compare if supports_numeric : test = (result!=other_v) elif supports_numpy : try : test = not equals(result, other_v) except Exception,e: print e print "********SHOULD BE SAME!!", repr(result), repr(other_v), repr(result)==repr(other_v), type(result), type(other_v), arr_disp, pretty(v) test = not ((result.shape() == other_v.shape()) and (numpy.alltrue(a,b))) if test : if (repr(result)!=repr(other_v)) : ### KLUDE print "********SHOULD BE SAME!!", repr(result), repr(other_v), repr(result)==repr(other_v), type(result), type(other_v), arr_disp, pretty(v) sys.exit(1) # Read dumping POD data slightly differently: dump pod array as data result_pod = None oss = StringIO.StringIO() dss = XMLDumper(oss, XML_DUMP_PRETTY | XML_STRICT_HDR | XML_DUMP_POD_LIST_AS_XML_LIST, arr_disp) dss.XMLDumpKeyValue("top",v) ss = oss.getvalue() sys.stdout.write("POD DATA in XML as lists::"+ss+"\n") sys.stdout.flush() isss = StringIO.StringIO(ss) ## BUG!!! Should be ss result_pod = ReadFromXMLStream(isss, XML_LOAD_DROP_TOP_LEVEL|XML_LOAD_EVAL_CONTENT, arr_disp) # cout << "POD DATA result::" << result_pod.prettyPrint(cout); if not supports_numpy : test = result_pod != other_v else : test = not equals(result_pod, other_v) if test : # (result_pod!=other_v) : if (repr(result_pod) != repr(other_v)) : #### KLUDE around empty Numeric lists print "********SHOULD BE SAME!!", result_pod, other_v sys.exit(1)