Top-rated recipes tagged "shortcuts"http://code.activestate.com/recipes/tags/shortcuts/top/2010-08-01T01:19:23-07:00ActiveState Code RecipesNamed 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> 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> 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> Load data in a web browser without using temp files (Python) 2004-12-21T07:50:58-08:00Jimmy Retzlaffhttp://code.activestate.com/recipes/users/98735/http://code.activestate.com/recipes/347810-load-data-in-a-web-browser-without-using-temp-file/ <p style="color: grey"> Python recipe 347810 by <a href="/recipes/users/98735/">Jimmy Retzlaff</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>It is often nice to display something in a web browser, perhaps as an easy way to display rich output or for testing purposes. Sometimes the baggage of a temporary file is not desired when doing this.</p> Rebind (Python) 2008-02-12T05:37:23-08:00Neal Beckerhttp://code.activestate.com/recipes/users/2591287/http://code.activestate.com/recipes/546510-rebind/ <p style="color: grey"> Python recipe 546510 by <a href="/recipes/users/2591287/">Neal Becker</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Temporarily rebind an attribute using contextmanager</p> PathCatcher - Windows utility for right-click capture of file or folder path (Python) 2008-04-19T17:14:18-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528893-pathcatcher-windows-utility-for-right-click-captur/ <p style="color: grey"> Python recipe 528893 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>PathCatcher is a Windows utility that allows one to right-click on a file or a folder or a group of files and folders in Explorer and save its path to the clipboard.</p> <p>To install, save the code as PathCatcher.py, then double-click the PathCatcher.py file. Afterwards, PathCatcher will appear in the Explorer right-click menu.</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> Add 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> emulate collections.defaultdict (Python) 2007-07-09T14:15:39-07:00Jason Kirtlandhttp://code.activestate.com/recipes/users/4067388/http://code.activestate.com/recipes/523034-emulate-collectionsdefaultdict/ <p style="color: grey"> Python recipe 523034 by <a href="/recipes/users/4067388/">Jason Kirtland</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>A pure-Python version of Python 2.5's defaultdict</p> Fast constant function (Python) 2007-02-12T21:29:40-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/502215-fast-constant-function/ <p style="color: grey"> Python recipe 502215 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>Fast factory funtion for Py2.5's defaultdict making simple use of itertools. Equivalent to lambda:some_constant.</p> Implementing an Immutable Dictionary (Python) 2007-06-13T09:23:09-07:00Aristotelis Mikropouloshttp://code.activestate.com/recipes/users/2881737/http://code.activestate.com/recipes/498072-implementing-an-immutable-dictionary/ <p style="color: grey"> Python recipe 498072 by <a href="/recipes/users/2881737/">Aristotelis Mikropoulos</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 7. </p> <p>The implementation of a dictionary, whose items cannot be reset or deleted, nor new can be added.</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> 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> Binary Search with the bisect Module (Python) 2005-02-22T15:27:36-08:00Chris Perkinshttp://code.activestate.com/recipes/users/113957/http://code.activestate.com/recipes/389169-binary-search-with-the-bisect-module/ <p style="color: grey"> Python recipe 389169 by <a href="/recipes/users/113957/">Chris Perkins</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Writing a binary search algorithm is surprisingly error-prone. The solution: trick the built-in bisect module into doing it for you.</p> <p>The documentation for bisect says that it works on lists, but it really works on anything with a __getitem__ method. You can exploit this fact to make bisect work in ways that you may not have thought of.</p> <p>Example: Using a library that controls a digital video camera, I wanted to do a poor-man's auto-exposure. The goal is to find the exposure time, in milliseconds, that makes the mean pixel value about 128 (out of 0 to 255).</p> <p>The trick is to create an object that "looks like" a list to the bisect module.</p> A nicer syntax for super(cls,self) (Python) 2004-05-19T10:59:30-07:00Michele Simionatohttp://code.activestate.com/recipes/users/1122360/http://code.activestate.com/recipes/284528-a-nicer-syntax-for-superclsself/ <p style="color: grey"> Python recipe 284528 by <a href="/recipes/users/1122360/">Michele Simionato</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>Makes cooperative calls looks nicer: super.method instead of super(cls,self).method .</p> Assignment in expression (Python) 2003-05-26T14:15:40-07:00Sébastien Keimhttp://code.activestate.com/recipes/users/131730/http://code.activestate.com/recipes/202234-assignment-in-expression/ <p style="color: grey"> Python recipe 202234 by <a href="/recipes/users/131730/">Sébastien Keim</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>An evil equivalent for C expression: 'while x=f()'.</p> transposing a list of lists (Python) 2003-05-15T15:12:06-07:00Fee Theehttp://code.activestate.com/recipes/users/195662/http://code.activestate.com/recipes/199944-transposing-a-list-of-lists/ <p style="color: grey"> Python recipe 199944 by <a href="/recipes/users/195662/">Fee Thee</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>By using the zip builtin function you can easily convert a list of lists into a transposed list of tuples. This can be used for easily selecting columns.</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> Iterating Over Dates (Python) 2008-06-30T07:51:43-07:00Michael Corneliushttp://code.activestate.com/recipes/users/4160122/http://code.activestate.com/recipes/574441-iterating-over-dates/ <p style="color: grey"> Python recipe 574441 by <a href="/recipes/users/4160122/">Michael Cornelius</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Iterate over a range of dates, [begin, end), day by day. By using datetime objects, rather than date object, it is possible to iterate over smaller time-granularities if desired.</p> Error logging with context manager and decorator (Python) 2007-09-19T06:52:58-07:00Kent Johnsonhttp://code.activestate.com/recipes/users/2016182/http://code.activestate.com/recipes/531821-error-logging-with-context-manager-and-decorator/ <p style="color: grey"> Python recipe 531821 by <a href="/recipes/users/2016182/">Kent Johnson</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>A context manager and decorator that wrap common exception trapping and handling code so they may be applied with just a single line of code in the client.</p>