Popular recipes tagged "meta:loc=15"http://code.activestate.com/recipes/tags/meta:loc=15/2017-06-29T22:54:25-07:00ActiveState Code RecipesInsert 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> 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> 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> 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> Dice (Python) 2013-07-11T15:34:51-07:00superducktoxichttp://code.activestate.com/recipes/users/4187037/http://code.activestate.com/recipes/578601-dice/ <p style="color: grey"> Python recipe 578601 by <a href="/recipes/users/4187037/">superducktoxic</a> . </p> <p>Choose a die and the program will give you the answer.</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> 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> Averaging Literal Numbers (Python) 2012-12-06T13:44:42-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578365-averaging-literal-numbers/ <p style="color: grey"> Python recipe 578365 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/demonstration/">demonstration</a>, <a href="/recipes/tags/numbers/">numbers</a>). </p> <p>One program that a teacher may assign to beginning programming students would be to take a list of numbers and average them together. This recipe does just that and is designed to be easy to read and follow, showing what is possible with a few lines of Python.</p> unicode Command line histograms (Python) 2012-10-18T10:08:09-07:00Enrico Giampierihttp://code.activestate.com/recipes/users/4180642/http://code.activestate.com/recipes/578293-unicode-command-line-histograms/ <p style="color: grey"> Python recipe 578293 by <a href="/recipes/users/4180642/">Enrico Giampieri</a> . Revision 3. </p> <p>A simple recipe to show on a command line the histogram of a data distribution, using numpy to perform the histogram and the unicode bar characters to show the resulting plot</p> URL shortner using Bit.ly api (Python) 2012-09-14T08:37:39-07:00aamir hussainhttp://code.activestate.com/recipes/users/4178272/http://code.activestate.com/recipes/578263-url-shortner-using-bitly-api/ <p style="color: grey"> Python recipe 578263 by <a href="/recipes/users/4178272/">aamir hussain</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/bitly/">bitly</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/urlshortner/">urlshortner</a>). </p> <p>URL shortner using Bit.ly api</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> Converts from decimal to any base ( between 2 and 26 ) (Python) 2011-02-24T00:33:07-08:00Shashwat Anandhttp://code.activestate.com/recipes/users/4172995/http://code.activestate.com/recipes/577586-converts-from-decimal-to-any-base-between-2-and-26/ <p style="color: grey"> Python recipe 577586 by <a href="/recipes/users/4172995/">Shashwat Anand</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/base/">base</a>). </p> <p>This function takes in any base-10 integer and returns the string representation of that number in its specified base-n form. The code was inspired from <a href="http://code.activestate.com/recipes/65212-convert-from-decimal-to-any-base-number/" rel="nofollow">http://code.activestate.com/recipes/65212-convert-from-decimal-to-any-base-number/</a> , thereby improving upon it.</p> JSON instead of pickle for memcached (Python) 2012-01-10T22:31:36-08:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578011-json-instead-of-pickle-for-memcached/ <p style="color: grey"> Python recipe 578011 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/memcache/">memcache</a>, <a href="/recipes/tags/memcached/">memcached</a>, <a href="/recipes/tags/pickle/">pickle</a>). </p> <p>Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) does'n know pickle, but json yes.</p> Syntactic sugar to create XHTML hierarchies with ElementTree (Python) 2012-01-18T22:15:58-08:00Alain Mellanhttp://code.activestate.com/recipes/users/4065697/http://code.activestate.com/recipes/578020-syntactic-sugar-to-create-xhtml-hierarchies-with-e/ <p style="color: grey"> Python recipe 578020 by <a href="/recipes/users/4065697/">Alain Mellan</a> (<a href="/recipes/tags/xml/">xml</a>). </p> <p>Simplify the code when creating XHTML or XML hierarchies with ElementTree.</p> <p>Usually, I have code like this:</p> <pre class="prettyprint"><code>table = ET.SubElement(body, 'table') table.attrib['border'] = '1' tr = ET.SubElement(table, 'tr') ET.SubElement(tr, 'td').text = 'some text' </code></pre> <p>Using Python's __getattr__ and partial function evaluation allows to create an object that will yield a much simplified syntax.</p> A Komodo macro for duplicating the current file in Komodo 6 (JavaScript) 2011-02-03T23:47:41-08:00Eric Promislowhttp://code.activestate.com/recipes/users/4166930/http://code.activestate.com/recipes/577562-a-komodo-macro-for-duplicating-the-current-file-in/ <p style="color: grey"> JavaScript recipe 577562 by <a href="/recipes/users/4166930/">Eric Promislow</a> (<a href="/recipes/tags/copy/">copy</a>, <a href="/recipes/tags/files/">files</a>). </p> <p>The project/file API changed significantly moving from Komodo 5 to 6. Specifically, the project manager and file manager have been split into two separate modules.</p> <p>Here's some code to duplicate the current file, using Komodo 6. Note that it uses an internal function - I've made a note that the function has been effectively published, and needs to preserve its current interface.</p> Get the Name of the Current Method (JavaScript) 2011-01-03T20:49:58-08:00Eric Promislowhttp://code.activestate.com/recipes/users/4166930/http://code.activestate.com/recipes/577532-get-the-name-of-the-current-method/ <p style="color: grey"> JavaScript recipe 577532 by <a href="/recipes/users/4166930/">Eric Promislow</a> (<a href="/recipes/tags/codeintel/">codeintel</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/sections/">sections</a>). </p> <p>Here's a snippet for Komodo JavaScript macros that gives the name of the current method</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> Support and warranty information using WMI (Python) 2011-03-17T22:07:43-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/577123-support-and-warranty-information-using-wmi/ <p style="color: grey"> Python recipe 577123 by <a href="/recipes/users/4172244/">Michael Grünewald</a> (<a href="/recipes/tags/thinkpad/">thinkpad</a>, <a href="/recipes/tags/win32/">win32</a>, <a href="/recipes/tags/wmi/">wmi</a>). Revision 2. </p> <p>Shows the product code and serial number of a Thinkpad (of any computer with those information actually) and opens the Lenovo support and warranty pages for that system. It might work with other Lenovo computers too.</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>