Question: New ( additional ) requirements You need to add a new method, named left _ outer _ join that implements a LEFT OUTER JOIN and

New (additional) requirements
You need to add a new method, named left_outer_join that implements a LEFT OUTER JOIN and returns a new table that is the result of such a join. This method takes a table (the right side of a join), a column name (a string) that denotes the name of the left tables column, and another column name (a string) that denotes the name of the right tables column. The rows of the focal (self) table should be joined on equality of the values of the two specified columns.
Note: this method returns a new table, neither of the original tables should be altered. You can assume that all column names are unique and that all rows are well-formed.
here's my code:
class Table:
def __init__(self):
self.data =[]
def insert_into(self, row):
self.data.append(row)
def select(self, columns_to_display, columns_to_order_by=None, ascending=True):
if columns_to_order_by:
self.data.sort(
key=lambda row: tuple(row.get(col,"") for col in columns_to_order_by),
reverse=not ascending,
)
result =[
[row.get(col, None) for col in columns_to_display] for row in self.data
]
return result
def left_outer_join(self, right_table, left_column, right_column):
result_table = Table()
for left_row in self.data:
match_found = False
for right_row in right_table.data:
if left_row.get(left_column)== right_row.get(right_column):
result_row = left_row.copy()
result_row.update(right_row)
result_table.insert_into(result_row)
match_found = True
if not match_found:
result_row = left_row.copy()
result_table.insert_into(result_row)
return result_table
here's the test case I am failing:
import unittest
from pprint import pprint
import sys
import os
sys.path.append("/home/codio/workspace/student_code/")
from table_code import Table
def clean_file(filename):
# remove file if exists for a clean test.
if os.path.exists(filename):
os.remove(filename)
else:
print("Can not delete the file as it doesn't exists")
class TestTableCode(unittest.TestCase):
def no_sqlite3_import(self):
#!NO_IMPORT
sys.modules['sqlite3']= None
import table_code
def test_advanced(self):
left_table = Table()
left_table.insert_into({'name': 'Josh', 'age': 31})
left_table.insert_into({'name': 'Josh', 'age': 10})
left_table.insert_into({'name': 'Josh', 'age': 12})
left_table.insert_into({'name': 'Emily', 'age': 29})
left_table.insert_into({'name': 'Charles', 'age': 40})
left_table.insert_into({'name': None, 'age': 67})
left_table.insert_into({'name': None, 'age': 99})
right_table = Table()
right_table.insert_into({'first_name': 'Josh', 'city': 'Pomona'})
right_table.insert_into({'first_name': 'Josh', 'city': 'Gig Harbor'})
right_table.insert_into({'first_name': 'Emily', 'city': 'San Diego'})
right_table.insert_into({'first_name': 'Cliff', 'city': 'Los Angles'})
right_table.insert_into({'first_name': 'Josh', 'city': 'Seattle'})
right_table.insert_into({'first_name': None, 'city': 'East Lansing'})
join_table = left_table.left_outer_join(right_table, 'name', 'first_name')
# check join table
result = join_table.select(['city', 'first_name', 'name', 'age'],['age', 'city'])
pprint(result)
expected =[['Gig Harbor', 'Josh', 'Josh', 10],
['Pomona', 'Josh', 'Josh', 10],
['Seattle', 'Josh', 'Josh', 10],
['Gig Harbor', 'Josh', 'Josh', 12],
['Pomona', 'Josh', 'Josh', 12],
['Seattle', 'Josh', 'Josh', 12],
['San Diego', 'Emily', 'Emily', 29],
['Gig Harbor', 'Josh', 'Josh', 31],
['Pomona', 'Josh', 'Josh', 31],
['Seattle', 'Josh', 'Josh', 31],
[None, None, 'Charles', 40],
[None, None, None, 67],
[None, None, None, 99]]
self.assertEqual(expected, result)
if __name__=='__main__':
unittest.main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!