Popular recipes tagged "meta:loc=109"http://code.activestate.com/recipes/tags/meta:loc=109/2017-06-25T17:17:43-07:00ActiveState Code RecipesGuard against an exception in the wrong place (Python) 2017-06-25T17:17:43-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/580808-guard-against-an-exception-in-the-wrong-place/ <p style="color: grey"> Python recipe 580808 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/context/">context</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/guard/">guard</a>, <a href="/recipes/tags/manager/">manager</a>). Revision 2. </p> <p>Sometimes exception handling can obscure bugs unless you guard against a particular exception occurring in a certain place. One example is that <a href="https://www.python.org/dev/peps/pep-0479/">accidentally raising <code>StopIteration</code> inside a generator</a> will halt the generator instead of displaying a traceback. That was solved by PEP 479, which automatically has such <code>StopIteration</code> exceptions change to <code>RuntimeError</code>. See the discussion below for further examples.</p> <p>Here is a class which can be used as either a decorator or context manager for guarding against the given exceptions. It takes an exception (or a tuple of exceptions) as argument, and if the wrapped code raises that exception, it is re-raised as another exception type (by default <code>RuntimeError</code>).</p> <p>For example:</p> <pre class="prettyprint"><code>try: with exception_guard(ZeroDivisionError): 1/0 # raises ZeroDivisionError except RuntimeError: print ('ZeroDivisionError replaced by RuntimeError') @exception_guard(KeyError) def demo(): return {}['key'] # raises KeyError try: demo() except RuntimeError: print ('KeyError replaced by RuntimeError') </code></pre> random send mail, sms or popup window (Python) 2013-09-04T19:43:19-07:00peekaahttp://code.activestate.com/recipes/users/2919471/http://code.activestate.com/recipes/578598-random-send-mail-sms-or-popup-window/ <p style="color: grey"> Python recipe 578598 by <a href="/recipes/users/2919471/">peekaa</a> (<a href="/recipes/tags/popup/">popup</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sendmail/">sendmail</a>, <a href="/recipes/tags/sms/">sms</a>). Revision 4. </p> <p>If you need to do something irregularly, randomly during the day, you often forget. This script gives you mail, sms or popup window indefinitely in random interval to remind you of doing it. It runs forever. If you want to send emails, uncomment the row sendMail() and fill variable me, to, smtp, name, login in function sendMail().</p> Spoken Word to Number (Python) 2012-09-11T18:42:09-07:00Pushpendrehttp://code.activestate.com/recipes/users/4183526/http://code.activestate.com/recipes/578258-spoken-word-to-number/ <p style="color: grey"> Python recipe 578258 by <a href="/recipes/users/4183526/">Pushpendre</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/nlp/">nlp</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/spoken/">spoken</a>). </p> <p>convert number represented the way they are spoken to actual numbers</p> Komodo JS Macro - Copy all "Find Results 1" data into a new editor window (JavaScript) 2014-07-03T15:18:03-07:00Dave Waldhttp://code.activestate.com/recipes/users/4181625/http://code.activestate.com/recipes/578100-komodo-js-macro-copy-all-find-results-1-data-into-/ <p style="color: grey"> JavaScript recipe 578100 by <a href="/recipes/users/4181625/">Dave Wald</a> (<a href="/recipes/tags/findresults/">findresults</a>, <a href="/recipes/tags/javascript/">javascript</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>). Revision 5. </p> <p>Gets the Find Results Manager for the "Find Results 1" tab, if it exists. If the tab has rows in it, then copies out all the data from each row and adds it to an output buffer. Creates a new editor view (or optionally reuses the existing one) and appends the output buffer to it. Note: Be sure to see Bruno's update below, until I get it merged in. ;)</p> Emulate Keyword-Only Arguments in Python 2 (Python) 2011-11-03T18:31:52-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577940-emulate-keyword-only-arguments-in-python-2/ <p style="color: grey"> Python recipe 577940 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/argument/">argument</a>, <a href="/recipes/tags/keyword_only/">keyword_only</a>, <a href="/recipes/tags/kwonly/">kwonly</a>, <a href="/recipes/tags/parameter/">parameter</a>). </p> <p>Python 3 introduced a useful feature: keyword-only arguments. In order to get the same effect in Python 2, you must use **kwargs in your parameter list. Then, at the beginning of your function body you must manually extract what would be the keyword-only arguments once you upgrade to 3.</p> <p>This recipe helps reduce the boilerplate to a single function call. You still don't get those parameters in your "def" clause (where they are more obvious), but at least it reduces the clutter.</p> Special Range Function for Different Kinds of Ranges (int, float, character) (Python) 2011-03-30T16:42:47-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577583-special-range-function-for-different-kinds-of-rang/ <p style="color: grey"> Python recipe 577583 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/character/">character</a>, <a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/special/">special</a>). Revision 3. </p> <p>This module allows the user to create a more verbose set of ranges. Simple character ranges, and float ranges are supported.</p> <p>Supported Ranges:</p> <ul> <li>Basic Integer Ranges</li> <li>Float Ranges (as accurate as a float range can get)</li> <li>Simple character ranges (lowercase to lowercase, uppercase to uppercase, etc.)</li> </ul> <p>It should work in Python 2 and Python 3.</p> <p><strong>If you tested this for speed, or want to test this for speed, please post the results! (And your system specs)</strong></p> <p><strong>Edit:</strong> Found a really silly error of mine when using range instead of xrange in these functions!</p> Sierpinski Square combination fractal (Python) 2010-08-11T20:06:42-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577354-sierpinski-square-combination-fractal/ <p style="color: grey"> Python recipe 577354 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>). </p> <p>Combination of Sierpinski Square and 2 other square fractals!</p> XML to Python data structure (de-serialization) (Python) 2010-06-16T20:25:36-07:00David McCuskeyhttp://code.activestate.com/recipes/users/4174198/http://code.activestate.com/recipes/577267-xml-to-python-data-structure-de-serialization/ <p style="color: grey"> Python recipe 577267 by <a href="/recipes/users/4174198/">David McCuskey</a> (<a href="/recipes/tags/deserialize/">deserialize</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 3. </p> <p>This code de-serializes XML into a Python data structure.</p> <p>This is one part of a trio of recipes:</p> <ul> <li><a href="http://code.activestate.com/recipes/577266">Simple API</a></li> <li><a href="http://code.activestate.com/recipes/577267">XML2Py De-serialization</a></li> <li><a href="http://code.activestate.com/recipes/577268">Py2XML Serialization</a></li> </ul> <h5>For more information</h5> <p>See <a href="http://code.activestate.com/recipes/577266">XML to Python data structure <a href="http://code.activestate.com/recipes/577266/">Recipe #577266</a></a></p> Compare CSV Inventory files (Python) 2009-08-18T14:14:04-07:00Mike Burkehttp://code.activestate.com/recipes/users/4171487/http://code.activestate.com/recipes/576885-compare-csv-inventory-files/ <p style="color: grey"> Python recipe 576885 by <a href="/recipes/users/4171487/">Mike Burke</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/csv/">csv</a>). </p> <p>Program will compare two CSV files using a unique ID field and save any changes to ID as well as two secondary fields (qty &amp; price). The code was written to pick out updates from supplier inventory files.</p> Binary floating point summation accurate to full precision (Python) 2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/ <p style="color: grey"> Python recipe 393090 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p> Reevaluate functions when called, v3 (Python) 2009-05-14T15:18:41-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576754-reevaluate-functions-when-called-v3/ <p style="color: grey"> Python recipe 576754 by <a href="/recipes/users/4170000/">geremy condra</a> (<a href="/recipes/tags/annotations/">annotations</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>). Revision 3. </p> <p>This small snippet came about as a result of this discussion on python-ideas, requesting a new syntax for dynamically reevaluating a function each time it is called.</p> <p>It is a minor alteration of version 2 of this recipe that, instead of calling eval() on string annotations, simply requires that the annotations be callable and calls them at runtime.</p> Stacked graphs using matplotlib (Python) 2009-01-26T08:11:59-08:00Anand Patilhttp://code.activestate.com/recipes/users/4168675/http://code.activestate.com/recipes/576633-stacked-graphs-using-matplotlib/ <p style="color: grey"> Python recipe 576633 by <a href="/recipes/users/4168675/">Anand Patil</a> (<a href="/recipes/tags/broadband/">broadband</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/mobile/">mobile</a>, <a href="/recipes/tags/stacked_graph/">stacked_graph</a>, <a href="/recipes/tags/stream_graph/">stream_graph</a>). </p> <p>Creates stacked graphs (sometimes known as stream graphs, apparently) as recommended by Byron and Wattenberg, <a href="http://www.leebyron.com/else/streamgraph/download.php?file=stackedgraphs_byron_wattenberg.pdf" rel="nofollow">http://www.leebyron.com/else/streamgraph/download.php?file=stackedgraphs_byron_wattenberg.pdf</a></p> openplus(): open pipes, urls ... uniformly with Popen, urlopen ... open (Python) 2008-12-11T05:40:15-08:00denishttp://code.activestate.com/recipes/users/4168005/http://code.activestate.com/recipes/576582-openplus-open-pipes-urls-uniformly-with-popen-urlo/ <p style="color: grey"> Python recipe 576582 by <a href="/recipes/users/4168005/">denis</a> (<a href="/recipes/tags/file/">file</a>). Revision 3. </p> <p>openplus() opens pipes and some other objects that quack like files, as well as files:</p> <pre class="prettyprint"><code>| pipe ... -- Popen() a shell <a href="http://" rel="nofollow">http://</a> ftp:// ... -- urlopen() `ls $x`, `geturl web data` -- shell -&gt; filename or url ~/a/b -- sh ls .gz -- gunzip - -- stdin / stdout else -- the builtin open() </code></pre> <p>Users can then read e.g. "| filter data | sort" "| convert ... xx.jpg" "`geturl web data`" like files, just by importing openplus and changing open() -> openplus().</p> Asyncore/asynchat in GTK (Python) 2008-03-17T13:51:05-07:00Niklas Janlerthttp://code.activestate.com/recipes/users/4132331/http://code.activestate.com/recipes/551774-asyncoreasynchat-in-gtk/ <p style="color: grey"> Python recipe 551774 by <a href="/recipes/users/4132331/">Niklas Janlert</a> (<a href="/recipes/tags/ui/">ui</a>). </p> <p>This recipe makes it possible to run asyncore/asynchat dispatchers in the GTK main thread.</p> Bounded Buffer Example (1) (Python) 2006-03-29T09:59:42-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/475216-bounded-buffer-example-1/ <p style="color: grey"> Python recipe 475216 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>The following recipe shows an example of the bounded buffer problem and its solution. Fortunately in Python, this is very easily solved with the Queue class from the Queue module. Even creating a buffer with a maximum size limit becomes rather easy with the automatic blocking feature (when trying to put when the Queue is full or when trying to get when the Queue is empty). Overall, this is just a simple example and approach to a classic problem.</p> Cloudscape - stop network server. (Tcl) 2005-12-07T21:50:34-08:00Patrick Finneganhttp://code.activestate.com/recipes/users/1220635/http://code.activestate.com/recipes/461729-cloudscape-stop-network-server/ <p style="color: grey"> Tcl recipe 461729 by <a href="/recipes/users/1220635/">Patrick Finnegan</a> (<a href="/recipes/tags/database/">database</a>). </p> <p>Stop the Cloudscape Network Server.</p> Getting stock quotes over the internet (Tcl) 2002-06-11T15:33:05-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/132604-getting-stock-quotes-over-the-internet/ <p style="color: grey"> Tcl recipe 132604 by <a href="/recipes/users/117230/">andreas kupries</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Origin: <a href="http://wiki.tcl.tk/636" rel="nofollow">http://wiki.tcl.tk/636</a> Author: The original author is unknown.</p> <p>Here's a simple Tcl script which will fire up a window to track a small portfolio of stocks. Nothing too fancy. I've included an example portfolio of three stocks, and a cash holding. To see your own portfolio, simply edit the 'shares' array, and the 'cash' variable</p> Simplified mega-widiget creation without class libraries (Tcl) 2003-08-08T20:16:55-07:00Michael Kirkhamhttp://code.activestate.com/recipes/users/331882/http://code.activestate.com/recipes/122547-simplified-mega-widiget-creation-without-class-lib/ <p style="color: grey"> Tcl recipe 122547 by <a href="/recipes/users/331882/">Michael Kirkham</a> . Revision 3. </p> <p>If you're creating a mega-widget (aka composite widget) that you want to be extensible, but you aren't quite ready to take the plunge into a full Tcl class extension (or haven't found one quite for you), the following is a simple "next best thing".</p>