Popular recipes tagged "meta:min_python_2=3"http://code.activestate.com/recipes/tags/meta:min_python_2=3/2016-05-30T11:48:10-07:00ActiveState Code Recipeschroma (Python) 2016-05-30T11:48:10-07:00aaahttp://code.activestate.com/recipes/users/4194184/http://code.activestate.com/recipes/580671-chroma/ <p style="color: grey"> Python recipe 580671 by <a href="/recipes/users/4194184/">aaa</a> (<a href="/recipes/tags/2d/">2d</a>, <a href="/recipes/tags/game/">game</a>). </p> <p>Python personnal game code</p> easyjson.py - parsing JSON from buffer and from file (Python) 2013-05-27T05:32:07-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578529-easyjsonpy-parsing-json-from-buffer-and-from-file/ <p style="color: grey"> Python recipe 578529 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/parser/">parser</a>). Revision 4. </p> <p><strong>JSON Parser</strong>:</p> <ul> <li>Refering to <a href="http://www.json.org/" rel="nofollow">http://www.json.org/</a></li> <li>Just one simple Python file you can integrate where you want to</li> <li>No imports (very important) -> no dependencies!!!</li> <li>Should work with really older versions of Python!!!</li> </ul> <p><strong>Todo's</strong>:</p> <ul> <li>Doesn't cover full number format</li> <li>...</li> </ul> <p><strong>Done</strong></p> <ul> <li>Allows string in string (revision 2)</li> <li>Covers objects in an array (revision 2)</li> <li>Provides a mechanism to allow other dictionaries (like collections.OrderedDict) (revision 3)</li> <li>Conversion of numbers to integer or float types (revision 4)</li> </ul> Truth Value Aware Iterable (Python) 2013-06-11T08:00:25-07:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/578549-truth-value-aware-iterable/ <p style="color: grey"> Python recipe 578549 by <a href="/recipes/users/4169882/">Alan Franzoni</a> (<a href="/recipes/tags/boolean/">boolean</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/truth/">truth</a>). </p> <p>This small recipe enables truth value testing on iterables.</p> <p>It is quite common to do things like:</p> <pre class="prettyprint"><code>if somesequence: ... else: ... </code></pre> <p>Such constructs, that enter the if block if the sequence's got one or more elements and the else block if it's empty, work fine on non-lazy builtin sequences (lists, strings, tuples) and dictionaries as well, but doesn't necessarily work on generic iterables - most of them are always true regardless of their contents, since they're some kind of object. A classical example is generators, but such behaviour can be extended to any object implementing the Iterable interface.</p> <p>Just wrap your iterable with this decorator and you'll get a truth-aware iterable which supports proper truth testing by doing a small first element prefetching and can then be used just like the original iterable.</p> Create a nested dictionary from os.walk (Python) 2011-09-26T23:38:24-07:00Andrew Clarkhttp://code.activestate.com/recipes/users/4179419/http://code.activestate.com/recipes/577879-create-a-nested-dictionary-from-oswalk/ <p style="color: grey"> Python recipe 577879 by <a href="/recipes/users/4179419/">Andrew Clark</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/os_walk/">os_walk</a>). Revision 2. </p> <p>Creates a nested dictionary that represents a folder structure. Here is an example of what the resulting dictionary might look like:</p> <pre class="prettyprint"><code>{ "root": { "folder2": { "item2": None, "item1": None }, "folder1": { "subfolder1": { "item2": None, "item1": None }, "subfolder2": { "item3": None } } } } </code></pre> Bresenham's line algorithm in n-dimensions (Python) 2012-05-16T16:15:04-07:00Vikas Dhimanhttp://code.activestate.com/recipes/users/4174758/http://code.activestate.com/recipes/578112-bresenhams-line-algorithm-in-n-dimensions/ <p style="color: grey"> Python recipe 578112 by <a href="/recipes/users/4174758/">Vikas Dhiman</a> (<a href="/recipes/tags/bresenham/">bresenham</a>, <a href="/recipes/tags/ray/">ray</a>, <a href="/recipes/tags/tracing/">tracing</a>). Revision 2. </p> <p>Given start and end point, produce a list of points through which line (or ray) will traverse.</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>&gt;&gt;&gt; solve('3*x + 5*y = 29; 12*x - 3*y = 24') (3.0, 4.0) </code></pre> Cheap-date trick; a different way to parse (Python) 2012-03-06T14:08:10-08:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578064-cheap-date-trick-a-different-way-to-parse/ <p style="color: grey"> Python recipe 578064 by <a href="/recipes/users/4181178/">Scott S-Allen</a> (<a href="/recipes/tags/cheap/">cheap</a>, <a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/sharp/">sharp</a>). </p> <p>... a light meal with a heavy dose of "tutorial mash" on the side.</p> <p>In the constructive spirit of "more ways to solve a problem"; this is a portion of my lateral, occasionally oblique, solutions. Nothing new in le régime de grande, but hopefully the conceptual essence will amuse.</p> <p>Initially started as a response to <a href="http://code.activestate.com/recipes/577135/">recipe 577135</a> which parses incremental date fragments and preserves micro-seconds where available. That script does more work than this, for sure, but requires special flow-control and iterates a potentially incumbering shopping list (multi-dimensional with some detail).</p> <p>So here's a different box for others to play with. Upside-down in a sense, it doesn't hunt for anything but a numerical "pulse"; sequences of digits punctuated by other 'stuff' we don't much care about.</p> <p>Missing a lot of things, intentionally, this snippet provides several examples demoin' flexibility. Easy to button-up, redecorate and extend later for show, till then the delightful commentary makes it hard enough to see bones already -- all six lines or so!</p> <p><strong>Note:</strong> <em>The core script is repeated for illustrative purposes. The first is step-by-step, the second is lean and condensed for utilitarian purposes. It is the second, shorter, version that I yanked from a file and gussied up.</em></p> Dijkstra shortest path implementation (Python) 2011-10-05T09:06:50-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577892-dijkstra-shortest-path-implementation/ <p style="color: grey"> Python recipe 577892 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/dijkstra/">dijkstra</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/shortest/">shortest</a>). </p> <p>This code snippet is the implementation of Dijkstra's algorithm.</p> Banner (Python) 2011-01-11T05:16:12-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577537-banner/ <p style="color: grey"> Python recipe 577537 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/banner/">banner</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/printing/">printing</a>). Revision 2. </p> <p>Easily customizable banner program.</p> 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> Axis POJO stripper. (Python) 2011-01-04T12:05:12-08:00Chris Wolfhttp://code.activestate.com/recipes/users/4173108/http://code.activestate.com/recipes/577533-axis-pojo-stripper/ <p style="color: grey"> Python recipe 577533 by <a href="/recipes/users/4173108/">Chris Wolf</a> (<a href="/recipes/tags/axis/">axis</a>, <a href="/recipes/tags/pojo/">pojo</a>, <a href="/recipes/tags/wsdl2java/">wsdl2java</a>). </p> <p>This utility lets us use the 'wsdl2java' utility from Axis-1 to generate POJOs from the schema embedded in a WSDL. If we just want POJOs without the Axis marshalling/unmarshalling code, then this script will strip out the Axis code, leaving just the POJOs.</p> Logging asserts (Python) 2010-03-01T03:22:43-08:00d.schlabinghttp://code.activestate.com/recipes/users/4168903/http://code.activestate.com/recipes/577074-logging-asserts/ <p style="color: grey"> Python recipe 577074 by <a href="/recipes/users/4168903/">d.schlabing</a> (<a href="/recipes/tags/logging/">logging</a>). Revision 2. </p> <p>This tries to marry assert statements with logging. If you use asserts together with logging and the script stops because an assertion is not fulfilled, you will not find any information about this in the logs. Unless you do something like this:</p> <pre class="prettyprint"><code>meal = ["eggs", "bacon", "spam"] try: assert "spam" not in meal, "But I don't like spam!" except AssertionError: log.exception("But I don't like spam!") raise </code></pre> <p>With the proposed recipe a somewhat similar behaviour can be achieved by:</p> <pre class="prettyprint"><code>log_assert("spam" not in meal, "But I don't like spam!") </code></pre> whois client (Python) 2010-09-15T12:08:34-07:00Chris Wolfhttp://code.activestate.com/recipes/users/4173108/http://code.activestate.com/recipes/577364-whois-client/ <p style="color: grey"> Python recipe 577364 by <a href="/recipes/users/4173108/">Chris Wolf</a> (<a href="/recipes/tags/nic/">nic</a>, <a href="/recipes/tags/whois/">whois</a>). Revision 6. </p> <p>Python whois utility which looks up records in the databases maintained by severa Network Information Centers (NICs). Functions exactly like the UNIX whois(1) utility.</p> <p>Callable from the command-line, but also has an function entry point for calling from code.</p> Fast, re-entrant, optimistic lock implemented in Cython (Python) 2010-07-27T20:25:20-07:00Stefan Behnelhttp://code.activestate.com/recipes/users/4174506/http://code.activestate.com/recipes/577336-fast-re-entrant-optimistic-lock-implemented-in-cyt/ <p style="color: grey"> Python recipe 577336 by <a href="/recipes/users/4174506/">Stefan Behnel</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>). Revision 3. </p> <p>This is a C-level implementation of a fast, re-entrant, optimistic lock for CPython. It is written in Cython. Under normal conditions, it is about 10x faster than threading.RLock because it avoids all locking unless two or more threads try to acquire it at the same time. Under congestion, it is still about 10% faster than RLock due to being implemented in Cython.</p> slugify: make a string usable in a URL or filename (Python) 2010-06-07T04:11:55-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577257-slugify-make-a-string-usable-in-a-url-or-filename/ <p style="color: grey"> Python recipe 577257 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/django/">django</a>, <a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/slug/">slug</a>, <a href="/recipes/tags/slugify/">slugify</a>, <a href="/recipes/tags/url/">url</a>). Revision 2. </p> <p>"Slugify" a string so it is ascii, has only alphanumeric and hyphen characters. Useful for URLs and filenames. This is heavily based on the slugify in Django.</p> <p>Note: presumes that you've <code>import re</code>d higher up in your module.</p> BigDirs - Where's my disk space gone? (Python) 2010-07-18T02:52:18-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577301-bigdirs-wheres-my-disk-space-gone/ <p style="color: grey"> Python recipe 577301 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/administration/">administration</a>, <a href="/recipes/tags/shortcut/">shortcut</a>). Revision 2. </p> <p>BigDirs locates directories taking up the most disk space in files at that directory level, not in the nested directories below it. In my experience this is the easiest way to find where my disk space got chewed up.</p> <p>Set DEFAULT_DIR for the root directory you wish to check. Set DEFAULT_THRESHOLD for the minimum size of the directories you wish to see in the output.</p> Yet another roundrobin (Python) 2010-07-19T13:53:41-07:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577309-yet-another-roundrobin/ <p style="color: grey"> Python recipe 577309 by <a href="/recipes/users/4172918/">Daniel Cohn</a> (<a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/roundrobin/">roundrobin</a>). Revision 2. </p> <p>This recipe provides a decently simple implementation of a roundrobin using itertools and deque.</p> Wait to tomorrow (Python) 2010-05-01T15:19:28-07:00vojta rylkohttp://code.activestate.com/recipes/users/4173557/http://code.activestate.com/recipes/577183-wait-to-tomorrow/ <p style="color: grey"> Python recipe 577183 by <a href="/recipes/users/4173557/">vojta rylko</a> . Revision 3. </p> <p>Get tomorrow morning time, make delta from now and wait x seconds.</p> Adding the directory of the python executable to the system PATH under windows (Python) 2010-05-19T19:01:31-07:00Anthon van der Neuthttp://code.activestate.com/recipes/users/2403822/http://code.activestate.com/recipes/577233-adding-the-directory-of-the-python-executable-to-t/ <p style="color: grey"> Python recipe 577233 by <a href="/recipes/users/2403822/">Anthon van der Neut</a> (<a href="/recipes/tags/environment/">environment</a>, <a href="/recipes/tags/executable/">executable</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/variable/">variable</a>). </p> <p>If you install python under windows and then open a command shell (DOS-prompt, you normally get an error message if you type "python" at the prompt. This is because the directory of the python executable is not in the PATH environment variable. If you know where you installed python, you can add this via Control Panel -> System -> Advanced -> Environment Variables but this is not very user friendly way of doing things and error prone.</p> <p>This program, if run by double clicking the file or by dragging the file to a command shell, will add the directory of the executable associated with the .py extension to the PATH env. var (if it is not already in there). It will notify other programs of this change, but unfortunately <a href="http://command.com" rel="nofollow">command.com</a> is not smart enough to understand that. You have to open a new command shell after running the program in order to be able to run "python" at the dos prompt.</p> <p>If run with the optional command line parameter 'remove' the directory will be removed from the PATH.</p> query yes/no/quit (Python) 2010-03-09T17:57:18-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577097-query-yesnoquit/ <p style="color: grey"> Python recipe 577097 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/no/">no</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/quit/">quit</a>, <a href="/recipes/tags/raw_input/">raw_input</a>, <a href="/recipes/tags/yes/">yes</a>). </p> <p>Ask the user a question using raw_input() and looking something like this:</p> <pre class="prettyprint"><code>QUESTION [Y/n/q] ...validate... </code></pre> <p>See also: <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> (query yes/no), <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p>