Base Classes¶
The Base module provides foundational classes and abstract interfaces for the entire project. These classes establish common patterns and behaviors that are inherited by concrete implementations like the Calculator class.
Overview¶
The BaseExample class serves as the foundation for all example implementations in this project. It provides common functionality, error handling patterns, and interface definitions that ensure consistency across all modules.
Architecture¶
graph TD
A[BaseExample] --> B[Calculator]
A --> C[Other Examples]
B --> D[Mathematical Operations]
C --> E[Custom Implementations]
Key Features¶
- Abstract Interface: Defines common methods and properties
- Error Handling: Standardized exception handling patterns
- Logging: Integrated logging functionality
- Validation: Input validation hooks for subclasses
Inheritance Patterns¶
Classes that extend BaseExample include:
Class | Module | Purpose |
---|---|---|
Calculator | calculator |
Mathematical operations |
Custom Examples | Various | User-defined implementations |
Usage Patterns¶
Creating Custom Classes¶
To create a new class that follows project patterns:
from src.example.base import BaseExample
from src.example.utils import validate_input, format_number
class MyCustomExample(BaseExample):
"""Custom example following base patterns."""
def process_data(self, data):
"""Process data using inherited patterns."""
# Validate input using utility functions
validated_data = validate_input(data)
# Process using inherited methods
result = self._process_internal(validated_data)
# Format output
return format_number(result)
Integration with Utilities¶
BaseExample integrates seamlessly with utility functions:
- Input Validation: Use validate_input for consistent validation
- Output Formatting: Apply format_number for consistent display
- Error Handling: Follow patterns established in BaseExample
Error Handling¶
BaseExample implements comprehensive error handling:
Exception Types¶
BaseExampleError
- Base exception for all example-related errorsValidationError
- Input validation failuresProcessingError
- Internal processing issues
Error Handling Patterns¶
try:
result = my_example.process_data(input_data)
except BaseExampleError as e:
logger.error(f"Example processing failed: {e}")
# Handle gracefully
For specific error handling in subclasses, see:
Configuration¶
BaseExample supports configuration through:
# Configure logging and validation
base_config = {
'enable_logging': True,
'validation_strict': True,
'error_on_warning': False
}
example = BaseExample(config=base_config)
Extension Points¶
BaseExample provides several extension points for subclasses:
Abstract Methods¶
_process_internal()
- Core processing logic_validate_specific()
- Class-specific validation_format_output()
- Custom output formatting
Hook Methods¶
_before_process()
- Pre-processing hooks_after_process()
- Post-processing hooks_on_error()
- Error handling hooks
Best Practices¶
When extending BaseExample:
- Follow Naming Conventions: Use consistent method and variable naming
- Implement Required Methods: Override all abstract methods
- Use Utility Functions: Leverage utils module for common operations
- Handle Errors Gracefully: Follow established error handling patterns
- Document Thoroughly: Provide comprehensive docstrings
Testing¶
BaseExample includes comprehensive test utilities:
from src.example.base import BaseExampleTestCase
class MyExampleTests(BaseExampleTestCase):
"""Test custom example implementation."""
def test_custom_processing(self):
"""Test custom processing logic."""
# Use inherited test utilities
self.assert_valid_processing(input_data, expected_output)
Examples and Tutorials¶
For practical examples:
- Calculator Implementation - Complete example extending BaseExample
- Code Samples - Usage patterns and examples
- Best Practices - Recommended implementation patterns
Related Documentation¶
- API Overview - Complete API reference
- Utilities - Helper functions and utilities
- MkDocstrings Configuration - Documentation setup