<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Dumping Ground</title>
	<atom:link href="http://colinnewell.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://colinnewell.wordpress.com</link>
	<description>Nothing's too trivial</description>
	<lastBuildDate>Sat, 20 Apr 2013 16:14:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='colinnewell.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Dumping Ground</title>
		<link>http://colinnewell.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://colinnewell.wordpress.com/osd.xml" title="The Dumping Ground" />
	<atom:link rel='hub' href='http://colinnewell.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Adhoc parameters to joins in DBIx::Class</title>
		<link>http://colinnewell.wordpress.com/2013/02/14/adhoc-parameters-to-joins-in-dbixclass/</link>
		<comments>http://colinnewell.wordpress.com/2013/02/14/adhoc-parameters-to-joins-in-dbixclass/#comments</comments>
		<pubDate>Thu, 14 Feb 2013 18:44:11 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[perl5]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=695</guid>
		<description><![CDATA[Update: there are a few updates to the caveats on this post based on the comments by Peter Rabbitson. I’ve been using DBIx::Class for a couple of years now and I’ve always been impressed with it’s flexibility.  I’ve rarely felt the need to write my own SQL queries because it’s simply so good at it, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=695&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Update: there are a few updates to the caveats on this post based on the comments by Peter Rabbitson.<br />
</strong><br />
I’ve been using <code>DBIx::Class</code> for a couple of years now and I’ve always been impressed with it’s flexibility.  I’ve rarely felt the need to write my own SQL queries because it’s simply so good at it, and it’s generally easy to get it to do what I want. </p>
<p>The one exception to that was custom adhoc joins.  In general <code>DBIx::Class</code> wants to know about how you’re going to join up front.  Anything else tends to require a custom view.  </p>
<p>The other night I realised I could come up with a way to deal with slightly more complex joins while still making the most of all my result definitions.  The extended relationships explained by frew on his <a href="http://blog.afoolishmanifesto.com/archives/1582">blog</a> demonstrate how to add decent join conditions.  The one thing missing was adhoc parameters.  They can be added by providing a bind parameter.  Since relationships don’t traditionally require any extra search parameters, I’d recommend indicating that the relationship isn’t for public consumption, and providing a wrapper method around it.</p>
<p>For example, here is a the relationship in the Result class,</p>
<pre class="brush: perl; title: ; notranslate">
__PACKAGE__-&gt;has_many(
  _date_range =&gt; 'DBICTest::Schema::CD',
  sub {
    my $args = shift;
    return (
      { &quot;$args-&gt;{foreign_alias}.artist&quot; =&gt; { -ident =&gt; &quot;$args-&gt;{self_alias}.artistid&quot; },
        -and =&gt; [
            &quot;$args-&gt;{foreign_alias}.year&quot;   =&gt; { '&gt;' =&gt; \&quot;?&quot; },
            &quot;$args-&gt;{foreign_alias}.year&quot;   =&gt; { '&lt;' =&gt; \&quot;?&quot; },
        ],
      }
    );
  }
);
</pre>
<p>And then a method to exploit it in the ResultSet class.</p>
<pre class="brush: perl; title: ; notranslate">
sub join_date_range
{
    my $self = shift;
    my $start = shift;
    my $end = shift;
    $self-&gt;search(undef, {
        join =&gt; ['_by_name'],
        bind =&gt; [ $start, $end ],
    });
}
</pre>
<p>Then you can use it like this,</p>
<pre class="brush: perl; title: ; notranslate">
$schema-&gt;resultset(&quot;Artist&quot;)-&gt;join_date_range(1990, 1992)-&gt;search(...)-&gt;all;
</pre>
<p>You can of course do more complex joins too, even joining up multiple times.  All you need to do is specify all the necessary bind parameters.</p>
<p>The most impressive thing is that the joins work fine even with the chained searches that DBIC is so good at.  You can of course also do search_related and most of the usual links, you just need to specify the bind parameters.  </p>
<p>There are a few of caveats however.  This isn&#8217;t strictly speaking an intentional feature, or at least it wasn&#8217;t consciously designed to work this way (as far as I know).</p>
<ol>
<li>Attempting to do a prefetch across the join fails with the error <code>“Can't prefetch has_many _date_range (join cond too complex)”</code>.  <strong>Update: there is a new version of DBIC in the pipeline that fixes this.  Right now that version is <a href="https://metacpan.org/source/RIBASUSHI/DBIx-Class-0.08241-TRIAL/Changes">0.08241-TRIAL</a>.  The feature you&#8217;re probably looking for in the Changes file is &#8220;Multiple has_many prefetch&#8221; when checking if a new live release has it.</strong>
</li>
<li><del datetime="2013-02-25T06:45:44+00:00">You might have looked at that <code>-and =&gt; []</code> construction and thought that&#8217;s a bit pointless, a standard hash would be simpler and achieve the same effect.  </del>Unfortunately the bind parameters are positional, and a hash doesn&#8217;t have a guaranteed order.  That means you need to be extra careful with your query when you have multiple parameters you need to specify, to ensure the binds happen to the correct place holders. <strong>Update: as Peter Rabbitson pointed out, it&#8217;s not actually that simple.  DBIC does try to make sure you have a guaranteed order by sorting the keys of the hashes so that it always produces the same SQL.  This means that you probably just need to try it and see which order in which it requires the parameters most of the time.</strong>
</li>
<li><strong>Update:  I was incorrect about not being able to use <a href="https://metacpan.org/module/DBIx::Class::ResultSet#DBIC-BIND-VALUES">extra binding</a> information with bind, the syntax Peter Rabbitson suggested works perfectly. i.e. <code>bind =&gt; [ [ $opt1 =&gt; $val1 ], [ $opt =&gt; $val 2 ]… ]</code></strong><del datetime="2013-02-25T06:45:44+00:00">The final caveat is that the bind parameters don&#8217;t currently take any extra type information.  Normally most of the places you are exposed directly to bindings you can <a href="https://metacpan.org/module/DBIx::Class::ResultSet#DBIC-BIND-VALUES">specify types</a> in order to help DBIC create the correct query.  It doesn&#8217;t appear to be possible to provide that information via the bind parameter on a search.</del></li>
</ol>
<p>This isn&#8217;t strictly a documented feature, but hopefully it&#8217;s helpful to a few people.  If you’re wondering why you’d need to do this at all, consider the difference between these two queries.</p>
<pre class="brush: sql; title: ; notranslate">
SELECT *
FROM a LEFT JOIN b ON a.id = b.owner_id AND a.name = b.name
</pre>
<p>and </p>
<pre class="brush: sql; title: ; notranslate">
SELECT *
FROM a LEFT JOIN b ON a.id = b.owner_id
WHERE a.name = b.name
</pre>
<blockquote><p>In the course of figuring this out, I also discovered the <code>-ident</code> key which indicates that you’re dealing with something like a column name, and should therefore be quoted if you have column name quoting turned on.  A handy feature to go along with using references to pass in literal bits of SQL.
</p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/695/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/695/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=695&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2013/02/14/adhoc-parameters-to-joins-in-dbixclass/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>GDB</title>
		<link>http://colinnewell.wordpress.com/2013/02/07/gdb/</link>
		<comments>http://colinnewell.wordpress.com/2013/02/07/gdb/#comments</comments>
		<pubDate>Thu, 07 Feb 2013 23:14:38 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=686</guid>
		<description><![CDATA[I only occasionally use gdb so I end up spending my time relearning it each time.  Hopefully these notes will make that process easier in the future.  This document assumes you already know the s and n commands and how to list the source code (l).  These are the commands I don’t know off by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=686&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I only occasionally use gdb so I end up spending my time relearning it each time.  Hopefully these notes will make that process easier in the future.  This document assumes you already know the <code>s</code> and <code>n</code> commands and how to list the source code (<code>l</code>).  These are the commands I don’t know off by heart, and have found useful.  It&#8217;s just a subset, but a useful one so far.  It’s also well worth downloading the <a href="https://www.gnu.org/software/gdb/documentation/">manual</a> pdf.</p>
<p>To compile a program with the debug symbols use the <code>-ggdb</code> command line switch.</p>
<p>Here is a basic summary of some of the useful commands.</p>
<pre class="brush: bash; title: ; notranslate">
set args [command line args]
r
bt # stack trace
up/down # move up the stack
p # print
p/x # display hex value
p/x (int[5]) *0xffffd320 # display values at address as integers
x address # display contents of memory location
x/16b address # display 16 bytes
x/32cb address # display 16 bytes as characters
x/5i address # display as instructions (i.e. assembly)
x/5i $pc - 6 # displays current code
info registers # display register contents
$pc, $sp # program counter and stack pointer, can also use $eip
set $sp += 4 # add 4 to stack pointer
set var variablename = 4 # set a program variable
set {int}0x800321 = 4 # sets memory location 0x800321 to 4
j 0x32211 # jump to 0x32211
find # search memory
b *0xaddress # set breakpoint at an address (b nnn sets at a source line)
</pre>
<p>I’ve also been using lxc a lot recently and being able to create an x86 based machine is useful.  This creates a machine named x86 for testing x86 code.  The apt-get line is to be run on the machine once it’s loaded to install gdb and the compilers.</p>
<pre class="brush: bash; title: ; notranslate">
lxc-create -n x86 -t ubuntu -- -a i386
apt-get install build-essential gdb
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/686/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/686/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=686&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2013/02/07/gdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>Catalyst Config Hack</title>
		<link>http://colinnewell.wordpress.com/2012/11/16/catalyst-config-hack/</link>
		<comments>http://colinnewell.wordpress.com/2012/11/16/catalyst-config-hack/#comments</comments>
		<pubDate>Fri, 16 Nov 2012 20:29:30 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[catalyst]]></category>
		<category><![CDATA[perl5]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=681</guid>
		<description><![CDATA[With a lot of modules for our Catalyst systems we have separate models. We then use a subset of them in a single application, and it makes sense to actually store all those database models in a single physical database. This means we end up with a lot of duplicate model config keys in our [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=681&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>With a lot of modules for our Catalyst systems we have separate models.  We then use a subset of them in a single application, and it makes sense to actually store all those database models in a single physical database.  This means we end up with a lot of duplicate model config keys in our catalyst config.  </p>
<pre class="brush: xml; title: ; notranslate">
&lt;Model::Processor&gt;
    connect_info dbi:Pg:dbname=app_db;host=db-server
&lt;/Model::Processor&gt;
&lt;Model::SysParams&gt;
    connect_info dbi:Pg:dbname=app_db;host=db-server
&lt;/Model::SysParams&gt;
&lt;Model::AuditTrail&gt;
    connect_info dbi:Pg:dbname=app_db;host=db-server
&lt;/Model::AuditTrail&gt;
</pre>
<p>A lot of database configurations aren’t just a single line, and you end up spending forever copy/pasting and then modifying the config.  I wanted to come up with a way to avoid all that repetition.</p>
<p>The <a href="http://search.cpan.org/perldoc?Catalyst%3A%3APlugin%3A%3AConfigLoader">Catalyst::Plugin::ConfigLoader</a> provides two potential hooks for things to do after the configuration has been loaded.  One is a finalize_config, the other is config_substitutions, via the substitutions settings.  Because we are using <a href="http://search.cpan.org/perldoc?CatalystX%3A%3AAppBuilder">CatalystX::AppBuilder</a> the finalize_config doesn’t appear to be hookable, or at least I didn’t figure out how to.  The substitutions is however perfectly usable because that just requires config setup in code.  </p>
<pre class="brush: perl; title: ; notranslate">
   $config-&gt;{'Plugin::ConfigLoader'}-&gt;{substitutions} = {
        duplicate =&gt; sub {
            my $c = shift;
            my $from = shift;
            my $to = shift;
            $c-&gt;config-&gt;{$to} = $c-&gt;config-&gt;{$from};
        }
    };
</pre>
<p>Then this lets me do this in the config file.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;Model::Processor&gt;
    connect_info dbi:Pg:dbname=app_db;host=db-server
    connect_info dbusername
    connect_info dbpassword
    &lt;connect_info&gt;
      quote_char &quot;\&quot;&quot;
      name_sep .
    &lt;/connect_info&gt;
&lt;/Model::Processor&gt;

__duplicate(Model::TokenProcessor,Model::SysParams)__
__duplicate(Model::TokenProcessor,Model::AuditTrail)__
__duplicate(Model::TokenProcessor,Model::AuthDB)__
</pre>
<p>This copies the configuration specified for the Processor to the SysParams, AuditTrail and AuthDB model config settings.  This happens right after the configuration has been loaded, and before the models are loaded so all the settings are there just in time.  That saves me lots of copy/paste, and even more editing.  I don’t even need to copy those directives into my _local.conf because the _local.conf settings for the Processor model will be what get copied.  </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/681/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/681/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=681&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2012/11/16/catalyst-config-hack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>Lenovo S205 wifi woes</title>
		<link>http://colinnewell.wordpress.com/2012/10/20/lenovo-s205-wifi-woes/</link>
		<comments>http://colinnewell.wordpress.com/2012/10/20/lenovo-s205-wifi-woes/#comments</comments>
		<pubDate>Sat, 20 Oct 2012 13:51:30 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Configuration]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[s205]]></category>
		<category><![CDATA[wifi]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=663</guid>
		<description><![CDATA[I finally decided to stop mincing about my netbooks wifi compatibility woes with Linux and fix them.  My friends and colleagues should be relieved that my constant moaning about the worst linux compatibility problem I’ve had with a computer in a long time will be over.  I should definitely thank them though, as it was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=663&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I finally decided to stop mincing about my netbooks wifi compatibility woes with Linux and fix them.  My friends and colleagues should be relieved that my constant moaning about the worst linux compatibility problem I’ve had with a computer in a long time will be over.  I should definitely thank them though, as it was their suggestions and research that has helped me solve this issue in the end.</p>
<p>The core problem I’ve had, along with a lot of other S205 users, is that the wifi doesn’t work out of the box with Linux.  This won’t affect every S205, just those that report their wifi hardware switch in a particular way.  The irony is that the wifi driver is loaded and works fine, it’s just that the kernel believes that the hardware disable switch for the wifi has been permanently set to disabled.  </p>
<p>As with lots of manufacturers, Lenovo produce their laptops with lots of variants, so you can choose the specification that suits you.  There may also be variants based on what parts they decided to put under the hood, even though two computers may functionally have the same spec.  They quite handily label these differences as models.  Commonly things like 10382JG and 1038DPG (Lenovo tend to use a consistent length string for the model info which also turns out to be handy).  You can find this model name here, <code>/sys/class/dmi/id/product_name</code>.</p>
<p>This bug appears to reference my problem, but unfortunately, it didn’t actually cure *my* problem.  </p>
<ul>
<li><a href="https://bugs.launchpad.net/ubuntu/+source/linux/+bug/875659">https://bugs.launchpad.net/ubuntu/+source/linux/+bug/875659</a>
</li>
<li><a href="https://bugzilla.kernel.org/show_bug.cgi?id=43007#c11">https://bugzilla.kernel.org/show_bug.cgi?id=43007#c11</a>
</li>
<li><a href="https://bugzilla.kernel.org/attachment.cgi?id=78621">https://bugzilla.kernel.org/attachment.cgi?id=78621</a>
</li>
</ul>
<p>Those 3 links are various relevant bugs and patches, but a quick comparison of the models revealed that this didn’t fix my problem, simply because my model was different.  Looking at that patch shows that there is a quirks mechanism to detect  the S205 based on the product name.  If that’s the case then it looks at EC register 0&#215;78 for the hardware switch.</p>
<p>The first thing to do was to confirm whether my computer used that same register, in which case I’d just need to enable the quirk.  Otherwise, I’d have to delve deeper, and add my own.</p>
<p>I downloaded acer_ec.pl from this site &#8211; <a href="http://code.google.com/p/aceracpi/wiki/EmbeddedController">http://code.google.com/p/aceracpi/wiki/EmbeddedController</a>.  And gave it a run.</p>
<p>When I flipped the hardware switch it did appear to signal using the 0&#215;78 EC register as suggested in the patch.  Since it did, it looked like all I’d need to do is enable the quirk the patch is designed to enable.  </p>
<p><strong>Here be dragons &#8211; don’t do this unless you’ve read it all, and made backups.</strong></p>
<p>I then started to prepare my computer to build kernel modules before laziness kicked in.  I found my <code>acer-wmi.ko</code> module in <code>/lib/modules/$(uname -r)/kernel/drivers/platform/x86/</code> and  took a backup of it.  I then took a second copy of the file and modified it.  I found the existing reference to 10382JG and changed the 2 letters ‘2J’ at the end to DP to make it specify my module.  This was possible because the new value I wanted was an identical length to the existing one.  I then did an <code>ls -l</code> to check the files were the same length as a sanity check.  Once I copied the modified module back to it’s original location and rebooted I had wifi!  Theory confirmed.</p>
<blockquote><p>The tool I used to modify the file was bless, since obviously using a text editor for that is generally not a good idea.  Just remember to make sure you switch to overwrite mode and that you don’t append any characters to the file.
</p></blockquote>
<p>Now all I need to do is ask the nice kernel people to add a reference to the quirk for this model.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/663/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/663/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=663&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2012/10/20/lenovo-s205-wifi-woes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>Skippng Python unit tests if a dependency is missing (fixed)</title>
		<link>http://colinnewell.wordpress.com/2012/09/19/skippng-python-unit-tests-if-a-dependency-is-missing-fixed/</link>
		<comments>http://colinnewell.wordpress.com/2012/09/19/skippng-python-unit-tests-if-a-dependency-is-missing-fixed/#comments</comments>
		<pubDate>Wed, 19 Sep 2012 21:10:42 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unittest]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=654</guid>
		<description><![CDATA[I got some feedback on my previous post about skipping tests in python unittests pointing out my solution was flawed.  As Mu Mind pointed out, the denizens of stackoverflow pointed out the solution has a problem when run directly from python.  At first I didn’t realise how flawed; technically I had run my tests via [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=654&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I got some feedback on my <a href="http://colinnewell.wordpress.com/2012/08/31/skippng-python-unit-tests-if-a-dependency-is-missing/">previous post</a> about skipping tests in python unittests pointing out my solution was flawed.  As <a href="http://stackoverflow.com/users/307705/mu-mind">Mu Mind</a> pointed out, the denizens of stackoverflow pointed out the solution has a <a href="http://stackoverflow.com/questions/12487532/how-do-i-skip-a-whole-python-unittest-module-at-run-time#comment16804432_12488542">problem</a> when run directly from python.  At first I didn’t realise how flawed; technically I had run my tests via python and nosetest regularly.  I just hadn’t realised that I’d never run the tests via python when I was missing the dependency.  If you do that you get this ugly error,</p>
<pre class="brush: python; title: ; notranslate">
ERROR: test_openihm_gui_interface_db_mixin (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_openihm_gui_interface_db_mixin
Traceback (most recent call last):
  File &quot;python2.7/unittest/loader.py&quot;, line 252, in _find_tests
    module = self._get_module_from_name(name)
  File &quot;python2.7/unittest/loader.py&quot;, line 230, in _get_module_from_name
    __import__(name)
  File &quot;tests/test_openihm_gui_interface_db_mixin.py&quot;, line 6, in 
    raise unittest.SkipTest(&quot;Need PyQt4 installed to do gui tests&quot;)
SkipTest: Need PyQt4 installed to do gui tests
</pre>
<p>It does tell you what the problem was clearly, but it really wasn’t the intention.  The idea was to silently skip the test.</p>
<p>From the answers and comments on the stackoverflow post I stitched together this ugly but hopefully working solution for whatever your unit test runner of choice is.</p>
<pre class="brush: python; title: ; notranslate">
import unittest
try:
    import PyQt4
    # the rest of the imports


    class TestDataEntryMixin(unittest.TestCase):
        def test_somefeature(self):
            # actual tests go here.

except ImportError, e:
    if e.message.find('PyQt4') &gt;= 0:
        class TestMissingDependency(unittest.TestCase):

            @unittest.skip('Missing dependency - ' + e.message)
            def test_fail():
                pass
    else:
        raise

if __name__ == '__main__':
    unittest.main()
</pre>
<p>I dislike the fact that I can’t hide away the logic at the top, but surrounding your whole test with the code works.  Then if the import fails we create a dummy test case that does a skip to indicate the problem.  I&#8217;ve also tried to ensure that we only catch the exception we&#8217;re expecting, and pass through any we aren&#8217;t.</p>
<p>Now if you run the tests in verbose mode you’ll see this when there is a missing dependency.</p>
<pre class="brush: plain; title: ; notranslate">
test_fail (test_openihm_gui_interface_mixins.TestMissingDependency) ... skipped 'Missing dependency - No module named PyQt4'
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/654/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/654/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=654&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2012/09/19/skippng-python-unit-tests-if-a-dependency-is-missing-fixed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>Hooking into the OpenERP ORM</title>
		<link>http://colinnewell.wordpress.com/2012/09/19/hooking-into-the-openerp-orm/</link>
		<comments>http://colinnewell.wordpress.com/2012/09/19/hooking-into-the-openerp-orm/#comments</comments>
		<pubDate>Wed, 19 Sep 2012 20:07:05 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[openerp]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=648</guid>
		<description><![CDATA[I’ve been hooking into the OpenERP ORM layer of a few of the models to add full text search via an external engine. Thanks to the way OpenERP is structured that appears to be quite a reliable approach. As I was doing it I found that I wanted a common set of hooks on several [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=648&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I’ve been hooking into the OpenERP ORM layer of a few of the models to add full text search via an external engine.  Thanks to the way OpenERP is structured that appears to be quite a reliable approach.  As I was doing it I found that I wanted a common set of hooks on several models.  Doing that suggested I should refactor the hooks to a mixin or a base class.  After playing about with my OpenERP module I’ve come to the conclusion that creating a base class seems to be the most reliable way to hook the methods.  The one trick you need to be aware of is the <code>_register</code> flag that you want to set to <code>False</code> for your base class.</p>
<pre class="brush: python; title: ; notranslate">
class search_base(osv.osv):

    _register = False

    def write(self, cr, user, ids, vals, context=None):
       success = super(search_base, self).write(cr, user, ids,
                                                    vals, context)
       … do stuff
       return success


class product_template_search(search_base):
    _name = &quot;product.template&quot;
    _inherit = &quot;product.template&quot;
    _register = True


class product_search(search_base):
    _name = &quot;product.product&quot;
    _inherit = &quot;product.product&quot;
    _register = True

product_search()
product_template_search()
</pre>
<p>Without that you’ll end up with the orm whining that the <code>search_base</code> class has no <code>_name</code> attribute as it tries to register it as a model.</p>
<pre class="brush: python; title: ; notranslate">
openerp.osv.orm: The class search_base has to have a _name attribute
openerp.netsvc: ValueError
The class search_base has to have a _name attribute
&gt; /usr/lib/pymodules/python2.7/openerp/osv/orm.py(967)__init__()
-&gt; raise except_orm('ValueError', msg)
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/648/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/648/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=648&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2012/09/19/hooking-into-the-openerp-orm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>Skippng Python unit tests if a dependency is missing</title>
		<link>http://colinnewell.wordpress.com/2012/08/31/skippng-python-unit-tests-if-a-dependency-is-missing/</link>
		<comments>http://colinnewell.wordpress.com/2012/08/31/skippng-python-unit-tests-if-a-dependency-is-missing/#comments</comments>
		<pubDate>Fri, 31 Aug 2012 21:13:16 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=641</guid>
		<description><![CDATA[Update: this is a flawed method of skipping the tests, I&#8217;ve written up an improved version based on the feedback I received. In all the examples of using python unittest to skip tests I’ve not seen anyone explaining how to skip tests if a library isn’t installed. The simplest way to do it appears to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=641&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong>Update: this is a flawed method of skipping the tests, I&#8217;ve written up an <a href="http://colinnewell.wordpress.com/2012/09/19/skippng-python-unit-tests-if-a-dependency-is-missing-fixed/" title="Skippng Python unit tests if a dependency is missing (fixed)">improved version</a> based on the feedback I received.</strong></p>
<p>In all the examples of using python <a href="http://docs.python.org/library/unittest.html">unittest</a> to skip tests I’ve not seen anyone explaining how to skip tests if a library isn’t installed.</p>
<p>The simplest way to do it appears to be to simply try to import the relevant libraries and catch the exception thrown if the library isn’t there.</p>
<pre class="brush: python; title: ; notranslate">
import unittest
try:
    import PyQt4.QtCore
    import PyQt4.QtGui
except:
    raise unittest.SkipTest(&quot;Need PyQt4 installed to do gui tests&quot;)
</pre>
<p>This example at the top of a test file simply skips all the tests if PyQt4 isn’t available.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/641/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/641/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=641&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2012/08/31/skippng-python-unit-tests-if-a-dependency-is-missing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>Perlbrew with options like threading</title>
		<link>http://colinnewell.wordpress.com/2012/04/24/perlbrew-with-options-like-threading/</link>
		<comments>http://colinnewell.wordpress.com/2012/04/24/perlbrew-with-options-like-threading/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 08:11:33 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[perl5]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=634</guid>
		<description><![CDATA[This blog post on the *n*x blog gives a great description of how to install perl with threading, something that you need to do if you want to run padre. The only thing I’d add that you can use the &#8211;as to install perl with an alias. This is useful if you want to build [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=634&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This <a href="http://nxadm.wordpress.com/2010/12/10/get-the-latest-perl-release-with-perlbrew/">blog post</a> on the <a href="http://nxadm.wordpress.com">*n*x blog</a> gives a great description of how to install perl with threading, something that you need to do if you want to run padre.   </p>
<p>The only thing I’d add that you can use the &#8211;as to install perl with an alias.  This is useful if you want to build a threaded version of a perl you already have installed.  You can simply do,</p>
<pre class="brush: bash; title: ; notranslate">
perlbrew install perl-5.14.2 -Dusethreads -Duselargefiles -Dcccdlflags=-fPIC -Doptimize=-O2 -Duseshrplib -Duse64bitall -Darchname=x86_64-linux-gnu -Dccflags=-DDEBIAN --as threaded-perl-5.14.2
</pre>
<p>(Note that I’ve customised this for my Ubuntu 64 bit os).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/634/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/634/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=634&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2012/04/24/perlbrew-with-options-like-threading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>Perl debugger</title>
		<link>http://colinnewell.wordpress.com/2012/02/14/perl-debugger/</link>
		<comments>http://colinnewell.wordpress.com/2012/02/14/perl-debugger/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 15:18:22 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[perl5]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=622</guid>
		<description><![CDATA[Since writing my initial post on my settings for the Perl debugger I&#8217;ve found another setting that I&#8217;ve found to be invaluable. If you create a file, ~/.perldb it will load your settings each time, mine now has two lines on whatever machine I&#8217;m using, The dumpDepth=2 trick is something I picked up from Chisel&#8217;s [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=622&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Since writing my initial <a href="http://colinnewell.wordpress.com/2010/10/21/perl-debugger-tips/">post</a> on my settings for the Perl debugger I&#8217;ve found another setting that I&#8217;ve found to be invaluable.  If you create a file, ~/.perldb it will load your settings each time, mine now has two lines on whatever machine I&#8217;m using,</p>
<pre class="brush: perl; title: ; notranslate">
$DB::deep = 1000;
parse_options('dumpDepth=2');
</pre>
<p>The <code>dumpDepth=2</code> trick is something I picked up from <a href="http://blogs.perl.org/users/chisel/2011/10/simple-config-for-the-perl-debugger.html">Chisel&#8217;s blog post</a> on the subject and simply limits your x to a depth of 2 by default.  This makes life a lot simpler and means I get caught out by accidentally dumping the entire state of an application less often.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/622/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/622/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=622&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2012/02/14/perl-debugger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
		<item>
		<title>Faking it with Test::WWW::Mechanize::PSGI</title>
		<link>http://colinnewell.wordpress.com/2012/02/02/faking-it-with-testwwwmechanizepsgi/</link>
		<comments>http://colinnewell.wordpress.com/2012/02/02/faking-it-with-testwwwmechanizepsgi/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 22:13:47 +0000</pubDate>
		<dc:creator>colinnewell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[perl5]]></category>

		<guid isPermaLink="false">http://colinnewell.wordpress.com/?p=615</guid>
		<description><![CDATA[Or should that be getting real?  The Test::WWW::Mechanize::Catalyst module has a really handy feature, with the CATALYST_SERVER environment variable you can set to point your tests at a real live server.  This is really handy for a couple of things.  One of those being monitoring real traffic for those cases where you can’t quite decide [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=615&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Or should that be getting real?  The <a href="http://search.cpan.org/perldoc?Test::WWW::Mechanize::Catalyst">Test::WWW::Mechanize::Catalyst</a> module has a really handy feature, with the <code>CATALYST_SERVER</code> environment variable you can set to point your tests at a real live server.  This is really handy for a couple of things.  One of those being monitoring real traffic for those cases where you can’t quite decide whether it’s your test or your server that’s broken.  You can fire up <a href="http://www.wireshark.org/">Wireshark</a> and watch the actual traffic going over the wire.</p>
<p>With <a href="http://search.cpan.org/perldoc?Test::WWW::Mechanize::PSGI">Test:::WWW:Mechanize::PSGI</a> there doesn’t appear to be that option, and <a href="http://search.cpan.org/perldoc?Test::WWW::Mechanize">Test::WWW::Mechanize</a> and the modules it wrap don’t really appear to have any simple way to provide that.  A look at the T:W:M:Catalyst module suggests it’s actually a nice chunk of work that they did to implement that feature.  Since I’m lazy and I wanted to solve a problem I came up with a simple way to fake it for now.  I converted the urls from simple <code>/path</code> to proper qualified <code><a href="http://localhost:5000/path" rel="nofollow">http://localhost:5000/path</a></code> urls in my tests then added a simple bit of code to flip between <code>Test::WWW:Mechanize</code> and <code>Test::WWW::Mechanize::PSGI</code>.</p>
<pre class="brush: perl; title: ; notranslate">
my $mech;
if($ENV{EXTERNAL_SERVER})
{
    $mech = Test::WWW::Mechanize-&gt;new;
}
else
{
    my $app = Plack::Util::load_psgi 'app.psgi';
    $mech = Test::WWW::Mechanize::PSGI-&gt;new( app =&gt; $app );
}
</pre>
<p>Now if I run the tests with <code>EXTERNAL_SERVER=1</code> it goes to a real server rather than straight to code.  That means I can listen on the loopback adaptor in Wireshark and see what’s actually going over the wire simply.  It’s not as neat as the <code>CATALYST_SERVER</code> feature, but it does for now.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/colinnewell.wordpress.com/615/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/colinnewell.wordpress.com/615/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=colinnewell.wordpress.com&#038;blog=1215813&#038;post=615&#038;subd=colinnewell&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://colinnewell.wordpress.com/2012/02/02/faking-it-with-testwwwmechanizepsgi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1bae18aebc08108ae3c9b58f45407e0d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">colinnewell</media:title>
		</media:content>
	</item>
	</channel>
</rss>
