Popular Python recipes tagged "meta:requires=traceback"http://code.activestate.com/recipes/langs/python/tags/meta:requires=traceback/2015-07-05T23:46:59-07:00ActiveState Code RecipesNondurableLogger class for use with concurrent.futures.ProcessPoolExecutor's submit and map methods (Python) 2014-02-10T17:50:59-08:00Peter Santorohttp://code.activestate.com/recipes/users/4189027/http://code.activestate.com/recipes/578827-nondurablelogger-class-for-use-with-concurrentfutu/ <p style="color: grey"> Python recipe 578827 by <a href="/recipes/users/4189027/">Peter Santoro</a> (<a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>). Revision 2. </p> <p>I needed a simple logging solution that I could use with with concurrent.futures.ProcessPoolExecutor and this is my initial recipe.</p> RecursionError exception: concise and informative output (Python) 2015-07-05T23:46:59-07:00elazarhttp://code.activestate.com/recipes/users/4187847/http://code.activestate.com/recipes/578660-recursionerror-exception-concise-and-informative-o/ <p style="color: grey"> Python recipe 578660 by <a href="/recipes/users/4187847/">elazar</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/recursion/">recursion</a>). Revision 3. </p> <p>Replaces the default exception hook with one that, upon "infinite recursion", removes the last cycle. This results in a significantly cleaner and shorter error message.</p> <p>Usage: simply import &lt;module&gt; as _</p> <p>For more details see the descussion here: <a href="https://mail.python.org/pipermail/python-ideas/2013-September/023190.html" rel="nofollow">https://mail.python.org/pipermail/python-ideas/2013-September/023190.html</a></p> 'Which' for Windows (Python) 2013-08-16T09:14:07-07:00Robert Pyronhttp://code.activestate.com/recipes/users/4174781/http://code.activestate.com/recipes/578642-which-for-windows/ <p style="color: grey"> Python recipe 578642 by <a href="/recipes/users/4174781/">Robert Pyron</a> (<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/which/">which</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 2. </p> <p>WHICH.PY scans through all directories specified in the system %PATH% environment variable, looking for the specified COMMAND(s). It tries to follow the sometimes bizarre rules for Windows command lookup.</p> The Easy Cow (Python) 2012-12-06T14:22:36-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578367-the-easy-cow/ <p style="color: grey"> Python recipe 578367 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/interpreter/">interpreter</a>). </p> <p>If you thought the programs for <a href="http://code.activestate.com/recipes/578366/">recipe 578366</a> were too difficult to understand, you have just found an upgrade to the COW language: mnemonics humans can understand. This recipe makes a slight alteration to the interpreter so as to take the decoded instructions instead of the COW (moo) instructions.</p> The Normal Cow (Python) 2012-12-06T14:14:28-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578366-the-normal-cow/ <p style="color: grey"> Python recipe 578366 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/interpreter/">interpreter</a>). </p> <p>Ever heard of an esoteric language named COW or wanted to run a program written with just the letters M and O? This recipe provides a COW interpreter that is able to run such programs and decodes them so that they are much easier to read for those who do not know the common bovine tongue.</p> Python Exception Chains (or Trees) (Python) 2013-02-04T15:15:22-08:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/578252-python-exception-chains-or-trees/ <p style="color: grey"> Python recipe 578252 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/cause/">cause</a>, <a href="/recipes/tags/chain/">chain</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reason/">reason</a>, <a href="/recipes/tags/reraise/">reraise</a>, <a href="/recipes/tags/tree/">tree</a>). Revision 2. </p> <p>I have here an approach for chaining exceptions in case a lower layer (<em>library</em>) raises an exception which is caught in an upper layer (<em>application</em>) and later given as <em>cause</em> when a different exception is raised. Passing this <em>cause</em> exception is meant to offer access to the stack trace of the inner exception for debugging.</p> <p>This approach is implemented in Python 3 and in Java, so it definitely makes sense; you also quickly find questions on <a href="http://stackoverflow.com">Stackoverflow</a> concerning it.</p> <p>I even extended this feature by not only using chains of exceptions but also trees. Trees, why trees? Because I had situations in which my application layer tried various approaches using the library layer. If all failed (raised an exception), my application layer also raised an exception; this is the case in which I wanted to pass more than one cause exception into my own exception (hence the tree of causes).</p> <p>My approach uses a special Exception class from which all my exceptions will inherit; standard exception must be wrapped (directly after catching, to preserve the exception stack trace). Please see the examples contained in the code below. The exception itself is rather small.</p> <p>I'd be happy to hear any comments regarding memory leaks (I didn't find any but one never knows), usability, enhancements or similar.</p> Directory Pruner 4 (Python) 2012-06-06T22:00:00-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578154-directory-pruner-4/ <p style="color: grey"> Python recipe 578154 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/delete/">delete</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/utility/">utility</a>). Revision 2. </p> <p>Module providing GUI capability to prune any directory.</p> <p>The code presented in this module is for the purposes of: (1) ascertaining the space taken up by a directory, its files, its sub-directories, and its sub-files; (2) allowing for the removal of the sub-files, sub-directories, files, and directory found in the first purpose; (3) giving the user a GUI to accomplish said purposes in a convenient way that is easily accessible.</p> Markov Encryption Demonstration 2 (Python) 2012-03-14T17:39:17-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578076-markov-encryption-demonstration-2/ <p style="color: grey"> Python recipe 578076 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/demonstration/">demonstration</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/encoding/">encoding</a>, <a href="/recipes/tags/encrypt/">encrypt</a>, <a href="/recipes/tags/encryption/">encryption</a>). </p> <p>This program is meant to act as an example of how to use ME with data that needs to be obfuscated. The functionality provided via the GUI demonstrates both the ability to encrypt and decrypt all text that the UTF-8 encoding can handle. Explanations come later.</p> Markov Encryption Demonstration 1 (Python) 2012-03-16T18:59:06-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578062-markov-encryption-demonstration-1/ <p style="color: grey"> Python recipe 578062 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/demonstration/">demonstration</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/encoding/">encoding</a>, <a href="/recipes/tags/encrypt/">encrypt</a>, <a href="/recipes/tags/encryption/">encryption</a>). </p> <p>This is a simple GUI program that shows Markov Encryption at work. It is built to be interactive and has all needed code embedded within itself. This version of ME library is not very efficient and represents an early attempt at developing and easily testing the code. Certain limits are built into the program, and the code is not meant to be robust at this stage. This program is a proof-of-concept design meant to ensure that the work being done was viable for future use and that the encryption process could be carried out both ways, both in encoding plaintext and decoding ciphertext.</p> Directory Pruner 2 (Python) 2011-04-05T01:37:14-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577633-directory-pruner-2/ <p style="color: grey"> Python recipe 577633 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/delete/">delete</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This program builds on work done in <a href="http://code.activestate.com/recipes/577632/">recipe 577632</a>, adds new options to the context menu, and experiments with threading the GUI. Directory Pruner 2 appear to work better on Windows 7 while Directory Pruner 1 (the non-threaded version) performs better on Windows XP. Those are the primary platforms on which the two programs have been tested. Please use these applications at your own risk.</p> <p>If you do not like something about this recipe and want to vote it down, please let everyone what you find fault with, how you would improve it, and an example of the code you would improve.</p> File Share Messenger 2.5 (Python) 2011-04-06T02:45:15-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577638-file-share-messenger-25/ <p style="color: grey"> Python recipe 577638 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/file_io/">file_io</a>, <a href="/recipes/tags/file_share/">file_share</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/messenger/">messenger</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/workaround/">workaround</a>). </p> <p>After writing the limited program shown in <a href="http://code.activestate.com/recipes/577637/">recipe 577637</a>, the following program was written with a better yet incompatible I/O system designed not to fill up a file share with many, separate files. This program had five revisions as outlined within the source code. To access settings within this program, use the "F2" key. Documentation may be accessed via the "F1" key (with future plans cut).</p> <p>If anyone wishes to comment or vote this recipe down, please provide your insight into the fault(s) of the program and provide a suggestion as to what solution you would implement to fix the problems.</p> File Share Messenger 1.0 (Python) 2011-04-06T00:03:20-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577637-file-share-messenger-10/ <p style="color: grey"> Python recipe 577637 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/file_io/">file_io</a>, <a href="/recipes/tags/file_share/">file_share</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/messenger/">messenger</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/workaround/">workaround</a>). </p> <p>File I/O has many potential uses, and in the case of network file shares, messaging can be conducted by creating files in a directory and scanning for new files to find new messages. In the case where there are restrictions within a network that prevent opening server sockets or creating and using client sockets, the technique in this recipe may be used with file shares instead. The program is simple and was written in about fifty minutes, so there are a great deal of enhancements that could be made to the recipe and its capabilities. Hopefully, this will provide inspiration for other program writers.</p> <p>If you have any comments or wish to down-vote this recipe, please provide your insight as to what could be improved upon and how you would go about fixing any problems that you might find.</p> Directory Pruner 3 (Python) 2011-04-05T23:15:58-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577635-directory-pruner-3/ <p style="color: grey"> Python recipe 577635 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/delete/">delete</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>After considering the monolithic design of <a href="http://code.activestate.com/recipes/577633/">recipe 577633</a> and deciding that it should be divided into several smaller modules, Directory Pruner 3 was written to take advantage of better abstraction methodologies. There are a total of twelve files in the project, and all of them except the first are contained within a directory called "Directory Pruner 3" such that they make up a package in Python. Like the other Directory Pruners, please use and configure this program at your own computer's risk.</p> <p>If you have any comments or wish to vote this recipe down, please provide an explanation as to what you find fault with in this program and also a solution that you would implement to fix the problem.</p> Universal eval to string function (Python) 2011-02-24T10:17:32-08:00Jakub Jankiewiczhttp://code.activestate.com/recipes/users/4028109/http://code.activestate.com/recipes/577585-universal-eval-to-string-function/ <p style="color: grey"> Python recipe 577585 by <a href="/recipes/users/4028109/">Jakub Jankiewicz</a> (<a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/exec/">exec</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python_scripts/">python_scripts</a>). </p> <p>This is a function that evaluate all expressions and statements and return the result as a string. It also return Exceptions as strings. It is used in <a href="http://trypython.jcubic.pl">trypython.jcubic.pl</a></p> Simple Regular Expression Tester (Python) 2010-12-25T00:12:44-08:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577517-simple-regular-expression-tester/ <p style="color: grey"> Python recipe 577517 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/quick/">quick</a>, <a href="/recipes/tags/re/">re</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/testing/">testing</a>). Revision 2. </p> <p><strong><em>Is it possible to create a simple command line program that I can use to quickly test my regular expressions?</em></strong></p> <p>Yes it is. This simple regular expression tester uses Python's re module as well as others to quickly allow you to test your regular expression in one go.</p> <p>TODO:</p> <ul> <li>Add Support For Multiple Regular Expression Input</li> </ul> <p>Recent Changes:</p> <ul> <li>Made the output prettier with a little more whitespace. More bytes, but at least it's easier to read!</li> </ul> Poor Man unit tests (Python) 2011-01-08T18:57:18-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577538-poor-man-unit-tests/ <p style="color: grey"> Python recipe 577538 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/assertions/">assertions</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/unittests/">unittests</a>). </p> <p>When building unit tests for modules many times using PyUnit feels like overkill. This is a simple implementation for testing single file modules.</p> Better quote module for bash shells (Python) 2010-12-03T09:16:45-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577483-better-quote-module-for-bash-shells/ <p style="color: grey"> Python recipe 577483 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/escape/">escape</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/quote/">quote</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/shell/">shell</a>). </p> <p>This Python module quotes a Python string so that it will be treated as a single argument to commands ran via os.system() (assuming bash is the underlying shell). In other words, this module makes arbitrary strings "command line safe" (for bash command lines anyway, YMMV if you're using Windows or one of the (less fine) posix shells).</p> Evaluator 2.0 (Python) 2010-11-25T18:24:09-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577469-evaluator-20/ <p style="color: grey"> Python recipe 577469 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/evaluator/">evaluator</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/parser/">parser</a>). </p> <p>This is a complete rewrite of <a href="http://code.activestate.com/recipes/576790/">recipe 576790</a>. While aiming to maintain similar functionality and continuing its implementation for self-academic purposes, a much cleaner parser / tokenizer and operator execution engine were developed. A slightly different math syntax is supported in this version, but it is arguably better and more capable than it previously was. Base prefixes are a feature now supported, and the single downgrade is calculating with integers instead of floats.</p> Fast flatten() with depth control and oversight over which subtrees to expand (Python) 2010-11-26T11:10:01-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577470-fast-flatten-with-depth-control-and-oversight-over/ <p style="color: grey"> Python recipe 577470 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/tuple/">tuple</a>). </p> <p>Extremely fast, non-recursive, depth limited flatten with powerful control over which subtrees are to be expanded. If this is what you need then look no further.</p> Named Sequences for environments containing large numbers of POD instances (Python) 2010-11-27T13:55:18-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577473-named-sequences-for-environments-containing-large-/ <p style="color: grey"> Python recipe 577473 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/slot/">slot</a>, <a href="/recipes/tags/unittests/">unittests</a>). </p> <p>Generate classes with named data attributes that can be sequenced. Useful for POD classes for which many records will exist concurrently.</p> <p>Compare the feature set to NamedTuples by Raymond Hettinger: <a href="http://code.activestate.com/recipes/500261-named-tuples/" rel="nofollow">http://code.activestate.com/recipes/500261-named-tuples/</a></p>