<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Adapted Data Contexts — CodeTools v3.1.0 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '3.1.0', COLLAPSE_MODINDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <link rel="shortcut icon" href="_static/et.ico"/> <link rel="top" title="CodeTools v3.1.0 documentation" href="index.html" /> <link rel="up" title="CodeTools Tutorial" href="tutorial.html" /> <link rel="next" title="Context Functions" href="tut_cxt_functions.html" /> <link rel="prev" title="MultiContexts" href="tut_multicontext.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="tut_cxt_functions.html" title="Context Functions" accesskey="N">next</a> |</li> <li class="right" > <a href="tut_multicontext.html" title="MultiContexts" accesskey="P">previous</a> |</li> <li><a href="index.html">CodeTools v3.1.0 documentation</a> »</li> <li><a href="tutorial.html" accesskey="U">CodeTools Tutorial</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="adapted-data-contexts"> <h1>Adapted Data Contexts<a class="headerlink" href="#adapted-data-contexts" title="Permalink to this headline">ΒΆ</a></h1> <p>Often the data inside a context needs to be transformed in some way before it is used:</p> <ul class="simple"> <li>possibly in different ways by different parts on an application</li> <li>with different transformations on different variables</li> <li>with multiple different transformations applied sequentially</li> </ul> <p>And the same sequence of transformations may need to be reversed when setting a value in to the context. These transformations may even need to be changed depending on the situation or a user request.</p> <p>The AdaptedDataContext class provides a framework for applying transformations to data as it comes in and out of a context. AdaptedDataContext provides a list of adapters in its <tt class="xref docutils literal"><span class="pre">adapters</span></tt> trait attribute, as well as <tt class="xref docutils literal"><span class="pre">push()</span></tt> and <tt class="xref docutils literal"><span class="pre">pop()</span></tt> methods for manipulating them. On get, set, or delete operations, the AdapterManagerMixin goes through each of the adapters and first calls the <tt class="xref docutils literal"><span class="pre">adapt_name()</span></tt> method to perform any manipulations of the name, and then calls <tt class="xref docutils literal"><span class="pre">adapt_getitem()</span></tt> or <tt class="xref docutils literal"><span class="pre">adapt_setitem()</span></tt> for get and set operations respectively. Once all the adapters have had a chance to perform transformations, the operation is applied to the underlying context.</p> <p>To create an adapter all you need to do is to implement the IAdapter interface by providing at least one of the <tt class="xref docutils literal"><span class="pre">adapt_name()</span></tt>, <tt class="xref docutils literal"><span class="pre">adapt_getitem()</span></tt>, or <tt class="xref docutils literal"><span class="pre">adapt_setitem()</span></tt> methods.</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">ndarray</span> <span class="kn">from</span> <span class="nn">numpy.fft</span> <span class="kn">import</span> <span class="n">fft</span><span class="p">,</span> <span class="n">ifft</span> <span class="kn">from</span> <span class="nn">enthought.traits.api</span> <span class="kn">import</span> <span class="n">HasTraits</span> <span class="kn">from</span> <span class="nn">enthought.contexts.api</span> <span class="kn">import</span> <span class="n">IAdapter</span> <span class="k">class</span> <span class="nc">FFTAdapter</span><span class="p">(</span><span class="n">HasTraits</span><span class="p">):</span> <span class="n">implements</span><span class="p">(</span><span class="n">IAdapter</span><span class="p">)</span> <span class="k">def</span> <span class="nf">adapt_getitem</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="sd">"""Take Fourier transform of 1-D Numpy arrays in the context."""</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">ndarray</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mf">1</span><span class="p">:</span> <span class="k">return</span> <span class="n">key</span><span class="p">,</span> <span class="n">fft</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">def</span> <span class="nf">adapt_setitem</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="sd">"""Take inverse Fourier transform of 1D numpy arrays when setting into</span> <span class="sd"> the context."""</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">ndarray</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mf">1</span><span class="p">:</span> <span class="k">return</span> <span class="n">key</span><span class="p">,</span> <span class="n">ifft</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> </pre></div> </div> <p>The <tt class="xref docutils literal"><span class="pre">enthought.contexts</span></tt> package contains a number of adapters to perform operations like masking, unit conversion, and name translation.</p> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <p class="logo"><a href="index.html"> <img class="logo" src="_static/e-logo-rev.png" alt="Logo"/> </a></p> <h4>Previous topic</h4> <p class="topless"><a href="tut_multicontext.html" title="previous chapter">MultiContexts</a></p> <h4>Next topic</h4> <p class="topless"><a href="tut_cxt_functions.html" title="next chapter">Context Functions</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/tut_adapted.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="tut_cxt_functions.html" title="Context Functions" >next</a> |</li> <li class="right" > <a href="tut_multicontext.html" title="MultiContexts" >previous</a> |</li> <li><a href="index.html">CodeTools v3.1.0 documentation</a> »</li> <li><a href="tutorial.html" >CodeTools Tutorial</a> »</li> </ul> </div> <div class="footer"> © Copyright 2008, Enthought. Last updated on Aug 21, 2009. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2. </div> </body> </html>