Execute functions and extract their local variables for debugging and testing
Project description
Troy Playground
A Python debugging tool that executes functions and extracts their local variables for inspection. Originally designed for analyzing test functions, it allows you to turn any function into a playground where you can access and examine internal variables after execution.
Installation
pip install troy-playground
Usage
Basic Example
from troy_playground import extract_locals, run_function_playground
# Example with a test function
from tests.test_module import TestClass
test_obj = TestClass()
vars_dict = extract_locals(
test_obj.test_method,
['result', 'data', 'processed'], # variables to extract
test_obj # instance for bound methods
)
# Access the extracted variables
print(vars_dict['result'])
print(vars_dict['data'])
Using with Module Path
# Extract variables from a test method
vars_dict = run_function_playground(
'tests.test_module',
'TestClass',
'test_method',
['result', 'response', 'mock_data']
)
# Also works with regular functions
vars_dict = run_function_playground(
'my_module',
None, # No class for module-level functions
'process_data',
['output', 'transformed']
)
Passing Arguments
# You can pass arguments to the function
vars_dict = extract_locals(
obj.method_with_params,
['result'],
obj, # instance
'arg1', 'arg2', # positional args
param1='value1', # keyword args
param2='value2'
)
API Reference
extract_locals(func, var_names, instance=None, *args, **kwargs)
Execute a function and extract specified local variables.
func: The function to executevar_names: List of variable names to extractinstance: Optional instance for bound methods*args,**kwargs: Arguments to pass to the function
Returns a dictionary with extracted variables. If an error occurs, the dict will contain an __error__ key.
run_function_playground(module_path, class_name, func_name, var_names, *args, **kwargs)
Import and run a function by module path.
module_path: Import path to moduleclass_name: Class name (None for module-level functions)func_name: Function/method namevar_names: List of variable names to extract*args,**kwargs: Arguments to pass to the function
Use Cases
- Test Debugging: Extract intermediate values from test functions to understand failures
- Function Analysis: Inspect internal state without modifying source code
- Interactive Development: Turn any function into a REPL-like playground
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file troy_playground-0.1.0.tar.gz.
File metadata
- Download URL: troy_playground-0.1.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1af9885753e85525fbb705b7215db6fe9232dee6e3e8c2829c98aff945894567
|
|
| MD5 |
7d54767f7cc958ea70747db7d00a3e2c
|
|
| BLAKE2b-256 |
9465ee848c7245f351842feedc38f542448c41b079c7df1c26251c6830c8948d
|
File details
Details for the file troy_playground-0.1.0-py3-none-any.whl.
File metadata
- Download URL: troy_playground-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a6a3a71d460817c46f178751c66b8ad70bbf531e531730a8dbde16822cd0669
|
|
| MD5 |
d35981e91bef34239484c59b16a9bd33
|
|
| BLAKE2b-256 |
2667e69ad2b0a1056f766cfc1c79c95135f2c04885740c5af394870e20e83353
|