Langsung ke konten
Skillpedia
Pemrograman umum48k Β· #5549

python-patterns

Fungsinya

Panduan ini membantu Anda mengambil keputusan penting tentang cara membangun program Python. Panduan ini mengajarkan prinsip di balik pemilihan tools dan pengorganisasian kode, sehingga Anda belajar berpikir tentang pengembangan, bukan sekadar menyalin contoh.

Kapan dipakai

  • βœ“Saat memilih 'tool' (framework) yang akan dipakai untuk proyek Anda.
  • βœ“Saat memutuskan apakah program Anda perlu cepat mengerjakan banyak tugas sekaligus (asinkron).
  • βœ“Saat mengatur file dan folder proyek Python Anda.

Link skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/python-patterns/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/python-patterns/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBagaimana sebaiknya saya menyusun struktur proyek Python untuk API?”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Panduan ini menyajikan prinsip dan pertanyaan untuk membantu keputusan Anda. Panduan ini tidak menyediakan kode siap pakai, melainkan cara berpikir yang bisa Anda terapkan.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

Python Patterns

Python development principles and decision-making for 2025. Learn to THINK, not memorize patterns.

When to Use

Use this skill when making Python architecture decisions, choosing frameworks, designing async patterns, or structuring Python projects.


⚠️ How to Use This Skill

This skill teaches decision-making principles, not fixed code to copy.

  • ASK user for framework preference when unclear
  • Choose async vs sync based on CONTEXT
  • Don't default to same framework every time

1. Framework Selection (2025)

Decision Tree

What are you building?
β”‚
β”œβ”€β”€ API-first / Microservices
β”‚   └── FastAPI (async, modern, fast)
β”‚
β”œβ”€β”€ Full-stack web / CMS / Admin
β”‚   └── Django (batteries-included)
β”‚
β”œβ”€β”€ Simple / Script / Learning
β”‚   └── Flask (minimal, flexible)
β”‚
β”œβ”€β”€ AI/ML API serving
β”‚   └── FastAPI (Pydantic, async, uvicorn)
β”‚
└── Background workers
    └── Celery + any framework

Comparison Principles

Factor FastAPI Django Flask
Best for APIs, microservices Full-stack, CMS Simple, learning
Async Native Django 5.0+ Via extensions
Admin Manual Built-in Via extensions
ORM Choose your own Django ORM Choose your own
Learning curve Low Medium Low

Selection Questions to Ask:

  1. Is this API-only or full-stack?
  2. Need admin interface?
  3. Team familiar with async?
  4. Existing infrastructure?

2. Async vs Sync Decision

When to Use Async

async def is better when:
β”œβ”€β”€ I/O-bound operations (database, HTTP, file)
β”œβ”€β”€ Many concurrent connections
β”œβ”€β”€ Real-time features
β”œβ”€β”€ Microservices communication
└── FastAPI/Starlette/Django ASGI

def (sync) is better when:
β”œβ”€β”€ CPU-bound operations
β”œβ”€β”€ Simple scripts
β”œβ”€β”€ Legacy codebase
β”œβ”€β”€ Team unfamiliar with async
└── Blocking libraries (no async version)

The Golden Rule

I/O-bound β†’ async (waiting for external)
CPU-bound β†’ sync + multiprocessing (computing)

Don't:
β”œβ”€β”€ Mix sync and async carelessly
β”œβ”€β”€ Use sync libraries in async code
└── Force async for CPU work

Async Library Selection

Need Async Library
HTTP client httpx
PostgreSQL asyncpg
Redis aioredis / redis-py async
File I/O aiofiles
Database ORM SQLAlchemy 2.0 async, Tortoise

3. Type Hints Strategy

When to Type

Always type:
β”œβ”€β”€ Function parameters
β”œβ”€β”€ Return types
β”œβ”€β”€ Class attributes
β”œβ”€β”€ Public APIs

Can skip:
β”œβ”€β”€ Local variables (let inference work)
β”œβ”€β”€ One-off scripts
β”œβ”€β”€ Tests (usually)

Common Type Patterns

# These are patterns, understand them:

# Optional β†’ might be None
from typing import Optional
def find_user(id: int) -> Optional[User]: ...

# Union β†’ one of multiple types
def process(data: str | dict) -> None: ...

# Generic collections
def get_items() -> list[Item]: ...
def get_mapping() -> dict[str, int]: ...

# Callable
from typing import Callable
def apply(fn: Callable[[int], str]) -> str: ...

Pydantic for Validation

When to use Pydantic:
β”œβ”€β”€ API request/response models
β”œβ”€β”€ Configuration/settings
β”œβ”€β”€ Data validation
β”œβ”€β”€ Serialization

Benefits:
β”œβ”€β”€ Runtime validation
β”œβ”€β”€ Auto-generated JSON schema
β”œβ”€β”€ Works with FastAPI natively
└── Clear error messages

4. Project Structure Principles

Structure Selection

Small project / Script:
β”œβ”€β”€ main.py
β”œβ”€β”€ utils.py
└── requirements.txt

Medium API:
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ services/
β”‚   └── schemas/
β”œβ”€β”€ tests/
└── pyproject.toml

Large application:
β”œβ”€β”€ src/
β”‚   └── myapp/
β”‚       β”œβ”€β”€ core/
β”‚       β”œβ”€β”€ api/
β”‚       β”œβ”€β”€ services/
β”‚       β”œβ”€β”€ models/
β”‚       └── ...
β”œβ”€β”€ tests/
└── pyproject.toml

FastAPI Structure Principles

Organize by feature or layer:

By layer:
β”œβ”€β”€ routes/ (API endpoints)
β”œβ”€β”€ services/ (business logic)
β”œβ”€β”€ models/ (database models)
β”œβ”€β”€ schemas/ (Pydantic models)
└── dependencies/ (shared deps)

By feature:
β”œβ”€β”€ users/
β”‚   β”œβ”€β”€ routes.py
β”‚   β”œβ”€β”€ service.py
β”‚   └── schemas.py
└── products/
    └── ...

5. Django Principles (2025)

Django Async (Django 5.0+)

Django supports async:
β”œβ”€β”€ Async views
β”œβ”€β”€ Async middleware
β”œβ”€β”€ Async ORM (limited)
└── ASGI deployment

When to use async in Django:
β”œβ”€β”€ External API calls
β”œβ”€β”€ WebSocket (Channels)
β”œβ”€β”€ High-concurrency views
└── Background task triggering

Django Best Practices

…

Pertanyaan umum

Apa itu framework?

Framework ibarat kerangka atau kumpulan alat siap pakai yang membantu membangun program lebih cepat dengan mengikuti aturan dan struktur tertentu.

Apa arti 'asinkron'?

Asinkron berarti program bisa mengerjakan beberapa hal sekaligus, tanpa menunggu satu tugas selesai sebelum memulai tugas berikutnya. Ini berguna untuk program yang harus merespons banyak permintaan dengan cepat.

Apa itu 'Type Hints'?

Ini adalah anotasi yang Anda tambahkan ke kode untuk menunjukkan tipe data yang diharapkan atau dikembalikan oleh sebuah variabel atau fungsi (misalnya angka atau teks). Ini membantu mencegah kesalahan dan membuat kode lebih mudah dipahami.

Detail

Deskripsi asli dari pembuat

Python development principles and decision-making. Framework selection, async patterns, type hints, project structure. Teaches thinking, not copying.

repo: sickn33/agentic-awesome-skills

Popularitas

Bintang GitHub
48k
Peringkat
#5549
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa