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
We have plenty of examples of how to read a file from a
StringIO
buffer or similar using aNamedStream
, but no examples of how towrite
to aStringIO
buffer using a similar methodology. We should add a simple example like the following.