Overview
Thedevtool module provides utility classes and decorators for development, testing, and internal library operations.
Structure
A dict-like object structure that provides convenient dot-notation access to dictionary keys.Usage
Structure is a base class that you should subclass to create custom data structures with both dictionary and attribute-style access patterns.
Features
- Nested structure support: Automatically converts nested dictionaries to Structure instances
- Sequence handling: Converts lists of dictionaries to lists of Structure instances
- Hashable: Implements
__hash__()and__eq__()for use in sets and as dict keys - String normalization: Provides
_normalize_strings()method to strip whitespace from string values
Methods
__init__(*a, **kw)
Creates a new Structure instance from dictionary data.
_normalize_strings()
Strips whitespace from all string values in the structure.
timeout
Decorator that adds timeout functionality to functions with optional timeout handling.Usage
Number of seconds before the function times out
Optional callback function to execute when timeout occurs. Receives the wrapped function as an argument. If not provided, raises TimeoutError.
Implementation Details
- Uses
threading.Timerto implement timeout functionality - Properly cancels the timer if the function completes successfully
- Preserves function metadata using
functools.wraps - Re-raises exceptions that occur during function execution
Internal Functions
test()
Internal test function that demonstrates usage of the collector pattern for Chrome DevTools Protocol event collection. This function is used for library development and testing purposes.
Note: This is an internal function and not part of the public API.
collector()
Internal utility for collecting Chrome DevTools Protocol events in a background thread. This function is used internally by the library for monitoring browser events.
Note: This is an internal function and not part of the public API.
Use Cases
Structure Class
TheStructure class is useful for:
- Creating configuration objects with dot-notation access
- Handling API responses as structured objects
- Building type-safe data containers without full class definitions
timeout Decorator
Thetimeout decorator is useful for:
- Adding timeout protection to potentially long-running operations
- Implementing fallback behavior when operations exceed time limits
- Testing and development scenarios where operations need time constraints