Top-rated recipes tagged "meta:loc=61"http://code.activestate.com/recipes/tags/meta:loc=61/top/2017-04-20T23:34:50-07:00ActiveState Code Recipesiter_except -- a useful variant of __builtin__.iter() (Python) 2010-03-27T02:04:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577155-iter_except-a-useful-variant-of-__builtin__iter/ <p style="color: grey"> Python recipe 577155 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). </p> <p>Variant of iter(func, sentinel) that looks for an exception rather than for a sentinel value. Good for making iterators from of APIs that advance over a data and return an exception when they are done.</p> PEP 367 like super (Python) 2007-05-16T09:57:37-07:00Dmitry Vasilievhttp://code.activestate.com/recipes/users/1571302/http://code.activestate.com/recipes/519645-pep-367-like-super/ <p style="color: grey"> Python recipe 519645 by <a href="/recipes/users/1571302/">Dmitry Vasiliev</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>The code based on <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195</a> but interface is PEP 367 like so you can write just super.base_method() inside a method. See doctests for examples.</p> Persistent environment variables on Windows (Python) 2007-07-24T18:46:47-07:00Gigi Sayfanhttp://code.activestate.com/recipes/users/101538/http://code.activestate.com/recipes/416087-persistent-environment-variables-on-windows/ <p style="color: grey"> Python recipe 416087 by <a href="/recipes/users/101538/">Gigi Sayfan</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 4. </p> <p>This script allows adding/modifying/removing environment variables persistently on Windows. It also allows adding entries to the PATH environment variable.</p> Generalized delegates and proxies (Python) 2003-11-18T18:24:40-08:00Gonçalo Rodrigueshttp://code.activestate.com/recipes/users/140909/http://code.activestate.com/recipes/252151-generalized-delegates-and-proxies/ <p style="color: grey"> Python recipe 252151 by <a href="/recipes/users/140909/">Gonçalo Rodrigues</a> . </p> <p>This is a recipe for new-style class proxies that can also delegate special methods.</p> Implementing class-based callbacks in Python (Python) 2017-04-20T23:34:50-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580788-implementing-class-based-callbacks-in-python/ <p style="color: grey"> Python recipe 580788 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/callbacks/">callbacks</a>, <a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a follow-on to this recently posted recipe:</p> <p>Implementing function-based callbacks in Python: <a href="https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351" rel="nofollow">https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351</a></p> <p>This new recipe shows how to create and use callbacks in Python, using classes with methods, instead of plain functions, as was done in the recipe linked above. All other points such as reasons and benefits for using callbacks, are more or less the same as mentioned in the previous recipe, except that class instances can carry state around, so to that extent, the two approaches are different.</p> <p><a href="https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html</a></p> Get memory usage of Windows processes using GetProcessMemoryInfo (via ctypes) (Python) 2013-04-25T01:26:19-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578513-get-memory-usage-of-windows-processes-using-getpro/ <p style="color: grey"> Python recipe 578513 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/win32/">win32</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>These functions call the Win32 function GetProcessMemoryInfo() using ctypes to get the memory usage of the current process. Works on both 32-bit and 64-bit Windows and Python 2.6+ (including Python 3.x).</p> A generator for an arbitrary number of 'for' loops (Python) 2007-01-30T09:16:34-08:00Colin Gillespiehttp://code.activestate.com/recipes/users/4029798/http://code.activestate.com/recipes/502194-a-generator-for-an-arbitrary-number-of-for-loops/ <p style="color: grey"> Python recipe 502194 by <a href="/recipes/users/4029798/">Colin Gillespie</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Python has a number of nice methods to handle 'for' loops. However, the situation often arises where you have a large number of nested loops. Using this solution reduces the number of loops to one.</p> "Safe" Eval (Python) 2006-03-26T18:13:43-08:00Michael Spencerhttp://code.activestate.com/recipes/users/2106310/http://code.activestate.com/recipes/364469-safe-eval/ <p style="color: grey"> Python recipe 364469 by <a href="/recipes/users/2106310/">Michael Spencer</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>Evaluate constant expressions, including list, dict and tuple using the abstract syntax tree created by compiler.parse. Since compiler does the work, handling arbitratily nested structures is transparent, and the implemenation is very straightforward.</p> ilines -- universal newlines from any data source (Python) 2004-06-23T11:33:52-07:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/286165-ilines-universal-newlines-from-any-data-source/ <p style="color: grey"> Python recipe 286165 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>ilines is a generator that takes an iterable and produces lines of text. The input iterable should produce blocks of bytes (as type str) such as might be produced by reading a file in binary. The output lines are formed by the same rule as the "universal newlines" file mode [f = file(name, 'U')] and are produced "on-line" -- when lines are discovered, they are produced.</p> Case Insensitive Strings (Python) 2003-04-16T12:26:48-07:00Dale Strickland-Clarkhttp://code.activestate.com/recipes/users/1107350/http://code.activestate.com/recipes/194371-case-insensitive-strings/ <p style="color: grey"> Python recipe 194371 by <a href="/recipes/users/1107350/">Dale Strickland-Clark</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>This is a class you use in place of a string and it emulates a string in all practical ways except that comparisons and lookups are case insensitive. All uses of the string for assignments, however, yield the original case.</p> Sort a string using numeric order (Python) 2002-06-25T08:17:18-07:00Chui Teyhttp://code.activestate.com/recipes/users/98139/http://code.activestate.com/recipes/135435-sort-a-string-using-numeric-order/ <p style="color: grey"> Python recipe 135435 by <a href="/recipes/users/98139/">Chui Tey</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>This recipe sorts a list of strings using the numeric order where possible.</p> Convert HTML text to PDF with Beautiful Soup and xtopdf (Python) 2015-01-28T22:32:41-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579015-convert-html-text-to-pdf-with-beautiful-soup-and-x/ <p style="color: grey"> Python recipe 579015 by <a href="/recipes/users/4173351/">Vasudev Ram</a> . </p> <p>This recipe shows how to convert the text in an HTML document to PDF. It uses the Beautiful Soup and xtopdf Python libraries. Beautiful Soup is a library for HTML parsing and content extraction. xtopdf is a library for PDF creation from other formats, including text and many others.</p> Convert HTML text to PDF with Beautiful Soup and xtopdf (Python) 2015-01-28T22:20:53-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579014-convert-html-text-to-pdf-with-beautiful-soup-and-x/ <p style="color: grey"> Python recipe 579014 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reportgeneration/">reportgeneration</a>, <a href="/recipes/tags/reporting/">reporting</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to convert the text in an HTML document to PDF. It uses the Beautiful Soup and xtopdf Python libraries. Beautiful Soup is a library for HTML parsing and content extraction. xtopdf is a library for PDF creation from other formats, including text and many others.</p> Highly branched Trees (Python) 2014-05-14T19:16:04-07:00Chris Eckerhttp://code.activestate.com/recipes/users/4180203/http://code.activestate.com/recipes/578879-highly-branched-trees/ <p style="color: grey"> Python recipe 578879 by <a href="/recipes/users/4180203/">Chris Ecker</a> (<a href="/recipes/tags/tree/">tree</a>). Revision 2. </p> <p>Trees are very common data structures and are usually considered to be very efficient. However, this is only true if the tree is balanced, meaning that all branches have roughly the same number of nodes. </p> <p>There are good balancing trees, such as rb-trees or avl-trees. Unfortunately they are quite difficult to implement. An alternative tree structure is the highly branched b-tree (<a href="https://en.wikipedia.org/wiki/B-tree" rel="nofollow">https://en.wikipedia.org/wiki/B-tree</a>). In the c language, binary trees are preferable in most cases. However, in python things are different. This recipe shows how simple it is to implement a b-tree in python. The example is a sorted dict.</p> Linux ioctl numbers in Python (Python) 2012-07-25T12:25:26-07:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/578225-linux-ioctl-numbers-in-python/ <p style="color: grey"> Python recipe 578225 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/fcntl/">fcntl</a>, <a href="/recipes/tags/ioctl/">ioctl</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Copy _IOR/_IOWR constants from kernel headers and use with fcntl.iocl()</p> Managment command to deploy a Django project (Python) 2011-06-21T22:10:31-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577767-managment-command-to-deploy-a-django-project/ <p style="color: grey"> Python recipe 577767 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/django/">django</a>). </p> <p>This recipe shows a management command for django project that enables automatic synchronization between the local and remote project. Of course, the deploy exclude to overwrite the settings.py located in the remote server.</p> <p>This is a clean way to deploy your own project without calling external commands, although this command needs rsync to work.</p> For AMIGA-Heads Only. PAR: As A VOLUME In READ Mode Only. (Python) 2011-06-12T16:34:12-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/577750-for-amiga-heads-only-par-as-a-volume-in-read-mode-/ <p style="color: grey"> Python recipe 577750 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/amiga/">amiga</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/parallel/">parallel</a>, <a href="/recipes/tags/parallel_port/">parallel_port</a>, <a href="/recipes/tags/port/">port</a>, <a href="/recipes/tags/read/">read</a>). </p> <p>PAR: as a VOLUME in READ mode using Python 1.4 onwards on Classic AMIGAs...</p> <p>Many years ago Irmen de Jong ported Python to the Classic AMIGA range of computers, (many thanks Irmen for your time in doing so). The versions were at least 1.4.x to 2.0.1 and now someone else has included version 2.4.6.</p> <p>This gives we lowly users of the AMIGA at least a chance to see and use Python in some guise or another. This code shows how to access the AMIGA parallel port for 8 bit READ only. This is so that ADCs could be attached to the port, read by Python code EASILY and utilised as a Data Logger/Transient Recorder, as just one example.</p> <p>There needs to be a single HW WIRE link only from the 23 way video port to the 25 way parallel port for this to work. See the archive......</p> <p><a href="http://aminet.net/package/docs/hard/PAR_READ" rel="nofollow">http://aminet.net/package/docs/hard/PAR_READ</a></p> <p>......on how to set about this extremely simple task.</p> <p>NO knowledge of the parallel port programming is needed at all to grab 8 bit data from it using Python and other languages; (ARexx is used in the archive above).</p> <p>There is a flaw, NOTE:- NOT A BUG!, in the Python code but for this DEMO it is ignored. ;o)</p> <p>I'll let all you big guns work out what it is; you will need a good working knowledge of the Classic AMIGA.</p> <p>Enjoy finding simple solutions to often very difficult problems. ;o)</p> <p>This code is Public Domain and you may do with it as you please.</p> <p>Bazza...</p> Balance / Refunds of dues between friends (Python) 2011-04-26T16:11:15-07:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/577677-balance-refunds-of-dues-between-friends/ <p style="color: grey"> Python recipe 577677 by <a href="/recipes/users/4135673/">Raphaël Jolivet</a> (<a href="/recipes/tags/balance/">balance</a>, <a href="/recipes/tags/due/">due</a>, <a href="/recipes/tags/refund/">refund</a>, <a href="/recipes/tags/simplex/">simplex</a>). </p> <p>Here is a handy script that uses the <em>simplex</em> algorithm to compute an optimum list of refunds (for example, after a trip with shared expenses with friends).</p> <p>It minimizes the number of transactions (refunds) required to reach the balance.</p> query custom answers (Python) 2010-03-09T17:57:22-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577096-query-custom-answers/ <p style="color: grey"> Python recipe 577096 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/query/">query</a>). </p> <p>Ask the user a question using raw_input() and looking something like this:</p> <pre class="prettyprint"><code>Frob nots the zids? [Yes (default), No, quit] _ ...validate... </code></pre> <p>See also: <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> (query yes/no), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p> wsgi SPNEGO middleware (Python) 2009-12-28T09:36:18-08:00Sven Ludwighttp://code.activestate.com/recipes/users/4172687/http://code.activestate.com/recipes/576992-wsgi-spnego-middleware/ <p style="color: grey"> Python recipe 576992 by <a href="/recipes/users/4172687/">Sven Ludwig</a> (<a href="/recipes/tags/kerberos/">kerberos</a>, <a href="/recipes/tags/spnego/">spnego</a>, <a href="/recipes/tags/wsgi/">wsgi</a>, <a href="/recipes/tags/wsgi_middleware/">wsgi_middleware</a>). </p> <p>Kerberos Single Sign On wsgi middleware</p>