User Guide¶
Welcome to the apywire user guide! This section provides comprehensive documentation on all features and capabilities of apywire.
Contents¶
Basic Usage¶
Learn the fundamentals of using apywire, including creating wiring containers, defining specs, using the Accessor pattern, and understanding caching behavior.
Configuration Files¶
Load specs from YAML, TOML, JSON, or INI files. Mix configuration files with environment variables and implement environment-based configs.
Async Support¶
Discover how to use apywire in asynchronous contexts with await wired.aio.name() for async object access.
Thread Safety¶
Understand thread-safe instantiation, optimistic locking mechanisms, and configuration options for multi-threaded applications.
Compilation¶
Learn how to generate standalone Python code from your wiring specs using WiringCompiler for production deployment and performance optimization.
Spec Generation¶
Quickly scaffold wiring specs from class constructor signatures using Generator.
Advanced Features¶
Explore advanced capabilities including factory methods, positional arguments, complex nested dependencies, error handling, and best practices.
Overview¶
apywire is designed around a few core principles:
- Lazy Loading: Objects are instantiated only when accessed, improving startup performance
- Dependency Injection: Use placeholder syntax to express dependencies between objects
- Flexibility: Support for keyword arguments, positional arguments, factory methods, and constants
- Type Safety: Strict mypy typing throughout the library
- Performance: Optional code generation via Cython for production deployments
Quick Navigation¶
Looking for something specific?
- Getting started? → Basic Usage
- Need async? → Async Support
- Multi-threaded app? → Thread Safety
- Production deployment? → Compilation
- Complex use cases? → Advanced Features
- Scaffold specs quickly? → Spec Generation
Common Workflows¶
Development¶
- Define your wiring spec with dependencies
- Create a
Wiringcontainer - Access objects via the Accessor pattern
- Use
thread_safe=Falsefor single-threaded development
Production¶
- Test your wiring configuration thoroughly
- Use
WiringCompilerto generate standalone code - Enable
thread_safe=Trueif deploying multi-threaded - Optionally compile with Cython for maximum performance
Testing¶
- Define test-specific specs with mock objects
- Use placeholder references to inject test doubles
- Leverage lazy loading to avoid unnecessary instantiation
- Test circular dependency error handling
Best Practices¶
- Keep specs simple: Break complex wiring into smaller, focused specs
- Use placeholders: Express dependencies explicitly with
{name}syntax - Validate early: Access all wired objects in tests to catch configuration errors
- Document dependencies: Comment complex dependency chains in your specs
- Consider thread safety: Enable
thread_safe=Trueonly when needed (has overhead) - Compile for production: Use
WiringCompilerto generate optimized code
Next Steps¶
Start with Basic Usage to learn the fundamentals, then explore the other guides based on your specific needs.