Popular recipes tagged "meta:score=7"http://code.activestate.com/recipes/tags/meta:score=7/2012-11-23T23:57:31-08:00ActiveState Code RecipesCompute Memory footprint of an object and its contents (Python) 2012-11-23T23:57:31-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577504-compute-memory-footprint-of-an-object-and-its-cont/ <p style="color: grey"> Python recipe 577504 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>). Revision 3. </p> <p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p> How to use super() effectively (Python) 2011-06-01T19:17:58-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577720-how-to-use-super-effectively/ <p style="color: grey"> Python recipe 577720 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>). Revision 8. </p> <p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p> Persistent dict with multiple standard file formats (Python) 2011-09-06T20:01:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/ <p style="color: grey"> Python recipe 576642 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/dbm/">dbm</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/persistent/">persistent</a>, <a href="/recipes/tags/shelve/">shelve</a>). Revision 10. </p> <p>dbdict: a dbm based on a dict subclass.</p> <p>On open, loads full file into memory. On close, writes full dict to disk (atomically). Supported output file formats: csv, json, and pickle. Input file format automatically discovered.</p> <p>Usable by the shelve module for fast access.</p> Technique for cyclical iteration (Python) 2009-12-03T23:08:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/ <p style="color: grey"> Python recipe 576961 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/concurrency_model/">concurrency_model</a>, <a href="/recipes/tags/cyclic_iterator/">cyclic_iterator</a>). Revision 7. </p> <p>Solution to the Hamming Number problem. Demonstrates a lazy evaluation evaluation technique using itertools.tee() to feed an iterator into itself. This is a common technique with Haskell. The deferred_output() function is the key technique for implementing a forward reference to the output of the stream.</p> Caller and Callee (Python) 2010-03-26T20:04:11-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/576925-caller-and-callee/ <p style="color: grey"> Python recipe 576925 by <a href="/recipes/users/4172244/">Michael Grünewald</a> (<a href="/recipes/tags/callee/">callee</a>, <a href="/recipes/tags/caller/">caller</a>, <a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>A recipe to find out which function is the caller of the current function. </p> <p>The <code>caller</code> function can be helpful for debugging &mdash; if there is no real debugger available. In terms of software engineering (loose coupling etc.) this should not be used in production code though.</p> Total ordering class decorator (Python) 2010-09-07T05:47:25-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576685-total-ordering-class-decorator/ <p style="color: grey"> Python recipe 576685 by <a href="/recipes/users/178123/">Raymond Hettinger</a> . Revision 11. </p> <p>Given a class defining one or more ordering methods, this decorator supplies the rest. This simplifies and speeds-up the approach taken in <a href="http://code.activestate.com/recipes/576529/">recipe 576529</a>.</p> Binary floating point summation accurate to full precision (Python) 2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/ <p style="color: grey"> Python recipe 393090 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p> Tail Call Optimization Decorator (Python) 2006-02-26T15:02:54-08:00Crutcher Dunnavanthttp://code.activestate.com/recipes/users/2792865/http://code.activestate.com/recipes/474088-tail-call-optimization-decorator/ <p style="color: grey"> Python recipe 474088 by <a href="/recipes/users/2792865/">Crutcher Dunnavant</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>This decorator implements tail call optimization through stack introspection.</p> Lightweight Unittester (Python) 2009-04-07T19:47:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/572194-lightweight-unittester/ <p style="color: grey"> Python recipe 572194 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>Inspired by py.test, the unittester is a single, simple function that is easily customized. The main virtue is that tests are much clearer than with the unittest.py.</p> XML to Python data structure (Python) 2009-05-31T16:10:38-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/534109-xml-to-python-data-structure/ <p style="color: grey"> Python recipe 534109 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 8. </p> <p>This simple method construct Python data structure from XML in one simple step. Data is accessed using the Pythonic "object.attribute" notation. See the discussion below for usage examples.</p> getch()-like unbuffered character reading from stdin on both Windows and Unix (Python) 2003-01-07T02:31:32-08:00Danny Yoohttp://code.activestate.com/recipes/users/98032/http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/ <p style="color: grey"> Python recipe 134892 by <a href="/recipes/users/98032/">Danny Yoo</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>A small utility class to read single characters from standard input, on both Windows and UNIX systems. It provides a getch() function-like instance.</p> Simple HTTP server supporting SSL secure communications (Python) 2008-08-02T16:04:56-07:00Sebastien Martinihttp://code.activestate.com/recipes/users/2637141/http://code.activestate.com/recipes/442473-simple-http-server-supporting-ssl-secure-communica/ <p style="color: grey"> Python recipe 442473 by <a href="/recipes/users/2637141/">Sebastien Martini</a> (<a href="/recipes/tags/https/">https</a>, <a href="/recipes/tags/openssl/">openssl</a>, <a href="/recipes/tags/ssl/">ssl</a>, <a href="/recipes/tags/web/">web</a>). Revision 8. </p> <p>This recipe describes how to set up a simple HTTP server supporting SSL secure communications. It extends the SimpleHTTPServer standard module to support the SSL protocol. With this recipe, only the server is authenticated while the client remains unauthenticated (i.e. the server will not request a client certificate). Thus, the client (typically the browser) will be able to verify the server identity and secure its communications with the server.</p> <p>This recipe requires you already know the basis of SSL and how to set up <a href="http://www.openssl.org">OpenSSL</a>. This recipe is mostly derived from the examples provided with the <a href="http://pyopenssl.sourceforge.net">pyOpenSSL</a> package.</p> <h5>In order to apply this recipe, follow these few steps:</h5> <ol> <li>Install the OpenSSL package in order to generate key and certificate. Note: you probably already have this package installed if you are under Linux, or *BSD.</li> <li>Install the pyOpenSSL package, it is an OpenSSL library binding. You'll need to import this module for accessing OpenSSL's components.</li> <li>Generate a self-signed certificate compounded of a certificate and a private key for your server with the following command (it outputs them both in a single file named server.pem): <code>openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes</code></li> <li>Assuming you saved this recipe in SimpleSecureHTTPServer.py, start the server (with the appropriate rights): <code>python SimpleSecureHTTPServer.py</code></li> <li>Finally, browse to <a href="https://localhost">https://localhost</a>, or <a href="https://localhost:port" rel="nofollow">https://localhost:port</a> if your server listens a different port than 443.</li> </ol> PyDbLite, a small in-memory database engine (Python) 2011-07-18T19:36:04-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/496770-pydblite-a-small-in-memory-database-engine/ <p style="color: grey"> Python recipe 496770 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/database/">database</a>). Revision 4. </p> <p>A small, fast, in-memory database management program</p> <p>The database object supports the iterator protocol, so that requests can be expressed with list comprehensions or generator expressions instead of SQL. The equivalent of :</p> <pre class="prettyprint"><code>cursor.execute("SELECT name FROM table WHERE age=30") rows = cursor.fetchall() </code></pre> <p>is :</p> <pre class="prettyprint"><code>rows = table(age=30) </code></pre> <p>The module stores data in a cPickled file. Records are indexed by a unique record identifier, that can be used for direct access. Since operations are processed in memory they are extremely fast, nearly as fast as SQLite in the few tests I made, and MUCH faster than other pure-Python modules such as Gadfly or KirbyBase. An index can be created on a field to even speed up selections</p> <p>Concurrency control is supported by a version number set for each record</p> <p>Complete documentation is <a href="http://www.pydblite.net">here</a></p> Atomically execute a block of Python statements (Python) 2011-07-27T05:04:42-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/475125-atomically-execute-a-block-of-python-statements/ <p style="color: grey"> Python recipe 475125 by <a href="/recipes/users/178123/">Raymond Hettinger</a> . Revision 3. </p> <p>&lt;removed&gt;</p> Language detection using character trigrams (Python) 2004-11-07T01:08:26-08:00Douglas Bagnallhttp://code.activestate.com/recipes/users/1629020/http://code.activestate.com/recipes/326576-language-detection-using-character-trigrams/ <p style="color: grey"> Python recipe 326576 by <a href="/recipes/users/1629020/">Douglas Bagnall</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>The Trigram class can be used to compare blocks of text based on their local structure, which is a good indicator of the language used. It could also be used within a language to discover and compare the characteristic footprints of various registers or authors. As all n-gram implementations should, it has a method to make up nonsense words.</p> Lazy property evaluation (Python) 2005-01-18T13:59:28-08:00Scott David Danielshttp://code.activestate.com/recipes/users/98131/http://code.activestate.com/recipes/363602-lazy-property-evaluation/ <p style="color: grey"> Python recipe 363602 by <a href="/recipes/users/98131/">Scott David Daniels</a> . </p> <p>Lazy properties can be easily built in Python 2.4 -- properties whose value may require some effort to calculate, but whose values remain constant once calculated. This recipe uses decorators to implements such properties.</p> Parsing the command line (Python) 2004-04-18T08:15:21-07:00Michele Simionatohttp://code.activestate.com/recipes/users/1122360/http://code.activestate.com/recipes/278844-parsing-the-command-line/ <p style="color: grey"> Python recipe 278844 by <a href="/recipes/users/1122360/">Michele Simionato</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>The module optparse was a great addition to Python 2.3, since it is much more powerful and easier to use than getopt. Using optparse, writing command-line tools is a breeze. However, the power of optparse comes together with a certain verbosity. This recipe allows to use optparse with a minimum of boilerplate, trading flexibility for easy of use. Still, it covers 95% of my common needs, so I think it may be useful to others.</p> The Secret Name of List Comprehensions (Python) 2003-06-09T20:41:25-07:00Chris Perkinshttp://code.activestate.com/recipes/users/113957/http://code.activestate.com/recipes/204297-the-secret-name-of-list-comprehensions/ <p style="color: grey"> Python recipe 204297 by <a href="/recipes/users/113957/">Chris Perkins</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 3. </p> <p>Sometimes you want to have a list comprehension refer to itself, but you can't because it isn't bound to a name until after it is fully constructed. However, the interpreter creates a secret name that only exists while the list is being built. That name is (usually) "_[1]", and it refers to the bound method "append" of the list. This is our back door to get at the list object itself.</p> Metaclass for Interface Checking (Python) 2003-06-12T02:00:39-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/204349-metaclass-for-interface-checking/ <p style="color: grey"> Python recipe 204349 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>Checks a class definition for required attributes &lt;br&gt; To use it, add two lines to your class, __metaclass__=InterfaceChecker and __implements__=[InterfaceName]. The example below generates the following error message: &lt;br&gt; InterfaceOmission: ['__delitem__'] &lt;br&gt; Verifying interfaces for an object becomes trivial. For instance, if you need to validate that variable 'x' implements a minimal sequence interface, verify that: &lt;br&gt; MinimalSequence in x.__implements__</p> Generator Attributes (Python) 2002-12-09T08:44:56-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/164044-generator-attributes/ <p style="color: grey"> Python recipe 164044 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/threads/">threads</a>). Revision 3. </p> <p>Function to enable attribute access for generator instances. Simplifies data sharing for advanced uses of generators and provides much of the functionality sought by PEP 288. &lt;br&gt; Most uses of generators have no need for data sharing. This recipe is for the few tough cases which can be written more elegantly when attribute access is enabled.</p>