<?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>RyanKaskel.com</title>
	<atom:link href="http://ryankaskel.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ryankaskel.com</link>
	<description></description>
	<lastBuildDate>Mon, 12 Dec 2011 15:14:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Polish words of the day</title>
		<link>http://ryankaskel.com/2011/11/19/polish-words-of-the-day/</link>
		<comments>http://ryankaskel.com/2011/11/19/polish-words-of-the-day/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 16:29:49 +0000</pubDate>
		<dc:creator>Ryan Kaskel</dc:creator>
				<category><![CDATA[languages]]></category>
		<category><![CDATA[polish]]></category>

		<guid isPermaLink="false">http://ryankaskel.com/?p=97</guid>
		<description><![CDATA[A few months ago I started taking Polish language classes at Polword near Oxford Circus in central London. It has since become my favorite weekly activity. The school is conveniently located and is staffed by lovely &#8230; <a class="more-link" href="http://ryankaskel.com/2011/11/19/polish-words-of-the-day/">More<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A few months ago I started taking <a title="Polish language" href="http://en.wikipedia.org/wiki/Polish_language">Polish language</a> classes at <a title="Learn polish in London" href="http://www.polword.co.uk/">Polword</a> near <a title="Oxford Circus, London" href="http://en.wikipedia.org/wiki/Oxford_Circus">Oxford Circus</a> in central London. It has since become my favorite weekly activity. The school is conveniently located and is staffed by lovely native Polish speakers who can teach well.</p>
<p>Even though I only have class once a week, I know that I won&#8217;t ever learn the language if I don&#8217;t practice every day. To help enrich my vocabulary, I&#8217;ve created a spreadsheet on Google Docs that has my <a title="Ryan's Polish words of the day" href="https://docs.google.com/spreadsheet/ccc?key=0AtdqXquJH4IYdDRLS0ZET2R5Nk12aGlxUVZDVVFKQ3c">Polish words of the day</a> (polskie słowa dnia Ryana). Every day I will add a new noun (rzeczownik), adjective (przymiotnik), and verb (czasownik) to the list.</p>
<p>I find daily doses of information coupled with weekly reviews to be good for long term retention. I used this same method when learning the basic key sequences for <a title="emacs" href="http://www.gnu.org/s/emacs/">emacs</a> commands and it definitely helps.</p>
<p>Today&#8217;s words, the first on the list so far, are:</p>
<ol>
<li>grzyb (m) &#8211; mushroom</li>
<li>głośny &#8211; loud, noisy</li>
<li>pilnować (aspekt dokonany: dopilnować) &#8211; to look after, watch, guard (among other meanings)</li>
</ol>
<div id="attachment_99" class="wp-caption alignnone" style="width: 310px"><a href="http://www.flickr.com/photos/32050584@N06/2999420684/"><img class="size-medium wp-image-99 " title="grzyb-1" src="http://ryankaskel.com/wp-content/uploads/2011/11/grzyb-1-300x225.jpg" alt="" width="300" height="225" /></a><p class="wp-caption-text">grzyb (m) - mushroom</p></div>
<p>If you are also a Polish language student and would like edit permissions, please <a title="contact Ryan Kaskel" href="http://ryankaskel.com/contact/">let me know</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryankaskel.com/2011/11/19/polish-words-of-the-day/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t use string literals for sender values in Django&#8217;s signal dispatcher</title>
		<link>http://ryankaskel.com/2011/08/08/dont-use-strings-as-sender-values-in-djangos-signal-dispatcher/</link>
		<comments>http://ryankaskel.com/2011/08/08/dont-use-strings-as-sender-values-in-djangos-signal-dispatcher/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 20:49:26 +0000</pubDate>
		<dc:creator>Ryan Kaskel</dc:creator>
				<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://ryankaskel.com/?p=36</guid>
		<description><![CDATA[TL; DR Don&#8217;t use a string literal as the value of the sender attribute when defining your own Django signals. I got bit by a nasty bug today using Django&#8217;s signal framework. I used the signal &#8230; <a class="more-link" href="http://ryankaskel.com/2011/08/08/dont-use-strings-as-sender-values-in-djangos-signal-dispatcher/">More<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>TL; DR</strong> Don&#8217;t use a string literal as the value of the sender attribute when defining your own <a title="Django Signals" href="https://docs.djangoproject.com/en/dev/topics/signals/">Django signals</a>.</p>
<p>I got bit by a nasty bug today using Django&#8217;s signal framework. I used the signal like so:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">show_user_widget_responses = show_user_widget.<span style="color: black;">send</span><span style="color: black;">&#40;</span>
    sender=<span style="color: #483d8b;">'my_widget_view'</span>,
    <span style="color: #dc143c;">user</span>=<span style="color: #dc143c;">user</span>
<span style="color: black;">&#41;</span></pre></div></div>

<p>I ran the tests to make sure the signal was being sent to the listeners (i.e receivers) and they all passed. I then noticed the view sent another signal in certain conditions and its sender was a set to a different string. For the sake of consistency, <strong>I changed the above signal&#8217;s sender string to &#8220;widget-user-view&#8221;</strong> and went to make a cup of tea.</p>
<p>When I returned to my desk, I got a bit distracted and continued to work on the feature. I ran the app&#8217;s tests and they started failing: the test listeners weren&#8217;t being called. Oops.</p>
<p><span id="more-36"></span></p>
<p>My first inclination was that I forgot to change the sender argument in &#8220;show_user_widget.connect.&#8221; Nope&#8230; the tests were still failing. I decided to change the sender argument in the send call to &#8220;widget&#8221; and the tests started passing again. What was going on? I proceeded to try new sender values and I soon came to believe that hyphens weren&#8217;t allowed in sender arguments that were strings. When I start thinking like this, I know that it&#8217;s time to take a step back and use Google.</p>
<p>One of the first hits I came across was <a title="Django ticket #16447" href="https://code.djangoproject.com/ticket/16447">ticket #16447: settings_changed signal uses strings for sender</a>, filed by Alex Gaynor. He noticed that when he ran Django&#8217;s tests using <a title="PyPy" href="http://pypy.org/">PyPy</a>, the test that checked the &#8220;settings_changed&#8221; signal started failing. This was exactly my problem, so what was going on?</p>
<p>It turns out that Django&#8217;s signal dispatcher code uses the id() of the sender argument as part of the &#8220;key&#8221; to the receiver (which I&#8217;ve always called a &#8220;listener&#8221;).  Here is the relevant code in <a href="https://code.djangoproject.com/browser/django/trunk/django/dispatch/dispatcher.py">django.dispatch.dispatcher</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> _make_id<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>target, <span style="color: #483d8b;">'im_func'</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span><span style="color: #008000;">id</span><span style="color: black;">&#40;</span>target.<span style="color: black;">im_self</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">id</span><span style="color: black;">&#40;</span>target.<span style="color: black;">im_func</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span></pre></div></div>

<p>Here is how the key is defined if &#8220;dispatch_uid&#8221; is not set (in Signal.connect):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">lookup_key = <span style="color: black;">&#40;</span>_make_id<span style="color: black;">&#40;</span>receiver<span style="color: black;">&#41;</span>, _make_id<span style="color: black;">&#40;</span>sender<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This is the core of Signal.send:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> receiver <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>._live_receivers<span style="color: black;">&#40;</span>_make_id<span style="color: black;">&#40;</span>sender<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
    response = receiver<span style="color: black;">&#40;</span><span style="color: #dc143c;">signal</span>=<span style="color: #008000;">self</span>, sender=sender, <span style="color: #66cc66;">**</span>named<span style="color: black;">&#41;</span>
    responses.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>receiver, response<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And finally, here is the important section of Signal._live_receivers:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> _live_receivers<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, senderkey<span style="color: black;">&#41;</span>
    ...
    <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>receiverkey, r_senderkey<span style="color: black;">&#41;</span>, receiver <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">receivers</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> r_senderkey == none_senderkey <span style="color: #ff7700;font-weight:bold;">or</span> r_senderkey == senderkey:
        ...
    ...</pre></div></div>

<p>Once this implementation detail is known, it becomes obvious that this is a manifestation of the <a href="http://stackoverflow.com/questions/2987958/how-is-the-is-keyword-implemented-in-python/2987975#2987975">difference between identity and equality</a>: id(&#8220;my-string&#8221;) is <em>not</em> guaranteed to always return the same integer even though &#8220;my-string&#8221; == &#8220;my-string&#8221;. Because I used a string literal as the argument, new string objects with potentially different identities were being created in each call. I don&#8217;t know exactly why their identity differs, but the behavior is easily observable in the shell:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'widget-user-view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951112</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'widget-user-view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951232</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'widget-user-view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951272</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'widget-user-view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951112</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'widget-user-view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951232</span></pre></div></div>

<p>But&#8230; certain strings, like my original value of sender, &#8220;my_widget_view,&#8221; <em>will</em> return the same integer ID:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'my_widget_view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951192</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'my_widget_view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951192</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'my_widget_view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951192</span>
<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'my_widget_view'</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">37951192</span></pre></div></div>

<p>So, the moral of the story is: don&#8217;t use string literals for the sender argument when implementing custom signals in Django. It turns out to be tricky to debug. If you&#8217;re sending a signal from a class and want a good value for sender, try &#8220;self.__class__.&#8221;  As <strong>Justin</strong> pointed out in a comment, you <em>can</em> use a string for the value of sender if you use that same string variable as the argument in each call to the signal methods (you must import the string along with the signal).</p>
<p>I still think it&#8217;s better to avoid strings as senders altogether. The ticket filed by Alex Gaynor referenced above used a setting name as the sender and it wouldn&#8217;t be long before someone tried:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">signals.<span style="color: black;">settings_changed</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>listener, sender=<span style="color: #483d8b;">&quot;SOME_SETTING&quot;</span>, ...<span style="color: black;">&#41;</span></pre></div></div>

<p>rather than the more correct</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
signals.<span style="color: black;">settings_changed</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>listener, sender=settings.<span style="color: black;">SOME_SETTING</span>, ...<span style="color: black;">&#41;</span></pre></div></div>

<p>It&#8217;s a subtle bug waiting to happen and is indeed <a href="https://code.djangoproject.com/changeset/16530/django/trunk/tests/regressiontests/settings_tests/tests.py">why the test failed</a> for Alex. Furthermore, you also may not even <em>need </em>to pass a value for sender, if, for example, you only use Signal.send in one place or if the listener (receiver) doesn&#8217;t need to use it. (see the code reference above: _make_id(None) is also checked).</p>
<p>Another mistake I made in my code is now obvious: always use &#8220;constants&#8221; (i.e. BIG_BOLD_VARIABLES defined at the top of the module) instead of peppering the same string literal around your code.</p>
<p>I later found <a href="https://code.djangoproject.com/ticket/13132">another ticket</a> that describes the same problem. One core dev marked it as &#8220;wontfix&#8221; and I agree that there is nothing to fix <em>in the code</em>. But given that another core dev made the same mistake in <a href="https://code.djangoproject.com/changeset/16237">changeset 16327</a>, I think this behavior should be documented and the next time I get some free time, I will submit a documentation patch (after I write my dissertation <img src='http://ryankaskel.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://ryankaskel.com/2011/08/08/dont-use-strings-as-sender-values-in-djangos-signal-dispatcher/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

