Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Serialize the Maze

00:00 Serialize the Maze. Unless you already have some data to work with, you are better off implementing the serialization code first, which will eventually write a file that you can later use to test the loading procedure.

00:16 Open the serializer module and type the code seen on-screen. First, the needed imports are performed.

00:42 Here you define the serialize() function, which accepts an instance of the Maze model that you implemented in part one of the course. This function produces a tuple consisting of the file header and body, which you’ll later dump into a binary file.

00:59 This prepares the file header by setting a known file format version based on a constant, as there’s only one version currently supported. Other header field values are copied from the maze object that was passed as an argument.

01:17 Here you populate the file body with binary values obtained by compressing the border patterns and square roles into a single byte for each square. Calling map() on the maze instance implicitly iterates over it and applies the compress() function on each square of the maze.

01:41 These lines define a helper function, which takes a Square instance as an argument and returns the corresponding Role and Border values encoded as a compound bit field.

01:51 It uses bitwise operators to compress the two values into a single number.

02:10 Writing the header and body to a file is pretty straightforward, thanks to the pathlib.Path object.

02:25 However, it’s important that you open the file for writing in binary mode to ensure that Python writes your data as is, without any implicit conversions.

02:39 With these three short functions, you can now take your miniature test maze that you created in part one and try dumping it to a file.

03:08 This should create a new file named miniature.maze in your current working directory where you started your Python REPL session. If the file already exists, then it will be overwritten without any warning, so be careful!

03:25 To quickly verify that the file was created, head over to the file manager in your operating system and look for the new file, which should be twenty-five bytes in size.

03:34 The first thirteen bytes are the header, followed by twelve bytes corresponding to the twelve squares in the maze. If you have access to a hex editor or a tool like hexdump, then you can inspect the file’s binary content, which should look as seen on-screen.

03:54 The first column is the hexadecimal offset of the first byte in each line, which shows the next sixteen bytes of the file in a two-digit hexadecimal notation.

04:05 For example, the byte at offset zero is 4d16 hexadecimal or 77 in the decimal system. This corresponds to the uppercase letter M in ASCII. On the right, you see a text representation of the same data.

04:20 The dots represent non-printable characters. Other characters depict bytes that happen to have a visual representation in ASCII. If you discard the header, you’ll be left with the last twelve bytes of the file, which translate to the values seen on-screen.

04:50 The resulting sequence of numbers consists of bit fields that should match the squares in your miniature maze. The two outliers with noticeably higher values, 60 and 37, contain extra information about the special roles of these particular squares. In this case, one must be the entrance and the other the exit.

05:12 However, you can’t know that for sure yet because you haven’t implemented the maze deserialization code, and you’ll fix that in the next part of the course.

Become a Member to join the conversation.