Popular recipes tagged "shortcuts" but not "sysadmin"http://code.activestate.com/recipes/tags/shortcuts-sysadmin/2014-08-02T02:56:12-07:00ActiveState Code RecipesYet Another NamedTuple (Python)
2014-08-02T02:56:12-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578918-yet-another-namedtuple/
<p style="color: grey">
Python
recipe 578918
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>Yet another look at Raymond Hettinger's excellent "namedtuple" factory. Unlike Raymond's version, this one minimizes the use of exec. In the original, the entire inner class is dynamically generated then exec'ed, leading to the bulk of the code being inside a giant string template. This version uses a regular inner class for everything except the __new__ method.</p>
The Secret Name of List Comprehensions (Python)
2014-04-21T20:09:43-07:00He Guhttp://code.activestate.com/recipes/users/4189822/http://code.activestate.com/recipes/578865-the-secret-name-of-list-comprehensions/
<p style="color: grey">
Python
recipe 578865
by <a href="/recipes/users/4189822/">He Gu</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</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>
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>>>> solve('3*x + 5*y = 29; 12*x - 3*y = 24')
(3.0, 4.0)
</code></pre>
(Yet another) Assignment in expression Recipe (Python)
2011-12-17T20:03:26-08:00harish anandhttp://code.activestate.com/recipes/users/4180291/http://code.activestate.com/recipes/577987-yet-another-assignment-in-expression-recipe/
<p style="color: grey">
Python
recipe 577987
by <a href="/recipes/users/4180291/">harish anand</a>
(<a href="/recipes/tags/expression/">expression</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 3.
</p>
<p>Python does not support assignment in if and while statements such as "if (x=func()):". This is an attempt to bring similar functionality to python by injecting bytecode to all functions and methods in a module.
This recipe is inspired from recipes <a href="http://code.activestate.com/recipes/66061">66061</a>, <a href="http://code.activestate.com/recipes/202234-assignment-in-expression">202234</a> and <a href="http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time">277940</a>.</p>
namedtuple.abc - abstract base class + mix-in for named tuples (Python)
2011-04-02T02:07:00-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577629-namedtupleabc-abstract-base-class-mix-in-for-named/
<p style="color: grey">
Python
recipe 577629
by <a href="/recipes/users/4172762/">Jan Kaliszewski</a>
(<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/dry/">dry</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 7.
</p>
<p>If you need</p>
<ul>
<li>to define <strong>named tuple subclasses</strong> (including reusable abstract ones), adding/overriding some methods, in a convenient way;</li>
<li>to have the named tuple ABC (abstract base class) for <strong>isinstance/issubclass</strong> tests;</li>
<li>or simply would like to define your named tuple classes in a <strong>class-syntax-based and DRY way</strong> (without repeating type names...)</li>
</ul>
<p>-- <strong>this recipe is for you.</strong></p>
Win Shortcuts wrapper. (Python)
2010-07-23T08:10:44-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/577321-win-shortcuts-wrapper/
<p style="color: grey">
Python
recipe 577321
by <a href="/recipes/users/4035877/">Louis RIVIERE</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 3.
</p>
<p>Reads or Creates Windows shortcuts (links).</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>
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>
TwitterCmd - Interactive console for twittering (Python)
2009-10-08T02:15:19-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/576923-twittercmd-interactive-console-for-twittering/
<p style="color: grey">
Python
recipe 576923
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/microblogging/">microblogging</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/twitter/">twitter</a>).
Revision 4.
</p>
<p>TwitterCmd is an interactive console for twitter built on top of python-twitter library. It provides short, easy to remember commands for most twitter actions and provides a one-way interaction with the Python interpreter, exposing its objects to further inspection by the python-twitter API.</p>
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>
Composable Functions (Python)
2008-07-12T06:08:55-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/574458-composable-functions/
<p style="color: grey">
Python
recipe 574458
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>Decorator for the "f . g" style of function composition familiar to functional programmers.</p>
Convenient Utility for Making Dictionaries (Python)
2008-07-23T23:55:00-07:00Andrew Konstantarashttp://code.activestate.com/recipes/users/4141807/http://code.activestate.com/recipes/576373-convenient-utility-for-making-dictionaries/
<p style="color: grey">
Python
recipe 576373
by <a href="/recipes/users/4141807/">Andrew Konstantaras</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>This function creates a dictionary containing all of the variables passed as parameters with the variable name being the key and the value of the variable as the value. I use it frequently when passing parameters to another function.</p>
Automate creation of new Subversion project (Python)
2008-07-09T20:57:15-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/574453-automate-creation-of-new-subversion-project/
<p style="color: grey">
Python
recipe 574453
by <a href="/recipes/users/4076953/">Jack Trainor</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>NewSvnProject creates a new Subversion repository, imports new files to it, then
checks out those files back to original directory, with a single command or
drag'n'drop.</p>
PyGoogle (Python)
2008-06-06T16:46:38-07:00Erik Andersonhttp://code.activestate.com/recipes/users/4155471/http://code.activestate.com/recipes/573447-pygoogle/
<p style="color: grey">
Python
recipe 573447
by <a href="/recipes/users/4155471/">Erik Anderson</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>PyGoogle searches google. The google search engine uses 'http://www.google.com/search?hl=en&q=' plus whatever you are searching and replaces the spaces with '+' signs.</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>
Create single-file executables for windows (Python)
2008-03-31T09:02:29-07:00Chris Somerlothttp://code.activestate.com/recipes/users/2751654/http://code.activestate.com/recipes/552749-create-single-file-executables-for-windows/
<p style="color: grey">
Python
recipe 552749
by <a href="/recipes/users/2751654/">Chris Somerlot</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>This windows batch script uses python and py2exe to create a single, distributable .exe of your python code.</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>
Page through iterable N items at a time (Python)
2008-02-20T18:57:43-08:00Wade Leftwichhttp://code.activestate.com/recipes/users/98656/http://code.activestate.com/recipes/546526-page-through-iterable-n-items-at-a-time/
<p style="color: grey">
Python
recipe 546526
by <a href="/recipes/users/98656/">Wade Leftwich</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>Simple generator accepts an iterable L and an integer N and yields a series of sub-generators, each of which will in turn yield N items from L.</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>
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>