Popular recipes tagged "dispatcher"http://code.activestate.com/recipes/tags/dispatcher/2013-07-29T05:34:24-07:00ActiveState Code RecipesPython: A event/signal dispatcher (Python)
2013-07-29T05:34:24-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578307-python-a-eventsignal-dispatcher/
<p style="color: grey">
Python
recipe 578307
by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a>
(<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/signals/">signals</a>).
</p>
<p>This is a thread-safe dispatcher.</p>
Fast and elegant switch/case-like dispatch (Python)
2011-09-02T01:49:40-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577864-fast-and-elegant-switchcase-like-dispatch/
<p style="color: grey">
Python
recipe 577864
by <a href="/recipes/users/4172762/">Jan Kaliszewski</a>
(<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/switch/">switch</a>).
Revision 4.
</p>
<p>My approach to that common issue focuses on <strong>efficiency</strong> and <strong>elegant, declarative style</strong> of definition. That's why:</p>
<ul>
<li>The way switches work is based on unwrapped defaultdict/list lookup.</li>
<li>The way you define switches is based on classes and easy-to-use decorators (note that you can use subclassing in a flexible way -- without any negative impact on efficiency!).</li>
<li>Its use cases focus on a-lot-of-keys situations and it does not cover the <em>fall-through</em> feature (though you can reach its semantics if you really need that -- by calling class methods...).</li>
</ul>
A powerful yet simple switch-like dispatch system for Python (Python)
2010-12-21T04:38:48-08:00Michael Kenthttp://code.activestate.com/recipes/users/1184676/http://code.activestate.com/recipes/577507-a-powerful-yet-simple-switch-like-dispatch-system-/
<p style="color: grey">
Python
recipe 577507
by <a href="/recipes/users/1184676/">Michael Kent</a>
(<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/switch/">switch</a>).
Revision 2.
</p>
<p>This module provides a powerful 'switch'-like dispatcher system, using decorators to give a syntax somewhat like that of the switch statement in C. Values for switch cases can be anything comparable via '==', a string for use on the left-hand side of the 'in' operator, or a regular expression.
Iterables of these types can also be used.</p>
Simple event dispatcher (Python)
2010-10-19T21:18:45-07:00Daniele Espostihttp://code.activestate.com/recipes/users/4174769/http://code.activestate.com/recipes/577432-simple-event-dispatcher/
<p style="color: grey">
Python
recipe 577432
by <a href="/recipes/users/4174769/">Daniele Esposti</a>
(<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/event/">event</a>).
</p>
<p>An example of a simple event dispatcher mini-framework</p>
Method-based URL dispatcher for the Tornado web server (Python)
2009-11-20T11:51:47-08:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/576958-method-based-url-dispatcher-for-the-tornado-web-se/
<p style="color: grey">
Python
recipe 576958
by <a href="/recipes/users/4169722/">Dan McDougall</a>
(<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/subclass/">subclass</a>, <a href="/recipes/tags/tornado/">tornado</a>, <a href="/recipes/tags/url/">url</a>, <a href="/recipes/tags/web/">web</a>).
Revision 5.
</p>
<p>The MethodDispatcher is a subclass of <a href="http://www.tornadoweb.org/">tornado</a>.web.RequestHandler that will use
the methods contained in subclasses of MethodDispatcher to handle requests. In
other words, instead of having to make a new RequestHandler class for every URL
in your application you can subclass MethodDispatcher and use the methods
contained therein <em>as</em> your URLs.</p>
<p>The MethodDispatcher also adds the convenience of automatically passing
arguments to your class methods. So there is no need to use Tornado's
get_argument() method.</p>
<h5><strong>Example</strong></h5>
<p>To demonstrate the advantages of using MethodDispatcher I'll present a standard
Tornado app with multiple URLs and re-write it using MethodDispatcher...</p>
<h5><strong>The standard Tornado way</strong></h5>
<pre class="prettyprint"><code>class Foo(tornado.web.RequestHandler):
def get(self):
self.write('foo')
class Bar(tornado.web.RequestHandler):
def get(self):
self.write('bar')
class SimonSays(tornado.web.RequestHandler):
def get(self):
say = self.get_argument("say")
self.write('Simon says, %s' % `say`)
application = tornado.web.Application([
(r"/foo", Foo),
(r"/bar", Bar),
(r"/simonsays", SimonSays),
])
</code></pre>
<h5><strong>The MethodDispatcher way</strong></h5>
<pre class="prettyprint"><code>class FooBar(MethodDispatcher):
def foo(self):
self.write("foo")
def bar(self):
self.write("bar")
def simonsays(self, say):
self.write("Simon Says, %s" % `say`)
application = tornado.web.Application([
(r"/.*", FooBar)
])
</code></pre>
<h5><strong>Notes</strong></h5>
<p>As you can see from the above example, using the MethodDispatcher can
significantly reduce the complexity of Tornado applications. Here's some other
things to keep in mind when using the MethodDispatcher:</p>
<ul>
<li>MethodDispatcher will ignore any methods that begin with an underscore (_).
This prevents builtins and private methods from being exposed to the web.</li>
<li>The '/' path is special: It always maps to self.index().</li>
<li>MethodDispatcher does not require that your methods distinquish between GET
and POST requests. Whether a GET or POST is performed the matching method
will be called with any passed arguments or POSTed data. Because of the way
this works you should not use get() and post() in your MethodDispatcher
subclasses unless you want to override this functionality.</li>
<li>When an argument is passed with a single value (/simonsays?say=hello) the
value passed to the argument will be de-listed. In other words, it will be
passed to your method like so: {'say': 'hello'}. This overrides the
default Tornado behavior which would return the value as a list:
{'say': ['hello']}. If more than one value is passed MethodDispatcher will
use the default behavior.</li>
</ul>