Skip to main content

68 posts tagged with "python"

python tag description

View All Tags

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.

__init__.py use cases in Python

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

__init__.py is a special file that designates a directory as a Python package. You can leverage it to perform powerful package-level operations that go beyond a simple empty file, including managing the public API, handling circular dependencies, and dynamic module loading.