Example Module¶
The Example module serves as the main entry point and orchestrator for the entire example framework. It provides a unified interface to the BaseExample classes, Calculator functionality, and utility functions.
Module Architecture¶
The example module is organized into several key components:
graph TB
A[src.example] --> B[base.py]
A --> C[calculator.py]
A --> D[utils.py]
B --> E[BaseExample]
C --> F[Calculator]
D --> G[Utility Functions]
F -.-> E
F -.-> G
E -.-> G
Submodules Overview¶
Core Components¶
Submodule | Purpose | Key Exports |
---|---|---|
base | Foundation classes and interfaces | BaseExample , abstract patterns |
calculator | Mathematical operations | Calculator class, arithmetic methods |
utils | Helper functions and utilities | validate_input , format_number |
Quick Start¶
Basic Usage¶
# Import the main components
from src.example import Calculator
from src.example.utils import validate_input, format_number
# Create calculator and perform operations
calc = Calculator()
# Validate inputs and perform calculation
a = validate_input("10.5")
b = validate_input("5.2")
result = calc.add(a, b)
# Format output
print(format_number(result)) # Output: 15.70
Advanced Integration¶
from src.example.base import BaseExample
from src.example.calculator import Calculator
from src.example.utils import process_list, merge_dicts
class AdvancedCalculator(BaseExample):
"""Advanced calculator extending base patterns."""
def __init__(self, config=None):
super().__init__(config)
self.calc = Calculator()
def batch_calculate(self, operations):
"""Process multiple calculations."""
results = []
for op, a, b in operations:
result = getattr(self.calc, op)(a, b)
results.append(result)
return results
Package Configuration¶
The example module supports package-level configuration:
# Package configuration
EXAMPLE_CONFIG = {
'default_calculator': Calculator,
'strict_validation': True,
'error_handling': 'graceful',
'logging_enabled': True
}
Integration Patterns¶
With External Libraries¶
import numpy as np
from src.example import Calculator
from src.example.utils import format_number
# Integrate with NumPy
def numpy_calculator_bridge(np_array):
"""Bridge between NumPy and Calculator."""
calc = Calculator()
results = []
for value in np_array:
# Use calculator for individual operations
result = calc.multiply(value, 2)
results.append(result)
return np.array(results)
With Configuration Management¶
from src.example.base import BaseExample
from src.example.utils import merge_dicts
class ConfigurableExample(BaseExample):
"""Example with external configuration support."""
@classmethod
def from_config_file(cls, config_path):
"""Create instance from configuration file."""
with open(config_path) as f:
config = json.load(f)
# Merge with defaults
default_config = cls.default_config()
final_config = merge_dicts(default_config, config)
return cls(config=final_config)
Error Handling Strategy¶
The example module implements a unified error handling strategy:
Exception Hierarchy¶
ExampleError
├── ValidationError (from utils)
├── CalculationError (from calculator)
├── BaseExampleError (from base)
└── ConfigurationError (from main module)
Centralized Error Handling¶
from src.example.exceptions import ExampleError
def safe_example_operation(operation, *args, **kwargs):
"""Safely execute any example operation."""
try:
return operation(*args, **kwargs)
except ExampleError as e:
logger.error(f"Example operation failed: {e}")
return None
except Exception as e:
logger.critical(f"Unexpected error: {e}")
raise
Testing Framework¶
The example module provides a comprehensive testing framework:
Test Utilities¶
from src.example.testing import ExampleTestCase
class TestMyExample(ExampleTestCase):
"""Test custom example implementations."""
def setUp(self):
"""Set up test fixtures."""
self.calc = self.create_calculator()
self.test_data = self.load_test_data()
def test_calculation_accuracy(self):
"""Test calculation accuracy."""
result = self.calc.add(1.1, 2.2)
self.assertAlmostEqual(result, 3.3, places=10)
Integration Testing¶
class TestExampleIntegration(ExampleTestCase):
"""Test integration between modules."""
def test_full_pipeline(self):
"""Test complete processing pipeline."""
# Test validation -> calculation -> formatting
raw_input = "42.5"
validated = validate_input(raw_input)
result = self.calc.multiply(validated, 2)
formatted = format_number(result)
self.assertEqual(formatted, "85.00")
Performance Optimization¶
Caching Strategies¶
from functools import lru_cache
from src.example.calculator import Calculator
class OptimizedCalculator(Calculator):
"""Calculator with performance optimizations."""
@lru_cache(maxsize=1000)
def expensive_operation(self, a, b):
"""Cached expensive calculation."""
return super().expensive_operation(a, b)
Batch Processing¶
from src.example.utils import process_list
def batch_validate_and_calculate(inputs, operation='add'):
"""Efficiently process multiple inputs."""
# Batch validate inputs
validated_inputs = process_list(inputs, validator=validate_input)
# Batch process calculations
calc = Calculator()
results = []
for i in range(0, len(validated_inputs), 2):
a, b = validated_inputs[i:i+2]
result = getattr(calc, operation)(a, b)
results.append(result)
return results
Documentation Generation¶
This module's documentation is automatically generated using mkdocstrings. Configuration details can be found in:
Migration and Upgrade¶
Version Compatibility¶
The example module maintains backward compatibility across versions:
# Handle version differences
import sys
from src.example import __version__
if __version__ >= "2.0.0":
from src.example.calculator import AdvancedCalculator as Calculator
else:
from src.example.calculator import Calculator
Best Practices¶
Module Usage¶
- Import Strategy: Import specific components rather than the entire module
- Configuration: Always provide explicit configuration for production use
- Error Handling: Use the unified error handling patterns
- Testing: Leverage the provided testing utilities
- Performance: Use batch operations for multiple items
Code Organization¶
# Recommended import pattern
from src.example.base import BaseExample
from src.example.calculator import Calculator
from src.example.utils import validate_input, format_number
# Avoid importing the entire module
# from src.example import * # Not recommended
Examples and Tutorials¶
For comprehensive examples and tutorials:
- Getting Started Guide - Basic usage
- Code Samples - Practical examples
- Jupyter Notebooks - Interactive tutorials
- Best Practices - Recommended patterns
Related Documentation¶
- API Overview - Complete API reference
- Individual Modules - Detailed module documentation
- Configuration Guide - Setup and configuration
- Feature Documentation - Available features
API Reference¶
Example module for demonstrating mkdocstrings.
This module contains example classes and functions to showcase API documentation generation capabilities.
Classes¶
AbstractBase
¶
Abstract base class for creating custom implementations.
This abstract class defines the interface that all concrete implementations must follow. It uses generics to allow flexible type handling.
Type Parameters
See Also
- :class:
BaseClass
: Non-abstract base implementation - :mod:
typing
: Type hints documentation
Initialize the abstract base.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_value
|
Optional[T]
|
Optional initial value to store |
None
|
Source code in src/example/base.py
Attributes¶
value
property
writable
¶
Functions¶
process
abstractmethod
¶
Process the input data.
This method must be implemented by subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
T
|
Input data to process |
required |
Returns:
Type | Description |
---|---|
T
|
Processed data of the same type |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If called on abstract class |
Source code in src/example/base.py
BaseClass
¶
A base class demonstrating basic documentation.
This class shows how to document a simple Python class with attributes, methods, and properties.
Attributes:
Name | Type | Description |
---|---|---|
name |
The name of the instance |
|
created_at |
Timestamp of instance creation |
|
_internal_id |
Internal identifier (private) |
Example
Initialize a new BaseClass instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name to assign to this instance |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If name is empty or None |
Source code in src/example/base.py
Attributes¶
age_seconds
property
¶
Calculate age of instance in seconds.
Returns:
Type | Description |
---|---|
float
|
Number of seconds since instance creation |
Functions¶
update_name
¶
Update the instance name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_name
|
str
|
The new name to set |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If new_name is empty |
Note
This method validates the input before updating.
Source code in src/example/base.py
Calculator
¶
Advanced calculator with history tracking.
This calculator supports basic arithmetic operations and maintains a history of all calculations performed.
Attributes:
Name | Type | Description |
---|---|---|
history |
List[str]
|
List of calculation history entries |
precision |
Number of decimal places for results |
Example
Note
The calculator automatically rounds results based on the specified precision.
Initialize calculator with specified precision.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
precision
|
int
|
Number of decimal places for results (default: 4) |
4
|
Raises:
Type | Description |
---|---|
ValueError
|
If precision is negative |
Source code in src/example/calculator.py
Functions¶
add
¶
Add two numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
First number |
required |
b
|
Union[int, float]
|
Second number |
required |
Returns:
Type | Description |
---|---|
float
|
Sum of a and b |
Example
calc = Calculator() calc.add(5, 3) 8.0
Source code in src/example/calculator.py
subtract
¶
Subtract b from a.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
Number to subtract from |
required |
b
|
Union[int, float]
|
Number to subtract |
required |
Returns:
Type | Description |
---|---|
float
|
Difference of a and b |
Example
calc = Calculator() calc.subtract(10, 4) 6.0
Source code in src/example/calculator.py
multiply
¶
Multiply two numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
First factor |
required |
b
|
Union[int, float]
|
Second factor |
required |
Returns:
Type | Description |
---|---|
float
|
Product of a and b |
Example
calc = Calculator() calc.multiply(3, 7) 21.0
Source code in src/example/calculator.py
divide
¶
Divide a by b.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
Dividend |
required |
b
|
Union[int, float]
|
Divisor |
required |
Returns:
Type | Description |
---|---|
float
|
Quotient of a divided by b |
Raises:
Type | Description |
---|---|
ZeroDivisionError
|
If b is zero |
Example
calc = Calculator() calc.divide(15, 3) 5.0
Source code in src/example/calculator.py
power
¶
Raise base to the power of exponent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base
|
Union[int, float]
|
The base number |
required |
exponent
|
Union[int, float]
|
The exponent |
required |
Returns:
Type | Description |
---|---|
Union[int, float]
|
base raised to the power of exponent |
Example
calc = Calculator() calc.power(2, 3) 8 calc.power(2.5, 2) 6.25
Source code in src/example/calculator.py
sqrt
¶
Calculate square root.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number
|
Union[int, float]
|
Number to find square root of |
required |
Returns:
Type | Description |
---|---|
float
|
Square root of the number |
Raises:
Type | Description |
---|---|
ValueError
|
If number is negative |
Example
calc = Calculator() calc.sqrt(16) 4.0
Source code in src/example/calculator.py
clear_history
¶
get_statistics
¶
Get statistics about calculator usage.
Returns:
Type | Description |
---|---|
dict
|
Dictionary containing usage statistics including: |
dict
|
|
dict
|
|
Example
calc = Calculator() calc.add(1, 2) 3.0 calc.multiply(3, 4) 12.0 stats = calc.get_statistics() stats['total_operations'] 2
Source code in src/example/calculator.py
MathOperations
¶
Bases: Enum
Enumeration of supported mathematical operations.
This enum defines all operations supported by the Calculator class.
Attributes:
Name | Type | Description |
---|---|---|
ADD |
Addition operation |
|
SUBTRACT |
Subtraction operation |
|
MULTIPLY |
Multiplication operation |
|
DIVIDE |
Division operation |
|
POWER |
Exponentiation operation |
|
ROOT |
Root operation (square root by default) |
Functions¶
format_number
¶
format_number(number: Union[int, float], decimals: int = 2, thousands_separator: bool = True) -> str
Format a number for display.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number
|
Union[int, float]
|
The number to format |
required |
decimals
|
int
|
Number of decimal places (default: 2) |
2
|
thousands_separator
|
bool
|
Whether to use thousands separator (default: True) |
True
|
Returns:
Type | Description |
---|---|
str
|
Formatted number as string |
Example
format_number(1234567.89) '1,234,567.89' format_number(1234567.89, decimals=0) '1,234,568' format_number(1234.5, thousands_separator=False) '1234.50'
See Also
- :func:
validate_input
: Validate numeric input locale.format_string
: For locale-specific formatting
Source code in src/example/utils.py
timer
¶
Decorator to measure function execution time.
This decorator wraps a function and prints the time taken to execute it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
F
|
The function to time |
required |
Returns:
Type | Description |
---|---|
F
|
Wrapped function that prints execution time |
Example
Note
The timing includes all operations within the function, including any I/O operations.
Source code in src/example/utils.py
validate_input
¶
validate_input(value: Any, expected_type: type, min_value: Optional[Union[int, float]] = None, max_value: Optional[Union[int, float]] = None, allow_none: bool = False) -> bool
Validate input value against constraints.
This function checks if a value meets specified type and range constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
The value to validate |
required |
expected_type
|
type
|
Expected type of the value |
required |
min_value
|
Optional[Union[int, float]]
|
Minimum allowed value (for numeric types) |
None
|
max_value
|
Optional[Union[int, float]]
|
Maximum allowed value (for numeric types) |
None
|
allow_none
|
bool
|
Whether None is a valid value |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if value is valid, False otherwise |
Example
validate_input(42, int, min_value=0, max_value=100) True validate_input(-5, int, min_value=0) False validate_input(None, str, allow_none=True) True
Warning
This function only performs basic validation. For complex
validation logic, consider using a dedicated validation library
like pydantic
or cerberus
.
Source code in src/example/utils.py
Modules¶
base
¶
Base classes and abstract interfaces.
This module provides base classes that can be extended for creating custom implementations.
Classes¶
BaseClass
¶
A base class demonstrating basic documentation.
This class shows how to document a simple Python class with attributes, methods, and properties.
Attributes:
Name | Type | Description |
---|---|---|
name |
The name of the instance |
|
created_at |
Timestamp of instance creation |
|
_internal_id |
Internal identifier (private) |
Example
Initialize a new BaseClass instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name to assign to this instance |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If name is empty or None |
Source code in src/example/base.py
Attributes¶
age_seconds
property
¶Calculate age of instance in seconds.
Returns:
Type | Description |
---|---|
float
|
Number of seconds since instance creation |
Functions¶
update_name
¶Update the instance name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_name
|
str
|
The new name to set |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If new_name is empty |
Note
This method validates the input before updating.
Source code in src/example/base.py
AbstractBase
¶
Abstract base class for creating custom implementations.
This abstract class defines the interface that all concrete implementations must follow. It uses generics to allow flexible type handling.
Type Parameters
See Also
- :class:
BaseClass
: Non-abstract base implementation - :mod:
typing
: Type hints documentation
Initialize the abstract base.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_value
|
Optional[T]
|
Optional initial value to store |
None
|
Source code in src/example/base.py
Attributes¶
value
property
writable
¶Functions¶
process
abstractmethod
¶Process the input data.
This method must be implemented by subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
T
|
Input data to process |
required |
Returns:
Type | Description |
---|---|
T
|
Processed data of the same type |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If called on abstract class |
Source code in src/example/base.py
calculator
¶
Calculator module with mathematical operations.
This module provides calculator functionality with support for basic arithmetic operations and advanced mathematical functions.
Classes¶
MathOperations
¶
Bases: Enum
Enumeration of supported mathematical operations.
This enum defines all operations supported by the Calculator class.
Attributes:
Name | Type | Description |
---|---|---|
ADD |
Addition operation |
|
SUBTRACT |
Subtraction operation |
|
MULTIPLY |
Multiplication operation |
|
DIVIDE |
Division operation |
|
POWER |
Exponentiation operation |
|
ROOT |
Root operation (square root by default) |
Calculator
¶
Advanced calculator with history tracking.
This calculator supports basic arithmetic operations and maintains a history of all calculations performed.
Attributes:
Name | Type | Description |
---|---|---|
history |
List[str]
|
List of calculation history entries |
precision |
Number of decimal places for results |
Example
Note
The calculator automatically rounds results based on the specified precision.
Initialize calculator with specified precision.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
precision
|
int
|
Number of decimal places for results (default: 4) |
4
|
Raises:
Type | Description |
---|---|
ValueError
|
If precision is negative |
Source code in src/example/calculator.py
Functions¶
add
¶Add two numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
First number |
required |
b
|
Union[int, float]
|
Second number |
required |
Returns:
Type | Description |
---|---|
float
|
Sum of a and b |
Example
calc = Calculator() calc.add(5, 3) 8.0
Source code in src/example/calculator.py
subtract
¶Subtract b from a.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
Number to subtract from |
required |
b
|
Union[int, float]
|
Number to subtract |
required |
Returns:
Type | Description |
---|---|
float
|
Difference of a and b |
Example
calc = Calculator() calc.subtract(10, 4) 6.0
Source code in src/example/calculator.py
multiply
¶Multiply two numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
First factor |
required |
b
|
Union[int, float]
|
Second factor |
required |
Returns:
Type | Description |
---|---|
float
|
Product of a and b |
Example
calc = Calculator() calc.multiply(3, 7) 21.0
Source code in src/example/calculator.py
divide
¶Divide a by b.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
Dividend |
required |
b
|
Union[int, float]
|
Divisor |
required |
Returns:
Type | Description |
---|---|
float
|
Quotient of a divided by b |
Raises:
Type | Description |
---|---|
ZeroDivisionError
|
If b is zero |
Example
calc = Calculator() calc.divide(15, 3) 5.0
Source code in src/example/calculator.py
power
¶Raise base to the power of exponent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base
|
Union[int, float]
|
The base number |
required |
exponent
|
Union[int, float]
|
The exponent |
required |
Returns:
Type | Description |
---|---|
Union[int, float]
|
base raised to the power of exponent |
Example
calc = Calculator() calc.power(2, 3) 8 calc.power(2.5, 2) 6.25
Source code in src/example/calculator.py
sqrt
¶Calculate square root.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number
|
Union[int, float]
|
Number to find square root of |
required |
Returns:
Type | Description |
---|---|
float
|
Square root of the number |
Raises:
Type | Description |
---|---|
ValueError
|
If number is negative |
Example
calc = Calculator() calc.sqrt(16) 4.0
Source code in src/example/calculator.py
clear_history
¶
get_statistics
¶Get statistics about calculator usage.
Returns:
Type | Description |
---|---|
dict
|
Dictionary containing usage statistics including: |
dict
|
|
dict
|
|
Example
calc = Calculator() calc.add(1, 2) 3.0 calc.multiply(3, 4) 12.0 stats = calc.get_statistics() stats['total_operations'] 2
Source code in src/example/calculator.py
utils
¶
Utility functions for the example package.
This module contains helper functions and decorators that can be used throughout the application.
Classes¶
DateFormatter
¶
Format dates in various formats.
This class provides methods to format datetime objects into different string representations.
Attributes:
Name | Type | Description |
---|---|---|
default_format |
Default format string for dates |
Example
Initialize formatter with default format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default_format
|
str
|
Default strftime format string |
'%Y-%m-%d %H:%M:%S'
|
Source code in src/example/utils.py
Functions¶
format
¶Format a datetime object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date
|
datetime
|
The datetime to format |
required |
format_string
|
Optional[str]
|
Optional format string (uses default if None) |
None
|
Returns:
Type | Description |
---|---|
str
|
Formatted date string |
Example
formatter = DateFormatter() date = datetime(2024, 1, 15, 14, 30, 45) formatter.format(date) '2024-01-15 14:30:45'
Source code in src/example/utils.py
relative_time
¶Get relative time string (e.g., "2 hours ago").
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date
|
datetime
|
The datetime to compare against now |
required |
Returns:
Type | Description |
---|---|
str
|
Human-readable relative time string |
Example
formatter = DateFormatter() past = datetime.now() - timedelta(hours=2) formatter.relative_time(past) '2 hours ago'
Source code in src/example/utils.py
parse_iso
staticmethod
¶Parse ISO format date string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date_string
|
str
|
ISO format date string |
required |
Returns:
Type | Description |
---|---|
datetime
|
Parsed datetime object |
Raises:
Type | Description |
---|---|
ValueError
|
If date string is invalid |
Example
DateFormatter.parse_iso("2024-01-15T14:30:45") datetime.datetime(2024, 1, 15, 14, 30, 45)
Source code in src/example/utils.py
Functions¶
timer
¶
Decorator to measure function execution time.
This decorator wraps a function and prints the time taken to execute it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
F
|
The function to time |
required |
Returns:
Type | Description |
---|---|
F
|
Wrapped function that prints execution time |
Example
Note
The timing includes all operations within the function, including any I/O operations.
Source code in src/example/utils.py
format_number
¶
format_number(number: Union[int, float], decimals: int = 2, thousands_separator: bool = True) -> str
Format a number for display.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number
|
Union[int, float]
|
The number to format |
required |
decimals
|
int
|
Number of decimal places (default: 2) |
2
|
thousands_separator
|
bool
|
Whether to use thousands separator (default: True) |
True
|
Returns:
Type | Description |
---|---|
str
|
Formatted number as string |
Example
format_number(1234567.89) '1,234,567.89' format_number(1234567.89, decimals=0) '1,234,568' format_number(1234.5, thousands_separator=False) '1234.50'
See Also
- :func:
validate_input
: Validate numeric input locale.format_string
: For locale-specific formatting
Source code in src/example/utils.py
validate_input
¶
validate_input(value: Any, expected_type: type, min_value: Optional[Union[int, float]] = None, max_value: Optional[Union[int, float]] = None, allow_none: bool = False) -> bool
Validate input value against constraints.
This function checks if a value meets specified type and range constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
The value to validate |
required |
expected_type
|
type
|
Expected type of the value |
required |
min_value
|
Optional[Union[int, float]]
|
Minimum allowed value (for numeric types) |
None
|
max_value
|
Optional[Union[int, float]]
|
Maximum allowed value (for numeric types) |
None
|
allow_none
|
bool
|
Whether None is a valid value |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if value is valid, False otherwise |
Example
validate_input(42, int, min_value=0, max_value=100) True validate_input(-5, int, min_value=0) False validate_input(None, str, allow_none=True) True
Warning
This function only performs basic validation. For complex
validation logic, consider using a dedicated validation library
like pydantic
or cerberus
.