Popular recipes tagged "yaml"http://code.activestate.com/recipes/tags/yaml/popular/2012-06-20T08:09:13-07:00ActiveState Code RecipesSharing-aware tree transformations (Python) 2012-05-07T08:20:58-07:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578117-sharing-aware-tree-transformations/ <p style="color: grey"> Python recipe 578117 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/fold/">fold</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/sharing/">sharing</a>, <a href="/recipes/tags/tree/">tree</a>, <a href="/recipes/tags/yaml/">yaml</a>). Revision 2. </p> <p>The function <code>foldsh</code> in this recipe is a general purpose tool for transforming tree-like recursive data structures while keeping track of shared subtrees.</p> <pre class="prettyprint"><code># By default, a branch is encoded as a list of subtrees; each subtree can be a # branch or a leaf (=anything non-iterable). Subtrees can be shared: &gt;&gt;&gt; subtree = [42,44] &gt;&gt;&gt; tree = [subtree,[subtree]] # We can apply a function to all leaves: &gt;&gt;&gt; foldsh(tree, leaf= lambda x: x+1) [[43, 45], [[43, 45]]] # Or apply a function to the branches: &gt;&gt;&gt; foldsh(tree, branch= lambda t,c: list(reversed(c))) [[[44, 42]], [44, 42]] # The sharing is preserved: &gt;&gt;&gt; _[0][0] is _[1] True # Summing up the leaves without double counting of shared subtrees: &gt;&gt;&gt; foldsh(tree, branch= lambda t,c: sum(c), shared= lambda x: 0) 86 </code></pre> <p>In particular, it is useful for transforming YAML documents. An example of this is given below.</p> Cycle-aware tree transformations (Python) 2012-06-20T08:09:13-07:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578118-cycle-aware-tree-transformations/ <p style="color: grey"> Python recipe 578118 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/cyclic/">cyclic</a>, <a href="/recipes/tags/fold/">fold</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/yaml/">yaml</a>). </p> <p>A variation on <a href="http://code.activestate.com/recipes/578117/">Recipe 578117</a> that can deal with cycles. A cycle means that a tree has itself as a subtree somewhere. A fold over such a data structure has a chicken-and-egg-problem: it needs its own result in order to construct its own result. To solve this problem, we let <code>branch</code> construct a <em>part</em> of its result before going into recursion. After the recursion, <code>branch</code> gets a chance to complete its result using its children's results. Python's support for coroutines (using <code>yield</code>) provides a nice way to define such a two-stage <code>branch</code> function.</p> Twitter incremental backup in YAML format - by HTML get and parse (Python) 2011-09-24T18:09:00-07:00Robert Lujohttp://code.activestate.com/recipes/users/4044016/http://code.activestate.com/recipes/577877-twitter-incremental-backup-in-yaml-format-by-html-/ <p style="color: grey"> Python recipe 577877 by <a href="/recipes/users/4044016/">Robert Lujo</a> (<a href="/recipes/tags/backup/">backup</a>, <a href="/recipes/tags/twitter/">twitter</a>, <a href="/recipes/tags/yaml/">yaml</a>). </p> <p>Yet another script for backing up Twitter posts (statuses). More information can be found <a href="http://robert-lujo.com/post/10603896621/twitter-backup">here</a>.</p> <p>The script is based on <a href="http://code.activestate.com/recipes/576594/" rel="nofollow">http://code.activestate.com/recipes/576594/</a>, <a href="http://movingtofreedom.org/2009/03/18/python-script-for-backing-up-twitter-statuses/." rel="nofollow">http://movingtofreedom.org/2009/03/18/python-script-for-backing-up-twitter-statuses/.</a></p> YAML include support (Python) 2011-03-17T22:18:30-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/577613-yaml-include-support/ <p style="color: grey"> Python recipe 577613 by <a href="/recipes/users/4172244/">Michael Grünewald</a> (<a href="/recipes/tags/yaml/">yaml</a>). </p> <p>This recipe shows how one can add <em>include</em> support to applications that use PyYAML.</p>