Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

4247 bites

Page 6

Pydantic: Reusable Validation with Annotated Types
Python & FastAPI2 min read

Pydantic: Reusable Validation with Annotated Types

Pydantic's Annotated attaches validation logic directly to a type, making it reusable. Define a custom type like SquareNumber once and apply it to any model field, ensuring consistent validation without repeating code.

Python & FastAPI2 min read

FastAPI's StreamingResponse: Send Data in Chunks

StreamingResponse sends data piece by piece, like a live broadcast, instead of sending a complete file all at once. This keeps your server's memory low for huge responses like file downloads, video streams, or live data from AI models.

Layered Architecture: Separating API from Business Logic
Python & FastAPI2 min read

Layered Architecture: Separating API from Business Logic

A layered architecture separates your API into distinct jobs: routing, controlling, and serving. This keeps code maintainable, like an organized toolbox. It's crucial for growing FastAPI apps.

asyncio Streams: High-Level Async Network I/O
Python & FastAPI2 min read

asyncio Streams: High-Level Async Network I/O

asyncio Streams are like async file handles for the network. You get a reader/writer pair to await data, simplifying TCP clients and servers for basic protocols. The footgun: the default buffer limit is small; reading large data will fail unexpectedly.

Python & FastAPI2 min read

ARQ for FastAPI: Async Background Tasks

ARQ lets your FastAPI app offload heavy work to background workers, keeping the API responsive. It's a task queue built for asyncio. Use it for slow tasks like sending emails or processing data. The footgun is using blocking task libraries with async code.

Python & FastAPI2 min read

Serialize Pydantic Models with model_dump

model_dump turns a Pydantic model into a plain Python dict, bridging typed objects and JSON serializers in FastAPI endpoints. Call it when you need raw data before returning a response. Do not confuse it with model_dump_json, which emits a string, not a dict.

Python & FastAPI2 min read

Per-Field Validation with @field_validator

@field_validator scrubs a single Pydantic field before it enters the model. Use it for rules like 'password must contain a digit' or 'port must exceed 1024'. It only sees one field at a time, so cross-field checks belong in a model validator instead.

Python & FastAPI3 min read

Custom Field Serialization with @field_serializer

@field_serializer is an exit-only adapter for one field: it reshapes data leaving the Pydantic model without changing internals. Use it to format decimals, mask secrets, or tweak datetimes for FastAPI JSON. Never use it for validation; it only runs on output.

Python & FastAPI2 min read

Docker Compose for Local FastAPI Stacks

Docker Compose turns your laptop into a one-command datacenter. Define Postgres, Redis, and your FastAPI app in one YAML file and they boot as a networked stack.

Flutter & Dart2 min read

Dart Variables: var, final, and const

In Dart, var creates a mutable variable, while final and const are for single-assignment. Use var for changing state, final for runtime values set once, and const for compile-time constants. The footgun is confusing final with const.

Flutter & Dart2 min read

Dart's Core Data Types: Everything is an Object

In Dart, everything is an object, from numbers to functions. This means even a simple int or String has methods and properties, unlike primitive types in other languages. The main footgun is forgetting that null itself is a type, Null.

Flutter & Dart2 min read

Dart's Control Flow: Telling Your Code What to Do Next

Control flow statements are the road signs for your code, directing execution beyond a simple top-to-bottom path. Use if/else for decisions, for/while for loops, and try/catch to handle errors. The footgun is forgetting break in a switch case.

Flutter & Dart2 min read

Dart Function Syntax: Block Body vs. Arrow Notation

Dart functions use a block body {} for multiple statements or arrow syntax => for a single expression. Use => for simple one-liners like bool isEven(int n) => n % 2 == 0;. The footgun is using => for multi-step logic; it only works for.

Flutter & Dart2 min read

Dart Collections: Choosing List, Set, or Map

Dart collections organize data: use a List for ordered items, a Set for unique items, and a Map for key-value pairs. This choice is fundamental for storing UI widgets or parsing JSON. The common footgun is using a List for lookups, which is slow; use.

Flutter & Dart2 min read

Dart's Sound Null Safety: No More Null Errors

In Dart, variables can't be null unless you explicitly allow it. This flips the usual model, turning potential runtime null pointer crashes into compile-time errors you can fix immediately.

Dart's Null-Aware Operators: Safely Handle Nulls
Flutter & Dart2 min read

Dart's Null-Aware Operators: Safely Handle Nulls

Null-aware operators let you work with potentially null values without a cascade of if (x != null) checks. Use them to access properties, provide defaults, or assign values only when a variable is null. The footgun is confusing safe ?. with unsafe !.

Flutter & Dart1 min read

Dart's Cascade Notation: Chain Calls on One Object

Cascade notation (..) lets you perform a sequence of operations on the same object without repeating its name. It's ideal for configuring new instances in one block.

Flutter & Dart2 min read

Dart's async/await: Non-Blocking Code That Reads Synchronously

Dart's async/await makes non-blocking code read like a simple script. Use it for network requests or file I/O to keep your UI from freezing. The biggest footgun is calling an async function but forgetting to await its Future result.

Flutter & Dart2 min read

Dart Generics: Type-Safe Containers and Reusable Code

Generics let you define code that works with multiple types without sacrificing type safety. A List<String> is a list that only accepts strings. This is essential for collections.

Flutter & Dart2 min read

Dart Streams: Asynchronous Data Sequences

A Dart Stream is like a conveyor belt for asynchronous data, delivering events or file chunks as they arrive. Use them for continuous data flows like button clicks or reading large files.