|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/serialize-python-maze/ |
| Your Learning Plan →Your personalized Python learning plan | https://realpython.com/account/learning-plan/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest&_from=nav_library |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/search?kind=exercise&kind=quiz&_from=nav_library |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Mentor AI →NewYour personal Python mentor | https://realpython.com/mentor/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Live Courses →Live, instructor-led Python courses | https://realpython.com/live/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Unlock All Content and Become a Python Expert →Get unlimited access to all Real Python has to offer | https://realpython.com/account/join/?from=navbar_learn |
| Unlock All Content →Become a Python Expert | https://realpython.com/account/join/?from=navbar_learn |
|
More
| https://realpython.com/lessons/serialize-python-maze/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Contributor | https://realpython.com/jobs/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?intent=navbar&next=%2Flessons%2Fserialize-python-maze%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-maze-solver |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-maze-solver |
|
| https://realpython.com/courses/python-maze-solver/#team |
| Mazes in Python: Build, Visualize, Store, and Solve | https://realpython.com/courses/python-maze-solver/ |
| Darren Jones | https://realpython.com/courses/python-maze-solver/#team |
| Recommended Tutorial | https://realpython.com/python-maze-solver/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-maze-solver/downloads/mazes-in-python-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-maze-solver/downloads/mazes-in-python-code/ |
| Ask a Question | https://realpython.com/lessons/serialize-python-maze/#discussion |
| https://realpython.com/feedback/survey/course/python-maze-solver/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-maze-solver/disliked/?from=lesson-title |
| Contents | https://realpython.com/lessons/serialize-python-maze/#description |
| Transcript | https://realpython.com/lessons/serialize-python-maze/#transcript |
| Discussion | https://realpython.com/lessons/serialize-python-maze/#discussion |
| 00:00 | https://realpython.com/lessons/serialize-python-maze/#t=0.87 |
| Serialize the Maze. | https://realpython.com/lessons/serialize-python-maze/#t=0.87 |
| Unless you already have some data to work with, | https://realpython.com/lessons/serialize-python-maze/#t=2.232 |
| you are better off implementing the serialization code first, | https://realpython.com/lessons/serialize-python-maze/#t=5.72 |
| which will eventually write a file that you can later use to test the loading | https://realpython.com/lessons/serialize-python-maze/#t=8.79 |
| procedure. | https://realpython.com/lessons/serialize-python-maze/#t=12.9 |
| 00:16 | https://realpython.com/lessons/serialize-python-maze/#t=16.05 |
| Open the serializer module and type the code seen on-screen. | https://realpython.com/lessons/serialize-python-maze/#t=16.05 |
| First, the needed imports are performed. | https://realpython.com/lessons/serialize-python-maze/#t=21.33 |
| 00:42 | https://realpython.com/lessons/serialize-python-maze/#t=42.61 |
| Here you define the serialize() function, | https://realpython.com/lessons/serialize-python-maze/#t=42.61 |
| which accepts an instance of the Maze model that you implemented in part one of | https://realpython.com/lessons/serialize-python-maze/#t=44.75 |
| the course. | https://realpython.com/lessons/serialize-python-maze/#t=48.74 |
| 00:49 | https://realpython.com/lessons/serialize-python-maze/#t=49.506 |
| This function produces a tuple consisting of the file header | https://realpython.com/lessons/serialize-python-maze/#t=49.506 |
| and body, which you’ll later dump into a binary file. | https://realpython.com/lessons/serialize-python-maze/#t=53.4 |
| This prepares the file header by setting a known file format version based on a | https://realpython.com/lessons/serialize-python-maze/#t=59.84 |
| constant, as there’s only one version currently supported. | https://realpython.com/lessons/serialize-python-maze/#t=64.53 |
| 01:07 | https://realpython.com/lessons/serialize-python-maze/#t=67.93 |
| Other header field values are copied from the maze object that was passed as an | https://realpython.com/lessons/serialize-python-maze/#t=67.93 |
| argument. | https://realpython.com/lessons/serialize-python-maze/#t=71.84 |
| Here you populate the file body with binary values obtained by compressing the | https://realpython.com/lessons/serialize-python-maze/#t=77.2 |
| border patterns and square roles into a single byte for each square. | https://realpython.com/lessons/serialize-python-maze/#t=81.13 |
| 01:25 | https://realpython.com/lessons/serialize-python-maze/#t=85.36 |
| Calling map() on the maze instance implicitly iterates over it and applies the | https://realpython.com/lessons/serialize-python-maze/#t=85.36 |
| compress() function on each square of the maze. | https://realpython.com/lessons/serialize-python-maze/#t=90.13 |
| 01:41 | https://realpython.com/lessons/serialize-python-maze/#t=101.29 |
| These lines define a helper function, | https://realpython.com/lessons/serialize-python-maze/#t=101.29 |
| which takes a Square instance as an argument and returns the corresponding Role | https://realpython.com/lessons/serialize-python-maze/#t=103.21 |
| and Border values encoded as a compound bit field. | https://realpython.com/lessons/serialize-python-maze/#t=107.38 |
| 01:51 | https://realpython.com/lessons/serialize-python-maze/#t=111.23 |
| It uses bitwise operators to compress the two values into a single number. | https://realpython.com/lessons/serialize-python-maze/#t=111.23 |
| 02:10 | https://realpython.com/lessons/serialize-python-maze/#t=130.57 |
| Writing the header and body to a file is pretty straightforward, thanks to the | https://realpython.com/lessons/serialize-python-maze/#t=130.57 |
| pathlib.Path object. | https://realpython.com/lessons/serialize-python-maze/#t=134.82 |
| 02:25 | https://realpython.com/lessons/serialize-python-maze/#t=145.9 |
| However, | https://realpython.com/lessons/serialize-python-maze/#t=145.9 |
| it’s important that you open the file for writing in binary mode to ensure that | https://realpython.com/lessons/serialize-python-maze/#t=146.7 |
| Python writes your data as is, without any implicit conversions. | https://realpython.com/lessons/serialize-python-maze/#t=150.79 |
| 02:39 | https://realpython.com/lessons/serialize-python-maze/#t=159.27 |
| With these three short functions, | https://realpython.com/lessons/serialize-python-maze/#t=159.27 |
| you can now take your miniature test maze that you created in part one and try | https://realpython.com/lessons/serialize-python-maze/#t=161.1 |
| dumping it to a file. | https://realpython.com/lessons/serialize-python-maze/#t=165.0 |
| 03:08 | https://realpython.com/lessons/serialize-python-maze/#t=188.69 |
| This should create a new file named miniature.maze in your current working | https://realpython.com/lessons/serialize-python-maze/#t=188.69 |
| directory where you started your Python REPL session. | https://realpython.com/lessons/serialize-python-maze/#t=192.94 |
| If the file already exists, then it will be overwritten without any warning, | https://realpython.com/lessons/serialize-python-maze/#t=196.72 |
| so be careful! | https://realpython.com/lessons/serialize-python-maze/#t=200.24 |
| 03:25 | https://realpython.com/lessons/serialize-python-maze/#t=205.28 |
| To quickly verify that the file was created, | https://realpython.com/lessons/serialize-python-maze/#t=205.28 |
| head over to the file manager in your operating system and look for the new | https://realpython.com/lessons/serialize-python-maze/#t=207.49 |
| file, which should be twenty-five bytes in size. | https://realpython.com/lessons/serialize-python-maze/#t=211.22 |
| 03:34 | https://realpython.com/lessons/serialize-python-maze/#t=214.76 |
| The first thirteen bytes are the header, | https://realpython.com/lessons/serialize-python-maze/#t=214.76 |
| followed by twelve bytes corresponding to the twelve squares in the maze. | https://realpython.com/lessons/serialize-python-maze/#t=216.98 |
| If you have access to a hex editor or a tool like hexdump, | https://realpython.com/lessons/serialize-python-maze/#t=223.06 |
| then you can inspect the file’s binary content, | https://realpython.com/lessons/serialize-python-maze/#t=226.23 |
| which should look as seen on-screen. | https://realpython.com/lessons/serialize-python-maze/#t=228.57 |
| 03:54 | https://realpython.com/lessons/serialize-python-maze/#t=234.8 |
| The first column is the hexadecimal offset of the first byte in each line, | https://realpython.com/lessons/serialize-python-maze/#t=234.8 |
| which shows the next sixteen bytes of the file in a two-digit hexadecimal notation. | https://realpython.com/lessons/serialize-python-maze/#t=239.11 |
| 04:05 | https://realpython.com/lessons/serialize-python-maze/#t=245.06 |
| For example, | https://realpython.com/lessons/serialize-python-maze/#t=245.06 |
| the byte at offset zero is 4d16 hexadecimal or 77 in the decimal | https://realpython.com/lessons/serialize-python-maze/#t=245.74 |
| system. | https://realpython.com/lessons/serialize-python-maze/#t=250.64 |
| This corresponds to the uppercase letter M in ASCII. | https://realpython.com/lessons/serialize-python-maze/#t=251.12 |
| On the right, you see a text representation of the same data. | https://realpython.com/lessons/serialize-python-maze/#t=256.08 |
| 04:20 | https://realpython.com/lessons/serialize-python-maze/#t=260.2 |
| The dots represent non-printable characters. | https://realpython.com/lessons/serialize-python-maze/#t=260.2 |
| Other characters depict bytes that happen to have a visual representation | https://realpython.com/lessons/serialize-python-maze/#t=263.63 |
| in ASCII. | https://realpython.com/lessons/serialize-python-maze/#t=267.4 |
| If you discard the header, you’ll be left with the last twelve bytes of the file, | https://realpython.com/lessons/serialize-python-maze/#t=271.37 |
| which translate to the values seen on-screen. | https://realpython.com/lessons/serialize-python-maze/#t=275.32 |
| 04:50 | https://realpython.com/lessons/serialize-python-maze/#t=290.4 |
| The resulting sequence of numbers consists of bit fields that should match the | https://realpython.com/lessons/serialize-python-maze/#t=290.4 |
| squares in your miniature maze. | https://realpython.com/lessons/serialize-python-maze/#t=294.34 |
| The two outliers with noticeably higher values, | https://realpython.com/lessons/serialize-python-maze/#t=296.296 |
| 60 and 37, contain extra information about the special roles of these | https://realpython.com/lessons/serialize-python-maze/#t=299.64 |
| particular squares. | https://realpython.com/lessons/serialize-python-maze/#t=304.31 |
| 05:06 | https://realpython.com/lessons/serialize-python-maze/#t=306.057 |
| In this case, | https://realpython.com/lessons/serialize-python-maze/#t=306.057 |
| one must be the entrance and the other the exit. | https://realpython.com/lessons/serialize-python-maze/#t=307.5 |
| However, | https://realpython.com/lessons/serialize-python-maze/#t=312.27 |
| you can’t know that for sure yet because you haven’t implemented the maze | https://realpython.com/lessons/serialize-python-maze/#t=312.98 |
| deserialization code, and you’ll fix that in the next part of the course. | https://realpython.com/lessons/serialize-python-maze/#t=316.46 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/python-maze-file-header-and-body/ |
| Overview | https://realpython.com/courses/python-maze-solver/ |
| https://realpython.com/lessons/deserialize-python-maze/ |
|
Mazes in Python: Build and Visualize 02:47
| https://realpython.com/videos/python-maze-solver-part-1-overview/ |
|
Check Out What You'll Make 02:39
| https://realpython.com/videos/python-maze-solver-project/ |
|
Lay the Groundwork 04:02
| https://realpython.com/videos/python-maze-solver-groundwork/ |
|
Scaffold the Project Structure 04:50
| https://realpython.com/lessons/python-maze-solver-structure/ |
|
Represent the Maze With Objects 06:37
| https://realpython.com/lessons/represent-maze-with-objects/ |
|
Create Border Patterns 06:32
| https://realpython.com/lessons/python-maze-border-patterns/ |
|
Model the Square 02:25
| https://realpython.com/lessons/model-python-maze-square/ |
|
Build the Maze 09:07
| https://realpython.com/lessons/build-python-maze/ |
|
Visualize the Maze With SVG 04:45
| https://realpython.com/lessons/python-maze-svg/ |
|
Implement Geometric Primitives 04:48
| https://realpython.com/lessons/python-maze-geometric-primitives/ |
|
Add More Primitives 03:47
| https://realpython.com/lessons/python-maze-more-primitives/ |
|
Decompose the Border Into Primitives 04:41
| https://realpython.com/lessons/decompose-python-maze-border/ |
|
Add More Border Patterns 03:58
| https://realpython.com/lessons/python-maze-more-border-patterns/ |
|
Build the SVG Renderer 03:49
| https://realpython.com/lessons/python-maze-svg-renderer/ |
|
Add Features to the Renderer 06:50
| https://realpython.com/lessons/python-maze-renderer-features/ |
|
Draw the Solution 03:13
| https://realpython.com/lessons/draw-python-maze-solution/ |
|
Preview the Rendered Maze and Its Solution 04:56
| https://realpython.com/lessons/preview-python-maze/ |
|
Mazes in Python: Build and Visualize (Recap) 01:14
| https://realpython.com/lessons/python-maze-solver-part-1-summary/ |
|
Mazes in Python: Store and Solve 01:48
| https://realpython.com/videos/python-maze-solver-part-2-overview/ |
|
Load the Maze From a Binary File 04:55
| https://realpython.com/videos/python-maze-binary-file/ |
|
Choose Your Data Alignment 05:09
| https://realpython.com/videos/python-maze-data-alignment/ |
|
Define the File Header and Body 08:12
| https://realpython.com/lessons/python-maze-file-header-and-body/ |
|
Serialize the Maze 05:22
| https://realpython.com/lessons/serialize-python-maze/ |
|
Deserialize the Maze 04:32
| https://realpython.com/lessons/deserialize-python-maze/ |
|
Make the Maze Self-Contained 04:30
| https://realpython.com/lessons/python-self-contained-maze/ |
|
Update the Maze Class 03:16
| https://realpython.com/lessons/python-maze-class/ |
|
Solve the Maze Using a Graph-Based Approach 03:01
| https://realpython.com/lessons/solve-python-maze-graph-based/ |
|
Transform the Maze Into a Graph 07:06
| https://realpython.com/lessons/transform-python-maze-graph/ |
|
Find the Shortest Path 05:23
| https://realpython.com/lessons/python-maze-shortest-path/ |
|
Add Weights to Graph Edges 02:36
| https://realpython.com/lessons/python-map-weights-graph-edges/ |
|
Introduce Enemies and Rewards 06:04
| https://realpython.com/lessons/python-maze-enemies-rewards/ |
|
Make a Runnable Script 03:59
| https://realpython.com/lessons/python-maze-runnable-script/ |
|
Mazes in Python: Store and Solve (Recap) 01:23
| https://realpython.com/lessons/python-maze-solver-part-2-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover