Code Explainers

Code explainers tagged #decorators

python
import functools
import inspect
from typing import get_type_hints, get_origin, get_args, Union
 

A runtime type checker from annotations

decorators runtime-validation type-hints
Advanced 8 steps
python
import hashlib
import json
from functools import wraps
from flask import g, request, jsonify, current_app

Building an idempotency decorator in Flask

idempotency decorators redis
Advanced 7 steps
python
import fnmatch
from collections import defaultdict
from typing import Any, Callable
 

A pattern-matching event bus in Python

pub-sub decorators pattern-matching
Intermediate 8 steps
python
import functools
import time
 
from flask import current_app

Retrying deadlocked DB writes in Flask

decorators retry exponential-backoff
Intermediate 6 steps
python
from functools import wraps
from flask import request, jsonify
 
 

A parameterized query-param decorator in Flask

decorators validation type-casting
Intermediate 8 steps
typescript
import { Type, applyDecorators } from '@nestjs/common';
import {
  ApiExtraModels,
  ApiOkResponse,

A reusable paginated Swagger decorator in NestJS

decorators generics openapi
Intermediate 6 steps
typescript
import {
  IsEmail,
  IsNotEmpty,
  IsOptional,

How validation groups reuse one DTO in NestJS

validation dto decorators
Intermediate 9 steps
typescript
import { Body, Controller, Ip, Post, UnauthorizedException } from '@nestjs/common';
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
import { UseGuards } from '@nestjs/common';
import { AuthService } from './auth.service';

Rate-limiting an auth flow in NestJS

rate-limiting authentication guards
Intermediate 8 steps
python
from functools import wraps
 
from flask import Blueprint, abort, jsonify
from flask_login import current_user, login_required

Building an admin-only decorator in Flask

decorators authorization access-control
Intermediate 7 steps
python
from typing import Callable, Dict, Type
 
 
class PluginRegistry:

A decorator-based plugin registry in Python

decorators registry pattern factory
Intermediate 9 steps
typescript
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { ApiProperty } from '@nestjs/swagger';

Wiring validation and Swagger docs in NestJS

validation openapi decorators
Intermediate 8 steps
python
from functools import wraps
import asyncio
 
from fastapi import APIRouter, FastAPI, Request

Per-route request timeouts in FastAPI

decorators async timeouts
Intermediate 6 steps
typescript
import {
  CanActivate,
  ExecutionContext,
  Injectable,

Role-based access with a NestJS guard

authorization guards decorators
Intermediate 6 steps
typescript
import { Exclude, Expose, Transform, Type } from 'class-transformer';
 
export class AddressEntity {
  street: string;

Shaping API responses with class-transformer in NestJS

serialization decorators dto
Intermediate 10 steps
typescript
type AsyncMethod = (...args: any[]) => Promise<any>;
 
function LogExecutionTime(thresholdMs = 0) {
  return function <T extends AsyncMethod>(

A method decorator that times async calls

decorators higher-order-functions async
Advanced 9 steps
typescript
import { Controller, Get, Param, Query, Redirect, NotFoundException } from '@nestjs/common';
import { LinksService } from './links.service';
 
@Controller('links')

Handling HTTP redirects in NestJS

redirects routing decorators
Intermediate 7 steps
python
import threading
from functools import wraps
 
 

A thread-safe debounce decorator in Python

decorators debounce threading
Advanced 6 steps
python
import time
import threading
from enum import Enum
from functools import wraps

Building a circuit breaker in Python

circuit-breaker resilience decorators
Advanced 7 steps