Most viewed recipes tagged "shortcuts"http://code.activestate.com/recipes/tags/shortcuts/views/2012-01-24T14:20:01-08:00ActiveState Code RecipesAdd an entry to a dictionary, unless the entry is already there (Python) 2001-08-14T13:18:30-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66516-add-an-entry-to-a-dictionary-unless-the-entry-is-a/ <p style="color: grey"> Python recipe 66516 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>Often, when working with a dictionary D, you need to use the entry D[k] if it's already present, or add a new D[k] if k wasn't a key into D. The setdefault method of dictionaries is a very handy shortcut for this task.</p> Named Tuples (Python) 2009-05-26T22:44:39-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/500261-named-tuples/ <p style="color: grey"> Python recipe 500261 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 15. </p> <p>Fast, lightweight attribute-style access to tuples.</p> Looping through multiple lists (Python) 2001-06-29T16:53:42-07:00andy mckayhttp://code.activestate.com/recipes/users/92886/http://code.activestate.com/recipes/65285-looping-through-multiple-lists/ <p style="color: grey"> Python recipe 65285 by <a href="/recipes/users/92886/">andy mckay</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>Often you need to loop through every item of multiple lists and compare them. This can be done without a using a counter.</p> frange(), a range function with float increments (Python) 2001-08-07T05:00:48-07:00Dinu Ghermanhttp://code.activestate.com/recipes/users/124101/http://code.activestate.com/recipes/66472-frange-a-range-function-with-float-increments/ <p style="color: grey"> Python recipe 66472 by <a href="/recipes/users/124101/">Dinu Gherman</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Sadly missing in the Python standard library, this function allows to use ranges, just as the built-in function range(), but with float arguments.</p> <p>All thoretic restrictions apply, but in practice this is more useful than in theory.</p> Exception-based Switch-Case (Python) 2008-10-26T04:31:24-07:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/410695-exception-based-switch-case/ <p style="color: grey"> Python recipe 410695 by <a href="/recipes/users/2400454/">Zoran Isailovski</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 11. </p> <p>Here is yet another way to emulate a switch-case statement, perhaps one you might not have thought of.</p> Invert a dictionary (one-liner) (Python) 2003-11-17T20:38:28-08:00Joel Lawheadhttp://code.activestate.com/recipes/users/545119/http://code.activestate.com/recipes/252143-invert-a-dictionary-one-liner/ <p style="color: grey"> Python recipe 252143 by <a href="/recipes/users/545119/">Joel Lawhead</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Dictionaries map keys to values. Looking up a value is as simple as typing: mydict[key]. But what if you want to look up a key? The following one liner returns a new dictionary with the keys and values swapped:</p> Getting a value from a dictionary (Python) 2001-05-29T09:51:54-07:00andy mckayhttp://code.activestate.com/recipes/users/92886/http://code.activestate.com/recipes/59866-getting-a-value-from-a-dictionary/ <p style="color: grey"> Python recipe 59866 by <a href="/recipes/users/92886/">andy mckay</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Want to get a value from a dictionary but want to make sure that the value exists in the dictionary? Then use the incredibly useful get method.</p> LRU and LFU cache decorators (Python) 2010-08-01T01:19:23-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/ <p style="color: grey"> Python recipe 498245 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 6. </p> <p>One-line decorator call adds caching to functions with hashable arguments and no keyword arguments. When the maximum size is reached, the least recently used entry or least frequently used entry is discarded -- appropriate for long-running processes which cannot allow caches to grow without bound. Includes built-in performance instrumentation.</p> list of all combination from multiple lists (Python) 2006-06-19T17:19:27-07:00Wensheng Wanghttp://code.activestate.com/recipes/users/1513433/http://code.activestate.com/recipes/496807-list-of-all-combination-from-multiple-lists/ <p style="color: grey"> Python recipe 496807 by <a href="/recipes/users/1513433/">Wensheng Wang</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>With list comprehension, you can easily loop through all combinations of 2 or 3 lists. But what about more, like 4,6, 20? This recipe show a way to do this, without use of recursion.</p> Testing for an empty iterator (Python) 2005-05-17T15:51:08-07:00Michael Chermsidehttp://code.activestate.com/recipes/users/1782375/http://code.activestate.com/recipes/413614-testing-for-an-empty-iterator/ <p style="color: grey"> Python recipe 413614 by <a href="/recipes/users/1782375/">Michael Chermside</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>With lists, it is common to test whether the list is empty and perform special code for the empty case. With iterators, this becomes awkward -- testing whether the iterator is empty will use up the first item! The solution is an idiom based on itertools.tee().</p> Remove Duplicacy from a Python List (Python) 2005-09-02T15:55:14-07:00Bibha Tripathihttp://code.activestate.com/recipes/users/2437311/http://code.activestate.com/recipes/440509-remove-duplicacy-from-a-python-list/ <p style="color: grey"> Python recipe 440509 by <a href="/recipes/users/2437311/">Bibha Tripathi</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 4. </p> <p>I needed a list for a long bunch of elements but then I didn't want duplicates. I LOVE Sets, so I use a set to get the list I needed.</p> Solve simultaneous linear equations in two variables (Python) 2012-01-24T14:20:01-08:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/578024-solve-simultaneous-linear-equations-in-two-variabl/ <p style="color: grey"> Python recipe 578024 by <a href="/recipes/users/4169530/">Anand B Pillai</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/solver/">solver</a>). </p> <p>A function to solve simultaneous equations in two variables. </p> <pre class="prettyprint"><code>&gt;&gt;&gt; solve('3*x + 5*y = 29; 12*x - 3*y = 24') (3.0, 4.0) </code></pre> Finding the intersection of two dicts (Python) 2001-06-19T16:28:37-07:00andy mckayhttp://code.activestate.com/recipes/users/92886/http://code.activestate.com/recipes/59875-finding-the-intersection-of-two-dicts/ <p style="color: grey"> Python recipe 59875 by <a href="/recipes/users/92886/">andy mckay</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>A simple little task, how to find the intersection of two hashes. There's probably many ways to do this but there is one way to avoid...</p> Processing a string one character at a time (Python) 2001-06-10T15:44:26-07:00Hamish Lawsonhttp://code.activestate.com/recipes/users/98049/http://code.activestate.com/recipes/65118-processing-a-string-one-character-at-a-time/ <p style="color: grey"> Python recipe 65118 by <a href="/recipes/users/98049/">Hamish Lawson</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>You want to process a string one character at a time.</p> Some python style switches (Python) 2004-05-11T21:38:56-07:00Runsun Panhttp://code.activestate.com/recipes/users/1521341/http://code.activestate.com/recipes/269708-some-python-style-switches/ <p style="color: grey"> Python recipe 269708 by <a href="/recipes/users/1521341/">Runsun Pan</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 6. </p> <p>Python style switches. Showing several ways of making a 'switch' in python.</p> The Secret Name of List Comprehensions (Python) 2003-06-09T20:41:25-07:00Chris Perkinshttp://code.activestate.com/recipes/users/113957/http://code.activestate.com/recipes/204297-the-secret-name-of-list-comprehensions/ <p style="color: grey"> Python recipe 204297 by <a href="/recipes/users/113957/">Chris Perkins</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>Sometimes you want to have a list comprehension refer to itself, but you can't because it isn't bound to a name until after it is fully constructed. However, the interpreter creates a secret name that only exists while the list is being built. That name is (usually) "_[1]", and it refers to the bound method "append" of the list. This is our back door to get at the list object itself.</p> Basic Exception handling idiom using decorators (Python) 2005-04-06T04:25:10-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/408937-basic-exception-handling-idiom-using-decorators/ <p style="color: grey"> Python recipe 408937 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>A basic exception handling idiom using decorators which allows you to re-use the exception handler for different functions, while customizing your handlers using arguments passed to an exception handling "decorator".</p> Simple calls to Python functions from command line or shortcut (Python) 2008-07-22T02:17:06-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528891-simple-calls-to-python-functions-from-command-line/ <p style="color: grey"> Python recipe 528891 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>This module allows one to create a command to call a Python function from the command line, and to call that function with arguments from the command line without using getopt or optparse</p> Default Dictionary (Python) 2005-02-25T15:47:45-08:00Peter Norvighttp://code.activestate.com/recipes/users/161501/http://code.activestate.com/recipes/389639-default-dictionary/ <p style="color: grey"> Python recipe 389639 by <a href="/recipes/users/161501/">Peter Norvig</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>A dictionary with a default value (such as 0 or [], or whatever you want) for unassigned keys</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>