python-design
ما الذي تفعله
تضع هذه المهارة قواعد تصميم لسكربتات Python وأدواتها. وهي لمطوّرين يريدون كودًا سهل التغيير وسهل الفهم.
متى تستخدمها
- ✓عندما تكتب ملفات Python أو تغيّرها، خاصة سكربتات سطر الأوامر.
- ✓عندما تخطط لتقسيم مشروع Python إلى وحدات.
- ✓عندما تراجع كود Python وتريد اكتشاف مشاكل التصميم.
رابط المهارة
https://github.com/mindfold-ai/Trellis/blob/main/.agents/skills/python-design/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“راجع سكربت Python هذا وأخبرني أين أصبح تصميمه فوضويًا أو صعب التغيير.”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
يطبّق وكيلك قواعد من كتاب معروف في تصميم البرمجيات، مثل إبقاء الوحدات عميقة بواجهات بسيطة، ويفحص علامات التحذير التي تجعل الكود صعب التغيير.
المحتوى التقني (ملف SKILL.md الأصلي)
النص الأصلي من المؤلف، مباشرة من GitHub.
Python Design for CLI Scripts
Design patterns and principles for writing maintainable Python CLI tools and utilities. Based on A Philosophy of Software Design (Ousterhout), adapted for scripting contexts.
When to Activate
- Writing or modifying Python files
- Planning module decomposition
- Code review of Python changes
- Refactoring scripts that feel "messy"
- Adding a new subcommand or utility function
Core Thesis
The central challenge is managing complexity, not adding features.
Complexity is anything that makes code hard to understand or modify. It has three symptoms:
- Change Amplification — A small change requires edits in many places
- Cognitive Load — You must hold too much context to make a safe change
- Unknown Unknowns — You don't know what you don't know (the most dangerous)
Complexity is incremental. It accumulates through hundreds of small decisions, not one catastrophic mistake. Therefore: sweat the small stuff.
Principle 1: Deep Modules
A module's value is the ratio of functionality hidden vs. interface exposed.
Deep module (good): Shallow module (bad):
┌──────────┐ ┌──────────────────────────┐
│ simple │ │ complex interface │
│ interface│ │ many params, many methods │
├──────────┤ ├──────────────────────────┤
│ │ │ │
│ rich │ │ thin implementation │
│ impl │ │ │
│ │ └──────────────────────────┘
│ │
└──────────┘
Practical test: If a caller must understand how the module works internally to use it correctly, the module is too shallow.
Example: Task Data Access
# Shallow — caller must know JSON structure, file paths, error handling
def _read_json_file(path: Path) -> dict:
with open(path, encoding="utf-8") as f:
return json.load(f)
# Every caller does this independently:
task_path = tasks_dir / name / "task.json"
data = _read_json_file(task_path)
title = data.get("title") or data.get("name", "")
status = data.get("status", "planning")
assignee = data.get("assignee", "")
# Deep — caller gets what they need, module hides JSON/path/parsing
@dataclass(frozen=True)
class TaskInfo:
name: str
title: str
status: str
assignee: str
priority: str
directory: Path
def load_task(tasks_dir: Path, name: str) -> TaskInfo | None:
"""Load task by directory name. Returns None if not found."""
...
def list_active_tasks(tasks_dir: Path) -> list[TaskInfo]:
"""List all non-archived tasks, sorted by priority."""
...
The deep version absorbs complexity: JSON parsing, field defaults, directory scanning, archive filtering. Callers just work with typed data.
Principle 2: Type-First Development
Types define contracts before implementation. This workflow catches design problems early:
- Define data shapes — dataclass or TypedDict first
- Define function signatures — parameter and return types
- Implement to satisfy types — let the type checker guide completeness
- Validate at boundaries — runtime checks only where data enters the system
Frozen Dataclasses for Internal Data
from dataclasses import dataclass
from typing import Literal
@dataclass(frozen=True)
class AgentRecord:
agent_id: str
task_name: str
worktree_path: Path
platform: Literal["Codex", "codex", "cursor"]
status: Literal["running", "done", "failed"]
branch: str
Frozen dataclasses are immutable — no accidental mutation, safe to pass around.
TypedDict for External JSON Shapes
When the data comes from a file (task.json, config.yaml, registry.json), use TypedDict to document the expected shape:
from typing import TypedDict, Required, NotRequired
class TaskData(TypedDict):
title: Required[str]
status: Required[str]
assignee: NotRequired[str]
priority: NotRequired[str]
parent: NotRequired[str]
children: NotRequired[list[str]]
This eliminates scattered .get("field", default) calls — the shape is documented once.
NewType for Domain Primitives
When two strings mean different things, make the type system enforce it:
from typing import NewType
TaskName = NewType("TaskName", str) # directory name like "03-10-v040"
BranchName = NewType("BranchName", str)
الأسئلة الشائعة
ما المشاكل التي يبحث عنها؟
يبحث عن التغييرات التي تنتشر في أماكن كثيرة، والعبء الذهني الكبير، والمفاجآت الخفية التي تجعل الكود محفوفًا بالمخاطر عند التغيير.
هل هي للتطبيقات الإلكترونية فقط؟
لا. كُتبت المهارة للسكربتات وأدوات سطر الأوامر، فهي تناسبها أكثر.
هل تضيف ميزات جديدة؟
لا. الفكرة المحورية هي إدارة التعقيد، لا إضافة الميزات.
التفاصيل
الوصف الأصلي من المؤلف
Python design patterns for CLI scripts and utilities — type-first development, deep modules, complexity management, and red flags. Use when reading, writing, reviewing, or refactoring Python files, especially in .trellis/scripts/ or any CLI/scripting context. Also activate when planning module structure, deciding where to put new code, or doing code review.
repo: mindfold-ai/Trellis
الشعبية
- نجوم GitHub
- 15k
- الترتيب
- #11023
- الترخيص
- AGPL-3.0
- آخر تحديث
- 29/9/2026
التعليقات
- …
المزيد من المهارات من هذا المستودع
first-principles-thinking
15kيساعد وكيلك على اختبار خطة أو تصميم بتفكيكه إلى حقائق أساسية والاستدلال صعودًا منها، بدل الاعتماد على العادة أو العرف.
trellis-before-dev
15kيساعد وكيلك على قراءة إرشادات الكود وملاحظات المهام في المشروع قبل أن يبدأ الكتابة، حتى يتبع أعراف الفريق.
trellis-brainstorm
15kيساعد وكيلك على توضيح ميزة جديدة أو مهمة غامضة بطرح سؤال مفيد واحد في كل مرة، وفحص الكود، والاتفاق على نطاق ضيق.
trellis-break-loop
15kيساعد وكيلك على تحليل خلل بعد إصلاحه، فيجد نوع سببه الجذري ولماذا فشلت الإصلاحات السابقة، ويحفظ الدروس لمنع تكراره.
مهارات مشابهة
api-connector-builder
280kيبني وكيلك اتصالات API جديدة على النمط الموجود، ويفيد عندما تحتاج إلى تكامل إضافي دون ابتكار طريقة جديدة للعمل.
api-design
280kيساعد وكيلك على تصميم واجهات REST API وفق أفضل الممارسات، ويوضح كيف تتواصل وكيف تُعرض الأخطاء وكيف تُنظَّم البيانات.
backend-patterns
280kيساعد وكيلك على أنماط بنية الخادم وتحسين قاعدة البيانات، ويفيد في بناء الشيفرة التي تعمل على الخادم أو مراجعتها.