Popular recipes by Michael Foord http://code.activestate.com/recipes/users/1565518/2005-04-05T15:25:01-07:00ActiveState Code RecipesExample setattr & getattr overloading (Python)
2005-03-01T02:08:45-08:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/389916-example-setattr-getattr-overloading/
<p style="color: grey">
Python
recipe 389916
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<p>An example of overloading __setattr__ and __getattr__ in classes. This example maps attribute names to dicitonary members.</p>
HTTP basic authentication (Python)
2004-10-05T14:05:47-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/305288-http-basic-authentication/
<p style="color: grey">
Python
recipe 305288
by <a href="/recipes/users/1565518/">Michael Foord</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 3.
</p>
<p>A script demonstrating how to manually do basic authentication over http.</p>
GoogleCacheServer (Python)
2005-04-05T15:25:01-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/408991-googlecacheserver/
<p style="color: grey">
Python
recipe 408991
by <a href="/recipes/users/1565518/">Michael Foord</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>This is a simple implementation of a proxy server that fetches web pages
from the google cache. It allows you to view the internet through the eyes of google ! Fire it up and point your browser at localhost:8000</p>
cookielib Example (Python)
2004-12-28T11:26:41-08:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/302930-cookielib-example/
<p style="color: grey">
Python
recipe 302930
by <a href="/recipes/users/1565518/">Michael Foord</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>cookielib is a library new to Python 2.4
Prior to Python 2.4 it existed as ClientCookie, but it's not a drop in replacement - some of the function of ClientCookie has been moved into urllib2.</p>
<p>This example shows code for fetching URIs (with cookie handling - including loading and saving) that will work unchanged on :
a machine with python 2.4 (and cookielib)
a machine with ClientCookie installed
a machine with neither
(Obviously on the machine with neither the cookies won't be handled or saved).</p>
<p>Where either cookielib or ClientCookie is available the cookies will be saved in a file.
If that file exists already the cookies will first be loaded from it.
The file format is a useful plain text format and the attributes of each cookie is accessible in the Cookiejar instance (once loaded).</p>
<p>This may be helpful to those just using ClientCookie as the ClientCookie documentation doesn't appear to document the LWPCookieJar class which is needed for saving and loading cookies.</p>
webcounter (Python)
2004-10-15T05:33:47-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/310320-webcounter/
<p style="color: grey">
Python
recipe 310320
by <a href="/recipes/users/1565518/">Michael Foord</a>
(<a href="/recipes/tags/cgi/">cgi</a>).
</p>
<p>This CGI will generate a simple text 'hit counter' for several sites.
Useful to ocunt visitors to the 'front door' of your website.</p>
HTML Scraper (Python)
2004-09-06T08:18:49-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/286269-html-scraper/
<p style="color: grey">
Python
recipe 286269
by <a href="/recipes/users/1565518/">Michael Foord</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 5.
</p>
<p>A simple HTML 'parser' that will 'read' through an HTML file and call functions on data and tags etc.
Useful if you need to implement a straightforward parser that just extracts information from the file <em>or</em> modifies tags etc.</p>
<p>Shouldn't choke on bad HTML.</p>
http - Exploring Headers and Cookies from Servers (Python)
2004-08-18T10:23:52-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/298336-http-exploring-headers-and-cookies-from-servers/
<p style="color: grey">
Python
recipe 298336
by <a href="/recipes/users/1565518/">Michael Foord</a>
(<a href="/recipes/tags/cgi/">cgi</a>).
Revision 5.
</p>
<p>This CGI script allows you to specify a URL. It fetches the URL and displays all the headers sent by the server.
It is based on approx.py the CGI-proxy I'm building. It includes authentication circuitry and I'm using it to understand http authentication.</p>
<p>This script demostrates using urllib2 to fetch a URL - using a request object with User-Agent header. It also demostrates basic authentication and shows the possible http errors - using a dictionary 'borrowed' from BaseHTTPServer.</p>
<p>It will also save cookies using the ClientCookie module, if it's available.</p>
cgi-shell (Python)
2004-07-09T07:31:52-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/286217-cgi-shell/
<p style="color: grey">
Python
recipe 286217
by <a href="/recipes/users/1565518/">Michael Foord</a>
(<a href="/recipes/tags/cgi/">cgi</a>).
</p>
<p>Run an arbitrary string on the server as a shell command. Mimics a very basic shell environment on a server using CGI.</p>
cgi-shell (Python)
2004-07-09T07:30:50-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/286216-cgi-shell/
<p style="color: grey">
Python
recipe 286216
by <a href="/recipes/users/1565518/">Michael Foord</a>
(<a href="/recipes/tags/cgi/">cgi</a>).
</p>
<p>Run an arbitrary string on the server as a shell command. Mimics a very basic shell environment on a server using CGI.</p>
cgiproxy.py (Python)
2004-06-22T07:20:42-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/286160-cgiproxypy/
<p style="color: grey">
Python
recipe 286160
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<p>A very simple script that fetches a webpage for you. Useful for a cgiproxy in situations where web access is restricted - and to illustrate the basic case use of urllib2.</p>
Case Insensitive Sort (Python)
2004-07-07T06:09:12-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/286204-case-insensitive-sort/
<p style="color: grey">
Python
recipe 286204
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<p>This is a recipe that does a case insensitive sort. The normal sort methods of lists has 'B'<'a', which means that it would sort 'Pear' to come before 'apple' in a list. You can pass in a function to the sort method to change this... but this can be slow. This is a function that transforms the list, uses the sort method and then transforms it back.</p>
newaskstring Dialog (Python)
2004-06-19T02:58:10-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/286156-newaskstring-dialog/
<p style="color: grey">
Python
recipe 286156
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<p>A <em>tiny</em> adjustment to the askstring dialog in the standard module tkSimpleDialog. The underlying code allows a default string to be passed to in, but the actual dialog function doesn't.. This one does.</p>
caselessDict (Python)
2004-06-07T10:30:31-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/283455-caselessdict/
<p style="color: grey">
Python
recipe 283455
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
Revision 4.
</p>
<p>A dictionary with case insensitive keys - that retains the case of the keyword.
Only permits strings as keys.
An alternative implementation of <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66315" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66315</a></p>
caselessList (Python)
2004-05-18T05:11:45-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/284569-caselesslist/
<p style="color: grey">
Python
recipe 284569
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<p>A list that will only allow strings as members. Methods like count, __contains__ ( a in list ), remove, index etc are case insensitive.
See also the caselessDict - <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283455" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283455</a></p>
Listparse (Python)
2004-04-30T07:37:23-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/281056-listparse/
<p style="color: grey">
Python
recipe 281056
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<p>Turn a string representation of a list back into a list. Including nested lists - list elements, can themselves be lists. Includes functions for quoting and unquoting list elements....</p>
Windows Console (Python)
2004-04-26T08:06:32-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/279407-windows-console/
<p style="color: grey">
Python
recipe 279407
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
Revision 3.
</p>
<p>A simple console for windows.
Any commands that don't make sense as python commands are sent to the system. Fully expandable !!
You can reference variables in system commands using %:
e.g. echo %fish
echoes the contents of the variable fish.........</p>
Trapping and Displaying Exceptions (Python)
2004-05-04T11:47:39-07:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/259180-trapping-and-displaying-exceptions/
<p style="color: grey">
Python
recipe 259180
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
Revision 2.
</p>
<p>Traps exceptions - to be printed or displayed without actually raising the exception.</p>
<p>Sometimes useful to get the <em>full</em> error message from an exception - this code captures it as text - to be displayed, saved or whatever.</p>
Date to Day of the Week (Python)
2004-01-22T05:51:44-08:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/266464-date-to-day-of-the-week/
<p style="color: grey">
Python
recipe 266464
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<p>This simple function returns the day of the week (as an integer from 1 to 7) from an input date.....</p>
ConfigObj - Lightweight Config File Parser (Python)
2004-01-09T06:10:29-08:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/259175-configobj-lightweight-config-file-parser/
<p style="color: grey">
Python
recipe 259175
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<h4>A simple class of config object - allowing simple configs to be read in, updated in the file or rewritten out</h4>
<h4>Preserves comments inline with the keywords</h4>
<h4>Allows for multiple values - defined by the programmer what type of value a keyword is expected to have</h4>
<h4>Will read only the specified values from a file - allows sections of a larger config file to be</h4>
<h4>easily read and updated whilst preserving the rest of the file.</h4>
<h4>outputs non-fatal error to the stout object - this could be turned into an exception handler if you wanted</h4>
<h4>errors to be fatal instead of non-fatal.</h4>
<p>#</p>
<h4>Maintained at <a href="http://www.voidspace.org.uk/atlantibots/pythonutils.html" rel="nofollow">www.voidspace.org.uk/atlantibots/pythonutils.html</a></h4>
StandOut - Flexible Output Object (Python)
2004-01-09T06:28:44-08:00Michael Foordhttp://code.activestate.com/recipes/users/1565518/http://code.activestate.com/recipes/259176-standout-flexible-output-object/
<p style="color: grey">
Python
recipe 259176
by <a href="/recipes/users/1565518/">Michael Foord</a>
.
</p>
<p>A useful output object that can 'print' to various places (stdout, loggign file and potentially to a window as well) using a single command.
Built to be used in conjunction with the ConfigObj - simpel config file parser.</p>
<h4>maintained at <a href="http://www.voidspace.org.uk/atlantibots/pythonutils.html" rel="nofollow">http://www.voidspace.org.uk/atlantibots/pythonutils.html</a></h4>