Skip to content
← All Skills
🐍

Python

Scripting, automation และ data processing — ใช้สำหรับ tooling และ quick prototyping

What I Can Do

  • เขียน automation scripts สำหรับ data processing
  • สร้าง CLI tools และ utility scripts
  • ใช้ Python สำหรับ quick prototyping
  • จัดการ data transformation ด้วย pandas
  • เขียน tests ด้วย pytest

Commands I Use Daily

bash
# สร้าง virtual environment
python -m venv .venv

# activate virtual environment
source .venv/bin/activate

# install dependencies
pip install -r requirements.txt

# freeze current dependencies
pip freeze > requirements.txt

# run script
python main.py

# run tests
python -m pytest

# format code
python -m black .

# lint
python -m ruff check .

Virtual Environments

Virtual environment แยก dependencies ของแต่ละ project — ป้องกัน version conflicts ระหว่าง projects, python -m venv .venv สร้าง, source .venv/bin/activate เปิดใช้, ใส่ .venv/ ใน .gitignore

Data Types

  • Immutableint, float, str, tuple, frozenset, bool
  • Mutablelist, dict, set
  • Dynamic typing แต่รองรับ type hints สำหรับ static analysis

Functions

Dart functions รองรับ default parameters, *args, **kwargs — เช่น def process(data: list, *, verbose: bool = False) -> dict ที่ return dictionary กลับมา

Functions เป็น first-class objects — ส่งเป็น argument ได้, return จาก function ได้, assign ให้ variable ได้

List Comprehensions

เขียน loop + filter + transform ในบรรทัดเดียว — อ่านง่ายและเร็วกว่า manual loop เช่น list comprehension [u.name for u in users if u.is_active] หรือ dict comprehension u.id: u for u in users

Decorators

Decorators wrap function เพื่อเพิ่ม behavior — ใช้สำหรับ logging, caching, authentication, timing เช่น สร้าง @timer decorator ที่วัดเวลา execution ของ function โดย wrap ด้วย wrapper(*args, **kwargs) ที่จับเวลา start/end แล้ว print ออกมา

Type Hints

Python รองรับ type hints ตั้งแต่ 3.5+ — ไม่ enforce ที่ runtime แต่ช่วย IDE, linters (mypy, pyright) ตรวจ type errors เช่น def find_user(user_id: int) -> Optional[User] ใช้ from typing import Optional

Generators

Generators ใช้ yield สร้าง values ทีละตัว — ไม่โหลดทั้งหมดเข้า memory, ใช้สำหรับ large datasets เช่น def read_large_file(path: str) ที่ open file แล้ว yield line.strip() ทีละบรรทัด

Context Managers

with statement จัดการ resource lifecycle อัตโนมัติ — ปิดไฟล์, release connections, cleanup ให้เมื่อออกจาก block เช่น with open("data.json") as f แล้ว json.load(f) — file ถูกปิดอัตโนมัติเมื่อออกจาก with block

Exception Handling

ใช้ try / except / finally จัดการ errors — except ValueError as e สำหรับ specific error, except Exception as e สำหรับ catch-all, finally สำหรับ cleanup ที่ต้อง run เสมอ ใช้ raise เพื่อ re-raise error ขึ้นไป

Classes

@dataclass ลด boilerplate สำหรับ data-holding classes — auto-generate __init__, __repr__, __eq__ เช่น @dataclass class Order ที่มี fields id: str, symbol: str, quantity: float, price: float และ @property def total ที่ return self.quantity * self.price

Async Python

asyncio สำหรับ I/O-bound concurrency — ใช้เมื่อต้อง handle หลาย network requests พร้อมกัน เช่น async def fetch(url: str) ที่ใช้ aiohttp.ClientSession() กับ await resp.json() สำหรับ async HTTP requests

Related Skills

  • Go — backend language ที่ใช้เป็นหลัก
  • JavaScript — scripting language อีกตัว
  • Linux — shell scripting ที่ใช้คู่กัน