HEX
Server: LiteSpeed
System: Linux hz1.serversetup.co 4.18.0-513.18.1.el8_9.x86_64 #1 SMP Thu Feb 22 03:02:37 EST 2024 x86_64
User: axonvira (1009)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system,proc_open,curl_multi_exec,show_source
Upload Files
File: //usr/share/doc/python3-zope-event/html/usage.html
<!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="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Using zope.event &#8212; zope.event 4.2.0 documentation</title>
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></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="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.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" role="navigation" aria-label="related navigation">
      <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 class="nav-item nav-item-0"><a href="index.html">zope.event 4.2.0 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="using-zope-event">
<span id="usage-docs"></span><h1>Using <code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.event</span></code><a class="headerlink" href="#using-zope-event" title="Permalink to this headline">¶</a></h1>
<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">zope.event</span></code> package has a list of subscribers.  Application code
can manage subscriptions by manipulating this list.  For the examples here,
we’ll save the current contents away and empty the list. We’ll restore the
contents when we’re done with our examples.</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><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 <code class="xref py py-func docutils literal notranslate"><span class="pre">notify()</span></code> function, which is used to
notify subscribers that something has happened:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><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-pycon notranslate"><div class="highlight"><pre><span></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="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-pycon notranslate"><div class="highlight"><pre><span></span><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="s1">&#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-pycon notranslate"><div class="highlight"><pre><span></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">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="s1">&#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-pycon notranslate"><div class="highlight"><pre><span></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">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’re done, so we’ll restore the subscribers:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></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="p">[:]</span> <span class="o">=</span> <span class="n">old_subscribers</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter"><code class="docutils literal notranslate"><span class="pre">zope.event</span></code> Documentation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="theory.html"
                        title="next chapter">Theory of Operation</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/usage.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <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>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <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 class="nav-item nav-item-0"><a href="index.html">zope.event 4.2.0 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2010, Zope Foundation and Contributors.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.6.
    </div>
  </body>
</html>