Skip to main content

50 posts tagged with "python"

python tag description

View All Tags

Benchmark: msgspec vs. Pydantic v2

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

msgspec is an extremely fast serialization and validation library that consistently outperforms Pydantic v2 in benchmarks. This performance advantage comes from its design as a lean, compiled-code-based library focused on a narrow set of data handling tasks, whereas Pydantic v2 is a feature-rich framework.

The performance differences are most pronounced in two key areas: parsing/decoding (converting data like JSON into Python objects) and serialization/encoding (converting Python objects into data like JSON).

Ultimate pre-commit Configuration for Python

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

Maintaining a clean and consistent codebase is crucial for any successful project. Enforcing standards across your team can be challenging, but with pre-commit, you can automate this process directly into your development workflow. This guide outlines a powerful pre-commit configuration that combines formatting, linting, and static type checking to ensure your code is always in top shape.

A Guide to Preserving YAML Formatting with PyYAML

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

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
software engineer, creator, artist, programmer, projects founder

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.

Measure the execution time of a function or endpoint in Flask

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

The best way to precisely measure the execution time of a function or endpoint in Flask is by using a decorator or middleware. This approach allows you to wrap your functions with timing logic without directly altering the original function's code, which keeps your application clean and maintainable.

Measure execution time of a function or endpoint in FastAPI

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

The best and most precise way to measure the execution time of a function or endpoint in FastAPI is by using a custom decorator or a middleware. This approach allows you to wrap your functions with timing logic without modifying the function's code itself, promoting clean, reusable, and maintainable code.

Annotate JSON schema properties in Python with msgspec

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

To annotate JSON schema properties in Python using msgspec, you use msgspec.field to provide metadata and constraints for a struct field. This allows you to define a more detailed schema beyond just the Python type hints, including documentation, default values, and validation rules.

JSON encode python with msgspec

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

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.

Covert msgspec object to dict

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

To convert a msgspec object to a dict, you can use the msgspec.structs.asdict() function. This function recursively converts a msgspec.Struct instance into a dictionary, including any nested Struct objects.