<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stealthcopter.com &#187; yaml</title>
	<atom:link href="http://www.stealthcopter.com/blog/tag/yaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stealthcopter.com/blog</link>
	<description>Android, Linux, Python and stealthcopters</description>
	<lastBuildDate>Fri, 13 Jan 2012 16:29:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Saving settings in python with YAML (an XML alternative)</title>
		<link>http://www.stealthcopter.com/blog/2010/02/saving-settings-in-python-with-yaml-an-xml-alternative/</link>
		<comments>http://www.stealthcopter.com/blog/2010/02/saving-settings-in-python-with-yaml-an-xml-alternative/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 12:03:08 +0000</pubDate>
		<dc:creator>mat</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://www.stealthcopter.com/blog/?p=770</guid>
		<description><![CDATA[When transferring information or settings between computers and programs is is useful to use something simple and program independent. For a recent project I need to send a settings file between a PHP script and a python program, I needed something that would support trees so an INI file was out of the question, and [...]]]></description>
			<content:encoded><![CDATA[<p>When transferring information or settings between computers and programs is is useful to use something simple and program independent. For a recent project I need to send a settings file between a PHP script and a python program, I needed something that would support trees so an INI file was out of the question, and I naturally thought of using XML, however when looking for more information I stumbled upon YAML (seemingly forgotten alternative to XML).</p>
<p><a href="http://www.yaml.org/spec/1.2/spec.html">YAML</a> is a recursive acronym that stands for YAML Ain&#8217;t Markup Language and below is a simplified example taken from the YAML site. The layout is similar to python with indents representing the nested layers. Semicolons are used to seperate variable names and value pairs.</p>
<pre name="code" class="python">
name   : Joe Bloggs
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.5
</pre>
<p>To use YAML in python you will need the PyYAML library avaliable <a href="http://pyyaml.org/wiki/PyYAML">here</a> or in via your package manager in linux. (I.E. &#8220;sudo apt-get install http://pyyaml.org/wiki/PyYAML&#8221; in ubuntu/debian).</p>
<p>The code for then using YAML is very simple, first we import the yaml library, then we open our settings file we have created and then use yaml&#8217;s load feature to load our settings into a python dictionary, and then finally printing it out so we can see what it contains</p>
<pre name="code" class="python">
import yaml
f=open('settings.cfg')
settings=yaml.load(f)
print settings
</pre>
<p>Using the example YAML file above saved to settings.cfg this python script will output the following when run:</p>
<pre name="code" class="python">
{'product': [{'sku': 'BL394D', 'price': 450.0, 'description':
'Basketball', 'quantity': 4}, {'sku': 'BL4438H', 'price': 2392.0,
'description': 'Super Hoop', 'quantity': 1}], 'total': 4443.5, 'tax':
251.41999999999999, 'name': 'Joe Bloggs'}
</pre>
<p>We can now access all the information in the file very easily (I.E. settings['name'] will give us the name Joe Bloggs).</p>
<p>YAML supports much much more than what is discussed here and further information can be found on the YAML website.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stealthcopter.com/blog/2010/02/saving-settings-in-python-with-yaml-an-xml-alternative/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

