Question: I would like to see why the folloing python code is not fully passing when I run a tester on my code: Code: import tkinter

I would like to see why the folloing python code is not fully passing when I run a tester on my code:
Code:
import tkinter as tk
from rectpack import newPacker
import sys
class CustomCanvas:
def __init__(self, height: int, width: int):
self.canvas = tk.Canvas(width=width, height=height, bg='white')
self.canvas.pack()
class Rectangle:
def __init__(self, height: int, width: int, x: int =0, y: int =0):
self.height = height
self.width = width
self.x = x
self.y = y
def __repr__(self):
return f"Rectangle(height ={self.height}, width ={self.width}, x ={self.x}, y ={self.y})"
def pack(all_rect, canvas_size):
# Create a new packer instance
packer = newPacker()
# Add rectangles to the packer (note: dimensions are width x height)
for rect in all_rect:
packer.add_rect(rect.width, rect.height)
# Define the dimensions of the canvas
canvas_width, canvas_height = canvas_size
# Add the canvas as a bin to the packer
packer.add_bin(canvas_width, canvas_height)
# Perform the packing
packer.pack()
# Retrieve the packed rectangles
packed_rectangles =[]
for rect in packer.rect_list():
# Unpack the values: width, height, x, y, bin_index, rotation
width, height, x, y, bin_index, rotation = rect
# Adjust dimensions for rotation if needed
if rotation ==1:
# The rectangle was rotated, so swap width and height
packed_rectangles.append(Rectangle(width, height, x, y))
else:
# No rotation
packed_rectangles.append(Rectangle(height, width, x, y))
return packed_rectangles
def main():
if len(sys.argv)!=2:
print("Usage: python Assignment2.py ")
sys.exit(1)
filepath = sys.argv[1]
with open(filepath,'r') as file:
lines = file.readlines()
# Parse canvas size
canvas_height, canvas_width = map(int, lines[0].strip().split(','))
# Create canvas object
canvas = CustomCanvas(canvas_height, canvas_width)
# Parse the rectangles
rectangles =[]
for line in lines[1:]:
height, width = map(int, line.strip().split(','))
rectangles.append(Rectangle(height, width))
# Pack rectangles
packed_rectangles = pack(rectangles,(canvas_width, canvas_height))
# Draw rectangles on canvas
for rect in packed_rectangles:
canvas.canvas.create_rectangle(
rect.x, rect.y, rect.x + rect.width, rect.y + rect.height,
outline='black', fill='lightblue'
)
# Start Tkinter loop
tk.mainloop()
if __name__=="__main__":
main()
Tester Results:
Import Tests
PASS: Rectangle & pack imported without error
Rectangle Tests
PASS: Creating Rectangle with 4 args
PASS: Testing instantiated Rectangle properties
PASS: Creating Rectangle with 2 args
Pack Tests
Import Tests
PASS: Rectangle & pack imported without error
FAIL: Returned Rectangle was not logically equlivant to given rectangle at index 0
Import Tests
PASS: Rectangle & pack imported without error
PASS: 25% fill packed correctly
Import Tests
PASS: Rectangle & pack imported without error
PASS: 50% fill packed correctly
Import Tests
PASS: Rectangle & pack imported without error
PASS: 75% fill packed correctly
Import Tests
PASS: Rectangle & pack imported without error
PASS: 95% fill packed correctly

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 Programming Questions!