Latest recipes tagged "shortcuts"http://code.activestate.com/recipes/tags/shortcuts/new/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>
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>
DateTimeSyncer with Daylight Savings Time adjustment (Python)
2008-10-31T15:52:18-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/576546-datetimesyncer-with-daylight-savings-time-adjustme/
<p style="color: grey">
Python
recipe 576546
by <a href="/recipes/users/4076953/">Jack Trainor</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 5.
</p>
<p>DateTimeSyncer synchronizes computer's current date and time with Daylight Savings adjustment
directly without depending on operating system or Python's time.localtime().</p>
<p>This allows programmers to update Daylight Savings Time rules without
depending on external updates to OS or Python.</p>
<p>Double-clicking this module will synchronize your computer's date and time on a Windows machine. It can be easily extended other time zones and other operating systems.</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>
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>
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>
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>
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>
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>
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>
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>
RedirectedIO context manager and redirect_io decorator (Python)
2007-11-20T13:50:34-08:00Eduardo Padoanhttp://code.activestate.com/recipes/users/2153574/http://code.activestate.com/recipes/534166-redirectedio-context-manager-and-redirect_io-decor/
<p style="color: grey">
Python
recipe 534166
by <a href="/recipes/users/2153574/">Eduardo Padoan</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 5.
</p>
<p>Use the "with" keyword or a decorator to simplify a bit redirecting IO to a file.</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>
multi_cmp (Python)
2007-09-10T15:00:38-07:00Richard Harrishttp://code.activestate.com/recipes/users/4050195/http://code.activestate.com/recipes/528941-multi_cmp/
<p style="color: grey">
Python
recipe 528941
by <a href="/recipes/users/4050195/">Richard Harris</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>Comparing over multiples keys is a common task in Python. To make it easier, multi_cmp creates a compound comparison function using the supplied keys and getter.</p>