D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
usr
/
share
/
doc
/
python-zope-event-4.0.3
/
html
/
Filename :
usage.html
back
Copy
<!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 — zope.event 4.0.3 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.3', 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.3 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.3 documentation</a> »</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’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-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">zope.event</span> <span class="gp">>>> </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">>>> </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">>>> </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">>>> </span><span class="n">event</span> <span class="o">=</span> <span class="n">MyEvent</span><span class="p">()</span> <span class="gp">>>> </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">>>> </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">>>> </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">>>> </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">'got:'</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">>>> </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">>>> </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">>>> </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">'also got:'</span><span class="p">,</span> <span class="n">event</span> <span class="gp">>>> </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">>>> </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">>>> </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">>>> </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-python"><div class="highlight"><pre><span class="gp">>>> </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"><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.3 documentation</a> »</li> </ul> </div> <div class="footer"> © Copyright 2010, Zope Foundation and Contributors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Name
Size
Last Modified
Owner
Permissions
Actions
_sources
Dir
February 10 2022 8:31:22
root
0755
_static
Dir
February 10 2022 8:31:22
root
0755
api.html
5.655
KB
October 15 2014 1:44:09
root
0644
genindex.html
3.272
KB
October 15 2014 1:44:10
root
0644
hacking.html
21.583
KB
October 15 2014 1:44:09
root
0644
index.html
7.047
KB
October 15 2014 1:44:10
root
0644
objects.inv
0.253
KB
October 15 2014 1:44:10
root
0644
search.html
3.065
KB
October 15 2014 1:44:10
root
0644
searchindex.js
3.618
KB
October 15 2014 1:44:10
root
0644
theory.html
4.904
KB
October 15 2014 1:44:10
root
0644
usage.html
9.955
KB
October 15 2014 1:44:10
root
0644
2017 © D7net | D704T team