Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 leftouterjoin 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 wellformed.
here's my code:
class Table:
def initself:
self.data
def insertintoself row:
self.data.appendrow
def selectself columnstodisplay, columnstoorderbyNone, ascendingTrue:
if columnstoorderby:
self.data.sort
keylambda row: tuplerowgetcol for col in columnstoorderby
reversenot ascending,
result
rowgetcol None for col in columnstodisplay for row in self.data
return result
def leftouterjoinself righttable, leftcolumn, rightcolumn:
resulttable Table
for leftrow in self.data:
matchfound False
for rightrow in righttable.data:
if leftrow.getleftcolumn rightrow.getrightcolumn:
resultrow leftrow.copy
resultrow.updaterightrow
resulttable.insertintoresultrow
matchfound True
if not matchfound:
resultrow leftrow.copy
resulttable.insertintoresultrow
return resulttable
here's the test case I am failing:
import unittest
from pprint import pprint
import sys
import os
syspath.appendhomecodioworkspacestudentcode
from tablecode import Table
def cleanfilefilename:
# remove file if exists for a clean test.
if ospath.existsfilename:
osremovefilename
else:
printCan not delete the file as it doesn't exists"
class TestTableCodeunittestTestCase:
def nosqliteimportself:
#NOIMPORT
sysmodulessqlite None
import tablecode
def testadvancedself:
lefttable Table
lefttable.insertintoname: 'Josh', 'age':
lefttable.insertintoname: 'Josh', 'age':
lefttable.insertintoname: 'Josh', 'age':
lefttable.insertintoname: 'Emily', 'age':
lefttable.insertintoname: 'Charles', 'age':
lefttable.insertintoname: None, 'age':
lefttable.insertintoname: None, 'age':
righttable Table
righttable.insertintofirstname': 'Josh', 'city': 'Pomona'
righttable.insertintofirstname': 'Josh', 'city': 'Gig Harbor'
righttable.insertintofirstname': 'Emily', 'city': 'San Diego'
righttable.insertintofirstname': 'Cliff', 'city': 'Los Angles'
righttable.insertintofirstname': 'Josh', 'city': 'Seattle'
righttable.insertintofirstname': None, 'city': 'East Lansing'
jointable lefttable.leftouterjoinrighttable, 'name', 'firstname'
# check join table
result jointable.selectcity 'firstname', 'name', 'age'age 'city'
pprintresult
expected Gig Harbor', 'Josh', 'Josh',
Pomona 'Josh', 'Josh',
Seattle 'Josh', 'Josh',
Gig Harbor', 'Josh', 'Josh',
Pomona 'Josh', 'Josh',
Seattle 'Josh', 'Josh',
San Diego', 'Emily', 'Emily',
Gig Harbor', 'Josh', 'Josh',
Pomona 'Josh', 'Josh',
Seattle 'Josh', 'Josh',
None None, 'Charles',
None None, None,
None None, None,
self.assertEqualexpected result
if namemain:
unittest.main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started