<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Using YAML in python for structured data</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2014/02/03/Using-YAML-in-python-for-structured-data</link>
      <pubDate>Mon, 03 Feb 2014 09:36:29 EST</pubDate>
      <category><![CDATA[yaml]]></category>
      <category><![CDATA[template]]></category>
      <category><![CDATA[python]]></category>
      <guid isPermaLink="false">ArSfj5R_LUnzMBOZmC_61GOu-Qs=</guid>
      <description>Using YAML in python for structured data</description>
      <content:encoded><![CDATA[


&lt;p&gt;
&lt;a href="http://www.yaml.org/"&gt;YAML&lt;/a&gt; is a data format that is most text, with some indentation. It is like JSON, but without the braces. What is important here is that you can read a yaml document into a python dictionary. Here is an example of reading a yaml string so you can see the format.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; yaml
&lt;span style="color: #8b008b;"&gt;document&lt;/span&gt; = &lt;span style="color: #228b22;"&gt;"""&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;a: 1&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;b:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;  c: 3&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;  d: 4&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;"""&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; yaml.load(document)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
{'a': 1, 'b': {'c': 3, 'd': 4}}
&lt;/pre&gt;

&lt;p&gt;
Everything indented by the same level is grouped in its own dictionary. If we put that string into a file (&lt;a href="/media/2014-02-03-Using-YAML-in-python-for-structured-data/test.yaml"&gt;test.yaml&lt;/a&gt; ), we can read that in to python like this.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; yaml
&lt;span style="color: #8b008b;"&gt;document&lt;/span&gt; = &lt;span style="color: #cd0000;"&gt;open&lt;/span&gt;(&lt;span style="color: #228b22;"&gt;'test.yaml'&lt;/span&gt;).read()
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; yaml.load(document)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
{'a': 1, 'b': {'c': 3, 'd': 4}}
&lt;/pre&gt;

&lt;p&gt;
That example is pretty trivial. What I want to do is have  yaml file that represents a course syllabus. Then, if I had a set of these files, I could write code to analyze the collection of syllabi. For example, to figure out how many units of particular category there are. Alternatively, I could create different representations of the document, e.g. a pdf or html file for students or accreditation boards. Below is a YAML representtion of an ABET syllabus. It is pretty readable for a person.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; yaml
&lt;span style="color: #8b008b;"&gt;document&lt;/span&gt; = &lt;span style="color: #228b22;"&gt;"""&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;course:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;  course-number: 06-364&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;  title: Chemical Reaction Engineering&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;  units: 9&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;  description: Fundamental concepts in the kinetic modeling of chemical reactions, the treatment and analysis of rate data. Multiple reactions and reaction mechanisms. Analysis and design of ideal and non-ideal reactor systems. Energy effects and mass transfer in reactor systems. Introductory principles in heterogeneous catalysis. &lt;/span&gt;

&lt;span style="color: #228b22;"&gt;  textbook: H. S. Fogler, Elements of Chemical Reaction Engineering, 4th edition, Prentice Hall, New York, 2006.&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;  prerequisites: [06-321, 06-323, 09-347]&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;  required: Yes&lt;/span&gt;

&lt;span style="color: #228b22;"&gt;  goals:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    goal1: &lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      description: To analyze kinetic data and obtain rate laws &lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      outcomes: [a, k]&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      criteria: [A, F]&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    goal2:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      description: To develop a mechanism that is consistent with an experimental rate law &lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    goal3:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      description: To understand the behavior of different reactor types when they are used either individually or in combination &lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    goal4: &lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      description: To choose a reactor and determine its size for a given application&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    goal5:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      description: To work with mass and energy balances in the design of non-isothermal reactors &lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    goal6:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      description: To understand the importance of selectivity and know the strategies that are commonly used in maximizing yields&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    goal7:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;      description: To effectively use mathematical software in the design of reactors and analysis of data &lt;/span&gt;

&lt;span style="color: #228b22;"&gt;  topics:&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Conversion and reactor sizing&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Rate laws and stoichiometry&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Isothermal reactor design&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Collection and analysis of rate data&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Multiple reactions and selectivity&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Non-elementary reaction kinetics&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Non-isothermal reactor design&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Unsteady operation of reactors&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    - Catalysis and catalytic reactors&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;"""&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;with&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;open&lt;/span&gt;(&lt;span style="color: #228b22;"&gt;'06-364.yaml'&lt;/span&gt;, &lt;span style="color: #228b22;"&gt;'w'&lt;/span&gt;) &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; f:
    f.write(document)

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; yaml.load(document)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
{'course': {'description': 'Fundamental concepts in the kinetic modeling of chemical reactions, the treatment and analysis of rate data. Multiple reactions and reaction mechanisms. Analysis and design of ideal and non-ideal reactor systems. Energy effects and mass transfer in reactor systems. Introductory principles in heterogeneous catalysis.', 'title': 'Chemical Reaction Engineering', 'prerequisites': ['06-321', '06-323', '09-347'], 'topics': ['Conversion and reactor sizing', 'Rate laws and stoichiometry', 'Isothermal reactor design', 'Collection and analysis of rate data', 'Multiple reactions and selectivity', 'Non-elementary reaction kinetics', 'Non-isothermal reactor design', 'Unsteady operation of reactors', 'Catalysis and catalytic reactors'], 'required': True, 'textbook': 'H. S. Fogler, Elements of Chemical Reaction Engineering, 4th edition, Prentice Hall, New York, 2006.', 'goals': {'goal6': {'description': 'To understand the importance of selectivity and know the strategies that are commonly used in maximizing yields'}, 'goal7': {'description': 'To effectively use mathematical software in the design of reactors and analysis of data'}, 'goal4': {'description': 'To choose a reactor and determine its size for a given application'}, 'goal5': {'description': 'To work with mass and energy balances in the design of non-isothermal reactors'}, 'goal2': {'description': 'To develop a mechanism that is consistent with an experimental rate law'}, 'goal3': {'description': 'To understand the behavior of different reactor types when they are used either individually or in combination'}, 'goal1': {'outcomes': ['a', 'k'], 'description': 'To analyze kinetic data and obtain rate laws', 'criteria': ['A', 'F']}}, 'units': 9, 'course-number': '06-364'}}
&lt;/pre&gt;




&lt;p&gt;
You can see here the whole document is now stored as a dictionary. You
might ask why? I have the following interests:
&lt;/p&gt;
&lt;ol class="org-ol"&gt;
&lt;li&gt;If I have a set of these files, I could loop through them and
generate some kind of summary, e.g. total units of some category.
&lt;/li&gt;
&lt;li&gt;I could generate a consistent format using a template. 
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Let us explore the template. We will generate a LaTeX document using
the Cheetah template engine (&lt;a href="http://www.cheetahtemplate.org/"&gt;http://www.cheetahtemplate.org/&lt;/a&gt; ). I have also used &lt;a href="http://www.makotemplates.org/"&gt;Mako&lt;/a&gt; , and &lt;a href="http://jinja.pocoo.org/"&gt;jinja&lt;/a&gt; . A
template is a fancy string that has code in that can be evaluated and
substituted at generation time. We use this to replace elements of the
template with data from our yaml document.  Below I created a template
that generates a LaTeX document.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; yaml
&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; Cheetah.Template &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; Template

&lt;span style="color: #8b0000;"&gt;with&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;open&lt;/span&gt;(&lt;span style="color: #228b22;"&gt;'06-364.yaml'&lt;/span&gt;, &lt;span style="color: #228b22;"&gt;'r'&lt;/span&gt;) &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; f:
    &lt;span style="color: #8b008b;"&gt;document&lt;/span&gt; = yaml.load(f.read())

&lt;span style="color: #8b008b;"&gt;data&lt;/span&gt; = document[&lt;span style="color: #228b22;"&gt;'course'&lt;/span&gt;]

&lt;span style="color: #8b008b;"&gt;template&lt;/span&gt; = r&lt;span style="color: #228b22;"&gt;'''\documentclass{article}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\renewcommand{\abstractname}{Course Description}&lt;/span&gt;

&lt;span style="color: #228b22;"&gt;\begin{document}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\title{$data['course-number'] $data['title']}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\maketitle&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\begin{abstract}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;$data['description']&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\end{abstract}&lt;/span&gt;

&lt;span style="color: #228b22;"&gt;\textbf{Required:} $data['required']&lt;/span&gt;

&lt;span style="color: #228b22;"&gt;\textbf{Prerequisites:} #echo ', '.join($data['prerequisites'])&lt;/span&gt;

&lt;span style="color: #228b22;"&gt;{\textbf{Textbook:} $data['textbook']&lt;/span&gt;

&lt;span style="color: #228b22;"&gt;\section{Course goals}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\begin{enumerate}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;#for $goal in $data['goals']&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\item $data['goals'][$goal]['description'] \label{$goal}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;#end for&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\end{enumerate}&lt;/span&gt;

&lt;span style="color: #228b22;"&gt;\section{Topics}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\begin{itemize}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;#for $topic in $data['topics']&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\item $topic&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;#end for&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\end{itemize}&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;\end{document}'''&lt;/span&gt;

&lt;span style="color: #8b008b;"&gt;t&lt;/span&gt; = Template(template, searchList=&lt;span style="color: #cd0000;"&gt;locals&lt;/span&gt;())

&lt;span style="color: #ff0000; font-weight: bold;"&gt;#&lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;import sys; sys.exit()&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;with&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;open&lt;/span&gt;(&lt;span style="color: #228b22;"&gt;'06-364.tex'&lt;/span&gt;, &lt;span style="color: #228b22;"&gt;'w'&lt;/span&gt;) &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; f:
    f.write(t.respond())
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
None
&lt;/pre&gt;

&lt;p&gt;
You can see the results of the tex file here: &lt;a href="/media/2014-02-03-Using-YAML-in-python-for-structured-data/06-364.tex"&gt;06-364.tex&lt;/a&gt; , and the
corresponding pdf here: &lt;a href="/media/2014-02-03-Using-YAML-in-python-for-structured-data/06-364.pdf"&gt;06-364.pdf&lt;/a&gt; . It is not spectacular by any
means, but if I had 16 of these to create, this sure would be
convenient! And if we need some other format, we just make a new template!
&lt;/p&gt;

&lt;p&gt;
Some notes about this:
&lt;/p&gt;
&lt;ol class="org-ol"&gt;
&lt;li&gt;The course goals are not in the order defined in the yaml file. That is not too surprising, since dictionaries do not preserve order.
&lt;/li&gt;
&lt;li&gt;Yes in yaml apparently is read in as a boolean, so in the pdf, it is printed as True.
&lt;/li&gt;
&lt;li&gt;I have not thought about how to prepare a table that maps student outcomes (a-k in ABET) to the course goals
&lt;/li&gt;
&lt;li&gt;It would be nice if there were links in the pdf to other syllabi, e.g. the prerequisites. See &lt;a href="http://ctan.mirrorcatalogs.com/macros/latex/required/tools/xr.pdf"&gt;http://ctan.mirrorcatalogs.com/macros/latex/required/tools/xr.pdf&lt;/a&gt; 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Copyright (C) 2014 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2014/02/03/Using-YAML-in-python-for-structured-data.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;&lt;p&gt;Org-mode version = 8.2.5g&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
