Code Reuse Patterns in Pyvider¶
Use Standard Python Patterns for Production
For production providers, use these proven approaches:
🤖 AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
| Text Only | |
|---|---|
1 2 3 4 5 | |
Recommended Approaches¶
This guide shows you how to share code and functionality across your Pyvider components using standard Python patterns. These approaches are production-focused and well-tested.
1. Base Class Inheritance¶
Create shared base classes for common functionality:
2. Composition with Helper Classes¶
Use composition to share functionality:
3. Utility Modules¶
Create shared utility modules:
Component Bundling¶
You can package multiple related components together for distribution:
Package Structure¶
| Text Only | |
|---|---|
Configuration¶
| TOML | |
|---|---|
Using Bundled Components¶
| Bash | |
|---|---|
Example: pyvider-components¶
The pyvider-components repository provides a comprehensive collection of production-focused components:
- Resources: file_content, local_directory, timed_token
- Data Sources: env_variables, file_info, http_api, lens_jq
- Functions: String manipulation, numeric operations, JQ transformations
- 100+ Working Examples with complete Terraform configurations
Perfect for: - Learning by example - Quick prototyping - Production use - Understanding best practices
Experimental Capabilities¶
Experimental Feature
This feature is experimental and may change significantly in later releases; availability may change or be removed. Use in production at your own risk.
Stability: ⚠️ Experimental
Feedback welcome! Report issues
What are Capabilities?¶
Capabilities are a composition mechanism for reusable, modular components that extend provider functionality. Think of them as mixins or plugins.
Current Status:
- ✅ Basic infrastructure implemented (BaseCapability, decorators)
- ⚠️ Lifecycle hooks partially implemented
- 🔮 Advanced features remain exploratory
Exploratory Features¶
Capability Lifecycle¶
Status: Partial implementation
Exploratory lifecycle hooks:
- setup() - Initialize capability
- configure() - Configure with provider settings
- teardown() - Cleanup on shutdown
Advanced Composition¶
Status: Exploratory
Features under consideration: - Capability dependency management - Composition ordering - Conflict resolution - Dynamic capability loading
Configuration¶
Capabilities can be configured through provider configuration or environment:
| Python | |
|---|---|
Best Practices for Code Reuse¶
- Start Simple: Use inheritance for straightforward shared functionality
- Prefer Composition: Use helper classes for complex cross-cutting concerns
- Create Utility Modules: Package commonly-used functions in shared modules
- Test in Isolation: Test shared code independently from components
- Document Well: Provide clear usage examples and docstrings
- Version Carefully: Shared code is a dependency - version appropriately
- Avoid Over-Abstraction: Don't create abstractions until you need them in 3+ places
Exploratory Plans¶
Exploratory plans include: - Advanced composition features - Built-in capability library - Integration with telemetry systems
Related Documentation¶
- pyvider-components - Working examples
- Best Practices - Code reuse patterns
- Advanced Patterns - Advanced implementation techniques
Contributing¶
Interested in contributing to the capabilities system?
- Join the discussion on GitHub Discussions
- Review the Contributing Guidelines
- Share feedback on exploratory capability features
Note: For production providers, we recommend using well-tested patterns (inheritance, composition, utilities) until the capabilities system reaches 1.0 maturity.