🐍 Python Expert Skill
Asynchronous Mastery (Asyncio First):
- Trade bots are I/O bound. Prefer
async deffor everything involving Network/DB. - Use
asyncio.gather()for concurrent execution (e.g., fetching 10 stock prices at once). - Use
aiohttpfor HTTP requests. NEVER userequestsinside an async loop (it blocks). - Use
asyncio.to_thread()for CPU-bound tasks (e.g., heavy ML inference) to avoid freezing the event loop.
- Trade bots are I/O bound. Prefer
Error Handling & Logging:
- Fail Gracefully: The bot must never crash due to a single API failure.
- Use
loggingwith structured formats (JSON logs preferred in prod). - Create custom exception classes (e.g.,
ExchangeError,StrategyError).
Modern Pythonic Idioms:
- Use
match/casefor structural pattern matching. - Use
pathliboveros.path. - Use
f-stringsfor all string formatting. - Use
walrus operator (:=)sparingly but effectively for concise assignments.
- Use
class StockData(BaseModel): ticker: str price: float
async def fetch_price(session: aiohttp.ClientSession, ticker: str) -> StockData: url = f"https://api.example.com/price/{ticker}" async with session.get(url) as response: response.raise_for_status() data = await response.json() return StockData(ticker=ticker, price=data['price'])
async def get_market_snapshot(tickers: List[str]) -> List[StockData]: async with aiohttp.ClientSession() as session: # Launch all requests concurrently 🚀 tasks = [fetch_price(session, t) for t in tickers] results = await asyncio.gather(*tasks, return_exceptions=True)
# Filter out errors
valid_data = [r for r in results if isinstance(r, StockData)]
return valid_data
</examples>
---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/dldnwls07) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-15 -->