<?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>python &#8211; James Lin&#039;s Blog</title>
	<atom:link href="https://james.lin.net.nz/tag/python-2/feed/" rel="self" type="application/rss+xml" />
	<link>https://james.lin.net.nz</link>
	<description>Just bits and pieces of my life</description>
	<lastBuildDate>Fri, 19 Jan 2018 22:42:50 +0000</lastBuildDate>
	<language>en-NZ</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.2</generator>
<site xmlns="com-wordpress:feed-additions:1">22801464</site>	<item>
		<title>Posting image to thumbor using requests package</title>
		<link>https://james.lin.net.nz/2016/05/13/posting-image-to-thumbor-using-requests-package/</link>
		<comments>https://james.lin.net.nz/2016/05/13/posting-image-to-thumbor-using-requests-package/#respond</comments>
		<pubDate>Fri, 13 May 2016 03:48:46 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[thumbor]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3783</guid>
		<description><![CDATA[Seems like there are not many code sample there, and the requests documentation was giving the wrong hint to use files={'file': open('image.jpg')} which you will end up getting 415 response code. I think I will just share my working sample code here to save someone&#8217;s time [crayon-5a6645848b1cb974793662/] &#160;]]></description>
				<content:encoded><![CDATA[<p>Seems like there are not many code sample there, and the requests documentation was giving the wrong hint to use <code>files={'file': open('image.jpg')}</code> which you will end up getting 415 response code.</p>
<p>I think I will just share my working sample code here to save someone&#8217;s time</p><pre class="crayon-plain-tag">import requests

# local file
with open('image.jpg', 'rb') as file:
    resp = requests.post('http://localhost:8888/image', data=file.read())
    url = resp.headers['Location']

# remote file
remote_resp = requests.get(image_url, stream=True)
resp = requests.post('http://localhost:8888/image', data=remote_resp.content)
url = resp.headers['Location']</pre><p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/05/13/posting-image-to-thumbor-using-requests-package/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3783</post-id>	</item>
		<item>
		<title>What&#8217;s wrong with this statement?</title>
		<link>https://james.lin.net.nz/2014/04/17/whats-wrong-with-this-statement/</link>
		<comments>https://james.lin.net.nz/2014/04/17/whats-wrong-with-this-statement/#respond</comments>
		<pubDate>Thu, 17 Apr 2014 04:08:10 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3517</guid>
		<description><![CDATA[This morning I made a code change to log some stuff in a core package and created a pull request, the change was very simple: from [crayon-5a6645848cba8686315926/] to [crayon-5a6645848cbbe622646350/] One of the team lead pointed out the statement has a problem, and it was a life saver, but I won&#8217;t tell you why just yet,… <span class="read-more"><a href="https://james.lin.net.nz/2014/04/17/whats-wrong-with-this-statement/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>This morning I made a code change to log some stuff in a core package and created a pull request, the change was very simple:</p>
<p>from</p>
<p></p><pre class="crayon-plain-tag">self.execute_command(command, cwd=os.path.dirname(infile))</pre><p></p>
<p>to</p>
<p></p><pre class="crayon-plain-tag">logger.debug(self.execute_command(command, cwd=os.path.dirname(infile)))</pre><p></p>
<p>One of the team lead pointed out the statement has a problem, and it was a life saver, but I won&#8217;t tell you why just yet, see if you can spot it.</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2014/04/17/whats-wrong-with-this-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3517</post-id>	</item>
		<item>
		<title>Python OpenCV Facial Detection On OSX</title>
		<link>https://james.lin.net.nz/2014/03/06/python-opencv-facial-detection/</link>
		<comments>https://james.lin.net.nz/2014/03/06/python-opencv-facial-detection/#respond</comments>
		<pubDate>Wed, 05 Mar 2014 20:17:57 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[opencv]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3495</guid>
		<description><![CDATA[Just a code snippet to demo Python script using OpenCV [crayon-5a6645848d571756194022/] Installation: pip install numpy (sometimes you might need to run as sudo) brew tap homebrew/science brew install opencv export PYTHONPATH=/usr/local/Cellar/opencv/2.4.7.1/lib/python2.7/site-packages/ download the xml file https://warai.googlecode.com/files/haarcascade_frontalface_alt.xml and place it in the same folder.]]></description>
				<content:encoded><![CDATA[<p><strong>Just a code snippet to demo Python script using OpenCV</strong></p><pre class="crayon-plain-tag">import cv2, time

cap = cv2.VideoCapture(0)
time.sleep(1)
cascade = cv2.CascadeClassifier(&quot;haarcascade_frontalface_alt.xml&quot;)

def detect(image):
    #faces = cascade.detectMultiScale(img, 1.1, 2, cv2.cv.CV_HAAR_SCALE_IMAGE, (20,20), (200,200))
    faces = cascade.detectMultiScale(image)
    for _face in faces:
        cv2.rectangle(image, (_face[0], _face[1]), (_face[0]+_face[2], _face[1]+_face[3]), (255,255,255))

def repeat():
    ret, image = cap.read()
    detect(image)
    cv2.imshow(&quot;w1&quot;, image)
    cv2.waitKey(1)

while True:
    repeat()</pre><p><strong>Installation:</strong></p>
<ol>
<li>pip install numpy (sometimes you might need to run as sudo)</li>
<li>brew tap homebrew/science</li>
<li>brew install opencv</li>
<li>export PYTHONPATH=/usr/local/Cellar/opencv/2.4.7.1/lib/python2.7/site-packages/</li>
<li>download the xml file https://warai.googlecode.com/files/haarcascade_frontalface_alt.xml and place it in the same folder.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2014/03/06/python-opencv-facial-detection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3495</post-id>	</item>
		<item>
		<title>Django CMS Haystack 2.0 Search Index</title>
		<link>https://james.lin.net.nz/2013/11/06/django-cms-haystack-2-0-search-index/</link>
		<comments>https://james.lin.net.nz/2013/11/06/django-cms-haystack-2-0-search-index/#comments</comments>
		<pubDate>Wed, 06 Nov 2013 07:13:43 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[django-cms]]></category>
		<category><![CDATA[haystack]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3448</guid>
		<description><![CDATA[Currently django-cms-search doesn&#8217;t support haystack 2.0 yet, so here is my modified version to work on haystack 2.0 based on [crayon-5a6645848e081545201655/]]]></description>
				<content:encoded><![CDATA[<p>Currently django-cms-search doesn&#8217;t support haystack 2.0 yet, so here is my modified version to work on haystack 2.0 based on <a href="http://django-cms.readthedocs.org/en/2.1.3/extending_cms/searchdocs.html#id1" title="django cms documentation" target="_blank"></a></p>
<p></p><pre class="crayon-plain-tag">import datetime
from haystack import indexes   
from cms.models.managers import PageManager
from cms.models.pagemodel import Page
from cms.models.pluginmodel import CMSPlugin


class PageIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=False)
    pub_date = indexes.DateTimeField(model_attr='publication_date', null=True)
    login_required = indexes.BooleanField(model_attr='login_required')
    url = indexes.CharField(model_attr='get_absolute_url')
    title = indexes.CharField(model_attr='get_title')

    def prepare(self, obj):    
        self.prepared_data = super(PageIndex, self).prepare(obj)
        plugins = CMSPlugin.objects.filter(placeholder__in=obj.placeholders.all())
        text = ''              
        for plugin in plugins: 
            instance, _ = plugin.get_plugin_instance()
            if hasattr(instance, 'search_fields'):
                text += ''.join(getattr(instance, field) for field in instance.search_fields)
        text += obj.get_meta_description() or u''
        text += obj.get_title() or u''
        text += obj.get_meta_keywords() if hasattr(obj, 'get_meta_keywords') and obj.get_meta_keywords() else u''
        self.prepared_data['text'] = text
        return self.prepared_data       

    def get_model(self):
        return Page

    def index_queryset(self, using=None):
        return Page.objects.published().filter(publisher_is_draft=False).distinct()</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2013/11/06/django-cms-haystack-2-0-search-index/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3448</post-id>	</item>
		<item>
		<title>Pretty Good Insight On GIL In Python</title>
		<link>https://james.lin.net.nz/2013/08/19/pretty-good-insight-on-gil-in-python/</link>
		<comments>https://james.lin.net.nz/2013/08/19/pretty-good-insight-on-gil-in-python/#respond</comments>
		<pubDate>Mon, 19 Aug 2013 09:19:23 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3421</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<p><iframe src="http://blip.tv/play/g4VigcfoPgI.x?p=1" width="720" height="433" frameborder="0" allowfullscreen></iframe><embed type="application/x-shockwave-flash" src="http://a.blip.tv/api.swf#g4VigcfoPgI" style="display:none"></embed></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2013/08/19/pretty-good-insight-on-gil-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3421</post-id>	</item>
		<item>
		<title>Django admin UnicodeEncodeError</title>
		<link>https://james.lin.net.nz/2013/06/19/django-admin-unicodeencodeerror/</link>
		<comments>https://james.lin.net.nz/2013/06/19/django-admin-unicodeencodeerror/#respond</comments>
		<pubDate>Tue, 18 Jun 2013 21:59:24 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=1466</guid>
		<description><![CDATA[Don&#8217;t we all get confused by unicode encoding sometimes? Especially you wouldn&#8217;t expect this kind of error to happen in django admin. The problem is actually not in the admin section, the culprit is __unicode__ method in your model. when you have a unicode function like this: [crayon-5a66458490d3d669863298/] It is actually returning a ASCII string(which… <span class="read-more"><a href="https://james.lin.net.nz/2013/06/19/django-admin-unicodeencodeerror/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>Don&#8217;t we all get confused by unicode encoding sometimes? Especially you wouldn&#8217;t expect this kind of error to happen in django admin. The problem is actually not in the admin section, the culprit is __unicode__ method in your model.</p>
<p>when you have a unicode function like this:</p>
<p></p><pre class="crayon-plain-tag">def __unicode__(self):
  return &quot;{0}&quot;.format(self.field_one)</pre><p></p>
<p>It is actually returning a ASCII string(which means, it will try to convert field_one to ASCII), if the field_one contains characters outside of ASCII, you will get the problem as above.</p>
<p>Now consider this unicode function:</p>
<p></p><pre class="crayon-plain-tag">def __unicode__(self):
      return self.field_one</pre><p></p>
<p>This works fine, because you are returning unicode string directly, no conversion needed.</p>
<p>Lets revisit the first unicode function, to make it work, you just need to add u to make it a unicode string</p>
<p></p><pre class="crayon-plain-tag">def __unicode__(self):
      return u&quot;{0}&quot;.format(self.field_one)</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2013/06/19/django-admin-unicodeencodeerror/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">1466</post-id>	</item>
		<item>
		<title>Video: Django Rapid Development Demo</title>
		<link>https://james.lin.net.nz/2013/04/10/video-django-rapid-development-demo/</link>
		<comments>https://james.lin.net.nz/2013/04/10/video-django-rapid-development-demo/#respond</comments>
		<pubDate>Wed, 10 Apr 2013 08:15:10 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=1364</guid>
		<description><![CDATA[I think it&#8217;s necessary to create a video to demonstrate how quickly web application development can be. Enjoy! //www.youtube.com/watch?v=c3C3hYbo37c]]></description>
				<content:encoded><![CDATA[<p>I think it&#8217;s necessary to create a video to demonstrate how quickly web application development can be. Enjoy!</p>
<p><a href="//www.youtube.com/watch?v=c3C3hYbo37c">//www.youtube.com/watch?v=c3C3hYbo37c</a></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2013/04/10/video-django-rapid-development-demo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">1364</post-id>	</item>
	</channel>
</rss>
