Popular recipes tagged "error"http://code.activestate.com/recipes/tags/error/2016-11-13T20:17:03-08:00ActiveState Code RecipesTrap KeyboardInterrupt and EOFError for graceful program termination (Python) 2016-11-13T20:17:03-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580718-trap-keyboardinterrupt-and-eoferror-for-graceful-p/ <p style="color: grey"> Python recipe 580718 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/terminate/">terminate</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This recipe shows how to trap the KeyboardInterrupt and EOFError Python exceptions so that they do not crash your program. As a vehicle to show this, it uses a small Python utility that shows the ASCII code for any ASCII character you type.</p> 10 lines for a beginner to try out the Spyder IDE for python programming (Python) 2015-09-18T15:04:09-07:00mai3ahttp://code.activestate.com/recipes/users/4192857/http://code.activestate.com/recipes/579100-10-lines-for-a-beginner-to-try-out-the-spyder-ide-/ <p style="color: grey"> Python recipe 579100 by <a href="/recipes/users/4192857/">mai3a</a> (<a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/snippet/">snippet</a>, <a href="/recipes/tags/spyder/">spyder</a>, <a href="/recipes/tags/stuffthatworks/">stuffthatworks</a>). </p> <p>This python code generates no lint errors and just print out a few numbers.</p> <p>Useful if you use the Spyder IDE for the first time, which provides NOTHING to get you started.</p> No more UnicodeDecodeErrors when printing (Python) 2013-12-17T23:02:37-08:00Ádám Szieberthhttp://code.activestate.com/recipes/users/4188745/http://code.activestate.com/recipes/578788-no-more-unicodedecodeerrors-when-printing/ <p style="color: grey"> Python recipe 578788 by <a href="/recipes/users/4188745/">Ádám Szieberth</a> (<a href="/recipes/tags/decode/">decode</a>, <a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/print/">print</a>, <a href="/recipes/tags/terminal/">terminal</a>, <a href="/recipes/tags/unicode/">unicode</a>). Revision 2. </p> <p>I hate getting UnicodeDecodeErrors when I use print() for bugtracing or for some other reason. I decided to make a module which spares me the headache.</p> Python script main line for graceful exception handling and logging (Python) 2010-09-11T05:46:59-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577258-python-script-main-line-for-graceful-exception-han/ <p style="color: grey"> Python recipe 577258 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/main/">main</a>, <a href="/recipes/tags/mainline/">mainline</a>, <a href="/recipes/tags/script/">script</a>). Revision 5. </p> <p>This is a recipe is often use for the mainline of my Python scripts. With this recipe your Python script will:</p> <ul> <li>gracefully handle <code>Ctrl+C</code> (i.e. <code>KeyboardInterrupt</code>)</li> <li>log an error (using the <code>logging</code> module) for uncaught exceptions, importantly <strong>with the file and line number</strong> in your Python code where the exception was raised</li> <li>gracefully ignore a closed output pipe (common when the user pipes your script through <code>less</code> and terminates that)</li> <li>if your script logger is enabled for <code>DEBUG</code> level logging, a full traceback will be shown for an uncaught exception</li> </ul> <p>Presumptions:</p> <ul> <li><p>you have a global <code>log</code> variable a la:</p> <pre class="prettyprint"><code>import logging log = logging.setLevel("scriptname") </code></pre></li> <li><p>your script's entry point is a function <code>def main(argv): ...</code></p></li> </ul>