Skip to main content

90 posts tagged with "python"

python tag description

View All Tags

A Guide to Preserving YAML Formatting with PyYAML

6 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Python's PyYAML library is a powerful tool for working with YAML, but it has one common limitation: when you load a YAML file and dump it back, it doesn't preserve the original formatting. This can be a problem if you have multi-line strings formatted as literal blocks (|) and want to keep them that way for readability.

Fortunately, there's a straightforward and effective way to solve this by creating a custom representer for PyYAML. This guide will walk you through the process, using the code you provided, to ensure your multi-line strings are always dumped as literal blocks.

Preserve the original literal block format

5 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

No, it is not possible for standard Python YAML libraries like PyYAML to preserve the original literal block format (e.g., | or >) when you load a YAML file and then dump it back. This is because the parser converts the literal block content into a standard Python string, discarding the original formatting. When you dump the string back to YAML, the dumper uses its own rules to represent the string, which typically defaults to a quoted style or a folded block style, but not necessarily the original one.

How to Measure Execution Time in Python: Vanilla Python, FastAPI, Flask, and Django

8 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Measuring how long your Python code takes to run is a critical skill for performance optimization, benchmarking, and identifying bottlenecks in production applications. Depending on whether you are analyzing a single micro-operation, profiling a complex function, or tracking HTTP request-response lifecycles, Python offers a wide variety of tools.

This guide provides a comprehensive breakdown of how to measure execution time in vanilla Python, followed by specialized implementations for the three major web frameworks: FastAPI, Flask, and Django.

JSON Encoding and Validation in Python: Msgspec vs. Pydantic

5 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

To JSON-encode a Python object using msgspec, you use the msgspec.json.encode() function. This function takes a Python object and returns a bytes object containing the JSON representation. msgspec is known for its high performance and correctness in handling data serialization.

Here's a simple guide with examples.

Convert msgspec Struct to Python Dictionary

9 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

When building high-throughput Python backend services, I frequently rely on msgspec as a lightweight, high-performance alternative to Pydantic and standard dataclasses. While msgspec.Struct objects provide blazing-fast serialization and low memory overhead thanks to their C-extension implementation, integration with legacy libraries, database ORMs, or third-party SDKs often requires converting these structures into native Python dictionaries (dict).

In this article, I will walk through the primary techniques for converting msgspec objects to dictionaries, compare their performance characteristics, and explain when to use each method in production backend systems.

How to Connect and Integrate Supabase with Python: FastAPI, Flask, and Django

8 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Supabase is a popular PostgreSQL-based BaaS (Backend-as-a-Service). Because it runs on a standard PostgreSQL database, you can connect to it using traditional database drivers (like psycopg2 or SQLAlchemy) or leverage its built-in REST API using client SDKs.

This guide provides a comprehensive walkthrough for integrating Supabase into your Python web applications, detailing implementations for FastAPI, Flask, and Django.

Fixing Vercel Python Import Errors: Causes, Solutions, and Helper Functions

6 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

The primary cause for import errors on Vercel is often a mismatch between your local development environment's flexibility and Vercel's strict, serverless build process. Beyond the crucial __init__.py file, you need to pay attention to your project's overall structure, the way you write imports, and Vercel's build configuration.

Whats the point of __init__.py on Vercel

5 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

The imports working perfectly fine locally, but failed after deploying on Vercel. The error I've experiencing is common when deploying Python applications with a specific file structure. My local environment likely handles imports differently than Vercel's serverless environment(because of the folder structure), which can cause import errors. The solution is to ensure that my project structure is recognized as a Python package

Switching Python Versions: Complete Developer Guide to pyenv, Poetry, CLI Cheatsheet, and Troubleshooting

9 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Managing active Python interpreters is a fundamental workflow requirement, particularly when developing across multiple backend microservices with competing runtime constraints or legacy dependencies. Directly altering system Python binaries breaks core OS utilities.

This guide provides a comprehensive manual on how to switch Python versions safely, featuring pyenv (installation, precedence mechanics, CLI cheatsheet, and troubleshooting), Poetry (project-level virtual environments), operating system overrides (Linux, macOS, Windows), and containerized Docker environments.

Cloning a list in Python

7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Cloning a list in Python is essential to avoid unintended side effects, as simply assigning one list to another with the equals sign (=) creates a reference, not a new copy [1]. This means both variables point to the same list object in memory, and modifying one will modify the other. To properly clone a list, you need to understand the difference between a shallow copy and a deep copy.