Popular recipes tagged "meta:loc=13"http://code.activestate.com/recipes/tags/meta:loc=13/2015-10-16T12:10:58-07:00ActiveState Code RecipesAdd function to Python's __builtin__ module through C API - Python source (Python) 2015-10-16T12:10:58-07:00airweenhttp://code.activestate.com/recipes/users/4192997/http://code.activestate.com/recipes/579112-add-function-to-pythons-__builtin__-module-through/ <p style="color: grey"> Python recipe 579112 by <a href="/recipes/users/4192997/">airween</a> . Revision 2. </p> <p>Add function to __builtin__ module through C API - Python source</p> <p>References:</p> <p><a href="https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/" rel="nofollow">https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/</a></p> <p><a href="https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/" rel="nofollow">https://code.activestate.com/recipes/579111-add-function-to-__builtin__-module-through-c-api-c/</a></p> <p>Save this source as you want in same directory, where C source and compiled binary exists.</p> <p>After the "make", you can use it: $ ./pytestbin this_source</p> <p>without ".py" extension.</p> Examples for random float between 0 and 1 (Python) 2015-06-25T17:55:33-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/579072-examples-for-random-float-between-0-and-1/ <p style="color: grey"> Python recipe 579072 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>I was researching how floats are stored in a computer and was trying to think up a way to generate random values between 0 and 1. Python already provides an implementation allowing this already, and two of the functions below are directly inspired by that code, but the third is a slightly different way of doing the same thing. A similar version of this code has been used to implement similar functionality in C# at one time. Others might also find it useful if they want to create equivalent code in a separate language while having access to random bytes but not random floats. It should be noted that the various implementations get slower as you go down the list.</p> Use PDFWriter with context manager support (Python) 2013-12-16T21:43:27-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578790-use-pdfwriter-with-context-manager-support/ <p style="color: grey"> Python recipe 578790 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/with_statement/">with_statement</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>PDFWriter - a core class of the xtopdf toolkit - can now be used with a Python context manager, a.k.a. the Python <strong>with</strong> statement.</p> <p>Code example below. More details here:</p> <p><a href="http://jugad2.blogspot.in/2013/12/xtopdf-pdfwriter-now-has-context.html" rel="nofollow">http://jugad2.blogspot.in/2013/12/xtopdf-pdfwriter-now-has-context.html</a></p> Checking Gmail for new letters (Bash) 2013-10-11T08:11:38-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578685-checking-gmail-for-new-letters/ <p style="color: grey"> Bash recipe 578685 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/gmail/">gmail</a>). </p> <p>Note: enter name of your email without "@gmail.com"</p> wrist friendly dictionary (Python) 2013-08-18T22:54:26-07:00Ariel Keselmanhttp://code.activestate.com/recipes/users/4187559/http://code.activestate.com/recipes/578644-wrist-friendly-dictionary/ <p style="color: grey"> Python recipe 578644 by <a href="/recipes/users/4187559/">Ariel Keselman</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/nested/">nested</a>). </p> <p>this dictionary allows easy manual creation of nested hierarchies, like so:</p> <p>window.style.width=5</p> <p>or... </p> <p>window['background-color'].rgb= 255,255,255</p> ascii anamation using ascii anamation module (Python) 2013-08-08T19:50:40-07:00Andrew Wayne Teesdale Jr.http://code.activestate.com/recipes/users/4187305/http://code.activestate.com/recipes/578628-ascii-anamation-using-ascii-anamation-module/ <p style="color: grey"> Python recipe 578628 by <a href="/recipes/users/4187305/">Andrew Wayne Teesdale Jr.</a> (<a href="/recipes/tags/animation/">animation</a>). </p> <p>small animation using ascii</p> Get external IP (Batch) 2013-07-01T05:49:57-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578579-get-external-ip/ <p style="color: grey"> Batch recipe 578579 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/ipv4/">ipv4</a>). Revision 2. </p> <p>Checks IPv4</p> Pareto front (Python) 2012-08-01T09:04:04-07:00Jamie Bullhttp://code.activestate.com/recipes/users/4183059/http://code.activestate.com/recipes/578230-pareto-front/ <p style="color: grey"> Python recipe 578230 by <a href="/recipes/users/4183059/">Jamie Bull</a> (<a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/optimal/">optimal</a>, <a href="/recipes/tags/pareto_front/">pareto_front</a>, <a href="/recipes/tags/pareto_frontier/">pareto_frontier</a>, <a href="/recipes/tags/pareto_set/">pareto_set</a>). </p> <p>Function for finding a 2D <a href="http://en.wikipedia.org/wiki/Pareto_efficiency#Use_in_engineering">Pareto frontier</a> given two lists of matched length.</p> Output string with effect, python3+ (Python) 2012-10-17T18:23:05-07:00p@ntut$http://code.activestate.com/recipes/users/4183895/http://code.activestate.com/recipes/578292-output-string-with-effect-python3/ <p style="color: grey"> Python recipe 578292 by <a href="/recipes/users/4183895/">p@ntut$</a> (<a href="/recipes/tags/effects/">effects</a>). </p> <p>Snippet to output lines of characters with effect in python</p> Alternatve generation of the parity or sign of a permutation (Python) 2012-08-07T08:45:23-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/578236-alternatve-generation-of-the-parity-or-sign-of-a-p/ <p style="color: grey"> Python recipe 578236 by <a href="/recipes/users/398009/">Paddy McCarthy</a> (<a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/permutations/">permutations</a>). </p> <p>Although <a href="http://code.activestate.com/recipes/578227/">Recipe 578227</a> was straight-forward to derive, I came across <a href="http://stackoverflow.com/questions/337664/counting-inversions-in-an-array/6424847#6424847">this Stack Overflow answer</a> that gives another method for generating the sign based on comparing the perm and the sorted perm.</p> lreplace() and rreplace(): Replace the beginning and ends of a strings (Python) 2010-06-04T02:18:48-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/577252-lreplace-and-rreplace-replace-the-beginning-and-en/ <p style="color: grey"> Python recipe 577252 by <a href="/recipes/users/4169722/">Dan McDougall</a> (<a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>Python newbies will often make the following mistake (I certainly have =):</p> <pre class="prettyprint"><code>&gt;&gt;&gt; test = """this is a test: ... tis the season for mistakes.""" &gt;&gt;&gt; for line in test.split('\n'): ... print line.lstrip('this') ... is a test the season for mistakes. </code></pre> <p>The mistake is assuming that lstrip() (or rstrip()) strips a string (whole) when it actually strips all of the provided characters in the given string. Python actually comes with no function to strip a string from the left-hand or right-hand side of a string so I wrote this (very simple) recipe to solve that problem. Here's the usage:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; test = """this is a test: ... tis the season for mistakes.""" &gt;&gt;&gt; for line in test.split('\n'): ... print lreplace('this', '', line) ... is a test tis the season for mistakes. </code></pre> <p>I really wish Python had these functions built into the string object. I think it would be a useful addition to the standard library. It would also be nicer to type this:</p> <pre class="prettyprint"><code>line.lreplace('this', '') </code></pre> <p>Instead of this:</p> <pre class="prettyprint"><code>lreplace('this','',line) </code></pre> Integer square root function (Python) 2011-08-04T05:29:16-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577821-integer-square-root-function/ <p style="color: grey"> Python recipe 577821 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/isqrt/">isqrt</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/square/">square</a>). </p> <p>The <em>integer square root</em> function, or isqrt, is equivalent to floor(sqrt(x)) for non-negative x. For small x, the most convenient way to calculate isqrt is by calling int(x**0.5) or int(math.sqrt(x)), but if x is a large enough integer, the sqrt calculation overflows.</p> <p>You can calculate the isqrt without any floating point maths, using just pure integer maths, allowing the function to operate with numbers far larger than possible with floats:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; n = 1234567*(10**1000) &gt;&gt;&gt; n2 = n*n &gt;&gt;&gt; math.sqrt(n2) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; OverflowError: long int too large to convert to float &gt;&gt;&gt; isqrt(n2) == n True </code></pre> Enhancing dir() with globs (Python) 2011-07-01T04:03:46-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577774-enhancing-dir-with-globs/ <p style="color: grey"> Python recipe 577774 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/dir/">dir</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/glob/">glob</a>, <a href="/recipes/tags/globbing/">globbing</a>). </p> <p>dir() is useful for interactively exploring the attributes and methods of objects at the command line. But sometimes dir() returns a lot of information:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; len(dir(decimal.Decimal)) # too much information! 137 </code></pre> <p>It can be helpful to filter the list of names returned. This enhanced version of dir does exactly that, using simple string globbing:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; dir(decimal.Decimal, '*log*') # just attributes with "log" in the name ['_fill_logical', '_islogical', '_log10_exp_bound', 'log10', 'logb', 'logical_and', 'logical_invert', 'logical_or', 'logical_xor'] </code></pre> Perl URL encode and decode (Perl) 2010-11-02T17:52:46-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577450-perl-url-encode-and-decode/ <p style="color: grey"> Perl recipe 577450 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/decode/">decode</a>, <a href="/recipes/tags/encode/">encode</a>, <a href="/recipes/tags/url/">url</a>). </p> <p>URL encode and decode functions for Perl.</p> Adding __implements__ to subclasses during ABCMeta.register (Python) 2011-08-13T03:49:26-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577712-adding-__implements__-to-subclasses-during-abcmeta/ <p style="color: grey"> Python recipe 577712 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/abc/">abc</a>). </p> <p>This is an extension to the abc.ABCMeta class. It is related to <a href="http://code.activestate.com/recipes/577711/">recipe 577711</a>.</p> <p>Basically it has ABCMeta.register add __implements__ to any subclass that gets registered.</p> Amazing estimation of Pi using the Mandlebrot set (Python) 2011-02-20T07:34:56-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577584-amazing-estimation-of-pi-using-the-mandlebrot-set/ <p style="color: grey"> Python recipe 577584 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/approximation/">approximation</a>, <a href="/recipes/tags/mandlebrot/">mandlebrot</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/strange/">strange</a>). </p> <p>Miraculously, you can estimate π just by counting the number of iterations for a point near the neck to escape. The method has a rigorous error bound.</p> Generate List of Numbers from Hyphenated and comma separeted string like "1-5,25-30,4,5" (Python) 2010-07-01T02:08:09-07:00Siddhant Sanyamhttp://code.activestate.com/recipes/users/4174317/http://code.activestate.com/recipes/577279-generate-list-of-numbers-from-hyphenated-and-comma/ <p style="color: grey"> Python recipe 577279 by <a href="/recipes/users/4174317/">Siddhant Sanyam</a> (<a href="/recipes/tags/hypen/">hypen</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/range/">range</a>). </p> <p>This function takes a range in form of "a-b" and generate a list of numbers between a and b inclusive. Also accepts comma separated ranges like "a-b,c-d,f" will build a list which will include numbers from a to b, a to d and f Example: hyphen_range('54-78') hyphen_range('57-78,454,45,1-10') hyphen_range('94-100,1052,2-50')</p> receive UDP broadcasts (Python) 2010-06-30T16:46:16-07:00matt studieyhttp://code.activestate.com/recipes/users/4174312/http://code.activestate.com/recipes/577278-receive-udp-broadcasts/ <p style="color: grey"> Python recipe 577278 by <a href="/recipes/users/4174312/">matt studiey</a> (<a href="/recipes/tags/broadcast/">broadcast</a>, <a href="/recipes/tags/networking/">networking</a>, <a href="/recipes/tags/udp/">udp</a>). </p> <p>simplest implementation I could achieve</p> Split a string on capitalized / uppercase char using Python (Python) 2009-12-11T23:16:36-08:00activestatehttp://code.activestate.com/recipes/users/4172588/http://code.activestate.com/recipes/576984-split-a-string-on-capitalized-uppercase-char-using/ <p style="color: grey"> Python recipe 576984 by <a href="/recipes/users/4172588/">activestate</a> (<a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/string_parsing/">string_parsing</a>, <a href="/recipes/tags/text_processing/">text_processing</a>). Revision 6. </p> <p>By user <a href="http://code.activestate.com/recipes/users/2629617/" rel="nofollow">http://code.activestate.com/recipes/users/2629617/</a> in comment on <a href="http://code.activestate.com/recipes/440698/" rel="nofollow">http://code.activestate.com/recipes/440698/</a> but modified slightly.</p> <p>Splits any string on upper case characters.</p> <p>Ex.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; print split_uppercase("thisIsIt and SoIsThis") this Is It and So Is This </code></pre> <p>note the two spaces after 'and'</p> Komodo: avoid tab-buildup with a find-alternate-file macro (JavaScript) 2010-09-28T18:16:42-07:00Eric Promislowhttp://code.activestate.com/recipes/users/4166930/http://code.activestate.com/recipes/577412-komodo-avoid-tab-buildup-with-a-find-alternate-fil/ <p style="color: grey"> JavaScript recipe 577412 by <a href="/recipes/users/4166930/">Eric Promislow</a> (<a href="/recipes/tags/find_alternate_file/">find_alternate_file</a>, <a href="/recipes/tags/komodo/">komodo</a>). </p> <p>In Emacs I used to use find-alternate-file all the time to replace the current buffer with a different one, usually one in the same directory. Komodo doesn't provide an off-the-shelf way to do this, and if you can't be bothered to close buffers when you no longer need them, you'll soon suffer from the dreaded tab buildup problem, up there with the heartbreak of browser tab overload. But it's easy to write a macro to avoid this. </p>