From the course: Python Practice: Real-World Coding Challenges
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: Single file - Python Tutorial
From the course: Python Practice: Real-World Coding Challenges
Solution: Single file
(upbeat techno music) - [Instructor] For this solution, I chose to save the canvas and scribe objects as JSON files. This is the natural choice because we have a combination of dictionaries and lists going on here, and frankly, it's just the easiest. All you need is json.dumps and it just dumps the output of the toDict method to that JSON file. On the flip side, we need the from file method. Notice that there's no self argument in here, that's because there's no Canvas instance to work with at this point. It's just a static method on the Canvas class. It does take a file name string though, and that should be the same thing that you use to create the file. I also surrounded the fromDict method with this tricep block because really anything could go wrong here. The file could be missing, not readable, not actual JSON. Even if there is JSON, there might have been a problem with how it was created. I mean, who knows. So…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.