mdanalysis icon indicating copy to clipboard operation
mdanalysis copied to clipboard

Add example for writing to NamedStream StringIO buffer

Open hmacdope opened this issue 1 year ago • 3 comments

We have plenty of examples of how to read a file from a StringIO buffer or similar using a NamedStream, but no examples of how to write to a StringIO buffer using a similar methodology. We should add a simple example like the following.

import MDAnalysis as mda
from io import StringIO

u = mda.Universe("pdb.pdb")
buf  = StringIO()
prot = u.select_atoms("protein")
with mda.Writer(mda.lib.util.NamedStream(buf, "prot.xyz"), prot.n_atoms) as w:
   w.write(prot)
   
buf.getvalue()
>>> # xyz file


hmacdope avatar May 01 '24 00:05 hmacdope

This is an excellent and much-needed example! You're right @hmacdope , while reading from a NamedStream(StringIO, ...) is well-documented, the ability to write to an in-memory buffer is a highly useful pattern that currently lacks a clear example.

The proposed code snippet is perfect and demonstrates the usage of mda.lib.util.NamedStream with mda.Writer very clearly.

I'd be happy to open a Pull Request (PR) to add this example to the relevant section of the documentation (e.g., the User Guide or the mda.Writer documentation) to ensure it's easily discoverable for users.

ChinmayRout9040895625 avatar Oct 25 '25 16:10 ChinmayRout9040895625

@ChinmayRout9040895625 You are more than welcome to open a PR to fix things.

That being said, I have seen you respond in a few issues with (pardon me if I'm incorrect) what appears to be AI summaries of the issue.

May I recommend that you stick to resolving one issue first rather than trying to tackle many?

IAlibay avatar Oct 25 '25 17:10 IAlibay

Thank you for the clear feedback.

ChinmayRout9040895625 avatar Oct 25 '25 17:10 ChinmayRout9040895625