From the course: Effective Serialization with Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: repr and read pickle

Solution: repr and read pickle - Python Tutorial

From the course: Effective Serialization with Python

Start my 1-month free trial

Solution: repr and read pickle

(upbeat music) - [Instructor] We are adding a __repr__. So I've done the __repr__, and then we get the class name dynamically. We get the start, end, dist and we use short names and the same is for number of passengers and then we use the f string formatting to create a class. Which is a start, an end, a dist and a number of passengers using the !r to get the __repr__ of each value as well. And then, to load the data from the file, we open the file in a binary format and do a while True. We load the ride with pickle.load and print out the ride. And once there is no more data in the file, we will except and end the file Error, and we will break, exiting the loop. Let's see it in action. python rides.py and we see that we have a nice __repr__ for each ride that we loaded from the pickle file.

Contents