2026 সালে পাইথনের সর্বোত্তম অনুশীলনগুলি যুদ্ধ-পরীক্ষিত সফ্টওয়্যার ইঞ্জিনিয়ারিং নীতিগুলির সাথে ভাষার বিকাশমান বৈশিষ্ট্যগুলিকে একত্রিত করে। এই নির্দেশিকাটি কোডের গুণমান, প্রকল্পের কাঠামো, পরীক্ষা, কর্মক্ষমতা এবং পেশাদার পাইথন বিকাশে ব্যবহৃত আধুনিক টুলচেনকে কভার করে।
📋 Table of Contents
কোড স্টাইল এবং ফরম্যাটিং
# Modern Python toolchain (2026)
pip install ruff # fastest linter + formatter (replaces black + flake8 + isort)
# Format all Python files
ruff format .
# Lint and auto-fix
ruff check . --fix
# pyproject.toml configuration
# [tool.ruff]
# line-length = 100
# select = ["E", "F", "I", "N", "UP"] # rules to enable
# ignore = ["E501"]
# GOOD: meaningful names, type hints, docstrings
def calculate_compound_interest(
principal: float,
annual_rate: float,
years: int,
compounds_per_year: int = 12,
) -> float:
# FV = P * (1 + r/n)^(n*t)
rate_per_period = annual_rate / compounds_per_year
periods = compounds_per_year * years
return principal * (1 + rate_per_period) ** periods
# BAD: cryptic names, no types, magic numbers
def calc(p, r, y, n=12):
return p * (1 + r/n) ** (n*y)
প্রকল্পের কাঠামো
myapp/
pyproject.toml # project metadata + tool config
README.md
src/
myapp/
__init__.py
main.py # entry point
api/ # FastAPI routes
__init__.py
users.py
posts.py
models/ # SQLAlchemy/Pydantic models
user.py
post.py
services/ # business logic
user_service.py
repositories/ # database access
user_repo.py
core/ # config, dependencies
config.py
database.py
tests/
unit/
test_user_service.py
integration/
test_api.py
conftest.py # shared fixtures
কনফিগারেশন ম্যানেজমেন্ট
# core/config.py — Pydantic Settings
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
)
# Database
database_url: str
database_pool_size: int = 10
# API
api_host: str = "0.0.0.0"
api_port: int = 8000
debug: bool = False
# Security
secret_key: str
jwt_algorithm: str = "HS256"
access_token_expire_minutes: int = 15
# External services
redis_url: str = "redis://localhost:6379/0"
# Singleton via functools.cache
from functools import lru_cache
@lru_cache(maxsize=1)
def get_settings() -> Settings:
return Settings()
# Usage
settings = get_settings()
print(settings.database_url)
# FastAPI dependency
from fastapi import Depends
def get_db_url(settings: Settings = Depends(get_settings)) -> str:
return settings.database_url
নিদর্শন পরিচালনার ত্রুটি৷
# Domain exceptions
class AppError(Exception):
def __init__(self, message: str, code: str):
super().__init__(message)
self.code = code
class NotFoundError(AppError):
def __init__(self, resource: str, id: int | str):
super().__init__(f"{resource} {id} not found", "NOT_FOUND")
class ValidationError(AppError):
def __init__(self, field: str, message: str):
super().__init__(f"{field}: {message}", "VALIDATION_ERROR")
# Never swallow exceptions silently
# BAD
try:
result = process(data)
except Exception:
pass # NEVER DO THIS
# GOOD: log and re-raise or handle specifically
import logging
logger = logging.getLogger(__name__)
try:
result = process(data)
except ValueError as e:
logger.warning(f"Invalid data: {e}")
raise ValidationError("data", str(e)) from e
except Exception as e:
logger.exception(f"Unexpected error processing data: {e}")
raise
কর্মক্ষমতা সেরা অভ্যাস
# 1. Use generators for large data
def process_large_file(path: str):
with open(path) as f:
for line in f: # not f.readlines()!
yield process_line(line)
# 2. Use slots for memory-efficient classes
class Point:
__slots__ = ('x', 'y') # saves 40-50% memory vs dict-based attrs
def __init__(self, x: float, y: float):
self.x = x
self.y = y
# 3. Profile before optimizing
import cProfile
cProfile.run('your_slow_function()', sort='cumulative')
# 4. Use collections for appropriate data structures
from collections import deque, defaultdict, Counter
# deque for queue (O(1) append/pop from either end)
queue = deque()
queue.appendleft(item) # O(1) vs list.insert(0, item) which is O(n)
# Counter for counting
word_count = Counter(text.split())
top_10 = word_count.most_common(10)
# 5. Prefer comprehensions over loops for transformations
squares = [x**2 for x in range(1000)] # faster than for loop + append
# 6. Use functools.cache for expensive pure functions
from functools import cache
@cache
def fibonacci(n: int) -> int:
if n < 2: return n
return fibonacci(n-1) + fibonacci(n-2)
নির্ভরতা ব্যবস্থাপনা
# 2026 standard: use uv (blazing fast, from Astral/ruff team)
pip install uv
# Create virtual environment and install deps (10x faster than pip)
uv venv && source .venv/bin/activate
uv pip install -r requirements.txt
# Or manage with pyproject.toml
uv add fastapi sqlalchemy pydantic # adds to pyproject.toml
uv add --dev pytest ruff mypy # dev dependencies
# Generate lockfile
uv lock
# Install from lockfile (reproducible)
uv sync
পাইথন সেরা অনুশীলন চেকলিস্ট
- সমস্ত ফাংশন স্বাক্ষরে টাইপ ইঙ্গিত ব্যবহার করুন
- কঠোর মোডে mypy বা Pyright চালান
- linting এবং বিন্যাস জন্য ruff ব্যবহার করুন
- ব্যবসায়িক যুক্তিতে 80%+ পরীক্ষার কভারেজ
- পরিবেশ ভেরিয়েবলের মাধ্যমে কনফিগারেশন (Pydantic সেটিংস)
- স্ট্রাকচার্ড লগিং (স্ট্রাকচলগ বা স্ট্যান্ডার্ড লগিং)
- ব্যতিক্রম শ্রেণিবিন্যাস ডিজাইন করা হয়েছে (কেবল ব্যতিক্রম বাড়াতে হবে না)
- নির্ভরতা ইনজেকশন (গ্লোবাল স্টেট নয়)
- প্রকল্প প্রতি ভার্চুয়াল পরিবেশ (uv বা venv)
- প্রি-কমিট হুক (রাফ, মাইপি, টেস্ট)
2026 সালে পাইথনের সর্বোত্তম অনুশীলনগুলি আধুনিক টুলচেইনের কেন্দ্রে: লিন্টিং/ফরম্যাটিং-এর জন্য রাফ, বৈধতার জন্য পাইডানটিক v2, নির্ভরতা ব্যবস্থাপনার জন্য ইউভি এবং টাইপ চেকিংয়ের জন্য মাইপি/পাইরাইট। অর্থপূর্ণ নাম এবং টাইপ ইঙ্গিত সহ পরিষ্কার কোড লিখুন, সঠিক ব্যতিক্রম শ্রেণিবিন্যাস ডিজাইন করুন এবং আপনার ব্যবসার যুক্তি পুঙ্খানুপুঙ্খভাবে পরীক্ষা করুন। এই অনুশীলনগুলি স্ক্রিপ্টগুলি থেকে রক্ষণাবেক্ষণযোগ্য উত্পাদন কোডকে পৃথক করে যা শুধুমাত্র লেখক বুঝতে পারেন।
🔗 Share this article
✍️ Leave a Comment