class Attribute:
def __init__(self, an, at, ky):
self.aname = an # attribute name, e.g., "sno"
self.atype = at # attribute type, must be "INTEGER" or "STRING"
self.key = ky # boolean. True indicating attribute is KEY, False otherwise
# getters
def get_aname(self):
return self.aname
def get_atype(self):
return self.atype
def get_key(self):
return self.key
# setters
def set_aname(self,an):
self.aname = an
def set_atype(self,at):
self.atype = at
def set_key(self,ky):
self.key = ky
# __str__
def __str__(self):
if self.key:
return self.aname + ":" + self.atype + ":KEY"
else:
return self.aname + ":" + self.atype
a1 = Attribute("sno","INTEGER",True)
a2 = Attribute("sname","STRING",False)
a3 = Attribute("gpa","INTEGER",False)
print(a2)
sname:STRING
a2.set_aname("major")
print(a2)
major:STRING
a2.get_aname()
'major'
class Attributes:
def __init__(self):
self.attributes = []
# insert attribute a in position pos
# return True if success, False otherwise
def insert(self,a,pos):
if pos <= 0 or pos > len(self.attributes)+1:
return False
self.attributes.insert(pos-1,a)
#self.attributes = self.attribute[0:pos-1] + [a] + self.attributes[pos-1:]
# delete attribute with name aname
# return True if success, False otherwise
def delete(self,aname):
pass
# return True if attribute with name aname is present
# return False otherwise
def member(self,aname):
pass
# return number of attributes
def size(self):
return len(self.attributes)
# return string to print attributes.
# see sample run of program for exact formatting
def __str__(self):
pass
xs = [1,2,3]
pos = 3
#xs.insert(pos-1,10)
xs = xs[0:pos-1] + [10] + xs[pos-1:]
print(xs)
[1, 2, 10, 3]
Order in which you will complete the parts in P5:
class Relation:
def __init__(self, rname, attrs):
self.rname = rname
self.attributes = attrs
# return string representation of relation
# see sample run for formatting
def __str__(self):
pass
xs = [1,2,3]
print(xs)
xs.append(5)
print(xs)
[1, 2, 3] [1, 2, 3, 5]
#from Attributes import *
#from Relation import *
class Relations:
def __init__(self):
self.relations = {}
# insert relation object r into dictionary
# return True if success, False otherwise
def insert(self,r):
pass
# delete relation with name rname from dictionary
# return relation object for name if success, None otherwise
def delete(self,rname):
pass
# return relation object for relation with name rname
# return None if not found
def get_relation(self,rname):
pass
# return string representation of all relation objects
# see sample run for formatting
def __str__(self):
pass
# student,sno:i:y,sname:s:n,phone:s:n
def load_schema(fname):
with open(fname) as f:
data = f.read().splitlines()
relations = Relations()
for d in data:
# rname,*attrs = d.split(",") # this can replace next 3 lines
xs = d.split(",")
rname = xs[0]
attrs = xs[1:]
attrs_obj = Attributes()
for pos,attr in enumerate(attrs):
aname,atype,key = attr.split(":")
key = True if key=='y' else False
#key = (key == 'y')
atype = "STRING" if atype=='s' else "INTEGER"
a = Attribute(aname,atype,key)
attrs_obj.insert(a,pos+1)
r = Relation(rname,attrs_obj)
relations.insert(r)