Open a bidirectional pipe to R, julia, matlab, or python (etc.) and communicate with it via a socket.
Project description
talk2stat
A Python package for bidirectional communication with statistical software
talk2stat enables programs (such as LaTeX through runcode, Node.js, or any Python application) to communicate seamlessly with statistical packages. It uses sockets for client-server communication and bi-directional pipes for program I/O.
Supported languages: R, Julia, Matlab, Python
Key features:
- Bidirectional communication with multiple statistical backends
- Socket-based client-server architecture (localhost only)
- Configurable timeouts and debug logging
- Comprehensive test suite (35+ tests)
- Modern Python packaging (pyproject.toml + setup.py)
Requirements
- Python: 3.8 or higher
- Dependencies: pexpect
- Platform: Unix-like systems (Linux, macOS); Windows requires WSL
Installation
From PyPI
pip install talk2stat
Or in a virtual environment (recommended):
python -m venv myenv
source myenv/bin/activate
pip install talk2stat
From source
git clone <repository>
cd talk2stat
pip install -e .
With test dependencies
pip install -e ".[test]"
Quick Start
Server Setup
Create a configuration file (e.g., julia.config):
[SERVERCONFIG]
PORT = 54345
DEBUGFILE = debug.log
PIPETIMEOUT = 60
IDLE_TIMEOUT = 60
CONNECT_TIMEOUT = 10
Start the server:
from talk2stat import server
server('/path/to/config/dir', 'julia')
Client Usage
from talk2stat import client
client('/path/to/config/dir', 'julia', '```1 + 1```') # Inline code
client('/path/to/config/dir', 'julia', '/path/to/script.jl') # File
client('/path/to/config/dir', 'julia', 'QUIT') # Terminate server
Configuration
Each language requires a config file (<language>.config) in your project directory.
Required settings:
PORT— Socket port number (e.g., 54345)DEBUGFILE— Log file name (e.g., debug.log)
Optional settings (v0.1.7+):
PIPETIMEOUT— Code execution timeout in seconds (default: 60)IDLE_TIMEOUT— Client idle timeout in seconds (default: 60)CONNECT_TIMEOUT— Connection timeout in seconds (default: 10)
Example Configuration
[SERVERCONFIG]
PORT = 54345
DEBUGFILE = julia_debug.log
PIPETIMEOUT = 60
IDLE_TIMEOUT = 120
CONNECT_TIMEOUT = 15
Testing
Run all tests
pytest
Run with verbose output
pytest -v
Run specific test class
pytest tests/test_talk2stat.py::TestTimeoutDefaults -v
Generate coverage report
pip install -e ".[dev]"
pytest --cov=talk2stat --cov-report=html
Test suite covers:
- Argument validation for all supported languages
- Configuration file reading and defaults
- Timeout configuration and fallback behavior
- Error handling (connection refused, timeouts, missing files)
- Edge cases and parametrized scenarios
See tests/README.md for detailed testing documentation.
Architecture
Communication Flow
User Program
↓
Client (socket) ←→ Server (socket)
↓
Statistical Package
(via pexpect pipe)
Key Functions
server(dirname, language, doFork=True, messages=False)— Start server for statistical packageclient(dirname, language, inputfile)— Send code/commands to servercheck_server_args()— Validate server argumentscheck_client_args()— Validate client argumentsread_config()— Load configuration file
Timeout Management (added in v0.1.7)
Timeout Management
- IDLE_TIMEOUT — Server drops idle clients after N seconds (prevents zombie connections)
- CONNECT_TIMEOUT — Client connection timeout (default 10s)
- PIPETIMEOUT — Code execution timeout (already existed, now configurable)
All timeouts are user-configurable via config files and have sensible defaults.
Robustness Improvements
- Socket timeouts prevent indefinite blocking
- Configurable per-language, per-project
- Graceful handling of connection errors
- Detailed logging for debugging
Usage Examples
LaTeX Integration (runcode)
\begin{code}{julia}
x = randn(100, 100)
mean(x)
\end{code}
runcode uses talk2stat to execute Julia code and embed results.
Python Integration
import talk2stat
# Server runs in background (forked)
talk2stat.server('/projects/myproject', 'julia')
# Client sends code
talk2stat.client('/projects/myproject', 'julia', 'example.jl')
# Stop server
talk2stat.client('/projects/myproject', 'julia', 'QUIT')
Inline Code Execution
# Code between ``` ``` is executed directly
talk2stat.client('/config', 'R', '```plot(1:10)```')
Security Considerations
- Localhost only: Sockets bind to
127.0.0.1(no network exposure) - Single-user: Designed for single-user projects (no authentication)
- No token authentication: Not needed for local use, but would require modification for network deployment
Troubleshooting
Connection refused:
- Ensure server is running:
talk2stat.server() - Check port number in config file
- Verify config file exists and is readable
Timeout errors:
- Increase
PIPETIMEOUTfor slow computations - Increase
IDLE_TIMEOUTif clients are slow to respond - Check debug log file for details
Debug logging:
- Check
DEBUGFILE(specified in config) for server logs - Run with
messages=Truefor verbose console output:talk2stat.server('/config', 'julia', messages=True)
Development
Adding a New Statistical Backend
- Add language to
check_server_args()andcheck_client_args() - Define prompt character and executable command
- Specify how to run files (e.g.,
source()for R,include()for Julia) - Add test cases
Contributing
- Write tests first (see tests/README.md)
- Run test suite:
pytest -v - Ensure coverage:
pytest --cov=talk2stat - Follow existing code style
License
MIT License (see LICENSE file)
Citation
If you use talk2stat in research, please cite:
Bar, H. "How to Talk to Your Programs: Interprocess Communication for Data Scientists", The American Statistician (Teacher's Corner)
Documentation
- README.md — This file; user guide and quick start
- CONFIGURATION.md — Detailed configuration reference
- tests/README.md — Testing guide and examples
- llms.txt — AI-targeted technical documentation (for code analysis and generation)
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 talk2stat-0.1.8.tar.gz.
File metadata
- Download URL: talk2stat-0.1.8.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28c6c733c7a7e73034687a68085ba865ce61c92d4faa4cb121193bb377e887ba
|
|
| MD5 |
9d05146301353b5185c4bd7f193fe749
|
|
| BLAKE2b-256 |
b303a37f2c40791058a3dd31846d4e34560a67991ae62c82a14ccf38b79e4c75
|
File details
Details for the file talk2stat-0.1.8-py3-none-any.whl.
File metadata
- Download URL: talk2stat-0.1.8-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9580a100fff29307b935b844bd2f4129f9385140735d904e8878a18d7e74107
|
|
| MD5 |
01c5921ec5df555eb837333d7163494c
|
|
| BLAKE2b-256 |
5923fe903027edfe7a5961688288d0ccc5a69969ba874c83fdf37d5928adba2e
|