Most viewed recipes tagged "meta:loc=61"http://code.activestate.com/recipes/tags/meta:loc=61/views/2017-04-20T23:34:50-07:00ActiveState Code RecipesPersistent 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>
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>
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>
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>
"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>
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>
Migrate your Windows login scripts to Python (Python)
2005-06-27T07:26:39-07:00Fadly Tabranihttp://code.activestate.com/recipes/users/2143621/http://code.activestate.com/recipes/426742-migrate-your-windows-login-scripts-to-python/
<p style="color: grey">
Python
recipe 426742
by <a href="/recipes/users/2143621/">Fadly Tabrani</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>The following is a guide for Windows administrators who would like to use Python for their login scripts. Once, you've found Python, you'll never go back to VbScript or the dreaded batch files.</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>
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>
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>
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>
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>
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>
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>
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>
z_string.py (Python)
2006-07-07T06:02:57-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/496871-z_stringpy/
<p style="color: grey">
Python
recipe 496871
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This module provides several functions that can
encode and decode a string. It also has a
function called partition for designing faster
algorithms. For sample usage of this module,
z_service.py provides example code.</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>
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>
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>
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>