Popular recipes by Ben Hoyt http://code.activestate.com/recipes/users/4170919/2013-09-22T21:48:03-07:00ActiveState Code RecipesEncode multipart form data for uploading files via POST (Python) 2013-09-22T21:27:24-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578668-encode-multipart-form-data-for-uploading-files-via/ <p style="color: grey"> Python recipe 578668 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/form/">form</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/multipart/">multipart</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>This function lets you encode form fields <em>and</em> files in multipart/form-data format for uploading files via HTTP POST.</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> Get memory usage of Windows processes using GetProcessMemoryInfo (via ctypes) (Python) 2013-04-25T01:26:19-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578513-get-memory-usage-of-windows-processes-using-getpro/ <p style="color: grey"> Python recipe 578513 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/win32/">win32</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>These functions call the Win32 function GetProcessMemoryInfo() using ctypes to get the memory usage of the current process. Works on both 32-bit and 64-bit Windows and Python 2.6+ (including Python 3.x).</p> Function that supports dividing timedelta by float (Python) 2012-05-17T13:47:07-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578136-function-that-supports-dividing-timedelta-by-float/ <p style="color: grey"> Python recipe 578136 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/division/">division</a>, <a href="/recipes/tags/float/">float</a>). </p> <p>Python 2.x doesn't support dividing a timedelta by a float (only works with int). This function adds support for that.</p> Round number to specified number of significant digits (Python) 2012-04-26T20:15:48-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578114-round-number-to-specified-number-of-significant-di/ <p style="color: grey"> Python recipe 578114 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/decimalplaces/">decimalplaces</a>, <a href="/recipes/tags/round/">round</a>, <a href="/recipes/tags/rounding/">rounding</a>, <a href="/recipes/tags/significantfigures/">significantfigures</a>). </p> <p>This function rounds num to the specified number of significant digits (rather than decimal places). See doctests for more examples.</p> Pluralize word -- convert singular word to its plural form (Python) 2011-07-06T21:37:19-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/577781-pluralize-word-convert-singular-word-to-its-plural/ <p style="color: grey"> Python recipe 577781 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/plural/">plural</a>, <a href="/recipes/tags/pluralize/">pluralize</a>, <a href="/recipes/tags/singular/">singular</a>, <a href="/recipes/tags/word/">word</a>). </p> <p>The pluralize() function returns the plural form of the given lowercase singular word (English only). Based on ActiveState recipe <a href="http://code.activestate.com/recipes/413172/" rel="nofollow">http://code.activestate.com/recipes/413172/</a></p> Win32 named mutex class for system-wide mutex (Python) 2011-07-15T21:12:40-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/577794-win32-named-mutex-class-for-system-wide-mutex/ <p style="color: grey"> Python recipe 577794 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/mutex/">mutex</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/win32/">win32</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>NamedMutex is a class for using Windows (Win32) named mutexes for system-wide locks. For example, we use these to lock system-wide log files that multiple processes can write to.</p> Get user's IP address even when they're behind a proxy (Python) 2011-07-15T21:19:17-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/577795-get-users-ip-address-even-when-theyre-behind-a-pro/ <p style="color: grey"> Python recipe 577795 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/address/">address</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/ip/">ip</a>, <a href="/recipes/tags/web/">web</a>, <a href="/recipes/tags/webpy/">webpy</a>). </p> <p>Function to get the user's IP address in a web app or CGI script, even when they're behind a web proxy.</p> <p>We use web.py as our web framework, but change web.ctx.env and web.ctx.get('ip') to whatever the equivalents are for the CGI environment variables and REMOTE_ADDR are in your framework.</p> Prints full name of all occurrences of given filename in your PATH (Python) 2009-06-29T15:20:10-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/576823-prints-full-name-of-all-occurrences-of-given-filen/ <p style="color: grey"> Python recipe 576823 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/which/">which</a>). </p> <p>Simple program to print the full name of all occurrences of the given filename in your PATH. Kind of like the Unix "which" utility, but works for DLLs and other files as well.</p> <p>Usage: findinpath.py filename</p>