https://www.inspiredpython.com/Inspired Python2024-03-28T09:06:17.578915+00:00Inspired Pythonhello@removethis.inspiredpython.compython-feedgen/static/logo.pngCopyright (C) Inspired Python. All Rights Reserved.Grow your Python skills with our articles and courseshttps://www.inspiredpython.com/course/pattern-matching/python-pattern-matching-examples-working-with-paths-and-filesPython Pattern Matching Examples: Working with Paths and Files2021-11-16T16:32:34.736682+00:00Inspired Python<span>
<section>
<h2>Python Pattern Matching Examples: Working with Paths and Files</h2>
<span></span><p>Manipulating file and path strings is dreary work. It is a common activity, particularly in data science where the file structure may contain important semantic clues like a date or the source of the data. Contextualizing that information is usually done with a mixture of <code>if</code> statements and liberal use of pathlib’s <code>Path</code> or <code>os.path</code>, but the <em>structural pattern matching</em> feature in Python 3.10 is there to cut down on the tedium.</p>
<p>Consider a directory structure that looks a bit like this:</p>
<pre>cpi/<country>/by-month/<yyyy-mm-dd>/<filename>.<ext>
cpi/<country>/by-quarter/<yyyy-qq>/<filename>.<ext></pre>
<p>Where <code>cpi</code> means <em>Consumer Price Index</em>; <code>country</code> is the ISO-3166 code for a country; <code>yyyy-mm-dd</code> is the ISO date for the particular month; <code>yyyy-qq</code> is the year and quarter; and <code>filename</code> is an arbitrary filename and <code>ext</code> is an extension.</p>
<p>Ordinarily, you’d just split the path and write some quick logic that picks out what you need, and that’ll work fine for simple things, but if you have to deal with dozens of variadic <em>fields</em> in the file path, that approach will not scale. So let’s look at a way that will scale using the <code>match</code> and <code>case</code> keywords.</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/course/pattern-matching/python-pattern-matching-examples-working-with-paths-and-files"><strong>Read More -></strong></a>2021-11-16T16:32:34.736682+00:00https://www.inspiredpython.com/course/testing-with-hypothesis/testing-your-python-code-with-hypothesisTesting your Python Code with Hypothesis2021-12-02T15:31:21.748742+00:00Inspired Python<span>
<section>
<h2>Testing your Python Code with Hypothesis</h2>
<span></span><p>I can think of a several Python packages that greatly improved the quality of the software I write. Two of them are <a>pytest</a> and <a>hypothesis</a>. The former adds an ergonomic framework for writing tests and fixtures and a feature-rich test runner. The latter adds <em>property-based testing</em> that can ferret out all but the most stubborn bugs using clever algorithms, and that’s the package we’ll explore in this course.</p>
<p>In an ordinary test you interface with the code you want to test by generating one or more inputs to test against, and then you validate that it returns the right answer. But that, then, raises a tantalizing question: what about all the inputs you <em>didn’t</em> test? Your code coverage tool may well report 100% test coverage, but that does not, <em>ipso facto</em>, mean the code is bug-free.</p>
<p>One of the defining features of Hypothesis is its ability to generate test cases automatically in a manner that is:</p>
<dl>
<dt>
<dt>Reproducible</dt>
<dd>
<p>Repeated invocations of your tests result in reproducible outcomes, even though Hypothesis does use randomness to generate the data.</p>
</dd>
</dt>
<dt>
<dt>Methodical</dt>
<dd>
<p>You are given a detailed answer that explains how your test failed and <em>why</em> it failed. Hypothesis makes it clear how you, the human, can reproduce the invariant that caused your test to fail.</p>
</dd>
</dt>
<dt>
<dt>Configurable</dt>
<dd>
<p>You can refine its strategies and tell it where or what it should or should not search for. At no point are you compelled to modify your code to suit the whims of Hypothesis if it generates nonsensical data.</p>
</dd>
</dt>
</dl>
<p>So let’s look at how Hypothesis can help you discover errors in your code.</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/course/testing-with-hypothesis/testing-your-python-code-with-hypothesis"><strong>Read More -></strong></a>2021-12-02T15:31:21.748742+00:00https://www.inspiredpython.com/course/game-boy-emulator/game-boy-emulator-writing-the-z80-disassemblerGame Boy Emulator: Writing the Z80 Disassembler2021-12-12T20:23:44.065751+00:00Inspired Python<span>
<section>
<h2>Game Boy Emulator: Writing the Z80 Disassembler</h2>
<span></span><p>Let’s continue where we left off in the <a><span></span></a> with a deep dive into the Game Boy’s opcodes and operands – the language of the Z80 CPU – and how to make sense of it all.</p>
<p>As you’ll recall, the Z80 is an 8-bit CPU with a selection of 16-bit instructions, and each instruction has an associated <em>opcode</em> and zero or more <em>operands</em> used by the instruction.</p>
<p>Much later on we’ll implement the specifics of each instruction, but before we do, we need to understand how the Game Boy passes information to the CPU for processing; to understand that, we’ll start out with a quick run-through of what a <em>cartridge ROM</em> is, before moving on to writing our first piece of the emulator: a disassembler.</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/course/game-boy-emulator/game-boy-emulator-writing-the-z80-disassembler"><strong>Read More -></strong></a>2021-12-12T20:23:44.065751+00:00https://www.inspiredpython.com/article/five-advanced-pytest-fixture-patternsFive Advanced Pytest Fixture Patterns2021-12-17T12:22:01.814393+00:00Inspired Python<span>
<section>
<h2>Five Advanced Pytest Fixture Patterns</h2>
<span></span><p>The <a>pytest</a> package is a great test runner, and it comes with a battery of features — among them the <em>fixtures</em> feature. A pytest fixture lets you generate and initialize test data, faked objects, application state, configuration, and much more, with a single decorator and a little ingenuity.</p>
<p>But there’s more to pytest fixtures than meets the eye. Here’s five advanced fixture tips to improve your tests.</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/article/five-advanced-pytest-fixture-patterns"><strong>Read More -></strong></a>2021-12-17T12:22:01.814393+00:00https://www.inspiredpython.com/course/game-boy-emulator/game-boy-emulator-designing-the-cpuGame Boy Emulator: Designing the CPU2022-01-05T09:27:02.089424+00:00Inspired Python<span>
<section>
<h2>Game Boy Emulator: Designing the CPU</h2>
<span></span><p>In <a><span></span></a> we learned how to write an <em>instruction decoder</em> and <em>disassembler</em>. It’s an important first step towards writing a Game Boy emulator. Assembly language – or the binary machine code form of it anyway – is the language of the CPU, and so we must represent, in software, a facsimile of a real CPU that can execute these machine code instructions for us.</p>
<p>So let’s start off with a quick overview of what exactly a CPU is and does, and how we’ll emulate it.</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/course/game-boy-emulator/game-boy-emulator-designing-the-cpu"><strong>Read More -></strong></a>2022-01-05T09:27:02.089424+00:00https://www.inspiredpython.com/article/solving-wordle-puzzles-with-basic-pythonSolving Wordle Puzzles with Basic Python2022-01-10T15:38:06.674603+00:00Inspired Python<span>
<section>
<h2>Solving Wordle Puzzles with Basic Python</h2>
<span></span><p>Have you heard of <a>Wordle</a>? It’s a deceptively simple word puzzle. You’re asked to guess the word of the day, which is a five-letter word in English. If you guess wrong, you’re given a few hints: a letter in the word is green if your guess for that letter in that position is right; a yellow letter if that letter is present in the word, but not that position; and gray, if the letter is not in the word at all.</p>
<p>Deceptively simple, and yet quite challenging! Here’s how you can write a Wordle Solver with Python sets, list comprehensions, a bit of good luck!</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/article/solving-wordle-puzzles-with-basic-python"><strong>Read More -></strong></a>2022-01-10T15:38:06.674603+00:00https://www.inspiredpython.com/course/create-tower-defense-game/make-your-own-tower-defense-game-with-pygameMake your own Tower Defense Game with PyGame2022-03-29T12:42:14.818788+00:00Inspired Python<span>
<section>
<h2>Make your own Tower Defense Game with PyGame</h2>
<span></span><p>In this course you’ll learn how to write a 2d Tower Defense Game from scratch, using PyGame. Writing a mess of spaghetti code is easy; but writing code that is maintainable and easy to extend is not. A tower defense game is a perfect place to learn how to write a substantial game that will test your skills as a Python programmer. It is also a perfect template for many other 2d games.</p>
<span></span><p>Part of the challenge of writing a game is the many disparate disciplines that rear their heads once you move beyond the truly basic. During the course you’ll learn the following skills:</p>
<dl>
<dt>
<dt>What is a Game Loop?</dt>
<dd>
<p>How do games actually update and display things on the screen in a manner that is maintainable and easy to reason about for the budding game developer?</p>
<p>The <em>Game Loop</em> is a cornerstone of all games, big and small. You’ll learn how to create one, and how it’s used to handle keyboard and mouse input, graphics rendering, updating the physics of entities on the screen, and more.</p>
</dd>
</dt>
<dt>
<dt>State Machines and Transitions</dt>
<dd>
<p>Few games have just one screen, and thus one state. Most games have a main menu, a score board, the actual game, and possibly more states that a player interacts with during game play. Understanding how to transition your game’s <em>state</em> between these different concepts is critical to writing a game free of spaghetti code.</p>
<p>You’ll learn about <em>finite-state machines</em>, an important concept in Computer Science, and how it can easily make transform a complex set of confusing requirements into neat and tidy code.</p>
</dd>
</dt>
<dt>
<dt>Lazy evaluation, Generators and Iterables</dt>
<dd>
<p>Keeping track of the position of things – and calculating the next position of something, such as a flying bullet – is easily solved with a liberal use of Python’s <code>itertools</code> library and generators.</p>
<p>Master a part of Python that gets short thrift from most developers, as they’re harder to reason about than normal for-loops.</p>
</dd>
</dt>
<dt>
<dt>Drawing and manipulating graphics</dt>
<dd>
<p>Learn what a <em>sprite</em> is, how to manipulate it to move, rotate, scale it, and how to do it efficiently and in a manner that is clear and easy to reason about.</p>
</dd>
</dt>
<dt>
<dt>Level Editing</dt>
<dd>
<p>You’ll write a complete level editor capable of placing and editing all the entities that make up a tower defense game using a simple UI you will build yourself.</p>
<p>The level editor forms a core part of the game, and includes details on how to write a save and load feature, so you can share your levels with friends.</p>
</dd>
</dt>
<dt>
<dt>Path finding and Recursion</dt>
<dd>
<p>Learn about recursion, a powerful programming concept, to find valid paths through a map for the enemies to traverse. You’ll learn about basic <em>graph theory</em> and how <em>Depth-First Search</em> is used to traverse a map and find a route from start to end.</p>
</dd>
</dt>
<dt>
<dt>Vector Mathematics</dt>
<dd>
<p>Come to grips with the mathematics required to ensure a bullet travels in a straight line towards a target; that your enemies walk smoothly across the map; and how to do simple text-based animations using simple arithmetic.</p>
<p>You’ll learn about simple vector arithmetic, interpolation, and basic affine transformations (like scaling and rotating).</p>
</dd>
</dt>
<dt>
<dt>Object-Oriented Programming (OOP)</dt>
<dd>
<p>Improve your understanding of classes and objects and how to best leverage inheritance, the factory pattern, and Python’s dataclasses to succinctly describe your game world using simple classes.</p>
</dd>
</dt>
<dt>
<dt>Animation</dt>
<dd>
<p>Learn how to chain together frames of images into simple animations so the enemies walk across the screen and collapse when they’re struck by exploding projectiles.</p>
</dd>
</dt>
<dt>
<dt>Collision Detection</dt>
<dd>
<p>Important stuff: how does the turret know when to fire at an enemy that is in its crosshairs? What about when the bullet strikes an enemy?</p>
</dd>
</dt>
</dl>
<p>Are you ready? Let’s code!</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/course/create-tower-defense-game/make-your-own-tower-defense-game-with-pygame"><strong>Read More -></strong></a>2022-03-29T12:42:14.818788+00:00https://www.inspiredpython.com/course/create-tower-defense-game/tower-defense-game-getting-startedTower Defense Game: Getting Started2022-03-29T12:42:14.843402+00:00Inspired Python<span>
<section>
<h2>Tower Defense Game: Getting Started</h2>
<span></span><p>Python package management is not the easiest thing to wrap your head around. Nevertheless, it’s important that we capture the requirements our game has, properly, and commit them to a Python package.</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/course/create-tower-defense-game/tower-defense-game-getting-started"><strong>Read More -></strong></a>2022-03-29T12:42:14.843402+00:00https://www.inspiredpython.com/course/create-tower-defense-game/tower-defense-game-game-loop-and-initializing-pygameTower Defense Game: Game Loop and Initializing PyGame2022-03-29T12:42:14.870893+00:00Inspired Python<span>
<section>
<h2>Tower Defense Game: Game Loop and Initializing PyGame</h2>
<span></span><p>How do you instruct your computer game to draw things to the screen consistently and to a drum-beat that ensures there’s no awkward pauses or jitter? What about listening to the keyboard and mouse inputs, or updating the score board for your game?</p>
<p>Get any one of these things wrong, or forget to do them, and your game misbehaves. Worse, it can misbehave in ways that you won’t necessarily catch on your own machine.</p>
<p>That’s why all computer games – big or small – has one (or more!) <em>game loops</em> that ensure the game carries out its most essential tasks in a repeatable and stable manner.</p>
<p>It’s time to write our skeleton game loop that will serve us throughout the course of the development of our tower defense game.</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/course/create-tower-defense-game/tower-defense-game-game-loop-and-initializing-pygame"><strong>Read More -></strong></a>2022-03-29T12:42:14.870893+00:00https://www.inspiredpython.com/course/create-tower-defense-game/tower-defense-game-finite-state-automata-state-machinesTower Defense Game: Finite State Automata / State Machines2022-03-29T12:42:14.887520+00:00Inspired Python<span>
<section>
<h2>Tower Defense Game: Finite State Automata / State Machines</h2>
<span></span><p>Complex codebases – and games are usually complex – tend to rely on a lot of <em>state</em>, usually captured in variables. Navigating from one screen in a game to another involves a lot of change: you need to render different things; the key bindings you use may change also; and perhaps you need to clear out old objects, like if you change from game play to the score screen.</p>
<p>But instead of having an ever-increasing number of variables to represent what your code is supposed to do – like, <code>is_in_menu</code>, <code>has_won_game</code>, <code>is_in_level_editor</code>, etc. – you should consider formalizing your <em>stateful</em> code using <em>finite state automata</em>, or commonly called a <em>state machine</em>.</p>
<p>To improve our code’s extensibility, it’s time to consider how we can effectively use a simple state machine to represent the state the game is in, and how OOP and inheritance can help with separation of concerns.</p>
</section>
</span><br/><br/><a href="https://www.inspiredpython.com/course/create-tower-defense-game/tower-defense-game-finite-state-automata-state-machines"><strong>Read More -></strong></a>2022-03-29T12:42:14.887520+00:00