<?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>Xster.net &#187; Python</title>
	<atom:link href="http://tech.xster.net/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.xster.net</link>
	<description>Never relearn twice</description>
	<lastBuildDate>Fri, 20 Jan 2012 21:11:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Django Compress Static Files and Compile CSS</title>
		<link>http://tech.xster.net/tips/django-compress-static-files-and-compile-css/</link>
		<comments>http://tech.xster.net/tips/django-compress-static-files-and-compile-css/#comments</comments>
		<pubDate>Tue, 10 May 2011 01:49:32 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[compress]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[static]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=373</guid>
		<description><![CDATA[I started off looking for a request time compiler of LESS for Django and initially found django-css which seems to serve the purpose great. Compressing static files on the fly is definitely a nice added bonus as well. It does so by containing a fork of django_compressor. But on further inspections, I jumped ship. The [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I started off looking for a request time compiler of <a href="http://lesscss.org/">LESS</a> for Django and initially found <a href="http://github.com/dziegler/django-css">django-css</a> which seems to serve the purpose great. Compressing static files on the fly is definitely a nice added bonus as well. It does so by containing a fork of <a href="http://github.com/jezdez/django_compressor">django_compressor</a>. But on further inspections, I jumped ship. The original project, django_compressor, sees a more regular update and is now Django 1.3 ready while the &#8216;successor&#8217; isn&#8217;t. Funny thing is django_compressor supports compiling any CSS formats compilable via command line. With a better documentation overall, seems like the original has beat the sequel.</p>
<div class="shr-publisher-373"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fdjango-compress-static-files-and-compile-css%2F' data-shr_title='Django+Compress+Static+Files+and+Compile+CSS'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fdjango-compress-static-files-and-compile-css%2F' data-shr_title='Django+Compress+Static+Files+and+Compile+CSS'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/django-compress-static-files-and-compile-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django Log to File</title>
		<link>http://tech.xster.net/tips/django-log-to-file/</link>
		<comments>http://tech.xster.net/tips/django-log-to-file/#comments</comments>
		<pubDate>Mon, 09 May 2011 03:36:06 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[FileHandler]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=369</guid>
		<description><![CDATA[In the official Django logging docs, it wasn&#8217;t very clear about how to log to files. As you can understand, Django can use any Python logging classes that are all listed here. One of them is FileHandler. To use it, just add this to your settings.py LOGGING = &#123; ... 'handlers': &#123; ... 'file':&#123; 'level': [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>In the official Django <a href="http://docs.djangoproject.com/en/dev/topics/logging/">logging docs</a>, it wasn&#8217;t very clear about how to log to files. As you can understand, Django can use any Python logging classes that are all listed <a href="http://docs.python.org/library/logging.handlers.html">here</a>. One of them is FileHandler. To use it, just add this to your settings.py<span id="more-369"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">LOGGING = <span style="color: black;">&#123;</span>
    ...
    <span style="color: #483d8b;">'handlers'</span>: <span style="color: black;">&#123;</span>
        ...
	<span style="color: #483d8b;">'file'</span>:<span style="color: black;">&#123;</span>
	    <span style="color: #483d8b;">'level'</span>: <span style="color: #483d8b;">'DEBUG'</span>,
	    <span style="color: #483d8b;">'class'</span>: <span style="color: #483d8b;">'logging.FileHandler'</span>,
	<span style="color: black;">&#125;</span>
    <span style="color: black;">&#125;</span>,
    ...
<span style="color: black;">&#125;</span></pre></div></div>

<p>But the FileHandler class constructor needs more parameters. To add them, you have to add the parameter name key with its value into the dictionary.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">LOGGING = <span style="color: black;">&#123;</span>
    ...
    <span style="color: #483d8b;">'handlers'</span>: <span style="color: black;">&#123;</span>
        ...
	<span style="color: #483d8b;">'file'</span>:<span style="color: black;">&#123;</span>
	    <span style="color: #483d8b;">'level'</span>: <span style="color: #483d8b;">'DEBUG'</span>,
	    <span style="color: #483d8b;">'class'</span>: <span style="color: #483d8b;">'logging.FileHandler'</span>,
            <span style="color: #483d8b;">'filename'</span>: <span style="color: #483d8b;">'debug.log'</span>,
	<span style="color: black;">&#125;</span>
    <span style="color: black;">&#125;</span>,
    ...
<span style="color: black;">&#125;</span></pre></div></div>

<div class="shr-publisher-369"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fdjango-log-to-file%2F' data-shr_title='Django+Log+to+File'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fdjango-log-to-file%2F' data-shr_title='Django+Log+to+File'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/django-log-to-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validate and Format Addresses using Google API</title>
		<link>http://tech.xster.net/tips/validate-and-format-addresses-using-google-api/</link>
		<comments>http://tech.xster.net/tips/validate-and-format-addresses-using-google-api/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 18:54:38 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[address]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[Geocoding]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[pygeocoder]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[validate]]></category>
		<category><![CDATA[verify]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=356</guid>
		<description><![CDATA[Suppose you want to make some web app that lets users input addresses. I would be nice to Weed out minor misspelling Standardise format (proper capitalisation, abbreviated province/state or full name etc) Input in freeform but store civic number, street name, city etc separately Verify the address exists of course Some governmental entities provide this [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><div>
<p>Suppose you want to make some web app that lets users input addresses. I would be nice to</p>
<ol>
<li>Weed out minor misspelling</li>
<li>Standardise format (proper capitalisation, abbreviated province/state or full name etc)</li>
<li>Input in freeform but store civic number, street name, city etc separately</li>
<li>Verify the address exists of course</li>
</ol>
<p>Some governmental entities provide this information via public APIs but if you want a uniform service, why not use Google?<span id="more-356"></span></p>
<p>Fortunately, <a href="http://twitter.com/thormitchell">Thor Mitchell</a>, a Google Product Manager, posted an excellent <a href="http://www.thialfi.org/work/api/validate.html">example code</a> that does all of the above using Google Maps JavaScript API. Even better, it&#8217;s with the new V3 so it&#8217;s not obsolete anytime soon. It&#8217;s originally posted <a href="http://www.telnet80.com/2010/09/address-validation-using-google-maps.html?showComment=1285726654178#c2804710605065263449">here</a> in the comments of a blog by <a href="https://profiles.google.com/gabe.sumner/about">Gabe Sumner</a>. It definitely deserves more recognition so here it is again as is:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&amp;lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta name=&quot;viewport&quot; content=&quot;initial-scale=1.0, user-scalable=no&quot; /&amp;gt;
&amp;lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot;/&amp;gt;
&amp;lt;title&amp;gt;Google Maps JavaScript API v3 Example: Map Simple&amp;lt;/title&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot; src=&quot;http://maps.google.com/maps/api/js?sensor=false&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;
&nbsp;
  var geocoder, map, marker;
  var defaultLatLng = new google.maps.LatLng(30,0);
&nbsp;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var mapOptions = {
      zoom: 0,
      center: defaultLatLng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(
      document.getElementById(&quot;map_canvas&quot;),
      mapOptions
    );
    marker = new google.maps.Marker();
  }
&nbsp;
  function validate() {
    clearResults();
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address }, function(results, status) {
      switch(status) {
        case google.maps.GeocoderStatus.OK:
          document.getElementById('valid').value = 'YES';
          document.getElementById('type').value = results[0].types[0];
          document.getElementById('result').value = results[0].formatted_address;
          mapAddress(results[0]);
          break;
        case google.maps.GeocoderStatus.ZERO_RESULTS:
          document.getElementById('valid').value = 'NO';
          break;
        default:
          alert(&quot;An error occured while validating this address&quot;)
      }
    });
  }
&nbsp;
  function clearResults() {
    document.getElementById('valid').value = '';
    document.getElementById('type').value = '';
    document.getElementById('result').value = '';
    map.setCenter(defaultLatLng);
    map.setZoom(0);
    marker.setMap(null);
  }
&nbsp;
  function mapAddress(result) {
    marker.setPosition(result.geometry.location);
    marker.setMap(map);
    map.fitBounds(result.geometry.viewport);
  }
&amp;lt;/script&amp;gt;
&amp;lt;style&amp;gt;
body {
  font-family: sans-serif;
}
&nbsp;
#address {
  width:300px;
  height:150px;
  float: left;
  margin: 10px;
}
&nbsp;
#map_canvas {
  width:256px;
  height:150px;
  margin: 10px;
}
&nbsp;
#validate {
  clear: both;
}
&nbsp;
#results {
  margin-top: 10px;
}
&amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body onload=&quot;initialize()&quot;&amp;gt;
  &amp;lt;div&amp;gt;Address&amp;lt;/div&amp;gt;
  &amp;lt;textarea type=&quot;text&quot; id=&quot;address&quot;&amp;gt;&amp;lt;/textarea&amp;gt;
  &amp;lt;div id=&quot;map_canvas&quot;&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;div id=&quot;validate&quot;&amp;gt;&amp;lt;input type=&quot;button&quot; value=&quot;Validate&quot; onClick=&quot;validate()&quot;&amp;gt;&amp;lt;/input&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;div id=&quot;results&quot;&amp;gt;
    &amp;lt;table&amp;gt;
      &amp;lt;tr&amp;gt;&amp;lt;td align=&quot;right&quot;&amp;gt;Valid:&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;lt;input type=&quot;text&quot; id=&quot;valid&quot; size=&quot;60&quot;&amp;gt;&amp;lt;/input&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;
      &amp;lt;tr&amp;gt;&amp;lt;td align=&quot;right&quot;&amp;gt;Matched:&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;lt;input type=&quot;text&quot; id=&quot;type&quot; size=&quot;60&quot;&amp;gt;&amp;lt;/input&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;
      &amp;lt;tr&amp;gt;&amp;lt;td align=&quot;right&quot;&amp;gt;Result:&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;lt;input type=&quot;text&quot; id=&quot;result&quot; size=&quot;60&quot;&amp;gt;&amp;lt;/input&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;
    &amp;lt;/table&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;</pre></div></div>

<p>Only 100 lines! It uses Google&#8217;s GeoCoding JavaScript API and you can fancy it up easily with jQuery. In the validate function, the results variable contains of course json formatted responses as described in Google&#8217;s <a href="http://code.google.com/apis/maps/documentation/geocoding/#GeocodingResponses">docs</a>. Looking at &#8216;address_components&#8217; will give you the properly formatted address separated by parts which you can put into database.</p>
<p>For a backend solution, you can use <a href="http://code.xster.net/pygeocoder">pygeocoder</a>, a Python library, to validate addresses.</p>
</div>
<div class="shr-publisher-356"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fvalidate-and-format-addresses-using-google-api%2F' data-shr_title='Validate+and+Format+Addresses+using+Google+API'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fvalidate-and-format-addresses-using-google-api%2F' data-shr_title='Validate+and+Format+Addresses+using+Google+API'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/validate-and-format-addresses-using-google-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Debug with ipdb</title>
		<link>http://tech.xster.net/tips/python-debug-with-ipdb/</link>
		<comments>http://tech.xster.net/tips/python-debug-with-ipdb/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 18:30:47 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[breakpoint]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[ipdb]]></category>
		<category><![CDATA[IPython]]></category>
		<category><![CDATA[pdb]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=352</guid>
		<description><![CDATA[A quickie: import ipdb; ipdb.set_trace&#40;&#41; This puts a breakpoint in the code using ipdb. It has the advantage of having better formatted output, tab completion etc over the vanilla pdb]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>A quickie:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> ipdb<span style="color: #66cc66;">;</span> ipdb.<span style="color: black;">set_trace</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This puts a breakpoint in the code using ipdb. It has the advantage of having better formatted output, tab completion etc over the vanilla pdb</p>
<div class="shr-publisher-352"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpython-debug-with-ipdb%2F' data-shr_title='Python+Debug+with+ipdb'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpython-debug-with-ipdb%2F' data-shr_title='Python+Debug+with+ipdb'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/python-debug-with-ipdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PIL: Get RGB Value from GIF</title>
		<link>http://tech.xster.net/tips/pil-get-rgb-value-from-gif/</link>
		<comments>http://tech.xster.net/tips/pil-get-rgb-value-from-gif/#comments</comments>
		<pubDate>Sun, 03 Apr 2011 05:16:58 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[GIF]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[RGB]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=331</guid>
		<description><![CDATA[If you load a GIF file with PIL via Image.open(&#8216;giffile.gif&#8217;) and then try to look at its pixels, you would get integers instead of tuples since the GIF pixels refers to one of the 256 colours in the GIF colour palette. The palette would then contain the RGB value of the pixel. To avoid all [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>If you load a GIF file with PIL via Image.open(&#8216;giffile.gif&#8217;) and then try to look at its pixels, you would get integers instead of tuples since the GIF pixels refers to one of the 256 colours in the GIF colour palette. The palette would then contain the RGB value of the pixel.</p>
<p>To avoid all this hassle and just get RGB tuple directly:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">gif = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'giffile.gif'</span><span style="color: black;">&#41;</span>
rgbimage = GIF.<span style="color: black;">convert</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'RGB'</span><span style="color: black;">&#41;</span>
rgbimage.<span style="color: black;">getpixel</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">231</span>, <span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">54</span><span style="color: black;">&#41;</span></pre></div></div>

<div class="shr-publisher-331"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpil-get-rgb-value-from-gif%2F' data-shr_title='PIL%3A+Get+RGB+Value+from+GIF'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpil-get-rgb-value-from-gif%2F' data-shr_title='PIL%3A+Get+RGB+Value+from+GIF'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/pil-get-rgb-value-from-gif/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python Challenge Level 14: Italy – Code</title>
		<link>http://tech.xster.net/thoughts/python-challenge-level-14-italy-%e2%80%93-code/</link>
		<comments>http://tech.xster.net/thoughts/python-challenge-level-14-italy-%e2%80%93-code/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 03:35:29 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[NumPy]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[puzzle]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=325</guid>
		<description><![CDATA[Used a NumPy array. Doesn&#8217;t work if the diagonal+1s are white of course. import Image from numpy import array, zeros, uint8 &#160; line = Image.open&#40;'wire.png'&#41; # third dimension is for colours a = zeros&#40;&#40;100, 100, 3&#41;, dtype=uint8&#41; &#160; step = array&#40;&#91;0, 1&#93;&#41; position = array&#40;&#91;0, 0&#93;&#41; count = 0 &#160; # continue until when the [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Used a NumPy array. Doesn&#8217;t work if the diagonal+1s are white of course.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Image
<span style="color: #ff7700;font-weight:bold;">from</span> numpy <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">array</span>, zeros, uint8
&nbsp;
line = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'wire.png'</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># third dimension is for colours</span>
a = zeros<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>, dtype=uint8<span style="color: black;">&#41;</span>
&nbsp;
step = <span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
position = <span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
count = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># continue until when the next spot expected to be unfilled is filled</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#91;</span><span style="color: #008000;">tuple</span><span style="color: black;">&#40;</span>position<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
	<span style="color: #808080; font-style: italic;"># paint colour</span>
	a<span style="color: black;">&#91;</span><span style="color: #008000;">tuple</span><span style="color: black;">&#40;</span>position<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = <span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span>line.<span style="color: black;">getpixel</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>count, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># if reached a wall</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>position + step<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">99</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">min</span><span style="color: black;">&#40;</span>position + step<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#91;</span><span style="color: #008000;">tuple</span><span style="color: black;">&#40;</span>position + step<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
		<span style="color: #808080; font-style: italic;"># change direction</span>
		step = <span style="color: black;">&#40;</span><span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>step<span style="color: black;">&#41;</span> ^ <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span>-step<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">if</span> step<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">else</span> step<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># advance</span>
	position += step
	count += <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># convert to image</span>
Image.<span style="color: black;">fromarray</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span>.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'swirl.jpeg'</span><span style="color: black;">&#41;</span></pre></div></div>

</pre>
<div class="shr-publisher-325"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Fthoughts%2Fpython-challenge-level-14-italy-%25e2%2580%2593-code%2F' data-shr_title='Python+Challenge+Level+14%3A+Italy+%E2%80%93+Code'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Fthoughts%2Fpython-challenge-level-14-italy-%25e2%2580%2593-code%2F' data-shr_title='Python+Challenge+Level+14%3A+Italy+%E2%80%93+Code'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/thoughts/python-challenge-level-14-italy-%e2%80%93-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Convert Numpy Array to Tuple</title>
		<link>http://tech.xster.net/tips/how-to-convert-numpy-array-to-tuple/</link>
		<comments>http://tech.xster.net/tips/how-to-convert-numpy-array-to-tuple/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 01:43:00 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tuple]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=318</guid>
		<description><![CDATA[a = array&#40;&#40;1, 2, 3&#41;&#41; print a &#62;&#62;&#62; array&#40;&#91;1, 2, 3&#93;&#41; tuple&#40;a&#41; &#62;&#62;&#62; &#40;1, 2, 3&#41; Pretty lame right?]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic -->
<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">a = <span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">tuple</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Pretty lame right?</p>
<div class="shr-publisher-318"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fhow-to-convert-numpy-array-to-tuple%2F' data-shr_title='How+to+Convert+Numpy+Array+to+Tuple'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fhow-to-convert-numpy-array-to-tuple%2F' data-shr_title='How+to+Convert+Numpy+Array+to+Tuple'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/how-to-convert-numpy-array-to-tuple/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Free OBD2 Software for Mac</title>
		<link>http://tech.xster.net/tips/free-obd2-software-for-mac/</link>
		<comments>http://tech.xster.net/tips/free-obd2-software-for-mac/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 19:23:16 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[car]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[MacPorts]]></category>
		<category><![CDATA[mechanic]]></category>
		<category><![CDATA[OBD]]></category>
		<category><![CDATA[pyOBD]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[serial]]></category>
		<category><![CDATA[UART]]></category>
		<category><![CDATA[USB]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[X11]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=255</guid>
		<description><![CDATA[If you just picked up a generic OBD-II &#8211; USB interface on eBay and have a Mac, it is true that it is generally more convenient to access these hardware on Windows. Even embedded device developers tend to use Windows to develop against these generic FT232R chip based USB-UART devices simply because of more available [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>If you just picked up a generic OBD-II &#8211; USB interface on eBay and have a Mac, it is true that it is generally more convenient to access these hardware on Windows. Even embedded device developers tend to use Windows to develop against these generic FT232R chip based USB-UART devices simply because of more available supports. But worry not, it can be done on Mac (really on Linux with a more open-platform framework than Windows), it&#8217;s just a bit more complicated since it tends to come in be open-source source code rather than prepackaged self-sufficient binaries.<span id="more-255"></span></p>
<p>But in this case, it&#8217;s not too bad. You can use <a href="http://www.obdtester.com/pyobd">pyOBD</a>, an Python open-source project. You just need some packages from MacPorts</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> py26-serial
<span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> py26-wxpython</pre></div></div>

<p>Download and unpack the source code. And run directly with</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pythonw pyobd</pre></div></div>

<p>p.s. you need X11</p>
<div class="shr-publisher-255"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Ffree-obd2-software-for-mac%2F' data-shr_title='Free+OBD2+Software+for+Mac'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Ffree-obd2-software-for-mac%2F' data-shr_title='Free+OBD2+Software+for+Mac'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/free-obd2-software-for-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Convert ISO8601/UTC to Local Time</title>
		<link>http://tech.xster.net/tips/python-convert-iso8601utc-to-local-time/</link>
		<comments>http://tech.xster.net/tips/python-convert-iso8601utc-to-local-time/#comments</comments>
		<pubDate>Sat, 08 May 2010 23:51:54 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[astimezone]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[dateutil]]></category>
		<category><![CDATA[ISO]]></category>
		<category><![CDATA[ISO8601]]></category>
		<category><![CDATA[local]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[pytz]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[timezone]]></category>
		<category><![CDATA[UTC]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=230</guid>
		<description><![CDATA[Ok, I looked this up twice now&#8230; once too many Suppose you have a datetime of the form &#8220;2010-05-08T23:41:54.000Z&#8221; and you want a local datetime object import pytz, dateutil.parser utctime = dateutil.parser.parse&#40;&#34;2010-05-08T23:41:54.000Z&#34;&#41; localtime = utctime.astimezone&#40;pytz.timezone&#40;&#34;Canada/Eastern&#34;&#41;&#41; Boom]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Ok, I looked this up twice now&#8230; once too many</p>
<p>Suppose you have a datetime of the form &#8220;2010-05-08T23:41:54.000Z&#8221; and you want a local datetime object</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> pytz, dateutil.<span style="color: #dc143c;">parser</span>
utctime = dateutil.<span style="color: #dc143c;">parser</span>.<span style="color: black;">parse</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2010-05-08T23:41:54.000Z&quot;</span><span style="color: black;">&#41;</span>
localtime = utctime.<span style="color: black;">astimezone</span><span style="color: black;">&#40;</span>pytz.<span style="color: black;">timezone</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Canada/Eastern&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Boom</p>
<div class="shr-publisher-230"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpython-convert-iso8601utc-to-local-time%2F' data-shr_title='Python+Convert+ISO8601%2FUTC+to+Local+Time'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpython-convert-iso8601utc-to-local-time%2F' data-shr_title='Python+Convert+ISO8601%2FUTC+to+Local+Time'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/python-convert-iso8601utc-to-local-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploy PyQt Applications on Mac OS X with PyInstaller!</title>
		<link>http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/</link>
		<comments>http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 07:20:39 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[py2app]]></category>
		<category><![CDATA[PyInstaller]]></category>
		<category><![CDATA[PyQt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Snow Leopard]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=218</guid>
		<description><![CDATA[The interweb seem to incline on py2app when it come to deploying applications on mac. I&#8217;ve tried to make a single deployable .app file for my application for a long time trying to follow these instructions from ars technica. I&#8217;m not a hacker and just want to produce a deployable usable application for others to [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://www.pyinstaller.org/"><img class="alignright size-medium wp-image-221" title="PyInstaller" src="http://tech.xster.net/wp-content/uploads/2010/04/logo-300x60.png" alt="" width="240" height="48" /></a>The interweb seem to incline on <a href="http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html">py2app</a> when it come to deploying applications on mac. I&#8217;ve tried to make a single deployable .app file for my application for a long time trying to follow <a href="http://arstechnica.com/open-source/guides/2009/03/how-to-deploying-pyqt-applications-on-windows-and-mac-os-x.ars/2">these instructions from ars technica</a>. I&#8217;m not a hacker and just want to produce a deployable usable application for others to use. And it seems py2app from MacPorts wasn&#8217;t able to surmount the Snow Leopard&#8217;s 64-bit compatibility issue.</p>
<p>And then, I was slacking off while studying for my final and out of nowhere I found <a href="http://www.pyinstaller.org/">PyInstaller</a>&#8216;s explicit support for PyQt and its recent support for the mac. And after trying, almost everything works out without much of a kink. Credit goes to <a href="http://diotavelli.net/PyQtWiki/PyInstallerOnMacOSX">ChrisWayg</a> who produced an amazingly complete and up-to-date set of instructions to follow. I&#8217;m merely telling how my application did using his instructions (April 2010) and hopefully doing my part to draw more attention to the excellent <a href="http://www.pyinstaller.org/">PyInstaller</a>.<span id="more-218"></span></p>
<h3>My Application</h3>
<p>A PyQt application to help photographers apply custom watermarks in a manual but in an assisted way.</p>
<p>Uses:</p>
<ul>
<li>PIL 1.1.7</li>
<li>Qt4 4.6.2</li>
<li>PyQt4 4.7.2</li>
<li>SIP 4.10.1</li>
</ul>
<p>All from MacPorts on Python 2.6.5, OSX 10.6.3.</p>
<h3>Prepare PyInstaller</h3>
<p>PyInstaller is kinda special in that it&#8217;s not installed anywhere. It&#8217;s just a bunch of Python scripts that work on stuff in place. While you&#8217;re at it, read the <a href="http://www.pyinstaller.org/export/latest/tags/1.4/doc/Manual.html">manual</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.pyinstaller.org<span style="color: #000000; font-weight: bold;">/</span>trunk ~<span style="color: #000000; font-weight: bold;">/</span>PyInstaller</pre></div></div>

<p>Or put it somewhere you want.</p>
<p>Then you need to build the bootloaders (<strong>no longer necessary with the newest version!</strong>). If you&#8217;re making PyQt applications, you probably have XCode already. Anyway, you need it.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> source<span style="color: #000000; font-weight: bold;">/</span>linux
python Make.py
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ..
python Configure.py</pre></div></div>

<h3>Making your Application</h3>
<p><a href="http://www.pyinstaller.org/ticket/158">So far</a>, PyInstaller has a problem of not showing your application at the front when you run it. You can fix it in your own code by adding</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">form.raise_<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>right after</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">form.show<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>with &#8220;form&#8221; being whatever you called your instance of MainWindow().</p>
<p>Like with py2app, you gotta first make a &#8220;spec file&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python Makespec.py <span style="color: #660033;">--onefile</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>YourApplication.py</pre></div></div>

<p>This will make a YourApplication/YourApplication.spec file in your PyInstaller folder.<br />
To make it work on mac, add</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">sys</span>.<span style="color: #dc143c;">platform</span>.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;darwin&quot;</span><span style="color: black;">&#41;</span>:
    app = BUNDLE<span style="color: black;">&#40;</span>exe,
                 appname=<span style="color: #483d8b;">'WhateverYouWantToCallIt'</span>,
                 version=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></div></div>

<p>at the bottom.</p>
<p>For my application, it made 70 files inside the .app file on default. &#8211;onefile makes it more neat and 3 times smaller in size! but it does make it a bit slower. If you care to make it as fast as possible, leave out that option but you have to <a href="http://www.pyinstaller.org/ticket/155">fix a bug</a> yourself.</p>
<p>Finally, build the application!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python Build.py YourApplication<span style="color: #000000; font-weight: bold;">/</span>YourApplication.spec</pre></div></div>

<p>This will make your application and leave a YourApplication.app in your PyInstaller folder.</p>
<h3>Fix the Application</h3>
<p>Put your own nice icon on the application. Copy your graphic, right-click your application and choose Get Info, click the icon and paste the graphic.</p>
<p>Open the content of your application, open Contents/Info.plist, uncheck &#8220;Application is background only&#8221; and save. If you&#8217;re using a plain text editor, change LSBackgroundOnly to false.</p>
<p>There is only one last problem to deal with on mac. You have to copy the qt_menu.nib file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">locate</span> qt_menu.nib</pre></div></div>

<p>to find it. If you use MacPorts, it&#8217;s probably somewhere like /opt/local/libexec/qt4-mac/lib/QtGui.framework/Versions/4/Resources/qt_menu.nib</p>
<p>Copy it to your YourApplication.app/Contents/Resources</p>
<p>Boom, you can double click your application and it will run!</p>
<p>Ok, one last problem, I swear. When you run, it shows 2 icons on the dock&#8230; pretty ugly. <a href="http://www.pyinstaller.org/ticket/156">This hack</a> can fix it, but it&#8217;s not perfect as it leaves junk on your computer. Hopefully PyInstaller can address it soon.</p>
<h3>Conclusion</h3>
<p>The program is beautiful! You don&#8217;t have to worry about your Python plug-ins, your 64-bitness, anything (mostly). It just works. I have yet to figure out how to make 32-bit binaries on 64-bit systems and how to make upx work properly to reduce executable size. I&#8217;m currently running at 32mb for an application that really should be 1mb. But then again, it&#8217;s packaging a whole new GUI framework that doesn&#8217;t exist on the target computer with the application. Consider Skype (yes, it&#8217;s infinitely more complex than my thing), it is still 43mb.</p>
<div class="shr-publisher-218"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fdeploy-pyqt-applications-on-mac-os-x-with-pyinstaller%2F' data-shr_title='Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fdeploy-pyqt-applications-on-mac-os-x-with-pyinstaller%2F' data-shr_title='Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python&#8217;s Function Static Variable</title>
		<link>http://tech.xster.net/tips/pythons-function-static-variable/</link>
		<comments>http://tech.xster.net/tips/pythons-function-static-variable/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 05:25:11 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[attribute]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[static]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=211</guid>
		<description><![CDATA[So you want a variable that stays between different calls of a function. Not the sexiest thing ever but always handy in small programs. There&#8217;s tons of ways of doing this. You can embark on a quest to find the meaning of Pythonic or take this method that&#8217;s relatively simple: def a&#40;&#41;: if not hasattr&#40;a, [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>So you want a variable that stays between different calls of a function. Not the sexiest thing ever but always handy in small programs.</p>
<p>There&#8217;s tons of ways of doing this. You can embark on a quest to find the meaning of Pythonic or take this method that&#8217;s relatively simple:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> a<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>a, <span style="color: #483d8b;">&quot;b&quot;</span><span style="color: black;">&#41;</span>: a.<span style="color: black;">b</span> = <span style="color: #ff4500;">0</span>
    a.<span style="color: black;">b</span> += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> a.<span style="color: black;">b</span></pre></div></div>

<p>Calling a(), you&#8217;ll get 1, 2, 3, &#8230;</p>
<p>Note attribute &#8216;b&#8217; of &#8216;a&#8217; does not exist until you declare it for a first time. My main preference here is that &#8216;static&#8217; variables used this way does not spill onto the rest of your code. Also, it&#8217;s clean, no classes, no data structures.</p>
<div class="shr-publisher-211"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpythons-function-static-variable%2F' data-shr_title='Python%27s+Function+Static+Variable'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpythons-function-static-variable%2F' data-shr_title='Python%27s+Function+Static+Variable'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/pythons-function-static-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Suppress Scapy IPv6 Warning</title>
		<link>http://tech.xster.net/tips/suppress-scapy-ipv6-warning/</link>
		<comments>http://tech.xster.net/tips/suppress-scapy-ipv6-warning/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 22:45:13 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[ip]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[packet]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scapy]]></category>
		<category><![CDATA[sniffer]]></category>
		<category><![CDATA[TCP]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=207</guid>
		<description><![CDATA[When you run Scapy without a default IPv6 routing gateway, Scapy will display this annoying warning: WARNING: No route found for IPv6 destination :: (no default route?) You definitely don&#8217;t want to see it every time you run the script you built with Scapy. To get rid of it, simply add import logging logging.getLogger&#40;&#34;scapy.runtime&#34;&#41;.setLevel&#40;logging.ERROR&#41; before [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>When you run <a href="http://www.secdev.org/projects/scapy/" target="_blank">Scapy</a> without a default IPv6 routing gateway, Scapy will display this annoying warning:</p>
<pre>WARNING: No route found for IPv6 destination :: (no default route?)</pre>
<p>You definitely don&#8217;t want to see it every time you run the script you built with Scapy. To get rid of it, simply add</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">getLogger</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;scapy.runtime&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">setLevel</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">logging</span>.<span style="color: black;">ERROR</span><span style="color: black;">&#41;</span></pre></div></div>

<p>before importing Scapy to suppress all messages below error messages</p>
<div class="shr-publisher-207"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fsuppress-scapy-ipv6-warning%2F' data-shr_title='Suppress+Scapy+IPv6+Warning'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fsuppress-scapy-ipv6-warning%2F' data-shr_title='Suppress+Scapy+IPv6+Warning'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/suppress-scapy-ipv6-warning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Log Stdout to File</title>
		<link>http://tech.xster.net/tips/python-log-stdout-to-file/</link>
		<comments>http://tech.xster.net/tips/python-log-stdout-to-file/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 18:25:25 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[stdout]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=156</guid>
		<description><![CDATA[Python has the ability to alter its sys.stdout as to redirect its print commands to pretty much anything. If, for instance, you want to print to both standard output and to a log file, you can create a class to handle the stdout like such: class MyOutput&#40;&#41;: def __init__&#40;self, logfile&#41;: self.stdout = sys.stdout self.log = [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Python has the ability to alter its sys.stdout as to redirect its print commands to pretty much anything.</p>
<p>If, for instance, you want to print to both standard output and to a log file, you can create a class to handle the stdout like such:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> MyOutput<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, logfile<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">stdout</span> = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>logfile, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> write<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, text<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">stdout</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span>.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> close<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">stdout</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span> = MyOutput<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log.txt&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;blah blah blah&quot;</span></pre></div></div>

<p><span id="more-156"></span><br />
This class would implement stdout&#8217;s write and close functions. We then assign the stdout to an instance of this class. Inside the write/close implementations, we also add functionalities to write the texts in a log file as well. We flush the file buffer on each write because otherwise, we would only be able to see the file content when sys.stdout (therefore MyOutput) closes.</p>
<p>With this, outputs of print will both show in the screen and in the file log.txt</p>
<div class="shr-publisher-156"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpython-log-stdout-to-file%2F' data-shr_title='Python+Log+Stdout+to+File'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpython-log-stdout-to-file%2F' data-shr_title='Python+Log+Stdout+to+File'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/python-log-stdout-to-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PyQt and Snow Leopard</title>
		<link>http://tech.xster.net/tips/pyqt-and-snow-leopard/</link>
		<comments>http://tech.xster.net/tips/pyqt-and-snow-leopard/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 23:11:12 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[32]]></category>
		<category><![CDATA[64]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[PyQt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[SIP]]></category>
		<category><![CDATA[Snow Leopard]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=148</guid>
		<description><![CDATA[There are some 64-bit related issues when using PyQt and Snow Leopard. There&#8217;s the way to resolve it by reverting to 32-bits: Get the latest versions of PyQt and SIP. You need Qt installed of course. Configure SIP using python configure.py --arch i386 Configure PyQt using python configure.py --use-arch=i386 Finally, make sure your python is [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>There are some 64-bit related issues when using PyQt and Snow Leopard. There&#8217;s the way to resolve it by reverting to 32-bits:</p>
<ol>
<li>Get the <a href="http://www.riverbankcomputing.co.uk/software/pyqt/download">latest versions</a> of PyQt and SIP. You need Qt installed of course.</li>
<li>Configure SIP using</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python configure.py <span style="color: #660033;">--arch</span> i386</pre></div></div>

<li>Configure PyQt using</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python configure.py <span style="color: #660033;">--use-arch</span>=i386</pre></div></div>

<li>Finally, make sure your python is running in 32-bit mode because current Qt doesn&#8217;t support 64-bit mode. Add

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">VERSIONER_PYTHON_PREFER_32_BIT</span>=<span style="color: #c20cb9; font-weight: bold;">yes</span></pre></div></div>

<p> to your .bash_profile in your home directory</p>
</li>
<li>If your Python still refuses to run in 32 bit mode, try

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">arch</span> <span style="color: #660033;">-i386</span> python</pre></div></div>

</li>
</ol>
<div class="shr-publisher-148"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpyqt-and-snow-leopard%2F' data-shr_title='PyQt+and+Snow+Leopard'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpyqt-and-snow-leopard%2F' data-shr_title='PyQt+and+Snow+Leopard'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/pyqt-and-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PyQt Drag Images into List Widget for Thumbnail List</title>
		<link>http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/</link>
		<comments>http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/#comments</comments>
		<pubDate>Sun, 03 May 2009 05:47:18 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[drag]]></category>
		<category><![CDATA[drag-and-drop]]></category>
		<category><![CDATA[drop]]></category>
		<category><![CDATA[explorer]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[picture]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[PyQt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[QListWidget]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[thumbnail]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=61</guid>
		<description><![CDATA[This simple tutorial shows how you can create a program with Python and Qt to allow for image files from Explorer/Finder/Nautilus to be dropped in a list widget and create list items with thumbnails First we subclass a QListWidget to handle events class DragDropListWidget&#40;QListWidget&#41;: def __init__&#40;self, type, parent=None&#41;: super&#40;DragDropListWidget, self&#41;.__init__&#40;parent&#41; self.setAcceptDrops&#40;True&#41; self.setIconSize&#40;QSize&#40;72, 72&#41;&#41; &#160; def [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This simple tutorial shows how you can create a program with Python and Qt to allow for image files from Explorer/Finder/Nautilus to be dropped in a list widget and create list items with thumbnails</p>
<p style="text-align: center;"><img class="size-full wp-image-64  aligncenter" title="droppedthumbnails" src="http://tech.xster.net/wp-content/uploads/2009/05/picture-3.png" alt="droppedthumbnails" width="323" height="263" /></p>
<p>First we subclass a QListWidget to handle events<br />
<span id="more-61"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> DragDropListWidget<span style="color: black;">&#40;</span>QListWidget<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">type</span>, parent=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
		<span style="color: #008000;">super</span><span style="color: black;">&#40;</span>DragDropListWidget, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span>parent<span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">setAcceptDrops</span><span style="color: black;">&#40;</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">setIconSize</span><span style="color: black;">&#40;</span>QSize<span style="color: black;">&#40;</span><span style="color: #ff4500;">72</span>, <span style="color: #ff4500;">72</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> dragEnterEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: black;">mimeData</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hasUrls</span>:
			event.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			event.<span style="color: black;">ignore</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> dragMoveEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: black;">mimeData</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hasUrls</span>:
			event.<span style="color: black;">setDropAction</span><span style="color: black;">&#40;</span>Qt.<span style="color: black;">CopyAction</span><span style="color: black;">&#41;</span>
			event.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			event.<span style="color: black;">ignore</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> dropEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: black;">mimeData</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hasUrls</span>:
			event.<span style="color: black;">setDropAction</span><span style="color: black;">&#40;</span>Qt.<span style="color: black;">CopyAction</span><span style="color: black;">&#41;</span>
			event.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			l = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
			<span style="color: #ff7700;font-weight:bold;">for</span> url <span style="color: #ff7700;font-weight:bold;">in</span> event.<span style="color: black;">mimeData</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">urls</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
				l.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>url.<span style="color: black;">toLocalFile</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
			<span style="color: #008000;">self</span>.<span style="color: black;">emit</span><span style="color: black;">&#40;</span>SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;dropped&quot;</span><span style="color: black;">&#41;</span>, l<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			event.<span style="color: black;">ignore</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The initialiser simply allows drops into the widget. It also makes sure thumbnails are well visible.</p>
<p>Then each event performs a check to make sure the object being dropped is indeed a file. Finally, at the drop, the class sends an event with a list of local files dropped. Of course all handling could be done within the class but I will eventually need to manage dropped files in a parent form so an event is used here.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">pictureListWidget</span>, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;dropped&quot;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">pictureDropped</span><span style="color: black;">&#41;</span></pre></div></div>

<p>In the parent class, I first connect the signal I defined in the custom list widget.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> pictureDropped<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, l<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> url <span style="color: #ff7700;font-weight:bold;">in</span> l:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>:
			picture = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
			picture.<span style="color: black;">thumbnail</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">72</span>, <span style="color: #ff4500;">72</span><span style="color: black;">&#41;</span>, Image.<span style="color: black;">ANTIALIAS</span><span style="color: black;">&#41;</span>
			icon = QIcon<span style="color: black;">&#40;</span>QPixmap.<span style="color: black;">fromImage</span><span style="color: black;">&#40;</span>ImageQt.<span style="color: black;">ImageQt</span><span style="color: black;">&#40;</span>picture<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
			item = QListWidgetItem<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">basename</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;...&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">pictureListWidget</span><span style="color: black;">&#41;</span>
			item.<span style="color: black;">setStatusTip</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
			item.<span style="color: black;">setIcon</span><span style="color: black;">&#40;</span>icon<span style="color: black;">&#41;</span></pre></div></div>

<p>Here the Image and ImageQt modules of PIL are used to process the images. The Image class can help to create a thumbnail of the opened image. ImageQt can then convert the Image class into an ImageQt class which is a subclass of QImage. QIcons can only be constructed from QPixmap so we build one from the ImageQt class and send it to make a QIcon.</p>
<p>There you go, you now have a working list widget which allow image files to be dropped and can list them with thumbnail.</p>
<p>Of course, this snippet doesn&#8217;t check for other file types being dropped. More code would take away the focus.</p>
<div class="shr-publisher-61"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpyqt-drag-images-into-list-widget-for-thumbnail-list%2F' data-shr_title='PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpyqt-drag-images-into-list-widget-for-thumbnail-list%2F' data-shr_title='PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

