Top-rated recipes tagged "meta:loc=15"http://code.activestate.com/recipes/tags/meta:loc=15/top/2017-06-29T22:54:25-07:00ActiveState Code RecipesPure Python PDF to text converter (Python) 2007-04-12T11:05:10-07:00Dirk Holtwickhttp://code.activestate.com/recipes/users/636691/http://code.activestate.com/recipes/511465-pure-python-pdf-to-text-converter/ <p style="color: grey"> Python recipe 511465 by <a href="/recipes/users/636691/">Dirk Holtwick</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>This example shows how to extract text informations from a PDF file without the need of system dependent tools or code. Just use the pyPdf library from <a href="http://pybrary.net/pyPdf/" rel="nofollow">http://pybrary.net/pyPdf/</a></p> Progress Bar for Console Programs as Iterator (Python) 2010-03-26T20:04:02-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/576986-progress-bar-for-console-programs-as-iterator/ <p style="color: grey"> Python recipe 576986 by <a href="/recipes/users/4172244/">Michael Grünewald</a> (<a href="/recipes/tags/console/">console</a>, <a href="/recipes/tags/cui/">cui</a>, <a href="/recipes/tags/progress/">progress</a>). Revision 6. </p> <p>A small proxy iterator showing a progress bar on the console. Only works with iterators that provide a length (i.e. <code>len(iterator)</code> must be defined).</p> Context manager to atomically replace a file (Python) 2012-06-17T12:09:49-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578166-context-manager-to-atomically-replace-a-file/ <p style="color: grey"> Python recipe 578166 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/atomic/">atomic</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/replace/">replace</a>). Revision 3. </p> <p>This context manager ensures that a file's content is either replaced atomically with new contents or left unchanged. An exception or other error will not leave it empty or with partial content.</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> "public" decorator, adds an item to __all__ (Python) 2009-12-30T12:53:29-08:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/576993-public-decorator-adds-an-item-to-__all__/ <p style="color: grey"> Python recipe 576993 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/decorators/">decorators</a>). </p> <p>The DRY principle says, "Don't repeat yourself". A Python module typically defines a global variable named "__all__" that holds the names of everything the author wants visible. This means you have to type each function or class name a second time, and you have to maintain the contents of __all__ manually. This fixes that.</p> win32com - Accessing AutoCAD entities (Python) 2005-08-30T13:48:52-07:00Ed Blakehttp://code.activestate.com/recipes/users/2492237/http://code.activestate.com/recipes/440493-win32com-accessing-autocad-entities/ <p style="color: grey"> Python recipe 440493 by <a href="/recipes/users/2492237/">Ed Blake</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Autodesk's AutoCAD drafting software has for a number of versions included an increasingly complete COM interface. Using the Python win32com modules we have been able to automate some aspects the software; unfortunately because of the organization of the interface certain methods and properties were inaccessible from Python. In recent versions of the win32 modules a new function has been added though: win32com.client.CastTo(). By casting our objects as the correct type we now have access to much of the object model that used to be unreachable.</p> <p>In this example we search the ModelSpace collection for text objects. Once found we then cast them and alter one of the text specific properties. To test this code open AutoCAD and in a blank file add at least one dtext object with 'Spam' as its value.</p> changing file attributes on windows (Python) 2004-09-03T11:54:59-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/303343-changing-file-attributes-on-windows/ <p style="color: grey"> Python recipe 303343 by <a href="/recipes/users/135654/">John Nielsen</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>The win32api module offers SetFileAttributes whiles allows you to make changes to a file in windows. You can set a file to be read only, archive, hidden, etc. The function is simple and convenient to use.</p> Insert a Text Box in a PDF page (fitz / PyMuPDF) (Python) 2017-06-29T22:54:25-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580809-insert-a-text-box-in-a-pdf-page-fitz-pymupdf/ <p style="color: grey"> Python recipe 580809 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/textbox/">textbox</a>). </p> <p>This method inserts text into a predefined rectangular area of a (new or existing) PDF page. Words are distributed across the available space, put on new lines when required etc. Line breaks and tab characters are respected / resolved. Text can be aligned in the box (left, center, right) and fonts can be freely chosen. The method returns a float indicating how vertical space is left over after filling the area.</p> Reverse the sequence of annotations on a PDF page (Python) 2017-01-22T14:02:16-08:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580733-reverse-the-sequence-of-annotations-on-a-pdf-page/ <p style="color: grey"> Python recipe 580733 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/annotation/">annotation</a>, <a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>). Revision 2. </p> <p>Just another demonstration of PyMuPDF's features to deal with annotations:</p> <p>Take a page with several annotations and let them change places in reverse order: first and last annot exchange their rectangles, second and second to last, etc.</p> <p>The annotation images are enlarged or compressed as required to fit into their new areas.</p> Calculating e using Continued Fraction (Python) 2013-09-02T18:49:53-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578653-calculating-e-using-continued-fraction/ <p style="color: grey"> Python recipe 578653 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Calculating e using Continued Fraction</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> Automate CATIA V5 with Python and Pywin32 (Python) 2008-04-02T22:22:46-07:00Mustafa Görmezerhttp://code.activestate.com/recipes/users/2091634/http://code.activestate.com/recipes/347243-automate-catia-v5-with-python-and-pywin32/ <p style="color: grey"> Python recipe 347243 by <a href="/recipes/users/2091634/">Mustafa Görmezer</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 5. </p> <p>This is small application shows how to automate the CAD/PLM Software CATIA V5 via win32com. CATIA V5 must be installed and should be running when testing the application. Other examples you can find on <a href="http://win32com.goermezer.de/content/view/29/291/" rel="nofollow">http://win32com.goermezer.de/content/view/29/291/</a> .</p> fast prime number list creator (Python) 2004-07-17T09:12:38-07:00Kazuo Moriwakahttp://code.activestate.com/recipes/users/1932146/http://code.activestate.com/recipes/286245-fast-prime-number-list-creator/ <p style="color: grey"> Python recipe 286245 by <a href="/recipes/users/1932146/">Kazuo Moriwaka</a> . </p> <p>This function return a list of numbers which is less than argument. It is much faster than other implementations which I test. At my machine, prime_numbers_less_than(100000) takes about 0.78sec. This code is tested at Python 2.3.4 only.</p> Find the common beginning in a list of strings (Python) 2003-12-03T12:05:01-08:00Stephan Diehlhttp://code.activestate.com/recipes/users/774251/http://code.activestate.com/recipes/252177-find-the-common-beginning-in-a-list-of-strings/ <p style="color: grey"> Python recipe 252177 by <a href="/recipes/users/774251/">Stephan Diehl</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>I came up with this when I tried to implement some autocompletion feature. The problem is to find a common substring (beginning from the start) for all strings in a given list.</p> <p>I couldn't find an existing recipe that is doing this task, but I'm wondering if I just didn't look hard enough</p> Multi-character split (Tcl) 2001-09-10T10:07:20-07:00Jeff Hobbshttp://code.activestate.com/recipes/users/98167/http://code.activestate.com/recipes/68386-multi-character-split/ <p style="color: grey"> Tcl recipe 68386 by <a href="/recipes/users/98167/">Jeff Hobbs</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>The split command splits a string based on each character that is in the splitString. This version handles the splitString as a combined string, splitting the string into constituent parts.</p> Checking whether a string contains a set of chars (Python) 2001-07-03T21:00:41-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/65441-checking-whether-a-string-contains-a-set-of-chars/ <p style="color: grey"> Python recipe 65441 by <a href="/recipes/users/98061/">Jürgen Hermann</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>While the find() and count() string functions can check for string occurences, there is no function to check on the occurence of a set of characters.</p> Text to HTML (Tcl) 2001-06-21T16:27:26-07:00Jeff Hobbshttp://code.activestate.com/recipes/users/98167/http://code.activestate.com/recipes/65428-text-to-html/ <p style="color: grey"> Tcl recipe 65428 by <a href="/recipes/users/98167/">Jeff Hobbs</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Scan the files in the current directory and wrap them into html/body tags so that netscape will display them as HTML and not as text.</p> merge multiple list into a single list eliminating the duplicates (Python) 2016-04-04T09:41:07-07:00derienskhttp://code.activestate.com/recipes/users/4193876/http://code.activestate.com/recipes/580633-merge-multiple-list-into-a-single-list-eliminating/ <p style="color: grey"> Python recipe 580633 by <a href="/recipes/users/4193876/">deriensk</a> . </p> <p>I have got 20-25+ lists in python and I want to check all the items within the list and put single items into a new list.</p> Get external IP (Text) 2013-07-01T05:52:39-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578584-get-external-ip/ <p style="color: grey"> Text recipe 578584 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/ipv4/">ipv4</a>). Revision 2. </p> <p>In addition to the posts about IPv4. Examples on Python.</p> Hash collision probability / Birthday problem (Python) 2012-12-21T09:32:54-08:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578387-hash-collision-probability-birthday-problem/ <p style="color: grey"> Python recipe 578387 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/birthday/">birthday</a>, <a href="/recipes/tags/collision/">collision</a>, <a href="/recipes/tags/hash/">hash</a>). </p> <p>Calculates the probability that, when making <em>k</em> random selections out of <em>n</em> possibilities, at least two of the selections are the same. See: <a href="http://en.wikipedia.org/wiki/Birthday_problem" rel="nofollow">http://en.wikipedia.org/wiki/Birthday_problem</a></p> <p>What is the probability that (at least) two people in a class of 30 share their birthday?</p> <pre class="prettyprint"><code>&gt;&gt;&gt; collide(30,365) 0.7063162427192688 </code></pre> <p>What is the probability that ORA_HASH generates the same hash when hashing 25000 values?</p> <pre class="prettyprint"><code>&gt;&gt;&gt; collide(25000,int(4.3e9)) 0.07009388771353198 </code></pre>