Popular recipes tagged "meta:loc=61"http://code.activestate.com/recipes/tags/meta:loc=61/popular/2017-04-20T23:34:50-07:00ActiveState Code RecipesImplementing 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> 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> 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> 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> 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> 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> Password Card Generator (Python) 2012-01-26T02:37:30-08:00userendhttp://code.activestate.com/recipes/users/4179007/http://code.activestate.com/recipes/578027-password-card-generator/ <p style="color: grey"> Python recipe 578027 by <a href="/recipes/users/4179007/">userend</a> (<a href="/recipes/tags/card/">card</a>, <a href="/recipes/tags/create/">create</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/passwd/">passwd</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/secret/">secret</a>). </p> <p>This project was inspired by the <a href="http://passwordcard.org" rel="nofollow">passwordcard.org</a> website. I wanted to see if I could do the same thing using Python. Select a password using this convenient card that you carry with you. </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> iter_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> 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> How to detect the Linux distribution from an init.d script (Bash) 2010-03-16T13:24:22-07:00Gui Rhttp://code.activestate.com/recipes/users/4166241/http://code.activestate.com/recipes/576676-how-to-detect-the-linux-distribution-from-an-initd/ <p style="color: grey"> Bash recipe 576676 by <a href="/recipes/users/4166241/">Gui R</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/redhat/">redhat</a>). Revision 2. </p> <p>There is no trivial way to know what Linux you are running. Red Hat, SuSE, etc., each distribution has a different way to tell what version is installed.</p> make dictproxy object via ctypes.pythonapi and type() (Python) 2008-10-26T19:35:13-07:00Ikkei Shimomurahttp://code.activestate.com/recipes/users/987178/http://code.activestate.com/recipes/576540-make-dictproxy-object-via-ctypespythonapi-and-type/ <p style="color: grey"> Python recipe 576540 by <a href="/recipes/users/987178/">Ikkei Shimomura</a> . Revision 2. </p> <p>Python internal has type 'dictproxy'.</p> <p>You can get the dictproxy type object in Python.</p> <p>from types import DictProxyType</p> <p>any new-style classes has a __dict__ attribute, it's the dictproxy object. but, the type constructor disallow you to make the instance.</p> <p>DictProxyType({}) # TypeError</p> <p>This recipe explains how to make dictproxy object via Python API ... and type() more easy way</p> Find cyclical references (Python) 2007-06-29T08:54:52-07:00Michael Droettboomhttp://code.activestate.com/recipes/users/2323856/http://code.activestate.com/recipes/523004-find-cyclical-references/ <p style="color: grey"> Python recipe 523004 by <a href="/recipes/users/2323856/">Michael Droettboom</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>This recipe helps find cyclical references in Python code to a] optimize so the garbage collector doesn't have to work as hard, and b] deal with uncollectable objects, such as those with a __del__ method, or extension objects that don't participate in garbage collection.</p> Upgradable Pickles (Python) 2007-06-16T16:17:06-07:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/521901-upgradable-pickles/ <p style="color: grey"> Python recipe 521901 by <a href="/recipes/users/1523109/">Justin Shaw</a> . Revision 5. </p> <p>Stale Pickles ... New Classes. Pickles are by far the easiest way to achieve persistent objects in Python. A problem arises however, when a class is modified between the time when an object is pickled and when it is un-pickled. When a stale pickle is extracted, the old data is coupled with the new class code. This is not a problem as long as no new data attributes are required. For instance, a new method that relies only on old data may be added and still work with the old pickle.</p> <p>Of course, if attributes are added, the old pickle files will no longer function correctly with the new class definition.</p> <p>This recipe provides a framework for writing upgradable classes that support backward compatibility with stale pickles.</p> Find cyclical references (Python) 2007-06-29T08:51:26-07:00Michael Droettboomhttp://code.activestate.com/recipes/users/2323856/http://code.activestate.com/recipes/523003-find-cyclical-references/ <p style="color: grey"> Python recipe 523003 by <a href="/recipes/users/2323856/">Michael Droettboom</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>This recipe helps find cyclical references in Python code to a] optimize so the garbage collector doesn't have to work as hard, and b] deal with uncollectable objects, such as those with a __del__ method, or extension objects that don't participate in garbage collection.</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> 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>