Popular recipes by Shao-chuan Wang http://code.activestate.com/recipes/users/4168519/2013-08-09T00:17:00-07:00ActiveState Code RecipesWindows directory walk using ctypes (Python) 2013-08-09T00:17:00-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/578629-windows-directory-walk-using-ctypes/ <p style="color: grey"> Python recipe 578629 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/system/">system</a>). </p> <p>Windows shell/explorer has a limit size of full path, but both NTFS and ReFS can support full path longer than the limit; this is making os.walk on Windows bad if files are in deeply nested folders, and therefore this recipe.</p> Converts doc files into text files on Windows platform (Python) 2012-05-08T06:39:43-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/578121-converts-doc-files-into-text-files-on-windows-plat/ <p style="color: grey"> Python recipe 578121 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/pythoncom/">pythoncom</a>, <a href="/recipes/tags/word/">word</a>). </p> <p><strong>READ BEFORE YOU USE THE CODE</strong></p> <p><strong>Requirements</strong></p> <ol> <li>Windows platform</li> <li>Python 2.7</li> <li>pywin32, <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">http://sourceforge.net/projects/pywin32/</a></li> <li>Word application installed on running machine</li> </ol> Sync your facebook friends list to your address book on Mac/iPhone/iPad (Python) 2011-12-17T09:27:02-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577986-sync-your-facebook-friends-list-to-your-address-bo/ <p style="color: grey"> Python recipe 577986 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/address/">address</a>, <a href="/recipes/tags/book/">book</a>, <a href="/recipes/tags/facebook/">facebook</a>, <a href="/recipes/tags/sync/">sync</a>). </p> <p><strong>READ BEFORE YOU USE THE CODE*</strong></p> <p><strong>Requirement</strong></p> <ol> <li>Mac OS with python obj-c wrapper built-in. Typically, Mac OS Snow leopard, or Lion comes with python wrapper of objc.</li> <li>If you want to also sync to your iphone, simply enable iCloud feature provided by apple, and run this script on a macbook with iCloud enabled.</li> <li>Need fbconsole module, which can be downloaded at <a href="https://github.com/facebook/fbconsole" rel="nofollow">https://github.com/facebook/fbconsole</a></li> </ol> <p>This script will download the profile pictures, first name, last name of your friends, and insert them into your address book if it does not exist.</p> Permutation and combination using recursive generator (Python) 2011-10-04T05:44:19-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577890-permutation-and-combination-using-recursive-genera/ <p style="color: grey"> Python recipe 577890 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/combinatorics/">combinatorics</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/permutation/">permutation</a>, <a href="/recipes/tags/recursion/">recursion</a>). Revision 2. </p> <p>This recipes demonstrates how to use recursive generator to implement permutation and combination.</p> Implementation of Knuth–Morris–Pratt algorithm (C) 2011-10-15T00:33:41-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577908-implementation-of-knuthmorrispratt-algorithm/ <p style="color: grey"> C recipe 577908 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/kmp/">kmp</a>, <a href="/recipes/tags/matching/">matching</a>, <a href="/recipes/tags/string/">string</a>). Revision 2. </p> <p>Knuth–Morris–Pratt (KMP) is a linear time string matching algorithm.</p> <p><a href="http://en.wikipedia.org/wiki/Knuth%25E2%2580%2593Morris%25E2%2580%2593Pratt_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm</a></p> <p>This recipe gives a C-version implementation.</p> Dijkstra shortest path implementation (Python) 2011-10-05T09:06:50-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577892-dijkstra-shortest-path-implementation/ <p style="color: grey"> Python recipe 577892 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/dijkstra/">dijkstra</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/shortest/">shortest</a>). </p> <p>This code snippet is the implementation of Dijkstra's algorithm.</p> Conversion of PIL Image and numpy array (Python) 2011-03-05T03:47:56-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/ <p style="color: grey"> Python recipe 577591 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/pil/">pil</a>). Revision 2. </p> <p>The utility function that converts PIL image to numpy array and vice versa.</p> Calculation of total number of lines (Bash) 2011-05-23T10:55:45-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577717-calculation-of-total-number-of-lines/ <p style="color: grey"> Bash recipe 577717 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/line/">line</a>). </p> <p>This bash script enables you to calculate the total number of lines of source code.</p> <p>Example usage: 1. calculate total number of lines of java source files. $ line.sh java</p> <ol> <li>calculate total number of lines of python source files. $ line.sh py</li> </ol> Bit operation (C) 2011-02-27T05:21:35-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577589-bit-operation/ <p style="color: grey"> C recipe 577589 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/bit/">bit</a>, <a href="/recipes/tags/operation/">operation</a>). </p> <p>A bit operation (could be very long).</p> Vectorize Operation (Python) 2011-02-05T05:36:19-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577563-vectorize-operation/ <p style="color: grey"> Python recipe 577563 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/vector/">vector</a>). Revision 2. </p> <p>This python submodule enable you to do the vectorize calculation, such as vectorize add, and etc.</p> Command Threads (Python) 2010-12-25T23:23:49-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577505-command-threads/ <p style="color: grey"> Python recipe 577505 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/threading/">threading</a>). Revision 4. </p> <p>A thread that accepts callable objects as "commands" or "jobs" to execute.</p> A Profiling Tool (Python) 2011-03-04T05:41:04-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577323-a-profiling-tool/ <p style="color: grey"> Python recipe 577323 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/profiling/">profiling</a>, <a href="/recipes/tags/timing/">timing</a>). Revision 2. </p> <p>This is a decorator for doing the profiling of a function.</p> Minimum Cut Solver (Python) 2009-09-19T07:16:58-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576907-minimum-cut-solver/ <p style="color: grey"> Python recipe 576907 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/ford_fulkerson/">ford_fulkerson</a>, <a href="/recipes/tags/mininum_cut/">mininum_cut</a>, <a href="/recipes/tags/min_cut/">min_cut</a>, <a href="/recipes/tags/stoer_wagner/">stoer_wagner</a>). </p> <p>A Minimum Cut Solver</p> <p>This python script is for solving the ACM problem Q2914: Minimum Cut. <a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=2914" rel="nofollow">http://acm.pku.edu.cn/JudgeOnline/problem?id=2914</a></p> <p>Instead of using Ford-Fulkerson method, I use Stoer and Wagner's Min cut Algorithm. <a href="http://www.cs.dartmouth.edu/%7Eac/Teach/CS105-Winter05/Handouts/stoerwagner-mincut.pdf" rel="nofollow">http://www.cs.dartmouth.edu/~ac/Teach/CS105-Winter05/Handouts/stoerwagner-mincut.pdf</a></p> <p>However I also include the max flow method (from wiki) for benchmark. The code can be found at: <a href="http://en.wikipedia.org/wiki/Ford-Fulkerson_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Ford-Fulkerson_algorithm</a></p> Shutdown your PC by using ctypes (Win32 Platform) (Python) 2009-08-03T10:05:38-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576865-shutdown-your-pc-by-using-ctypes-win32-platform/ <p style="color: grey"> Python recipe 576865 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/reboot/">reboot</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/system_programming/">system_programming</a>, <a href="/recipes/tags/win32/">win32</a>). </p> <p>It is true that by using win32 extension python modules, such as win32api, win32con, and win32security, we can easily shutdown the computer with a few steps. However, sometimes your python's runtime environment does not provide win32com module (because it is not a build-in module), we may have to shutdown the pc on our own.</p> <p>By using ctypes, we are still able to shutdown or reboot the PC easily.</p> A Tree Finder (Python) 2009-09-26T09:36:58-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576912-a-tree-finder/ <p style="color: grey"> Python recipe 576912 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/tree/">tree</a>). </p> <p>This python script is for solving the ACM problem Q1308: Is It A Tree? <a href="http://acm.pku.edu.cn/JudgeOnline/problem?id=1308" rel="nofollow">http://acm.pku.edu.cn/JudgeOnline/problem?id=1308</a></p> Longest common subsequence problem solver (Python) 2009-08-06T06:36:56-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576869-longest-common-subsequence-problem-solver/ <p style="color: grey"> Python recipe 576869 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/lcs/">lcs</a>, <a href="/recipes/tags/longest_common_subsequence/">longest_common_subsequence</a>). Revision 2. </p> <p>Longest common subsequence problem is a good example of dynamic programming, and also has its significance in biological applications.</p> <p>For more information about LCS, please see: <a href="http://en.wikipedia.org/wiki/Longest_common_subsequence_problem" rel="nofollow">http://en.wikipedia.org/wiki/Longest_common_subsequence_problem</a></p> <p>Also, here, I use a 'cached' decorator to keep core algorithm neat. You can see how great the decorator could be. :)</p> <p>Also note that, this recipe is just a demonstration of LCS and the usage of a python decorator. However, the memory is not used very efficiently. If the problem is very large-scaled, it may lead to stack overflow or memory error. </p> <p>So, do not use this recipe to deal with large-scaled problems. ;)</p> An unofficial interface of Yahoo's Chinese segmentation. (Python) 2009-07-27T21:59:20-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576859-an-unofficial-interface-of-yahoos-chinese-segmenta/ <p style="color: grey"> Python recipe 576859 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/chinese/">chinese</a>, <a href="/recipes/tags/segmentation/">segmentation</a>, <a href="/recipes/tags/yahoo/">yahoo</a>). </p> <p>An unofficial interface of Yahoo's Chinese segmentation.</p> <ul> <li>Before use it, you MUST specify your APPID in the code. * </li> </ul> <p>Yahoo's api documents: <a href="http://tw.developer.yahoo.com/cas/" rel="nofollow">http://tw.developer.yahoo.com/cas/</a></p> Interval Execute a function (Python) 2009-04-24T08:58:27-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576726-interval-execute-a-function/ <p style="color: grey"> Python recipe 576726 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/timer/">timer</a>). Revision 4. </p> <p>Here I just want to keep it simple. If you want to execute a function every n second, this function should be competent. However, please note that the task is actually executed in another thread. </p> Huffman coding, Encoder/Deconder (Python) 2009-01-04T04:11:37-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576603-huffman-coding-encoderdeconder/ <p style="color: grey"> Python recipe 576603 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/compression/">compression</a>, <a href="/recipes/tags/decompression/">decompression</a>, <a href="/recipes/tags/huffman_code/">huffman_code</a>). Revision 2. </p> <p>Please refer to wikipedia: <a href="http://en.wikipedia.org/wiki/Huffman_coding" rel="nofollow">http://en.wikipedia.org/wiki/Huffman_coding</a></p> <p>Huffman coding is an entropy encoding algorithm used for lossless data compression. The term refers to the use of a variable-length code table for encoding a source symbol (such as a character in a file) where the variable-length code table has been derived in a particular way based on the estimated probability of occurrence for each possible value of the source symbol. It was developed by David A. Huffman while he was a Ph.D. student at MIT, and published in the 1952 paper "A Method for the Construction of Minimum-Redundancy Codes".</p> Control CPU Usage by using ctypes (Win32 Platform) (Python) 2009-01-26T09:32:34-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576634-control-cpu-usage-by-using-ctypes-win32-platform/ <p style="color: grey"> Python recipe 576634 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/cpu_usage/">cpu_usage</a>, <a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/system_programming/">system_programming</a>, <a href="/recipes/tags/win32/">win32</a>). Revision 3. </p> <p>This program will make your cpu work at a given cpu usage. It should be also able to work on machines with multi-processors. The program has been tested on Windows xp sp2 with python of version 2.5.4.</p> <p>The implementation is based on the fact that it will adjust the ratio of being busy over being idle in the main process to approach the target cpu usage rate. </p>