Popular recipes tagged "meta:loc=37"http://code.activestate.com/recipes/tags/meta:loc=37/2016-05-01T09:26:44-07:00ActiveState Code RecipesEqually-spaced numbers (linspace) (Python) 2015-01-12T22:16:37-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/579000-equally-spaced-numbers-linspace/ <p style="color: grey"> Python recipe 579000 by <a href="/recipes/users/4184316/">Andrew Barnert</a> (<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/linspace/">linspace</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/spread/">spread</a>). </p> <p>An equivalent of <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html"><code>numpy.linspace</code></a>, but as a pure-Python lazy sequence.</p> <p>Like NumPy's <code>linspace</code>, but unlike the <a href="http://code.activestate.com/recipes/577068/"><code>spread</code></a> and <a href="http://code.activestate.com/recipes/577068/"><code>frange</code></a> recipes listed here, the <code>num</code> argument specifies the number of values, not the number of intervals, and the range is closed, not half-open.</p> <p>Although this is primarily designed for floats, it will work for <code>Fraction</code>, <code>Decimal</code>, NumPy arrays (although this would be silly) and even <code>datetime</code> values.</p> <p>This recipe can also serve as an example for creating lazy sequences.</p> <p>See the discussion below for caveats.</p> How to delete pages in a PDF using fitz / MuPDF / PyMuPDF (Python) 2016-05-01T09:26:44-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580657-how-to-delete-pages-in-a-pdf-using-fitz-mupdf-pymu/ <p style="color: grey"> Python recipe 580657 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>). </p> <p>A new method <strong>select()</strong> in PyMuPDF 1.9.0 allows selecting pages of a PDF document to create a new one. Any Python list of integers (0 &lt;= n &lt; page count) can be taken.</p> <p>The resulting PDF contains all links, annotations and bookmarks (provided they still point to valid targets).</p> Simple Infix Expression Evaluation (Python) 2015-11-07T18:44:05-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579122-simple-infix-expression-evaluation/ <p style="color: grey"> Python recipe 579122 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/stack/">stack</a>). </p> <p>Simple infix expression evaluation using a stack.</p> Create Tiles of Images with fitz / MuPDF (PyMuPDF) (Python) 2016-04-26T19:54:31-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580629-create-tiles-of-images-with-fitz-mupdf-pymupdf/ <p style="color: grey"> Python recipe 580629 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>, <a href="/recipes/tags/tiles/">tiles</a>). Revision 4. </p> <p>Take an image file (like PNG) and create a new one consisting of arbitrary tiles of the original (or overlay an existing image with selective tiles of another).</p> Creating a single instance application (Python) 2014-11-20T11:59:46-08:00Matteo Bertinihttp://code.activestate.com/recipes/users/4191156/http://code.activestate.com/recipes/578966-creating-a-single-instance-application/ <p style="color: grey"> Python recipe 578966 by <a href="/recipes/users/4191156/">Matteo Bertini</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>Sometimes it is necessary to ensure that only one instance of application is running. This quite simple solution uses mutex to achieve this, and will run only on Windows platform.</p> Elementary regex without third party libraries (C++) 2013-07-13T12:58:14-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578602-elementary-regex-without-third-party-libraries/ <p style="color: grey"> C++ recipe 578602 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/wininet/">wininet</a>). </p> <p>This is a dirty trick how to access primitive regular expression on Windows.</p> Invoking PowerShell script from batch file (Batch) 2012-11-01T18:39:55-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578316-invoking-powershell-script-from-batch-file/ <p style="color: grey"> Batch recipe 578316 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/powershell/">powershell</a>). </p> <p>I'm not sure that there is a way do it without temporary files and why it need at last, maybe for tuning PowerShell host at start? This sample demonstrates how to launch PowerShell host inside CommandPrompt session and change it caption on clock. (Note: be sure that you have enough rights to execute PowerShell scripts - Get-ExecutionPolicy).</p> Demonstrate Rochambeau (Python) 2012-11-01T14:07:54-07:00Eric.sunhttp://code.activestate.com/recipes/users/4183602/http://code.activestate.com/recipes/578313-demonstrate-rochambeau/ <p style="color: grey"> Python recipe 578313 by <a href="/recipes/users/4183602/">Eric.sun</a> (<a href="/recipes/tags/python3/">python3</a>). </p> <p>demonstrate "stone,stainless,paper" game, user could select a sign, and system will generate random sign to compete with user.</p> Parse profile (Python) 2012-10-12T23:40:55-07:00Jason Friedmanhttp://code.activestate.com/recipes/users/4183835/http://code.activestate.com/recipes/578280-parse-profile/ <p style="color: grey"> Python recipe 578280 by <a href="/recipes/users/4183835/">Jason Friedman</a> (<a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/profile/">profile</a>, <a href="/recipes/tags/shell/">shell</a>). Revision 3. </p> <pre class="prettyprint"><code>export VAR1=foo export VAR2=bar export VAR3=$VAR1$VAR2 export VAR4=${VAR1}$VAR2 export VAR5=${VAR1}indent export VAR6="text${VAR1} " # With embedded spaces and a comment export VAR7='${VAR4}' # Leave text within tics as-is </code></pre> <p>will be read as:</p> <pre class="prettyprint"><code>{'VAR1': 'foo', 'VAR2': 'bar', 'VAR3': 'foobar', 'VAR4': 'foobar', 'VAR5': 'fooindent', 'VAR6': 'textfoo ', 'VAR7': '${VAR4}'} </code></pre> Select some nth smallest elements, quickselect, inplace (Python) 2010-11-30T07:32:42-08:00Teodor Kichatovhttp://code.activestate.com/recipes/users/4176095/http://code.activestate.com/recipes/577477-select-some-nth-smallest-elements-quickselect-inpl/ <p style="color: grey"> Python recipe 577477 by <a href="/recipes/users/4176095/">Teodor Kichatov</a> (<a href="/recipes/tags/quickselect/">quickselect</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/selection/">selection</a>). </p> <p>fork of <a href="http://code.activestate.com/recipes/269554-select-the-nth-smallest-element/" rel="nofollow">http://code.activestate.com/recipes/269554-select-the-nth-smallest-element/</a> O(n) quicksort style algorithm for looking up data based on rank order. Useful for finding medians, percentiles, quartiles, and deciles. Equivalent to [data[n] for n in positions] when the data is already sorted.</p> Newton Fractals (Python) 2010-03-30T04:50:09-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577166-newton-fractals/ <p style="color: grey"> Python recipe 577166 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>). </p> <p>It draws the Newton fractal of any given complex-variable function.</p> Simple BER decoding in python (Python) 2010-05-26T10:05:22-07:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/577244-simple-ber-decoding-in-python/ <p style="color: grey"> Python recipe 577244 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/ber/">ber</a>, <a href="/recipes/tags/ber_tlv/">ber_tlv</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Splits a string into BER TLV's and returns a dict of {type(hex): value(binary)} Doesn't interpret tags, Descend into compound tags.</p> using pyHook to block Windows Keys (Python) 2011-09-18T21:29:17-07:00Brian Davishttp://code.activestate.com/recipes/users/4119187/http://code.activestate.com/recipes/553270-using-pyhook-to-block-windows-keys/ <p style="color: grey"> Python recipe 553270 by <a href="/recipes/users/4119187/">Brian Davis</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Normally you do NOT want to block operating system key combinations but there are a few legitimate cases where you do. In my case I am making a pygame script for my 1 year old to bang on the keyboard and see/hear shapes/color/sounds in response. Brian Fischer on the pygame mailing list pointed me to pyHook. This example was taken from here: <a href="http://www.mindtrove.info/articles/pyhook.html" rel="nofollow">http://www.mindtrove.info/articles/pyhook.html</a> and modified to use the pygame event system.</p> Meta programming for generating combination (anagram) of a string (Python) 2008-03-24T15:24:54-07:00bussiere bussierehttp://code.activestate.com/recipes/users/4050557/http://code.activestate.com/recipes/551793-meta-programming-for-generating-combination-anagra/ <p style="color: grey"> Python recipe 551793 by <a href="/recipes/users/4050557/">bussiere bussiere</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>making a program that will write a program to find all the combinaison of a string</p> emulate collections.defaultdict (Python) 2007-07-09T14:15:39-07:00Jason Kirtlandhttp://code.activestate.com/recipes/users/4067388/http://code.activestate.com/recipes/523034-emulate-collectionsdefaultdict/ <p style="color: grey"> Python recipe 523034 by <a href="/recipes/users/4067388/">Jason Kirtland</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>A pure-Python version of Python 2.5's defaultdict</p> attribute proxy - forwarding attribute access (Python) 2007-03-21T10:28:42-07:00Anders Hammarquisthttp://code.activestate.com/recipes/users/136364/http://code.activestate.com/recipes/510402-attribute-proxy-forwarding-attribute-access/ <p style="color: grey"> Python recipe 510402 by <a href="/recipes/users/136364/">Anders Hammarquist</a> . </p> <p>This recipe lets you transparently forward attribute access to another object in your class. This way, you can expose functionality from some member of your class instance directly, e.g. foo.baz() instead of foo.bar.baz().</p> Yet Another Unique() Function (Python) 2007-02-28T22:09:52-08:00Jordan Callicoathttp://code.activestate.com/recipes/users/2948075/http://code.activestate.com/recipes/502263-yet-another-unique-function/ <p style="color: grey"> Python recipe 502263 by <a href="/recipes/users/2948075/">Jordan Callicoat</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </p> <p>Tim Peter's recipe (52560) and bearophile's version (438599) seem a bit too complex. There are speed an sorting issues with each. Not to mention that neither keeps the data type of the input object. Here is my take on a python unique() function for enumerables (list, tuple, str).</p> Creating a single instance application (Python) 2006-02-22T06:37:05-08:00Dragan Jovelichttp://code.activestate.com/recipes/users/2787616/http://code.activestate.com/recipes/474070-creating-a-single-instance-application/ <p style="color: grey"> Python recipe 474070 by <a href="/recipes/users/2787616/">Dragan Jovelic</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>Sometimes it is necessary to ensure that only one instance of application is running. This quite simple solution uses mutex to achieve this, and will run only on Windows platform.</p> Finding the value passed for a particular parameter to a function by name (Python) 2006-09-14T15:57:06-07:00Jacob Smullyanhttp://code.activestate.com/recipes/users/121058/http://code.activestate.com/recipes/498090-finding-the-value-passed-for-a-particular-paramete/ <p style="color: grey"> Python recipe 498090 by <a href="/recipes/users/121058/">Jacob Smullyan</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 3. </p> <p>Sometimes inside a decorator that creates a function with a generic (<em>args, *</em>kwargs) signature, you want to access a value passed for a particular parameter name to a wrapped function, but don't know whether that value will be passed as a positional or keyword argument, or whether the wrapped function defines a default value for the parameter. The following utility function extracts this information for you.</p> kexec the newest linux kernel (Python) 2007-03-20T05:32:17-07:00Scott Tsaihttp://code.activestate.com/recipes/users/2482534/http://code.activestate.com/recipes/491277-kexec-the-newest-linux-kernel/ <p style="color: grey"> Python recipe 491277 by <a href="/recipes/users/2482534/">Scott Tsai</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>Kexec is a mechanism to use linux itself to load a new kernel without going through the BIOS thus minimizing down time. This script kexecs the newest kernel on the system managed by rpm (assumes a Redhat like system).</p>