Latest recipes by Wai Yip Tung http://code.activestate.com/recipes/users/2382677/new/2011-04-25T03:41:08-07:00ActiveState Code RecipesObject snoop - experiment with Python special methods (Python) 2010-09-05T17:54:50-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/577383-object-snoop-experiment-with-python-special-method/ <p style="color: grey"> Python recipe 577383 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/metaprogramming/">metaprogramming</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/object/">object</a>). </p> <p>In Python, classes can define their own behavior with respect to language operators. For example, if a class defines __getitem__(), then x[i], where x is an instance of the clas, will be execute by a call to x.__getitem__(i).</p> <p>While Python has an extensive documentation on the special methods, reading a specification may not be the best way to reveal the intricate details. <strong>object_snoop</strong> allows user to observe how Python expressions and statements are translated into special method calls. object_snoop defines most special methods. It simple print a trace and returns a fixed but sensible result. Users are invited to build complex expressions to experiment how Python special methods work.</p> A multithreaded, concurrent version of map() (Python) 2010-08-16T23:04:48-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/577360-a-multithreaded-concurrent-version-of-map/ <p style="color: grey"> Python recipe 577360 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>map() applies a function to a list of data sequentially. This is a variation to map that execute each function call concurrently in a thread.</p> Update stock quote using Yahoo! Finance web services (Python) 2008-06-22T18:33:50-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/573471-update-stock-quote-using-yahoo-finance-web-service/ <p style="color: grey"> Python recipe 573471 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/excel/">excel</a>, <a href="/recipes/tags/math/">math</a>). Revision 2. </p> <p>This script update the stock quote on your spreadsheet by fetching the latest quote from Yahoo!Finance web services. It uses the pywin32 library to update the cells on an Excel spreadsheet.</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> Finding the percentile of the values (Python) 2011-04-25T03:41:08-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/511478-finding-the-percentile-of-the-values/ <p style="color: grey"> Python recipe 511478 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>This function find the percentile of a list of values. Note that the list must be sorted already.</p> Design mini-lanugage for data input (Python) 2006-03-20T22:24:06-08:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/475158-design-mini-lanugage-for-data-input/ <p style="color: grey"> Python recipe 475158 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Many programs need a set of initial data. For ease of use and flexibility, design a mini-language for your input data. Use Python's superb text handling capability to parse and build the data structure from the input text.</p> Guaranteed conversion to unicode or byte string (Python) 2006-01-23T22:14:48-08:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/466341-guaranteed-conversion-to-unicode-or-byte-string/ <p style="color: grey"> Python recipe 466341 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Python's built in function str() and unicode() return a string representation of the object in byte string and unicode string respectively. This enhanced version of str() and unicode() can be used as handy functions to convert between byte string and unicode. This is especially useful in debugging when mixup of the string types is suspected.</p> Efficient character escapes decoding (Python) 2006-01-14T01:55:59-08:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/466293-efficient-character-escapes-decoding/ <p style="color: grey"> Python recipe 466293 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>You have some string input with some specical characters escaped using syntax rules resemble Python's. For example the 2 characters '\n' stands for the control character LF. You need to decode these control characters efficiently. Use Python's builtin codecs to decode them efficiently.</p> look ahead one item during iteration (Python) 2005-04-14T20:12:32-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/409825-look-ahead-one-item-during-iteration/ <p style="color: grey"> Python recipe 409825 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Iteration is a fundamental Python idiom. It is simple and effective.</p> <p>for n in iterable: # do something with n</p> <p>But there are also cases when you might want to look ahead one item during iteration. For example, with a sorted list, one can eliminate duplicated items by dropping those equals to the next item. This generator based recipe that enumerate an item and its next in a list. For example,</p> <pre class="prettyprint"><code>&gt;&gt;&gt; for i,j in pairwise([1,2,3]): print i,j ... 1 2 2 3 3 None </code></pre>