Skip to main content

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 package
  • client(dirname, language, inputfile) — Send code/commands to server
  • check_server_args() — Validate server arguments
  • check_client_args() — Validate client arguments
  • read_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 PIPETIMEOUT for slow computations
  • Increase IDLE_TIMEOUT if clients are slow to respond
  • Check debug log file for details

Debug logging:

  • Check DEBUGFILE (specified in config) for server logs
  • Run with messages=True for verbose console output:
    talk2stat.server('/config', 'julia', messages=True)
    

Development

Adding a New Statistical Backend

  1. Add language to check_server_args() and check_client_args()
  2. Define prompt character and executable command
  3. Specify how to run files (e.g., source() for R, include() for Julia)
  4. Add test cases

Contributing

  1. Write tests first (see tests/README.md)
  2. Run test suite: pytest -v
  3. Ensure coverage: pytest --cov=talk2stat
  4. 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

Metadata

Release files for talk2stat 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for talk2stat 0.1.8
File Size Uploaded
talk2stat-0.1.8.tar.gz 15.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for talk2stat 0.1.8
File Interpreter ABI Platform
talk2stat-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 24.5 kB

Release files / talk2stat-0.1.8.tar.gz

Download URL talk2stat-0.1.8.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
28c6c733c7a7e73034687a68085ba865ce61c92d4faa4cb121193bb377e887ba
BLAKE2b-256 checksum
How to use checksums
b303a37f2c40791058a3dd31846d4e34560a67991ae62c82a14ccf38b79e4c75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.5

Release files / talk2stat-0.1.8-py3-none-any.whl

Download URL talk2stat-0.1.8-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9580a100fff29307b935b844bd2f4129f9385140735d904e8878a18d7e74107
BLAKE2b-256 checksum
How to use checksums
5923fe903027edfe7a5961688288d0ccc5a69969ba874c83fdf37d5928adba2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page