$ show labstreaminglayer/pylsl
pylsl is a Python binding for the Lab Streaming Layer (LSL), a real-time network protocol for exchanging time-series data, primarily used in neuroscience and biomedical research. The library provides a clean, ctypes-based interface to the native liblsl C library, exposing classes for stream declaration (StreamInfo), data output (StreamOutlet), data input (StreamInlet), and network discovery (resolve_streams, ContinuousResolver). The codebase is well-structured with thorough error handling, graceful degradation for older liblsl versions, and comprehensive test coverage. It includes a micro-benchmarking suite for measuring ctypes call overhead and data conversion costs. The library is actively maintained, with CI/CD pipelines, platform-specific wheels bundling liblsl, and support for Python 3.10–3.13.
The LLM identified 3 files worth highlighting for reuse:
A self-contained micro-benchmark harness for measuring the per-call cost of ctypes-based library wrappers. It runs an outlet and inlet in the same process over loopback, pre-fills buffers, and measures conversion overhead independent of network latency. Supports configurable channel counts, data formats, chunk sizes, and JSON output for before/after comparison. The pattern of timeit with best-of-repeats, result aggregation, and geometric mean speedup is reusable for any ctypes-bound library.
Contains the find_liblsl_libraries generator function that implements a robust, cross-platform search strategy for locating a native shared library. It checks environment variable (PYLSL_LIB), package directory, system library paths via ctypes.util.find_library, the active Python environment's lib directory, and macOS Frameworks. This pattern is directly reusable for any Python project that needs to locate and load a native library with fallback logic.
The _to_str function provides a robust fallback for decoding bytes that may not be valid UTF-8 (e.g., hostnames from Windows ANSI code page). It first attempts UTF-8 decoding and falls back to latin-1, which cannot fail and preserves all bytes. This is a small but critical utility pattern applicable to any project that receives text from external systems with uncertain encoding.