๐Ÿš— Abstraction in Python: The Single Bit Flip That Killed Toyotaโ€™s Accelerator

# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment

responses = initialize_assignment("14_toyota_accelerator_q", "week_7", "readings", assignment_points = 19.0, assignment_tag = 'week7-readings')

# Initialize Otter
import otter
grader = otter.Notebook("14_toyota_accelerator_q.ipynb")

๐Ÿš— Abstraction in Python: The Single Bit Flip That Killed Toyotaโ€™s Accelerator#

In 2009, a major recall of Toyota vehicles was triggered by sudden unintended acceleration incidents. Investigations revealed that a single bit flip in memory could cause a stuck accelerator, leading to catastrophic failures.

This was due to software complexityโ€”Toyotaโ€™s acceleration control system had millions of lines of code, with poor abstraction making debugging nearly impossible.

In Python, abstraction helps us hide complexity by defining base classes and abstract methods, ensuring only essential details are exposed.

Letโ€™s explore how abstraction in Python can help us design safer, more manageable software systemsโ€”just as Toyota engineers should have done.

# Run this block of code by pressing Shift + Enter to display the question
from questions._14_toyota_accelerator_q import Question1
Question1().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._14_toyota_accelerator_q import Question2
Question2().show()
# Run this block of code by pressing Shift + Enter to display the question
from questions._14_toyota_accelerator_q import Question3
Question3().show()

Question 1 (Points: 11.0): โš™๏ธ Free Response: Implement Abstraction for a Safer Accelerator System#

Implement a safe accelerator control system using abstraction:

  • Create an abstract class AcceleratorControl with:

    • An abstract method apply_brakes().

    • An abstract method override_acceleration().

Note: since we donโ€™t want to implement these methods in the base class, we use the @abstractmethod decorator, and pass as a placeholder.

  • Create a subclass ToyotaAccelerator that:

    • Implements apply_brakes() by printing "Brakes applied!".

    • Implements override_acceleration() by printing "Acceleration override engaged!".

Example Usage#

t = ToyotaAccelerator()
t.apply_brakes()
t.override_acceleration()

Expected Output:

Brakes applied!
Acceleration override engaged!
...

# Example usage
t = ToyotaAccelerator()
t.apply_brakes()
t.override_acceleration()
grader.check("Toyota-Abstraction-Class")

Submitting Assignment#

Please run the following block of code using shift + enter to submit your assignment, you should see your score.

from pykubegrader.submit.submit_assignment import submit_assignment

submit_assignment("week7-readings", "14_toyota_accelerator_q")