Skip to content

Conversation

@koenvo
Copy link
Contributor

@koenvo koenvo commented Nov 14, 2025

This PR adds write support. It also introduced a BufferedStream with spooling to a temp directory to prevent high memory pressure when reading for example tracking data.

This adds comprehensive write support to the open_as_file() function with
efficient memory management and streaming capabilities.

Key features:
- BufferedStream: SpooledTemporaryFile wrapper with chunked I/O (5MB memory threshold)
- Write modes: 'wb' (write), 'ab' (append) - binary only
- Adapter pattern: write_from_stream() method (opt-in for adapters)
- Compression support: .gz, .bz2, .xz files handled automatically
- Local files and S3 URIs supported via FSSpecAdapter
- Protocols for type safety: SupportsRead, SupportsWrite

Implementation details:
- read_from()/write_to() methods use shutil.copyfileobj for chunked copying
- Context manager pattern buffers writes and flushes on exit
- No breaking changes to existing read functionality
@UnravelSports UnravelSports added this to the 3.19.0 milestone Nov 18, 2025
@UnravelSports UnravelSports mentioned this pull request Dec 17, 2025
@probberechts probberechts marked this pull request as ready for review January 7, 2026 15:54
@probberechts probberechts changed the title WIP: add write support Add write support Jan 7, 2026
@probberechts probberechts added the enhancement New feature or request label Jan 8, 2026
@probberechts
Copy link
Contributor

@koenvo I think it should be ready to merge now. The main changes I've made are:

  • Refactored the tests to keep everything related to I/O in test_io.py and added more test cases
  • Added support for writing to ZIP files
  • For files, the expand_inputs method was inconsistent: in some cases, it returned POSIX paths, and in others it returned OS-specific paths. (this was a pre-existing issue)
  • There were some minor issues in the last part of the open_as_file method. I've rewritten it such that all possible inputs are now appropriately opened and closed.

Ready to merge? Or do you spot something that I messed up?

@koenvo
Copy link
Contributor Author

koenvo commented Jan 8, 2026

Looks good to me! Great changes. I also like the no_cache fix!

@probberechts probberechts merged commit 8709cb9 into PySport:master Jan 8, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants