Popular recipes tagged "meta:loc=12"http://code.activestate.com/recipes/tags/meta:loc=12/2017-07-11T18:57:54-07:00ActiveState Code RecipesHow to handle PDF embedded files with PyMuPDF (Python) 2017-07-11T18:57:54-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580796-how-to-handle-pdf-embedded-files-with-pymupdf/ <p style="color: grey"> Python recipe 580796 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/embedded_files/">embedded_files</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>). Revision 3. </p> <p>Version 1.11.0 (based on MuPDF v1.11) allows exporting, importing and interrogating files embedded in a PDF.</p> <p>PDF "/EmbeddedFiles" are similar to ZIP archives (or the Microsoft OLE technique), allowing arbitrary data to be incorporated in a PDF and benefit from its unique features.</p> Number of bits needed to store an integer, and its binary representation (Python) 2017-03-12T23:10:48-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580762-number-of-bits-needed-to-store-an-integer-and-its-/ <p style="color: grey"> Python recipe 580762 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/formats/">formats</a>, <a href="/recipes/tags/integers/">integers</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/representation/">representation</a>, <a href="/recipes/tags/type/">type</a>). </p> <p>This recipe shows how to find, via Python code, the number of bits needed to store an integer, and how to generate its binary representation. It does this for integers from 0 to 256.</p> <p>More details and full output here:</p> <p><a href="https://jugad2.blogspot.in/2017/03/find-number-of-bits-needed-to-store.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/find-number-of-bits-needed-to-store.html</a></p> Installed Modules (Perl) 2015-07-17T04:18:15-07:00Roger Mbiama Assogohttp://code.activestate.com/recipes/users/4178746/http://code.activestate.com/recipes/579084-installed-modules/ <p style="color: grey"> Perl recipe 579084 by <a href="/recipes/users/4178746/">Roger Mbiama Assogo</a> (<a href="/recipes/tags/application/">application</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/network/">network</a>). </p> <p>Additional Perl modules are installed on the server (aside from the standard libraries) run from a web broswer.</p> Draw a diamond with asterisks using recursion (Python) 2014-11-07T17:51:32-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578959-draw-a-diamond-with-asterisks-using-recursion/ <p style="color: grey"> Python recipe 578959 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/drawing/">drawing</a>). </p> <p>Given the following function header:</p> <pre class="prettyprint"><code>def triangles(n): </code></pre> <p>If n for example is 5, then the drawing would be like this:</p> <pre class="prettyprint"><code> * * * * * * * * * * * * * </code></pre> First n primes numbers (Python) 2014-09-20T19:33:41-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578923-first-n-primes-numbers/ <p style="color: grey"> Python recipe 578923 by <a href="/recipes/users/4190606/">juan</a> . Revision 6. </p> <p>Using the Sieve of Eratosthenes find the first n primes numbers.</p> Name a lambda (Python) 2014-07-02T20:54:01-07:00David Weilhttp://code.activestate.com/recipes/users/1296670/http://code.activestate.com/recipes/578902-name-a-lambda/ <p style="color: grey"> Python recipe 578902 by <a href="/recipes/users/1296670/">David Weil</a> (<a href="/recipes/tags/func_name/">func_name</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/naming/">naming</a>). </p> <p>A very simple recipe to allow you <em>easily</em> name lambda-objects (or other kind of objects, callable, for example, partial objects) you create, with little overhead and friendly syntax.</p> php backwards compatibility porting tips (PHP) 2013-02-10T00:40:26-08:00imam feriantohttp://code.activestate.com/recipes/users/633541/http://code.activestate.com/recipes/578452-php-backwards-compatibility-porting-tips/ <p style="color: grey"> PHP recipe 578452 by <a href="/recipes/users/633541/">imam ferianto</a> (<a href="/recipes/tags/backwards/">backwards</a>, <a href="/recipes/tags/compatibility/">compatibility</a>, <a href="/recipes/tags/old/">old</a>, <a href="/recipes/tags/php4/">php4</a>, <a href="/recipes/tags/php5/">php5</a>). </p> <p>Hello again, on december 2012, I was porting my old php code (php4) to new php 5.4.3. There are to many errors within code, especially for session, eregi, global variables and others. This some pieces step for compatibility porting:</p> <ol> <li><p>add global variable definiton on some function that need outside variable &lt;code&gt; function getuser(){ global $loginid,$password; } &lt;/code&gt;</p></li> <li><p>replace eregi(/find/,str) with preg_match(//,str)</p></li> <li><p>add function exists for function that not exists in php5.4.3. for example: &lt;code&gt; if(!function_exists("session_is_registered")){ function session_is_registered($var){ return isset($_SESSION[$var]); } } if(!function_exists("session_register")){ function session_register($var){ $_SESSION[$var]=$_GLOBALS[$var]; } } &lt;/code&gt;</p></li> </ol> <p>Ok there is my tips, how about you? </p> Traverse dotted attribute of an object using built-in function reduce (Python) 2013-01-05T05:49:27-08:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578398-traverse-dotted-attribute-of-an-object-using-built/ <p style="color: grey"> Python recipe 578398 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/bif/">bif</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/reduce/">reduce</a>). </p> <p>Making good use of reduce() to traverse dotted attribute of an object.</p> General decorators decorator (Python) 2012-11-13T09:44:07-08:00Hans Zauberhttp://code.activestate.com/recipes/users/4184246/http://code.activestate.com/recipes/578325-general-decorators-decorator/ <p style="color: grey"> Python recipe 578325 by <a href="/recipes/users/4184246/">Hans Zauber</a> (<a href="/recipes/tags/decorators/">decorators</a>). </p> <p>A general decorator aimed to decorate decorators, letting them keep (some of) the original attributes of the functions they decorate.</p> Running linux command inside your python script (Python) 2012-09-09T08:08:46-07:00Ahmed Kamelhttp://code.activestate.com/recipes/users/4183490/http://code.activestate.com/recipes/578254-running-linux-command-inside-your-python-script/ <p style="color: grey"> Python recipe 578254 by <a href="/recipes/users/4183490/">Ahmed Kamel</a> (<a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Running any linux command line inside your python script.</p> Primes (Python) 2012-06-05T05:16:42-07:00wycao2khttp://code.activestate.com/recipes/users/4182318/http://code.activestate.com/recipes/578155-primes/ <p style="color: grey"> Python recipe 578155 by <a href="/recipes/users/4182318/">wycao2k</a> . </p> <p>This program is to produce prime numbers less than and equal to n.</p> List of datasets in SPSS (Python) 2011-10-27T06:12:13-07:00Petr Zizkahttp://code.activestate.com/recipes/users/4179733/http://code.activestate.com/recipes/577925-list-of-datasets-in-spss/ <p style="color: grey"> Python recipe 577925 by <a href="/recipes/users/4179733/">Petr Zizka</a> (<a href="/recipes/tags/dataset/">dataset</a>, <a href="/recipes/tags/spss/">spss</a>). </p> <p>Return list with opened datasets</p> using `from somename.api import *` when somename is a single module (Python) 2011-08-12T20:24:11-07:00Ethan Furmanhttp://code.activestate.com/recipes/users/4177684/http://code.activestate.com/recipes/577839-using-from-somenameapi-import-when-somename-is-a-s/ <p style="color: grey"> Python recipe 577839 by <a href="/recipes/users/4177684/">Ethan Furman</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/__all__/">__all__</a>). </p> <p>Allow a module to export a subset of __all__, so a user can do <code>from module.api import *</code></p> sleep_min: Sleep for a minimum number of seconds (Python) 2011-02-08T16:44:36-08:00Drew Vogelhttp://code.activestate.com/recipes/users/4176907/http://code.activestate.com/recipes/577568-sleep_min-sleep-for-a-minimum-number-of-seconds/ <p style="color: grey"> Python recipe 577568 by <a href="/recipes/users/4176907/">Drew Vogel</a> (<a href="/recipes/tags/sleep/">sleep</a>, <a href="/recipes/tags/time/">time</a>). </p> <p><code>time.sleep(delay)</code> will sleep for, at most, <code>delay</code> seconds. This function will sleep for at least <code>delay</code> seconds.</p> Write a PNG image in native Python (Python) 2010-10-28T12:03:52-07:00Campbell Bartonhttp://code.activestate.com/recipes/users/4168177/http://code.activestate.com/recipes/577443-write-a-png-image-in-native-python/ <p style="color: grey"> Python recipe 577443 by <a href="/recipes/users/4168177/">Campbell Barton</a> (<a href="/recipes/tags/alpha/">alpha</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/save/">save</a>). </p> <p>This is a simple PNG writing function, intended to be as small as possible. It only supports RGBA 32bit PNGs and the input should be a buffer of rgba bytes.</p> <p>Tested for python 2 and 3.</p> Calculating PI using random numbers (Monte Carlo method) (Python) 2010-03-27T00:58:24-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577154-calculating-pi-using-random-numbers-monte-carlo-me/ <p style="color: grey"> Python recipe 577154 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>Calculating PI using random numbers (Monte Carlo method)</p> Convert XML to Dictionary (Python) 2009-10-28T10:42:17-07:00Adam M Prosthttp://code.activestate.com/recipes/users/4172124/http://code.activestate.com/recipes/576940-convert-xml-to-dictionary/ <p style="color: grey"> Python recipe 576940 by <a href="/recipes/users/4172124/">Adam M Prost</a> (<a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/simple/">simple</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>A simple function to convert a headerless XML string into a dictionary using only simplejson and re.</p> touch (Python) 2009-09-28T16:19:23-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/576915-touch/ <p style="color: grey"> Python recipe 576915 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/touch/">touch</a>, <a href="/recipes/tags/unix/">unix</a>). </p> <p>Python function a la the Unix <code>touch</code> program (<a href="http://www.manpagez.com/man/1/touch/">man touch</a>).</p> lazy property (Python) 2009-04-26T18:10:55-07:00Sridhar Ratnakumarhttp://code.activestate.com/recipes/users/4169511/http://code.activestate.com/recipes/576720-lazy-property/ <p style="color: grey"> Python recipe 576720 by <a href="/recipes/users/4169511/">Sridhar Ratnakumar</a> (<a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/lazy_evaluation/">lazy_evaluation</a>). Revision 6. </p> <p>Python does not have lazy evaluation syntax features built-in, but fortunately decorators can be used with new-style classes to emulate such a feature. There are cases where one wants <code>foo.property</code> to return the actual property whose calculation takes significant amount of time.</p> <p>This recipe adapts the existing <code>property</code> to provide a <code>lazypropery</code> decorator that does this.</p> <p>See the first comment below for an example usage.</p> <p>Also see: <a href="http://en.wikipedia.org/wiki/Lazy_initialization">lazy initialization</a></p> quick Python profiling with hotshot (Python) 2009-02-20T22:35:07-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/576656-quick-python-profiling-with-hotshot/ <p style="color: grey"> Python recipe 576656 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/hotshot/">hotshot</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/profile/">profile</a>). Revision 2. </p> <p>This is a quick snippet that I use occasionally to profile some pure-Python code, using <a href="http://docs.python.org/library/hotshot.html#module-hotshot"><code>hotshot</code></a>. Basically it is this:</p> <ol> <li>Put this <code>@hotshotit</code> decorator on the function you want to profile.</li> <li>Run your code through some representative paces. The result will be a <code>&lt;functionname&gt;.prof</code> in the current directory.</li> <li>Process the <code>.prof</code> file and print the top 20 hotspots with the given "show_stats.py" script.</li> </ol> <p>Props to <a href="http://blogs.activestate.com/toddw/">Todd</a> for slapping this code together.</p> <p>Hotshot <a href="http://docs.python.org/library/profile.html">is a little out of favour</a> now, so I should -- or Todd :) -- should really come up with an equivalent that uses <code>cProfile</code>.</p>