Beatbox Recorder
Overview
Beatbox Recorder is a lightweight Python library that records and replays function calls, making it perfect for testing, mocking, and debugging. It can capture the results of expensive operations, API calls, or complex computations and play them back instantly, significantly speeding up tests and development cycles.
Requirements
- Python 3.9 or higher
Features
- Record and playback function calls with identical results
- Support for both synchronous and asynchronous functions
- Handles complex Python types (sets, tuples, datetimes, custom objects)
- Graceful handling of circular references
- Easy storage management with JSON files
- Simple API with record/playback/bypass modes
Installation
Using pip:
pip install beatbox-recorder
Using poetry:
poetry add beatbox-recorder
Import Options
The package can be imported as follows:
from beatbox_recorder import Beatbox, Mode
Quick Start
from beatbox_recorder import Beatbox, Mode
# Create a Beatbox instance
bb = Beatbox("my_storage.json")
# Function to wrap
async def fetch_user_data(user_id: int):
# Expensive API call or database query
response = await api.get_user(user_id)
return response.data
# Wrap the function
wrapped_fetch = bb.wrap(fetch_user_data)
# Record mode - will make real API calls and store results
bb.set_mode(Mode.RECORD)
user_data = await wrapped_fetch(123) # Makes actual API call
# Playback mode - will use stored results without making API calls
bb.set_mode(Mode.PLAYBACK)
user_data = await wrapped_fetch(123) # Returns stored result instantly
# Bypass mode - makes real API calls without recording
bb.set_mode(Mode.BYPASS)
user_data = await wrapped_fetch(123) # Makes actual API call
Usage in Tests
import pytest
from beatbox_recorder import Beatbox, Mode
@pytest.fixture
def recorder():
bb = Beatbox("test_storage.json")
return bb
def test_user_service(recorder):
user_service = UserService()
# Record actual API responses
recorder.set_mode(Mode.RECORD)
result = user_service.get_user(123)
# Use recorded responses in future test runs
recorder.set_mode(Mode.PLAYBACK)
cached_result = user_service.get_user(123)
assert cached_result == result
Storage
Beatbox stores recorded function calls and their results in a JSON file. The storage format is:
{
"hash_of_function_args": {
"result": "serialized_result",
"timestamp": "2024-01-01T00:00:00"
}
}
Supported Types
Beatbox can handle serialization of:
- Basic Python types (str, int, float, bool, None)
- Collections (list, tuple, dict, set)
- Datetime objects
- Custom objects (serialized as dictionaries)
- Exceptions
- Circular references (replaced with placeholder)
- Range objects
Error Handling
from beatbox_recorder import NoRecordingError, SerializationError
try:
bb.set_mode(Mode.PLAYBACK)
result = wrapped_function(123)
except NoRecordingError:
# No recording found for these arguments
pass
except SerializationError:
# Failed to serialize/deserialize result
pass
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for beatbox-recorder 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| beatbox_recorder-1.1.0.tar.gz | 5.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| beatbox_recorder-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.3 kB
Release files / beatbox_recorder-1.1.0.tar.gz
| Download URL | beatbox_recorder-1.1.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
45cebcf5f58be23cb0ad258e317f0db5405d9e8a282d732098a095a4a693959c
|
|
BLAKE2b-256 checksum How to use checksums |
13b75eb6e93b336745c2c4251252b030fded50f68c83efc02d76a360ec6da8c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.11
|
Release files / beatbox_recorder-1.1.0-py3-none-any.whl
| Download URL | beatbox_recorder-1.1.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4a1a5b7f0691925fd66f705029e40e5b0e6347bf9f3ca9126a754ebcd3d3b634
|
|
BLAKE2b-256 checksum How to use checksums |
5668d6856561ccdb9dc6a8e4cfe33d14ded3496002dfc7a4bb4b92dcad0d2a69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.11
|
Security for beatbox-recorder 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.