Popular recipes by Andrew Dalke http://code.activestate.com/recipes/users/912777/2011-06-23T16:36:38-07:00ActiveState Code Recipeslazy ordered unique elements from an iterator (Python) 2011-06-23T16:36:38-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/577768-lazy-ordered-unique-elements-from-an-iterator/ <p style="color: grey"> Python recipe 577768 by <a href="/recipes/users/912777/">Andrew Dalke</a> (<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/unique/">unique</a>). Revision 2. </p> <p>This implements a "unique" filter. Its input is an iterator of hashable items. It returns an iterator containing only the unique items of the input, in input order. That is, list(unique("cabbage")) produces ["c", "a", "b", "g"]. The implementation is lazy. The function supports the "key" parameter, which provides an alternate form of comparison.</p> <p>(Note: a better version of this is available from the itertools documentation as unique_everseen )</p> resizable ctypes arrays (Python) 2006-03-28T23:03:58-08:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/475199-resizable-ctypes-arrays/ <p style="color: grey"> Python recipe 475199 by <a href="/recipes/users/912777/">Andrew Dalke</a> . </p> <p>a resizeable list-like object using ctypes arrays for the primary data storage</p> arithmetic coder (Python) 2004-10-02T17:54:59-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/306626-arithmetic-coder/ <p style="color: grey"> Python recipe 306626 by <a href="/recipes/users/912777/">Andrew Dalke</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>A simple but slow way of compression using arithmetic coding.</p> list permutation order indices (Python) 2004-10-06T16:31:23-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/306862-list-permutation-order-indices/ <p style="color: grey"> Python recipe 306862 by <a href="/recipes/users/912777/">Andrew Dalke</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Given a list, find the indices used to get the elements from the list in sorted order</p> proleptic Gregorian dates and strftime before 1900 (Python) 2004-10-02T11:25:58-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/306860-proleptic-gregorian-dates-and-strftime-before-1900/ <p style="color: grey"> Python recipe 306860 by <a href="/recipes/users/912777/">Andrew Dalke</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>A strftime implementation that supports proleptic Gregorian dates before 1900</p> another way to read lines backwards (Python) 2004-09-30T14:52:49-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/306569-another-way-to-read-lines-backwards/ <p style="color: grey"> Python recipe 306569 by <a href="/recipes/users/912777/">Andrew Dalke</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Another way to read lines from file backwards from the end to the beginning</p> test if a file or string is text or binary (Python) 2003-01-11T01:13:40-08:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/173220-test-if-a-file-or-string-is-text-or-binary/ <p style="color: grey"> Python recipe 173220 by <a href="/recipes/users/912777/">Andrew Dalke</a> (<a href="/recipes/tags/files/">files</a>). Revision 2. </p> <p>Here's a quick test to see if a file or string contains text or is binary. The difference between text and binary is ill-defined, so this duplicates the definition used by Perl's -T flag, which is: &lt;br/&gt; The first block or so of the file is examined for odd characters such as strange control codes or characters with the high bit set. If too many strange characters (&gt;30%) are found, it's a -B file, otherwise it's a -T file. Also, any file containing null in the first block is considered a binary file.</p> modal wxPython with Twisted for XML-RPC (Python) 2003-06-04T08:22:07-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/203471-modal-wxpython-with-twisted-for-xml-rpc/ <p style="color: grey"> Python recipe 203471 by <a href="/recipes/users/912777/">Andrew Dalke</a> . Revision 2. </p> <p>This expands on Uwe C. Schroeder's recipe titled "Using wxPython with Twisted Python" to show one way to implement a modal progress bar using Twisted to make an XML-RPC call.</p> OrderedMultiDict and UnorderedMultiDict (Python) 2003-01-09T16:13:57-08:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/173072-orderedmultidict-and-unorderedmultidict/ <p style="color: grey"> Python recipe 173072 by <a href="/recipes/users/912777/">Andrew Dalke</a> . </p> <p>This implements two types of dictionary-like objects where there can be more than one entry with the same key. One is OrderedMultiDict, which preserves the order of all entries across all keys. The other is UnorderedMultidict, which only preserves the order of entries for the same key.</p> <p>Download MultiDict.py Example:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import MultiDict &gt;&gt;&gt; od = MultiDict.OrderedMultiDict() &gt;&gt;&gt; od["Name"] = "Andrew"; od["Color"] = "Green" &gt;&gt;&gt; od["Name"] = "Karen"; od["Color"] = "Brown" &gt;&gt;&gt; od["Name"] 'Karen' &gt;&gt;&gt; od.getall("Name") ['Andrew', 'Karen'] &gt;&gt;&gt; for k, v in od.allitems(): ... print "%r == %r", (k, v) ... 'Name' == 'Andrew' 'Color' == 'Green' 'Name' == 'Karen' 'Color' == 'Brown' &gt;&gt;&gt; ud = MultDict.UnorderedMultiDict(od) &gt;&gt;&gt; for k, v in ud.allitems(): ... print "%r == %r", (k, v) ... 'Name' == 'Andrew' 'Name' == 'Karen' 'Color' == 'Green' 'Color' == 'Brown' &gt;&gt;&gt; </code></pre> ReseekFile (Python) 2003-01-09T16:09:13-08:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/173071-reseekfile/ <p style="color: grey"> Python recipe 173071 by <a href="/recipes/users/912777/">Andrew Dalke</a> . </p> <p>Wrap a file handle to allow seeks back to the beginning</p> <p>Sometimes data coming from a socket or other input file handle isn't what it was supposed to be. For example, suppose you are reading from a buggy server which is supposed to return an XML stream but can also return an unformatted error message. (This often happens because the server doesn't handle incorrect input very well.)</p> <p>A ReseekFile helps solve this problem. It is a wrapper to the original input stream but provides a buffer. Read requests to the ReseekFile get forwarded to the input stream, appended to a buffer, then returned to the caller. The buffer contains all the data read so far.</p> <p>The ReseekFile can be told to reseek to the start position. The next read request will come from the buffer, until the buffer has been read, in which case it gets the data from the input stream. This newly read data is also appended to the buffer.</p> <p>When buffering is no longer needed, use the 'nobuffer()' method. This tells the ReseekFile that once it has read from the buffer it should throw the buffer away. After nobuffer is called, the behaviour of 'seek' is no longer defined.</p> <p>For example, suppose you have the server as above which either gives an error message is of the form:</p> <p>&nbsp;&nbsp;ERROR: cannot do that</p> <p>or an XML data stream, starting with "<?xml".</p> <p>&nbsp;&nbsp; infile = urllib2.urlopen("http://somewhere/") &nbsp;&nbsp; infile = ReseekFile.ReseekFile(infile) &nbsp;&nbsp; s = infile.readline() &nbsp;&nbsp; if s.startswith("ERROR:"): &nbsp;&nbsp;&nbsp;&nbsp; raise Exception(s[:-1]) &nbsp;&nbsp; infile.seek(0) &nbsp;&nbsp; infile.nobuffer() # Don't buffer the data &nbsp;&nbsp; ... process the XML from infile ...</p> <p>This module also implements 'prepare_input_source(source)' modeled on xml.sax.saxutils.prepare_input_source. This opens a URL and if the input stream is not already seekable, wraps it in a ReseekFile.</p>