P
python-resource-management
Python resource management with context managers, cleanup patterns, and streaming. Use when managing connections, file handles, implementing cleanup logic, or building streaming responses with accumulated state.
Installation
Pick a client and clone the repository into its skills directory.
Installation
About this skill
Python resource management with context managers, cleanup patterns, and streaming. Use when managing connections, file handles, implementing cleanup logic, or building streaming responses with accumulated state.
How to use
- Zainstaluj skill python-resource-management z repozytorium wshobson/agents — umieść go w katalogu plugins/python-development/skills swojego projektu agenta.
- Zaimportuj contextmanager z biblioteki contextlib lub zdefiniuj własną klasę implementującą protokół context managera z metodami
__enter__i__exit__. - Dla prostych zasobów użyj dekoratora @contextmanager — zdefiniuj funkcję, która nabywa zasób, yield go w bloku try, a następnie czyści go w finally.
- Dla złożonych zasobów stwórz klasę z metodami
__init__,__enter__(zwracającą zasób) i__exit__(wykonującą czyszczenie niezależnie od wyjątków). - Używaj zasobu w bloku
with— interpreter automatycznie wywoła__exit__po zakończeniu bloku, nawet jeśli wystąpi wyjątek. - Dla zasobów asynchronicznych implementuj
__aenter__i__aexit__, a następnie używaj zasobu w blokuasync with— przydatne do zarządzania połączeniami bazodanowymi i responsów strumieniowych ze stanem.