@ -86,7 +86,7 @@ class relation (object):
res + = " " . join ( r )
res + = " " . join ( r )
fp . write ( res )
fp . write ( res )
fp . close ( ) #Closing file
fp . close ( ) #Closing file
def rearrange( self , other ) :
def _ rearrange_ ( self , other ) :
''' If two relations share the same attributes in a different order, this method
''' If two relations share the same attributes in a different order, this method
will use projection to make them have the same attributes ' order.
will use projection to make them have the same attributes ' order.
It is not exactely related to relational algebra . Just a method used
It is not exactely related to relational algebra . Just a method used
@ -201,7 +201,7 @@ class relation (object):
Will return an empty one if there are no common items .
Will return an empty one if there are no common items .
Will return None if headers are different .
Will return None if headers are different .
It is possible to use projection and rename to make headers match . '''
It is possible to use projection and rename to make headers match . '''
other = self . rearrange( other ) #Rearranges attributes' order
other = self . _ rearrange_ ( other ) #Rearranges attributes' order
if ( self . __class__ != other . __class__ ) or ( self . header != other . header ) :
if ( self . __class__ != other . __class__ ) or ( self . header != other . header ) :
return None
return None
newt = relation ( )
newt = relation ( )
@ -219,7 +219,7 @@ class relation (object):
Will return an empty one if the second is a superset of first .
Will return an empty one if the second is a superset of first .
Will return None if headers are different .
Will return None if headers are different .
It is possible to use projection and rename to make headers match . '''
It is possible to use projection and rename to make headers match . '''
other = self . rearrange( other ) #Rearranges attributes' order
other = self . _ rearrange_ ( other ) #Rearranges attributes' order
if ( self . __class__ != other . __class__ ) or ( self . header != other . header ) :
if ( self . __class__ != other . __class__ ) or ( self . header != other . header ) :
return None
return None
newt = relation ( )
newt = relation ( )
@ -230,7 +230,20 @@ class relation (object):
if e not in other . content :
if e not in other . content :
newt . content . append ( list ( e ) )
newt . content . append ( list ( e ) )
return newt
return newt
def division ( self , other ) :
''' Division operator
The division is a binary operation that is written as R ÷ S . The
result consists of the restrictions of tuples in R to the
attribute names unique to R , i . e . , in the header of R but not in the
header of S , for which it holds that all their combinations with tuples
in S are present in R .
'''
d_headers = list ( set ( self . header . attributes ) - set ( other . header . attributes ) )
t = self . projection ( d_headers ) . product ( other )
u = t . difference ( self )
v = u . projection ( d_headers )
return self . projection ( d_headers ) . difference ( v )
def union ( self , other ) :
def union ( self , other ) :
''' Union operation. The result will contain items present in first
''' Union operation. The result will contain items present in first
and second operands .
and second operands .
@ -238,7 +251,7 @@ class relation (object):
Will not insert tuplicated items .
Will not insert tuplicated items .
Will return None if headers are different .
Will return None if headers are different .
It is possible to use projection and rename to make headers match . '''
It is possible to use projection and rename to make headers match . '''
other = self . rearrange( other ) #Rearranges attributes' order
other = self . _ rearrange_ ( other ) #Rearranges attributes' order
if ( self . __class__ != other . __class__ ) or ( self . header != other . header ) :
if ( self . __class__ != other . __class__ ) or ( self . header != other . header ) :
return None
return None
newt = relation ( )
newt = relation ( )
@ -372,7 +385,7 @@ class relation (object):
def __eq__ ( self , other ) :
def __eq__ ( self , other ) :
''' Returns true if the relations are the same, ignoring order of items.
''' Returns true if the relations are the same, ignoring order of items.
This operation is rather heavy , since it requires sorting and comparing . '''
This operation is rather heavy , since it requires sorting and comparing . '''
other = self . rearrange( other ) #Rearranges attributes' order so can compare tuples directly
other = self . _ rearrange_ ( other ) #Rearranges attributes' order so can compare tuples directly
if ( self . __class__ != other . __class__ ) or ( self . header != other . header ) :
if ( self . __class__ != other . __class__ ) or ( self . header != other . header ) :
return False #Both parameters must be a relation
return False #Both parameters must be a relation
@ -548,7 +561,4 @@ class header (object):
if i == self . attributes [ j ] :
if i == self . attributes [ j ] :
res . append ( j )
res . append ( j )
return res
return res
if __name__ == " __main__ " :
pass