Skip to main content

158 posts tagged with "python"

python tag description

View All Tags

Testing FastAPI Dependency Injection: A Comprehensive Guide

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Testing code that uses Dependency Injection (DI) is crucial because it allows you to isolate the logic of your route handlers from the complexity of external services (like databases, security checks, or configuration). FastAPI makes this straightforward using the app.dependency_overrides dictionary.

By implementing overrides, you replace the original, complex dependency function with a simple mock function that returns predictable test data, ensuring your tests run fast and consistently.

Advanced FastAPI Dependency Injection for Experts

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

For expert programmers, FastAPI Dependency Injection (DI) is not just about injecting configuration; it's a foundational tool for managing application state, enforcing transaction integrity, and implementing complex authorization logic cleanly and reliably. These advanced patterns leverage yield and nested dependencies to ensure stability and separation of concerns.

FastAPI Dependency Injection: Best Use Cases for Beginners

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

FastAPI's Dependency Injection (DI) system, powered by the Depends function, is a powerful concept built on simple Python functions. For beginners, the best use cases are those that demonstrate code reuse, logic separation, and early request validation without requiring complex external libraries.

By focusing on these three patterns, you learn to keep your route functions clean and focus only on the core business logic.

Singleton Pattern in FastAPI Dependency Injection

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. In web development, this is essential for managing resources like database pools, configuration objects, logging handlers, or complex machine learning models that are costly to initialize.

In FastAPI, while dependencies are generally executed once per request and the result is cached, this does not guarantee that the underlying class is instantiated only once across the entire application's lifecycle. To enforce a true global Singleton, we must use Python's built-in methods, typically overriding __new__.

FastAPI Dependency Injection: A Complete Guide to `Depends` and `yield`

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Dependency Injection (DI) is a core principle in FastAPI, allowing you to seamlessly integrate reusable logic, external resources (like database connections), configuration settings, and security checks into your API routes. FastAPI's DI system is built on standard Python features: type hinting and function calls, using the special Depends callable.

Fixing PIL/Pillow IOError: decoder zip not available

· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

The error IOError: decoder zip not available (or zip decoder not available) is a common hurdle when working with image processing in Python, particularly when handling PNG or TIFF files using the Pillow library.

This error indicates that the underlying C library responsible for image compression (Zlib) was missing or not detected when Pillow was compiled on your system.

Why and when to use the Pendulum module in Python

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Pendulum is a Python library designed to simplify working with dates and times, often serving as a more developer-friendly and robust replacement for Python's built-in datetime module. You should use Pendulum whenever your application involves timezone handling, complex arithmetic, or serialization/parsing of date strings, as it provides clear, unambiguous, and efficient methods for these tasks.

Python Pip most useful commands and use cases

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Pip is the standard package installer for Python. It allows you to manage the libraries and dependencies needed for your projects. Mastering a few core commands can significantly streamline your Python development workflow.

Switching Python Versions: Comprehensive Guide

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Managing the active Python version is critical for development, especially when working on projects with different dependencies or legacy requirements. The methods vary significantly depending on whether you are using a dedicated version manager, a package manager, or system-level tools.