Topic Archive

Concepts

  • SQLModel is a library that lets you interact with databases through Python code with Python objects and type annotations instead of writing direct SQL queries. Created by the same author of the extremely popular framework FastAPI, it aims to make interacting with SQL DBs in Python easier and elegant, with data validation and IDE support, without the need to learn SQL. It’s an ORM (Object-Relational Mapper), meaning: it translates between classes/objects and SQL. In this article, I will cover why you would use SQLModel over plain SQL queries, what benefits it brings to the table and the basics of using…


  • Python generators provide an elegant mechanism for handling iteration, particularly for large datasets where traditional approaches may be memory-intensive. Unlike standard functions that compute and return all values at once, generators produce values on demand through the yield statement, enabling efficient memory usage and creating new possibilities for data processing workflows. Generator Function Mechanics At their core, generator functions appear similar to regular functions but behave quite differently. The defining characteristic is the yield statement, which fundamentally alters the function’s execution model: When you call this function, it doesn’t execute immediately. Instead, it returns a generator object: This generator object…


  • Ever had a Python function behave strangely, remembering values between calls when it shouldn’t? You’re not alone! This is one of Python’s sneakiest pitfalls—mutable default parameters. Recently someone asked for help in our Pybites Circle Community with a Bite exercise that seemed to be behaving unexpectedly. It turned out that this was a result of modifying a mutable parameter passed to a function. For folks new to programming it is not obvious why modifying a variable inside a function might cause a change outside of that function. Let’s have a closer look at the underlying issue. What is a Python Variable When considering variables…


  • What is this pattern about? The Pipeline design pattern (also known as Chain of Command pattern) is a flexible way to handle a sequence of actions, where each handler in the chain processes the input data and passes it to the next handler. This pattern is commonly used in scenarios involving data processing, web scraping, or middleware systems. In this blog post, I’ll walk you through a specific example that leverages Python’s powerful functools.reduce and partial functions, along with the BeautifulSoup library for parsing HTML content. This code showcases the Pipeline pattern applied to HTML table extraction and processing. What…


  • The repository pattern is a design pattern that helps you separate business logic from data access code. It does so by providing a unified interface for interacting with different data sources, bringing the following advantages to your system: A practical example Let’s use Python and sqlmodel (PyPI) to demonstrate this pattern (code here): Note: In this implementation, we did not add error handling to keep the example relatively small. You should ensure that if something goes wrong (e.g., database connection issues or file access errors), the program can handle the error gracefully and provide useful feedback. The example might be a bit…


  • “KeyError: ‘GOOGLE_APPLICATION_CREDENTIALS‘” It was way too early in the morning for this error. See if you can spot the problem. I hadn’t had my coffee before trying to debug the code I’d written the night before, so it will probably take you less time than it did me. app.py: file_handling.py: See the problem? This was just the discussion of a recent Pybites article. When app.py imported initialize_constants from file_handling, the Python interpreter ran and looked for GOOGLE_APPLICATION_CREDENTIALS from the environment path, but load_dotenv hadn’t added them to the environment path from the .env file yet. Typically, configuration variables, secret keys,…


  • Welcome to Hell Cue the theme song from The Fresh Prince of Bel Air. This is a story that turns out well. But my code got stuck in import hell. I tried to teach my students all about pytest. Instead, for two days, I was sent on a debugging quest. I teach middle school students how to code in python. I reckoned it was time to talk about the datetime module. Students generally love projects that they can connect back to themselves. So I thought a great way to introduce datetime was to have python calculate how many days the…


  • Introduction One cool object-oriented programming (OOP) technique / pattern is enforcing consistent interfaces. In Python you can use Abstract Base Classes (ABCs) for that. 🐍 Using ABCs ensures that all subclasses implement the required methods. This can make it easier to maintain and extend the existing code base. Update Feb 2024: you can also leverage typing.Protocol (>= 3.8) for this, see this article. A real world Pybites example Check out this code example: Note: In the example above, we’ve used the ABCMeta metaclass directly for defining the abstract base class. However, Python provides a more modern and succinct way to…


  • Introduction Python’s versatility in supporting different programming paradigms, including procedural, object-oriented, and functional programming, opens up a rich landscape for software design and development. Among these paradigms, the use of static methods in Python, particularly in an object-oriented context, has been a topic of debate. This article delves into the role and implications of static methods in Python, weighing them against a more functional approach that leverages modules and functional programming principles. The Nature of Static Methods in Python Definition and Usage: Static methods in Python are defined within a class using the @staticmethod decorator. Unlike regular methods, they do…