Most viewed recipes by Brian Beck http://code.activestate.com/recipes/users/2425329/views/2006-03-31T18:52:03-08:00ActiveState Code RecipesReadable switch construction without lambdas or dictionaries (Python) 2005-04-26T10:51:04-07:00Brian Beckhttp://code.activestate.com/recipes/users/2425329/http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/ <p style="color: grey"> Python recipe 410692 by <a href="/recipes/users/2425329/">Brian Beck</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 8. </p> <p>Python's lack of a 'switch' statement has garnered much discussion and even a PEP. The most popular substitute uses dictionaries to map cases to functions, which requires lots of defs or lambdas. While the approach shown here may be O(n) for cases, it aims to duplicate C's original 'switch' functionality and structure with reasonable accuracy.</p> reducipes: Excuses to use the reduce built-in (Python) 2006-03-31T18:52:03-08:00Brian Beckhttp://code.activestate.com/recipes/users/2425329/http://code.activestate.com/recipes/476215-reducipes-excuses-to-use-the-reduce-built-in/ <p style="color: grey"> Python recipe 476215 by <a href="/recipes/users/2425329/">Brian Beck</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>reduce is a pretty nice built-in, but its usage in everyday code is seemingly rare. If you've been looking for excuses to use reduce, here are a few useful recipes to get you started.</p> Quickslice decorator: Abbreviated slice arguments in functions (Python) 2005-05-31T19:30:02-07:00Brian Beckhttp://code.activestate.com/recipes/users/2425329/http://code.activestate.com/recipes/415500-quickslice-decorator-abbreviated-slice-arguments-i/ <p style="color: grey"> Python recipe 415500 by <a href="/recipes/users/2425329/">Brian Beck</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 4. </p> <p>Sometimes I wish slice creation had a form that looked more like its invocation: [1:5], for example. This recipe allows slice arguments to be passed that look like {1:5}. NOTE: I don't recommend actually using this in practice. It will most likely just confuse any readers of your code. It is just a proof-of-concept. In an interview with Guido van Rossum, Guido was explaining the benefits of Python's built-in types. He gave an example of being able to pass slices as arguments instead of having individual start, stop and step arguments -- and this is very true. But I don't think a syntax to concisely create slices should be out of the question. (Step is not supported by this syntax, it wouldn't be possible and I rarely find myself using step anyway).</p> Tracking file requests in web server access logs (Python) 2005-08-02T18:18:33-07:00Brian Beckhttp://code.activestate.com/recipes/users/2425329/http://code.activestate.com/recipes/438809-tracking-file-requests-in-web-server-access-logs/ <p style="color: grey"> Python recipe 438809 by <a href="/recipes/users/2425329/">Brian Beck</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>Find out how often and by who a particular file is being requested. Prints the requesting addresses, hostnames, access times, and hit counts.</p> First and last item access with predicates to increase expressiveness (Python) 2005-08-03T01:44:57-07:00Brian Beckhttp://code.activestate.com/recipes/users/2425329/http://code.activestate.com/recipes/438810-first-and-last-item-access-with-predicates-to-incr/ <p style="color: grey"> Python recipe 438810 by <a href="/recipes/users/2425329/">Brian Beck</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 2. </p> <p>A common practice when dealing with sequences is to find the first or last item in the list that satisfies a predicate. This simple recipe increases the readability and writability for these tasks and nothing more.</p>