Top-rated recipes tagged "meta:loc=8"http://code.activestate.com/recipes/tags/meta:loc=8/top/2017-05-17T21:10:26-07:00ActiveState Code RecipesEight queens. Six lines. (Python)
2009-02-10T14:59:07-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576647-eight-queens-six-lines/
<p style="color: grey">
Python
recipe 576647
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
.
Revision 3.
</p>
<p>What six lines of Python can do</p>
Locating files throughout a directory tree (Python)
2009-12-02T07:30:27-08:00Simon Brunninghttp://code.activestate.com/recipes/users/98010/http://code.activestate.com/recipes/499305-locating-files-throughout-a-directory-tree/
<p style="color: grey">
Python
recipe 499305
by <a href="/recipes/users/98010/">Simon Brunning</a>
(<a href="/recipes/tags/files/">files</a>).
Revision 3.
</p>
<p>os.walk is a very nice replacement for os.path.walk, which I never did feel comfortable with. There's one very common pattern of usage, though, which still benefits from a simple helper-function; locating all files matching a given file-name pattern within a directory tree.</p>
Reorder a sequence (uses generators, and recursion!) (Python)
2003-11-28T12:54:43-08:00Michael Davieshttp://code.activestate.com/recipes/users/1502767/http://code.activestate.com/recipes/252178-reorder-a-sequence-uses-generators-and-recursion/
<p style="color: grey">
Python
recipe 252178
by <a href="/recipes/users/1502767/">Michael Davies</a>
(<a href="/recipes/tags/search/">search</a>).
</p>
<p>Small function to generate every permutation of a given sequence. Works for lists and strings</p>
groupby() For Unsorted Input (Python)
2017-05-12T10:40:58-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580800-groupby-for-unsorted-input/
<p style="color: grey">
Python
recipe 580800
by <a href="/recipes/users/4182236/">Alfe</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/grouping/">grouping</a>, <a href="/recipes/tags/lazy/">lazy</a>).
</p>
<p>We all know the <code>groupby()</code> which is available in the <code>itertools</code> standard module. This one yields groups of consecutive elements in the input which are meant to be together in one group. For non-consecutive elements this will yield more than one group for the same key.</p>
<p>So effectively, <code>groupby()</code> only reformats a flat list into bunches of elements from that list without reordering anything. In practice this means that for input sorted by key this works perfect, but for unsorted input it might yield several groups for the same key (with groups for other keys in between). Typically needed, though, is a grouping with reordering if necessary.</p>
<p>I implemented a likewise lazy function (yielding generators) which also accepts ungrouped input.</p>
Merge sorted sequences (Python)
2004-09-18T01:54:35-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/305269-merge-sorted-sequences/
<p style="color: grey">
Python
recipe 305269
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>New take on merging. Uses Python's "timsort" to merge in O(n) time.</p>
Lazy sorting (Python)
2004-05-03T12:30:10-07:00Matteo Dell'Amicohttp://code.activestate.com/recipes/users/1782068/http://code.activestate.com/recipes/280501-lazy-sorting/
<p style="color: grey">
Python
recipe 280501
by <a href="/recipes/users/1782068/">Matteo Dell'Amico</a>
(<a href="/recipes/tags/search/">search</a>).
Revision 2.
</p>
<p>This generator will sort only the requested part of a list. Useful for getting just the first few elements of a list and avoiding the overhead of sorting the whole thing.</p>
Inserting Images on PDF Pages (Python)
2017-05-17T21:10:26-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580803-inserting-images-on-pdf-pages/
<p style="color: grey">
Python
recipe 580803
by <a href="/recipes/users/4193772/">Jorj X. McKie</a>
(<a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>).
</p>
<p>Version 1.11.0 of PyMuPDF allows putting an image on an existing PDF page.
The following example puts the same image on every page of a given PDF - like a thumbnail.</p>
2D polygon area (Python)
2012-10-01T16:22:13-07:00Jamie Bullhttp://code.activestate.com/recipes/users/4183059/http://code.activestate.com/recipes/578275-2d-polygon-area/
<p style="color: grey">
Python
recipe 578275
by <a href="/recipes/users/4183059/">Jamie Bull</a>
(<a href="/recipes/tags/2d/">2d</a>, <a href="/recipes/tags/area/">area</a>, <a href="/recipes/tags/geometry/">geometry</a>, <a href="/recipes/tags/polygon/">polygon</a>).
</p>
<p>Function for finding the area of a polygon in a 2D co-ordinate system.</p>
Komodo JS Macro: move tabs (JavaScript)
2012-04-10T17:39:47-07:00Todd Whitemanhttp://code.activestate.com/recipes/users/2666241/http://code.activestate.com/recipes/578093-komodo-js-macro-move-tabs/
<p style="color: grey">
JavaScript
recipe 578093
by <a href="/recipes/users/2666241/">Todd Whiteman</a>
(<a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/position/">position</a>, <a href="/recipes/tags/tabs/">tabs</a>, <a href="/recipes/tags/toddw/">toddw</a>).
</p>
<p>This Komodo macro is an example to show how to programmatically re-position a Komodo editor tab. This macro moves the currently selected tab to the start of the tab list.</p>
node.js quicklog method to log to a file (JavaScript)
2010-08-11T05:08:57-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577351-nodejs-quicklog-method-to-log-to-a-file/
<p style="color: grey">
JavaScript
recipe 577351
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/nodejs/">nodejs</a>).
</p>
<p>I tend to have a "quicklog" method for a few of the languages I'm working in to do logging when stdout/stderr isn't necessarily available (GUI app) or convenient (lots of other output on stdout, etc.). My usage with <a href="http://nodejs.org">nodejs</a> was while working on the node REPL. Log output to stdout interfered with the REPL.</p>
Self-Printing Program (C)
2009-06-05T01:19:17-07:00J Yhttp://code.activestate.com/recipes/users/4170398/http://code.activestate.com/recipes/576798-self-printing-program/
<p style="color: grey">
C
recipe 576798
by <a href="/recipes/users/4170398/">J Y</a>
(<a href="/recipes/tags/tricky/">tricky</a>).
</p>
<p>The two key tricks here are using a string with an embedded %s specifier to allow the string to contain itself when printed, and to use the %c format specifier to allow printing out special characters like newlines, which could not otherwise be embedded in the output string. </p>
A Generator That Helps Simplify Queue Consumers (Python)
2003-12-04T17:29:35-08:00Jimmy Retzlaffhttp://code.activestate.com/recipes/users/98735/http://code.activestate.com/recipes/252498-a-generator-that-helps-simplify-queue-consumers/
<p style="color: grey">
Python
recipe 252498
by <a href="/recipes/users/98735/">Jimmy Retzlaff</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>My Queue usage typically involves a producer thread and a consumer thread. The producer calls queue.put(value) until it's done at which point it calls queue.put(sentinel). My consumers almost always look like this:</p>
<p>while True:
value = queue.get()
if value != sentinel:
# do something with value
else:
break</p>
<p>That logic can be abstracted away into a generator function that allows consumer code to look like this instead:</p>
<p>for value in iterQueue(queue, sentinel):
# do something with value</p>
Dynamically added methods to a class (Python)
2001-10-15T02:49:01-07:00Brett Cannonhttp://code.activestate.com/recipes/users/98030/http://code.activestate.com/recipes/81732-dynamically-added-methods-to-a-class/
<p style="color: grey">
Python
recipe 81732
by <a href="/recipes/users/98030/">Brett Cannon</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>Ruby has the functionality of being able to add a method to a class at an arbitrary point in your code. I figured Python must have some way for allowing this to happen, and it turned out it did. The method is available instantly to all already existing instances and of course ones yet to be created. If you specify method_name then that name is used for the method call.</p>
<p>One thing to make sure to do is that the function has a variable for the instance to be passed to (i.e. self).</p>
Hivemind (Python)
2015-07-15T12:37:13-07:00Oscar Byrnehttp://code.activestate.com/recipes/users/4192487/http://code.activestate.com/recipes/579082-hivemind/
<p style="color: grey">
Python
recipe 579082
by <a href="/recipes/users/4192487/">Oscar Byrne</a>
(<a href="/recipes/tags/borg/">borg</a>, <a href="/recipes/tags/defaultdict/">defaultdict</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/hashing/">hashing</a>, <a href="/recipes/tags/hivemind/">hivemind</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/singleton/">singleton</a>, <a href="/recipes/tags/state/">state</a>).
</p>
<p>Inspired by the ever-popular Borg pattern, objects inheriting from Hivemind share state if initialised with the same arguments</p>
Generating Balanced Parenthesis (Python)
2013-02-14T22:00:22-08:00bartenderhttp://code.activestate.com/recipes/users/4185259/http://code.activestate.com/recipes/578458-generating-balanced-parenthesis/
<p style="color: grey">
Python
recipe 578458
by <a href="/recipes/users/4185259/">bartender</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>).
</p>
<p>The python code generates balanced parenthesis recursively.
Example for depth=3 i.e. 3 opening brackets and 3 closing brackets,the sample output should be:</p>
<p>((()))
(()())
(())()
()(())
()()()</p>
<p>The number of such output ( 5 in above case) is a Catalan Number.</p>
Counting TCP\UDP endpoints (Batch)
2013-10-10T16:26:33-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578335-counting-tcpudp-endpoints/
<p style="color: grey">
Batch
recipe 578335
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/endpoint/">endpoint</a>, <a href="/recipes/tags/tcp/">tcp</a>, <a href="/recipes/tags/udp/">udp</a>).
Revision 2.
</p>
<p>Needs just for statistic, no further.</p>
Checking letters for drives that already using (Batch)
2012-11-19T08:32:33-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578334-checking-letters-for-drives-that-already-using/
<p style="color: grey">
Batch
recipe 578334
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/disk/">disk</a>).
</p>
<p>For keeping order drive naming with active <strong>diskpart</strong> using.</p>
Minimalistic Word Wrap using Regex (Python)
2012-06-07T14:31:44-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/578162-minimalistic-word-wrap-using-regex/
<p style="color: grey">
Python
recipe 578162
by <a href="/recipes/users/4182236/">Alfe</a>
(<a href="/recipes/tags/minimalistic/">minimalistic</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/word/">word</a>, <a href="/recipes/tags/wrap/">wrap</a>).
</p>
<p>I know there is the module textwrap and other recipes like <a href="http://code.activestate.com/recipes/148061-one-liner-word-wrap-function/" rel="nofollow">http://code.activestate.com/recipes/148061-one-liner-word-wrap-function/</a></p>
<p>But in some constellations my recipe for a very simple word wrap might come in handy nevertheless.</p>
Base64 encode-decode Five Liner (Python)
2013-04-26T09:37:21-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/577626-base64-encode-decode-five-liner/
<p style="color: grey">
Python
recipe 577626
by <a href="/recipes/users/4170754/">ccpizza</a>
(<a href="/recipes/tags/base64/">base64</a>).
Revision 2.
</p>
<p>Encodes or decodes base64-encoded files. If the input or output file(s) are ommited standard input/output will be used.</p>
<p>The encoding direction is detected from the file name: for decoding save the script e.g. as encode64.py. For decoding create a symlink with <code>decode</code> in the name.</p>
Flattening lists (Python)
2010-12-21T16:04:38-08:00Michael Pucketthttp://code.activestate.com/recipes/users/4176295/http://code.activestate.com/recipes/577509-flattening-lists/
<p style="color: grey">
Python
recipe 577509
by <a href="/recipes/users/4176295/">Michael Puckett</a>
(<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>).
</p>
<p>Classic / frequently asked, <em>'How to flatten a list, regardless the nesting.'</em></p>