Most viewed recipes tagged "meta:loc=8"http://code.activestate.com/recipes/tags/meta:loc=8/views/2017-05-17T21:10:26-07:00ActiveState Code RecipesLocating 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> Hamming distance (Python) 2006-12-11T10:22:15-08:00Michael Mayhewhttp://code.activestate.com/recipes/users/4018705/http://code.activestate.com/recipes/499304-hamming-distance/ <p style="color: grey"> Python recipe 499304 by <a href="/recipes/users/4018705/">Michael Mayhew</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Was doing some work with strings and threw this together. This will calculate the Hamming distance (or number of differences) between two strings of the same length.</p> E-mail Address Validation (Python) 2001-07-27T13:37:26-07:00Mark Nenadovhttp://code.activestate.com/recipes/users/114221/http://code.activestate.com/recipes/65215-e-mail-address-validation/ <p style="color: grey"> Python recipe 65215 by <a href="/recipes/users/114221/">Mark Nenadov</a> (<a href="/recipes/tags/web/">web</a>). Revision 5. </p> <p>This function simply validates an e-mail address. Ignore this recepie and go to my "StringValidator" recepie, which is a much better solution</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> 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> Eight 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> 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> 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> remove directories recursively (Python) 2008-03-26T16:14:58-07:00Dan Gunterhttp://code.activestate.com/recipes/users/2010587/http://code.activestate.com/recipes/552732-remove-directories-recursively/ <p style="color: grey"> Python recipe 552732 by <a href="/recipes/users/2010587/">Dan Gunter</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Extremely simple bit of code to remove a directory recursively. Simply feed it the path of the top-level directory to remove, and off it goes. As presented, there is no error-checking; failure at any point will stop the function and raise an IOError.</p> Importing any file without modifying sys.path (Python) 2002-10-28T10:08:43-08:00Walter Dörwaldhttp://code.activestate.com/recipes/users/776207/http://code.activestate.com/recipes/159571-importing-any-file-without-modifying-syspath/ <p style="color: grey"> Python recipe 159571 by <a href="/recipes/users/776207/">Walter Dörwald</a> . </p> <p>This recipe lets you import any file as a module. It's not required that the file is in the Python search path. This is done without having to modify sys.path (even temporarily)</p> script for calculating simple interest (Python) 2008-10-09T07:51:39-07:00Mahmoud Hossamhttp://code.activestate.com/recipes/users/4167486/http://code.activestate.com/recipes/576533-script-for-calculating-simple-interest/ <p style="color: grey"> Python recipe 576533 by <a href="/recipes/users/4167486/">Mahmoud Hossam</a> (<a href="/recipes/tags/accounting/">accounting</a>, <a href="/recipes/tags/interest/">interest</a>). </p> <p>a script which can be used to calculate the amount of interest using the formula I=Pit</p> <p>this is a very simple code,any suggestions are welcome</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> Remove control character ^M from opened html files (Python) 2004-07-12T18:51:02-07:00Liang Guohttp://code.activestate.com/recipes/users/1922801/http://code.activestate.com/recipes/286229-remove-control-character-m-from-opened-html-files/ <p style="color: grey"> Python recipe 286229 by <a href="/recipes/users/1922801/">Liang Guo</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>I used a URLOpener to get the HTML file from some web-sites for some parsing. However, the returned data file had ^M everywhere, and it was pretty annoying. Before parsing this file, I want to strip it of all occurences of this control character ^M. Of course, I can use dos2unix or similar tools to do that offline, but I wanna do it the pythonic way.</p> <p>First, I need to find out the ascii value for '^M'.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import curses.ascii &gt;&gt;&gt; ascii.ascii('^V^M') '\r' </code></pre> <p>Then, I can just do a search and replace '\r' in any string.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; string.replace( str, '\r', '' ) </code></pre> <p>In my code, I just have this line in the overriden method handle_data of my html parser class.</p> FILETIME to datetime (Python) 2007-03-27T05:32:07-07:00Julian Rathhttp://code.activestate.com/recipes/users/2514241/http://code.activestate.com/recipes/511425-filetime-to-datetime/ <p style="color: grey"> Python recipe 511425 by <a href="/recipes/users/2514241/">Julian Rath</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Converting a FILTIME in a datetime is sometime verry useful if you use ctypes with win API.</p> Pick Unused Port (Python) 2007-09-19T17:01:56-07:00Damon Kohlerhttp://code.activestate.com/recipes/users/4084059/http://code.activestate.com/recipes/531822-pick-unused-port/ <p style="color: grey"> Python recipe 531822 by <a href="/recipes/users/4084059/">Damon Kohler</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>Picking an unused port is easy to do using the socket module.</p> list all locale (Python) 2014-04-19T13:41:38-07:00Shane Wanghttp://code.activestate.com/recipes/users/4171830/http://code.activestate.com/recipes/578863-list-all-locale/ <p style="color: grey"> Python recipe 578863 by <a href="/recipes/users/4171830/">Shane Wang</a> . </p> <p>list all locales via Python</p> Flatten a list (or list of lists, etc.) (Python) 2011-03-01T03:46:26-08:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577255-flatten-a-list-or-list-of-lists-etc/ <p style="color: grey"> Python recipe 577255 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/extend/">extend</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/smart/">smart</a>). Revision 2. </p> <p>I created this function sometime ago for my own purposes. It simply flattens a list.</p> <p>Check out the first comment for another version of someone else's design.</p> Unsupported sitecustomize.py sinze Python 2.5 (Python) 2008-03-26T12:23:19-07:00Dirk Holtwickhttp://code.activestate.com/recipes/users/636691/http://code.activestate.com/recipes/552729-unsupported-sitecustomizepy-sinze-python-25/ <p style="color: grey"> Python recipe 552729 by <a href="/recipes/users/636691/">Dirk Holtwick</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Since Python 2.5 the automatic import of the module "sitecustomize.py" in the directory of the main program is not supported any more (even if the documentation says that it is). Putting this little script named "sitecustomize.py" in the default Python path like in "site-packages" should solve this problem.</p> shift chars in string (Python) 2002-04-10T10:59:39-07:00anurag uniyalhttp://code.activestate.com/recipes/users/127639/http://code.activestate.com/recipes/106430-shift-chars-in-string/ <p style="color: grey"> Python recipe 106430 by <a href="/recipes/users/127639/">anurag uniyal</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 4. </p> <p>One liner code to shift each char of a string by some passed value very simple encryption :) added version 2 fixing following bugs: 1. Capital chars are shifted correctly. 2. Only alpha numeric chars are shifted. 3. No hard-coded numbers so that as long as first alphabet is 'a' and last 'z' it will work :)</p> Avoiding lambda in writing callback functions (Python) 2001-08-16T12:41:55-07:00Danny Yoohttp://code.activestate.com/recipes/users/98032/http://code.activestate.com/recipes/66521-avoiding-lambda-in-writing-callback-functions/ <p style="color: grey"> Python recipe 66521 by <a href="/recipes/users/98032/">Danny Yoo</a> . </p> <p>A utility class that makes writing callbacks more pleasant than using lambda. Especially using with Tkinter.</p>