Popular Python recipes tagged "meta:requires=stringio"http://code.activestate.com/recipes/langs/python/tags/meta:requires=stringio/2013-09-25T01:54:53-07:00ActiveState Code RecipesWrap a a file-like object in another that calls a user callback whenever read() is called on it. (Python) 2013-09-25T01:54:53-07:00Martin Millerhttp://code.activestate.com/recipes/users/155538/http://code.activestate.com/recipes/578674-wrap-a-a-file-like-object-in-another-that-calls-a-/ <p style="color: grey"> Python recipe 578674 by <a href="/recipes/users/155538/">Martin Miller</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>Wraps a file-like object in another, but also calls a user callback with the number of bytes read whenever its <code>read()</code> method is called. Used for tracking upload progress, for example for a progress bar in a UI application.</p> Wrap a string in a file-like object that calls a user callback whenever read() is called on the stream (Python) 2013-09-22T21:48:03-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578669-wrap-a-string-in-a-file-like-object-that-calls-a-u/ <p style="color: grey"> Python recipe 578669 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>Wraps a string in a read-only file-like object, but also calls a user callback with the number of bytes read whenever <code>read()</code> is called on the stream. Used for tracking upload progress, for example for a progress bar in a UI application.</p> Calendar Maker (Python) 2012-07-03T05:23:57-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578187-calendar-maker/ <p style="color: grey"> Python recipe 578187 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/calendar/">calendar</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/old/">old</a>). </p> <p>This is an old program experimenting with CGI techniques in Python using <code>CGIHTTPServer</code> and designed for creating calendars. This is committed for archival to be run under Python 2.5 or later versions.</p> Update Google Chrome localstorage file for version compatibility (Python) 2011-08-23T13:41:49-07:00Russellhttp://code.activestate.com/recipes/users/4173357/http://code.activestate.com/recipes/577855-update-google-chrome-localstorage-file-for-version/ <p style="color: grey"> Python recipe 577855 by <a href="/recipes/users/4173357/">Russell</a> (<a href="/recipes/tags/chrome/">chrome</a>, <a href="/recipes/tags/chromium/">chromium</a>, <a href="/recipes/tags/localstorage/">localstorage</a>). </p> <p>Google chrome changes the way key/value stores in localstorage file during upgrading from 11 to 15. This breaks data of many websites and extensions which depends on localstorage. This script upgrade the old localstorage data to make them work again.</p> jardiff (Python) 2011-03-22T15:00:28-07:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/577620-jardiff/ <p style="color: grey"> Python recipe 577620 by <a href="/recipes/users/4135673/">Raphaël Jolivet</a> (<a href="/recipes/tags/diff/">diff</a>, <a href="/recipes/tags/jar/">jar</a>, <a href="/recipes/tags/war/">war</a>, <a href="/recipes/tags/zip/">zip</a>). </p> <p>Diff JAR / WAR / ZIP files (even recursively bundled).</p> <p>Possibility to ignore some files, or some patterns in text files (like MANIFEST.mf).</p> <p>This script is useful in order to know if a new build has changed anything to your binary JAR/WAR/Zip.</p> Universal eval to string function (Python) 2011-02-24T10:17:32-08:00Jakub Jankiewiczhttp://code.activestate.com/recipes/users/4028109/http://code.activestate.com/recipes/577585-universal-eval-to-string-function/ <p style="color: grey"> Python recipe 577585 by <a href="/recipes/users/4028109/">Jakub Jankiewicz</a> (<a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/exec/">exec</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python_scripts/">python_scripts</a>). </p> <p>This is a function that evaluate all expressions and statements and return the result as a string. It also return Exceptions as strings. It is used in <a href="http://trypython.jcubic.pl">trypython.jcubic.pl</a></p> retrieving emails from POP3 server (Python) 2010-03-19T09:18:45-07:00Shamil Bikineyevhttp://code.activestate.com/recipes/users/4173185/http://code.activestate.com/recipes/577127-retrieving-emails-from-pop3-server/ <p style="color: grey"> Python recipe 577127 by <a href="/recipes/users/4173185/">Shamil Bikineyev</a> (<a href="/recipes/tags/email/">email</a>). </p> <p>poplib utils (retr_text, retr_msg, ..) that useful while working with POP3 server</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> 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> Simple FTP Mirror (Python) 2009-05-26T01:54:46-07:00Thimo Kraemerhttp://code.activestate.com/recipes/users/4169283/http://code.activestate.com/recipes/576777-simple-ftp-mirror/ <p style="color: grey"> Python recipe 576777 by <a href="/recipes/users/4169283/">Thimo Kraemer</a> (<a href="/recipes/tags/ftp/">ftp</a>, <a href="/recipes/tags/mirror/">mirror</a>). </p> <p>A simple tool to create a local or remote mirror over ftp.</p> 'with' statement for StringIO (Python) 2009-02-24T00:01:16-08:00sridhttp://code.activestate.com/recipes/users/4053089/http://code.activestate.com/recipes/576650-with-statement-for-stringio/ <p style="color: grey"> Python recipe 576650 by <a href="/recipes/users/4053089/">srid</a> (<a href="/recipes/tags/stringio/">stringio</a>, <a href="/recipes/tags/with_statement/">with_statement</a>). Revision 3. </p> <p>NOTE: Consider this recipe obsolete. Instead use <code>contextlib.closing</code> (see comment below).</p> <p>This contextmanager adds 'with' statement support for StringIO. Peruse the following simple example:</p> <pre class="prettyprint"><code>with StringIO() as sio: function_accepting_file_handle(sio) print sio.getvalue() </code></pre> State Machine for Text Processing (Python) 2009-01-21T14:01:23-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/576624-state-machine-for-text-processing/ <p style="color: grey"> Python recipe 576624 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/state_machine/">state_machine</a>, <a href="/recipes/tags/text_processing/">text_processing</a>). </p> <p>General state machine mechanism plus a specialized version, LineStateMachine, for processing text files based by using regular expressions to determine state transitions.</p> Line Oriented State Machine (Python) 2008-06-30T05:13:27-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/574430-line-oriented-state-machine/ <p style="color: grey"> Python recipe 574430 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/text/">text</a>). Revision 4. </p> <p>State machine for processing text files on a line-by-line basis using regular expressions to determine transition to next state.</p> <p>Developed from sample code from "Text Processing in Python" by David Mertz,pp. 274-280.</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> gzip pipe for asynchronous use (Python) 2008-03-24T21:47:17-07:00Raphaël Jolivethttp://code.activestate.com/recipes/users/4135673/http://code.activestate.com/recipes/551784-gzip-pipe-for-asynchronous-use/ <p style="color: grey"> Python recipe 551784 by <a href="/recipes/users/4135673/">Raphaël Jolivet</a> (<a href="/recipes/tags/files/">files</a>). Revision 2. </p> <p>I've written a small class to handle a gzip pipe that won't read the whole source file at once, but will deliver small chunks of data on demand.</p> ffmpeg ppm.so vhook plugin (Python) 2008-05-26T09:41:05-07:00Konrads Smelkovshttp://code.activestate.com/recipes/users/2852558/http://code.activestate.com/recipes/572220-ffmpeg-ppmso-vhook-plugin/ <p style="color: grey"> Python recipe 572220 by <a href="/recipes/users/2852558/">Konrads Smelkovs</a> (<a href="/recipes/tags/graphics/">graphics</a>). </p> <p>This recipe shows how to read a continuous stream of PPM frames as output by ffmpeg ppm.so vhook. In this example, the image is flipped and output back.</p> RedirectedIO context manager and redirect_io decorator (Python) 2007-11-20T13:50:34-08:00Eduardo Padoanhttp://code.activestate.com/recipes/users/2153574/http://code.activestate.com/recipes/534166-redirectedio-context-manager-and-redirect_io-decor/ <p style="color: grey"> Python recipe 534166 by <a href="/recipes/users/2153574/">Eduardo Padoan</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 5. </p> <p>Use the "with" keyword or a decorator to simplify a bit redirecting IO to a file.</p> Yielding out of nested routines by metaclass transformation (Python) 2006-11-17T04:16:35-08:00Bernhard Mulderhttp://code.activestate.com/recipes/users/189076/http://code.activestate.com/recipes/498270-yielding-out-of-nested-routines-by-metaclass-trans/ <p style="color: grey"> Python recipe 498270 by <a href="/recipes/users/189076/">Bernhard Mulder</a> (<a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>Python 2.5 improved the support for generators, making it easier to use coroutines. If you want to use coroutines, however, you can not transfer control out of nested functions. You can eliminate this restriction by systematically converting regular functions into generator functions as demonstrated by this recipe <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474127" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474127</a> The script below might serve as a starting point to do this transformation automatically, making the use of coroutines (tasklets, lightweight threads...) more natural.</p> Example For winreg (2) (Python) 2011-05-20T13:22:37-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/498151-example-for-winreg-2/ <p style="color: grey"> Python recipe 498151 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>This recipe is another example of how to use the winreg module. The code had the first purpose of demonstrating the concept of a graphical shell built in Python. The shell was easily modified to make use of the Window's registry but retains traces of its original method of operation (all of which has been commented out).</p> <p>The program down below originally used the pickle module to save all of its settings when closing down. Now once the escape key is pressed, this Python 2.5 program can use the module presented in <a href="http://code.activestate.com/recipes/510392/">recipe 510392</a> to access Microsoft Window's Registry and store its settings there. Check <code>do_exit</code> and <code>do_config</code> functions for usage.</p> Read/write object for pkg-config files (Python) 2006-06-29T23:33:27-07:00Christopher Dunnhttp://code.activestate.com/recipes/users/1683375/http://code.activestate.com/recipes/496852-readwrite-object-for-pkg-config-files/ <p style="color: grey"> Python recipe 496852 by <a href="/recipes/users/1683375/">Christopher Dunn</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>Here is a convenient way to create, parse, alter, and write .pc files. Just pass PkgConfig() a file-like object, or None to start fresh. Print the object to create a new .pc file.</p> <p>Some people might want even more functionality, but I chose not to hinder the flexibility of the class.</p>