YAML (was: Python and Ruby)

Lou Pecora pecora at anvil.nrl.navy.mil
Thu Feb 4 09:57:59 EST 2010


In article <87eil1ddjp.fsf_-_ at castleamber.com>,
 John Bokma <john at castleamber.com> wrote:

> Lou Pecora <pecora at anvil.nrl.navy.mil> writes:
> 
> > That's a pretty accurate description of how I transitioned to Python 
> > from C and Fortran.
> 
> Not C, but C++ (but there are also C implementations): YAML, see:
> http://code.google.com/p/yaml-cpp/wiki/HowToParseADocument
> 
> I use YAML now and then with Perl for both reading/writing data and for
> debugging purposes (YAML is quite human readable, for programmers at least)
> 
> Of course there is also YAML support for Python:
> http://pyyaml.org/.

Well, that looks a bit more complicated than I would like, but maybe 
it's doing more stuff than I can grok.  Here's what I needed and how I 
did it in Python:

# Make some variables
x=1.234e-8
y=2
astr="An output string...whatever"
z=4.5+1j*1.3456  # a complex number

# Output them to a file already opened as fp
outlist=[x, y, astr, z]
repvars= repr(outlist)+"\n"
fp.write(repvars)

# Reading same list in:
instr=fp.readline()
inlist=eval(instr)
x1,y1,astr1,z1= inlist


That's what I needed.  3 lines to write or read a inhomogeneous 
collection of variables. I can add more variables, shuffle the order, 
whatever without messing with formatting, etc. That's pretty easy for me 
and it's easy for anyone to see and understand what's being done.  Not 
trying to start an argument, just showing how the former messasge I was 
replying to made a good point about Python's way of doing things and the 
effort to shake off old habits from other languages.

-- 
-- Lou Pecora



More information about the Python-list mailing list