<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>klenwell press &#187; Stupid Lambda Tricks</title>
	<atom:link href="http://klenwell.com/press/category/lambda/feed/" rel="self" type="application/rss+xml" />
	<link>http://klenwell.com/press</link>
	<description>A Developer's Broadsheet</description>
	<lastBuildDate>Fri, 13 Jan 2012 14:51:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>Get for Python Lists</title>
		<link>http://klenwell.com/press/2010/11/get-for-python-lists/</link>
		<comments>http://klenwell.com/press/2010/11/get-for-python-lists/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 04:56:15 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Stupid Lambda Tricks]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=201</guid>
		<description><![CDATA[With python&#8217;s dictionary objects, you have the useful get method: &#62;&#62;&#62; d = {&#039;a&#039;:1, &#039;b&#039;:2, &#039;c&#039;:3} &#62;&#62;&#62; d.get(&#039;a&#039;) 1 &#62;&#62;&#62; d.get(&#039;d&#039;, &#039;not found&#039;) &#039;not found&#039; Sometimes, I find something like this would be useful for lists, using the list&#8217;s index in place of the dict&#8217;s key: &#62;&#62;&#62; l = [0,1,2,3] &#62;&#62;&#62; l.get(2) Traceback (most recent [...]]]></description>
			<content:encoded><![CDATA[<p>With python&#8217;s dictionary objects, you have the useful <a href="http://docs.python.org/release/2.5.2/lib/typesmapping.html">get</a> method:</p>
<pre class="brush: python">
&gt;&gt;&gt; d = {&#039;a&#039;:1, &#039;b&#039;:2, &#039;c&#039;:3}
&gt;&gt;&gt; d.get(&#039;a&#039;)
1
&gt;&gt;&gt; d.get(&#039;d&#039;, &#039;not found&#039;)
&#039;not found&#039;
</pre>
<p>Sometimes, I find something like this would be useful for lists, using the list&#8217;s index in place of the dict&#8217;s key:</p>
<pre class="brush: python">
&gt;&gt;&gt; l = [0,1,2,3]
&gt;&gt;&gt; l.get(2)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
AttributeError: &#039;list&#039; object has no attribute &#039;get&#039;
</pre>
<p>Here&#8217;s a quick lambda that accomplishes that:</p>
<pre class="brush: python">
list_get = lambda l,i,a=False: (len(l) &lt;= i and a) or (len(l) &gt; i and l[i])
</pre>
<p>Usage:</p>
<pre class="brush: python">
&gt;&gt;&gt; list_get = lambda l,i,a=False: (len(l) &lt;= i and a) or (len(l) &gt; i and l[i])
&gt;&gt;&gt; my_list = [0,False,None,3]
&gt;&gt;&gt; list_get(my_list,0)
0
&gt;&gt;&gt; list_get(my_list,1)
False
&gt;&gt;&gt; list_get(my_list,2)  # returns None
&gt;&gt;&gt; list_get(my_list,3)
3
&gt;&gt;&gt; list_get(my_list,4)
False
&gt;&gt;&gt; list_get(my_list,4,0)  # Note: returns False instead of 0
False
&gt;&gt;&gt; list_get(my_list,4,1)
1
</pre>
<p>It&#8217;s not perfect. Be careful with False-equivalent values in the alt parameters as noted above. But otherwise it should work for most practical cases were such usage is desired.</p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2010/11/get-for-python-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Weighty Choices</title>
		<link>http://klenwell.com/press/2009/02/weighty-choices/</link>
		<comments>http://klenwell.com/press/2009/02/weighty-choices/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 04:57:37 +0000</pubDate>
		<dc:creator>klenwell</dc:creator>
				<category><![CDATA[Stupid Lambda Tricks]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://klenwell.com/press/?p=94</guid>
		<description><![CDATA[I was tasked at work last week with coming up with a 2 million+ record dataset for some load tests we&#8217;re running on our application. I had a day&#8217;s worth of production data that I need to extrapolate to six months. Another opportunity to use my favorite python module: random What I wanted was something [...]]]></description>
			<content:encoded><![CDATA[<p>I was tasked at work last week with coming up with a 2 million+ record dataset for some load tests we&#8217;re running on our application.  I had a day&#8217;s worth of production data that I need to extrapolate to six months.  Another opportunity to use my favorite python module: <a href="http://docs.python.org/library/random.html">random</a></p>
<p>What I wanted was something like the <a href="http://docs.python.org/library/random.html#random.choice">choice function</a>, but one to which I could pass a dictionary with the keys representing the choice list and the values representing their relative weights so that I could get a representative distribution.  Since the random module doesn&#8217;t offer one itself, I was left to my own devices, which are admittedly clumsy and slow.  The situation begged for a stupid lambda trick, but I was pressed for time and so I just threw this together:</p>
<pre class="brush: python">
def weighted_choice(ChoiceDict):
    wsum = sum([w for w in ChoiceDict.values()])
    n = random.uniform(0, wsum)
    for k in ChoiceDict:
        if n &lt; ChoiceDict[k]: break
        n = n - ChoiceDict[k]
    return k
</pre>
<p>A little later, I passed the lambda challenge off to a couple of my colleagues.  Later in the day, I had a chance to rattle off my own:</p>
<pre class="brush: python">
weighted_choice = lambda d: random.choice(reduce(list.__add__, [[a for a in k for n in range(d[k])] for k in d.keys()]))
</pre>
<p>But the Letterman spot goes to my colleague Leonard, for the conciseness and nice symmetry of his solution:</p>
<pre class="brush: python">
weighted_choice = lambda d: random.choice([k_ for s in [[k]*w for k,w in d.iteritems()] for k_ in s])
</pre>
<p>Find a script testing these and a couple other variations on the function here:</p>
<p><a href="http://klenwell.googlecode.com/svn/trunk/pastebin/weighted_choice.py">http://klenwell.googlecode.com/svn/trunk/pastebin/weighted_choice.py</a></p>
]]></content:encoded>
			<wfw:commentRss>http://klenwell.com/press/2009/02/weighty-choices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

