Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 5b16fd19785a7e52f49fb873ab22f966 > files > 56

python-zope-event-4.0.2-1.fc18.noarch.rpm



<!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>Using zope.event &mdash; zope.event 4.0.2 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:     '4.0.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="zope.event 4.0.2 documentation" href="index.html" />
    <link rel="next" title="Theory of Operation" href="theory.html" />
    <link rel="prev" title="zope.event Documentation" href="index.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="theory.html" title="Theory of Operation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="zope.event Documentation"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">zope.event 4.0.2 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="using-zope-event">
<h1>Using <tt class="xref py py-mod docutils literal"><span class="pre">zope.event</span></tt><a class="headerlink" href="#using-zope-event" title="Permalink to this headline">ΒΆ</a></h1>
<p>The <tt class="xref py py-mod docutils literal"><span class="pre">zope.event</span></tt> package has a list of subscribers.  Application code
can manage subscriptions by manipulating this list.  For the examples here,
we&#8217;ll save the current contents away and empty the list. We&#8217;ll restore the
contents when we&#8217;re done with our examples.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">zope.event</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">old_subscribers</span> <span class="o">=</span> <span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="p">[:]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="p">[:]</span>
</pre></div>
</div>
<p>The package provides a <tt class="xref py py-func docutils literal"><span class="pre">notify()</span></tt> function, which is used to
notify subscribers that something has happened:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MyEvent</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">pass</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">event</span> <span class="o">=</span> <span class="n">MyEvent</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="n">event</span><span class="p">)</span>
</pre></div>
</div>
<p>The notify function is called with a single object, which we call an
event.  Any object will do:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
</pre></div>
</div>
<p>An extremely trivial subscription mechanism is provided. Subscribers
are simply callback functions:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="s">&#39;got:&#39;</span><span class="p">,</span> <span class="n">event</span>
</pre></div>
</div>
<p>that are put into the subscriptions list:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">got: 42</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">event</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="s">&#39;also got:&#39;</span><span class="p">,</span> <span class="n">event</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">got: 42</span>
<span class="go">also got: 42</span>
</pre></div>
</div>
<p>To unsubscribe, simply remove a subscriber from the list:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">notify</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">also got: 42</span>
</pre></div>
</div>
<p>Generally, application frameworks will provide more sophisticated
subscription mechanisms that build on this simple mechanism. The
frameworks will install subscribers that then dispatch to other
subscribers based on event types or data.</p>
<p>We&#8217;re done, so we&#8217;ll restore the subscribers:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">zope</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">subscribers</span><span class="p">[:]</span> <span class="o">=</span> <span class="n">old_subscribers</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter"><tt class="docutils literal docutils literal docutils literal docutils literal docutils literal"><span class="pre">zope.event</span></tt> Documentation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="theory.html"
                        title="next chapter">Theory of Operation</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/usage.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" />
      <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="theory.html" title="Theory of Operation"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="zope.event Documentation"
             >previous</a> |</li>
        <li><a href="index.html">zope.event 4.0.2 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, Zope Foundation and Contributors.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>