Skip to main content

141 posts tagged with "python"

python tag description

View All Tags

Python enum framework

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

Python enum.Enum Advanced Practices for Expert Programmers

For veteran Python engineers, the enum.Enum class is more than just a container for constants; it is a powerful framework for metaprogramming, runtime validation, and object identity. These advanced techniques leverage Python's dunder methods (__new__, _missing_, __format__) and the underlying metaclass to inject behavior and data in highly customized ways, solving obscure but critical architectural problems.

Python Enum Integration with Typing

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

The combination of the standard enum.Enum class and the typing module is a powerful best practice in modern Python. By using an Enum class as a type hint, you signal to static type checkers (like MyPy, Pyright) and fellow developers that only specific, named constants are valid inputs or outputs, enforcing both type safety and value safety.

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.