<?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>Programming &#8211; James Lin&#039;s Blog</title>
	<atom:link href="https://james.lin.net.nz/category/technology/programming/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>Laravel for Django developers, part 3</title>
		<link>https://james.lin.net.nz/2018/01/20/laravel-for-django-developers-part-3/</link>
		<comments>https://james.lin.net.nz/2018/01/20/laravel-for-django-developers-part-3/#respond</comments>
		<pubDate>Fri, 19 Jan 2018 22:25:30 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3992</guid>
		<description><![CDATA[During the last day of Christmas break I&#8217;ve spent a few hours trying to pick up Laravel again, from the beginning. I remember I blogged about Laravel back in 2016 (time flies!), and reading them again, I am surprised that what I thought back in 2016 still applies today. Today I am going to talk… <span class="read-more"><a href="https://james.lin.net.nz/2018/01/20/laravel-for-django-developers-part-3/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>During the last day of Christmas break I&#8217;ve spent a few hours trying to pick up Laravel again, from the beginning.</p>
<p>I remember I blogged about Laravel back in 2016 (time flies!), and reading them again, I am surprised that what I thought back in 2016 still applies today.</p>
<p>Today I am going to talk about some black magic I&#8217;ve encountered writing a test blog project. The bit that filling out the blog form and validating the data. Normally, when a form is posted to an URL, the controller validates the form data and throws validation exceptions if form is not validated, then the controller can redirect the browser back to the form along some error data to be displayed on the form. In Laravel, the redirecting back to the form is automatically done for you and somehow the view magically has the &#8216;errors&#8217; variable in context.</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2018/01/20/laravel-for-django-developers-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3992</post-id>	</item>
		<item>
		<title>How it feels to learn JavaScript in 2016</title>
		<link>https://james.lin.net.nz/2016/10/05/how-it-feels-to-learn-javascript-in-2016/</link>
		<comments>https://james.lin.net.nz/2016/10/05/how-it-feels-to-learn-javascript-in-2016/#respond</comments>
		<pubDate>Wed, 05 Oct 2016 00:17:57 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3814</guid>
		<description><![CDATA[&#8220;I need to display data on a page, not perform Sub Zero’s original MK fatality.&#8221; https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f#.67qnsecr5]]></description>
				<content:encoded><![CDATA[<p>&#8220;I need to display data on a page, not perform Sub Zero’s original MK fatality.&#8221;</p>
<p><a href="https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f#.67qnsecr5">https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f#.67qnsecr5</a></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/10/05/how-it-feels-to-learn-javascript-in-2016/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3814</post-id>	</item>
		<item>
		<title>A good article about face recognition</title>
		<link>https://james.lin.net.nz/2016/09/27/a-good-article-about-face-recognition/</link>
		<comments>https://james.lin.net.nz/2016/09/27/a-good-article-about-face-recognition/#respond</comments>
		<pubDate>Mon, 26 Sep 2016 19:58:14 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Life In General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3811</guid>
		<description><![CDATA[View story at Medium.com]]></description>
				<content:encoded><![CDATA[<p><script async src="https://static.medium.com/embed.js"></script><a class="m-story" href="https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78#.we646ev8q" target="_blank" data-width="665" data-border="1" data-collapsed="">View story at Medium.com</a></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/09/27/a-good-article-about-face-recognition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3811</post-id>	</item>
		<item>
		<title>DRY recipe on Django pagination template</title>
		<link>https://james.lin.net.nz/2016/07/01/dry-recipe-on-django-pagination-template/</link>
		<comments>https://james.lin.net.nz/2016/07/01/dry-recipe-on-django-pagination-template/#respond</comments>
		<pubDate>Thu, 30 Jun 2016 21:11:16 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3797</guid>
		<description><![CDATA[Here is a quick recipe for Django templates, let&#8217;s say you are building a Django web which has many list views, and which has pagination, it&#8217;s wise to abstract the pagination template out to be reusable by all list view templates. My preference is to keep all reusable templates in a partials folder, and place the… <span class="read-more"><a href="https://james.lin.net.nz/2016/07/01/dry-recipe-on-django-pagination-template/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>Here is a quick recipe for Django templates, let&#8217;s say you are building a Django web which has many list views, and which has pagination, it&#8217;s wise to abstract the pagination template out to be reusable by all list view templates.</p>
<p>My preference is to keep all reusable templates in a <code>partials</code> folder, and place the pagination template in there.</p><pre class="crayon-plain-tag">{% if is_paginated %}
&lt;nav&gt;
    &lt;ul class="pagination"&gt;
    {% for page_num in paginator.page_range %}
        {% if page_num == page_obj.number %}
          &lt;li class="active"&gt;&lt;a href="#"&gt;{{ page_num }}&lt;/a&gt;&lt;/li&gt;
        {% else %}
        &lt;li&gt;&lt;a href="{% url urlname page_num %}?{{ request.GET.urlencode }}"&gt;{{ page_num }}&lt;/a&gt;&lt;/li&gt;
        {% endif %}
    {% endfor %}
    &lt;/ul&gt;
&lt;/nav&gt;
{% endif %}</pre><p>The pagination template is switched by the <code>ListView</code> populated context var <code>is_paginated, also</code> makes use of <code>urlname</code> variable to construct the page links.</p>
<p>Then in each list view template to include the pagination:</p><pre class="crayon-plain-tag">{% include 'partials/pagination.html' with urlname='quick-admin:listing-job-list' %}</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/07/01/dry-recipe-on-django-pagination-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3797</post-id>	</item>
		<item>
		<title>Django Rest Framework QuerySet</title>
		<link>https://james.lin.net.nz/2016/06/24/django-rest-framework-queryset/</link>
		<comments>https://james.lin.net.nz/2016/06/24/django-rest-framework-queryset/#respond</comments>
		<pubDate>Thu, 23 Jun 2016 22:42:24 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3794</guid>
		<description><![CDATA[I have just created this python library so that you can query the remote api just like the Django queryset. It is particularly usefully when used in Django ListView with pagination. https://github.com/variable/django-rest-framework-queryset ]]></description>
				<content:encoded><![CDATA[<p>I have just created this python library so that you can query the remote api just like the Django queryset. It is particularly usefully when used in Django ListView with pagination.</p>
<p><a href="https://github.com/variable/django-rest-framework-queryset">https://github.com/variable/django-rest-framework-queryset </a></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/06/24/django-rest-framework-queryset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3794</post-id>	</item>
		<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-5a655023cba33842186510/] &#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>Suppressing imported but unused warnings from flake</title>
		<link>https://james.lin.net.nz/2016/04/28/suppressing-imported-but-unused-warnings-from-flake/</link>
		<comments>https://james.lin.net.nz/2016/04/28/suppressing-imported-but-unused-warnings-from-flake/#respond</comments>
		<pubDate>Wed, 27 Apr 2016 21:12:09 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Life In General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[flake]]></category>
		<category><![CDATA[pep8]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3780</guid>
		<description><![CDATA[If you are a good python dev, surely you follow PEP8 coding style guidelines, and surely you have installed the flake8 plugin to constantly reminding yourself to format your code properly, and surely you have valid cases that you import stuff but not used, eg. __init__.py and surely your IDE will annoy the fuck out of… <span class="read-more"><a href="https://james.lin.net.nz/2016/04/28/suppressing-imported-but-unused-warnings-from-flake/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>If you are a good python dev, surely you follow PEP8 coding style guidelines, and surely you have installed the flake8 plugin to constantly reminding yourself to format your code properly, and surely you have valid cases that you import stuff but not used, eg. <code>__init__.py</code> and surely your IDE will annoy the fuck out of you for not using the stuff you imported, here is how you tell your IDE to &#8220;shut the fuck up and I did it on purpose&#8221;:</p><pre class="crayon-plain-tag">from .your_module import unused_stuff  # noqa</pre><p>Peace.</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/04/28/suppressing-imported-but-unused-warnings-from-flake/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3780</post-id>	</item>
		<item>
		<title>Make sure you are closing the DB connections after accessing Django ORM in your threads</title>
		<link>https://james.lin.net.nz/2016/04/22/make-sure-you-are-closing-the-db-connections-after-accessing-django-orm-in-your-threads/</link>
		<comments>https://james.lin.net.nz/2016/04/22/make-sure-you-are-closing-the-db-connections-after-accessing-django-orm-in-your-threads/#respond</comments>
		<pubDate>Thu, 21 Apr 2016 21:07:47 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3778</guid>
		<description><![CDATA[I was writing a service which import various feeds and then post to another RESTful API, simple enough as below: [crayon-5a655023cc94f699700718/] Then the feed model: [crayon-5a655023cc965099390275/] Then run this feed in trivial code: [crayon-5a655023cc972212761143/] And you will quickly notice the threads complaining [crayon-5a655023cc980247816777/] What it&#8217;s happening is Django will create a new connection per thread… <span class="read-more"><a href="https://james.lin.net.nz/2016/04/22/make-sure-you-are-closing-the-db-connections-after-accessing-django-orm-in-your-threads/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>I was writing a service which import various feeds and then post to another RESTful API, simple enough as below:</p><pre class="crayon-plain-tag">from django.db import connections
class Receiver(PolymorphicModel):
    ...

class Database(Receiver):
    name = models.CharField(max_length=255)
    def get_data_source(self):
        return connections[name]</pre><p>Then the feed model:</p><pre class="crayon-plain-tag">class Feed(models.Model):
    receiver = models.ForeignKey('Receiver')

    def get_data_source(self):
        return self.receiver.get_real_instance().get_data_source()</pre><p>Then run this feed in trivial code:</p><pre class="crayon-plain-tag">import threading

function transform_listing(feed):
    cursor = feed.get_data_source().cursor()
    ....

feed = Feed.objects.create(receiver=Database.objects.create(name='legacy'))

for i in range(100):
    th = threading.Thread(transform_listing, [feed])
    th.start()</pre><p></p>
<p>And you will quickly notice the threads complaining </p><pre class="crayon-plain-tag">Too many connections</pre><p></p>
<p>What it&#8217;s happening is Django will create a new connection per thread when you access the ORM, and connections are left open even the threads are terminated, in this case the culprit is</p><pre class="crayon-plain-tag">def get_data_source(self):
        return self.receiver.get_real_instance().get_data_source()</pre><p></p>
<p>So in this case, simply refactor the code to close the connection before return:</p><pre class="crayon-plain-tag">from django.db import connection
...

    def get_data_source(self):
        result = self.receiver.get_real_instance().get_data_source()
        connection.close()
        return result</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/04/22/make-sure-you-are-closing-the-db-connections-after-accessing-django-orm-in-your-threads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3778</post-id>	</item>
		<item>
		<title>EJB stateful vs stateless</title>
		<link>https://james.lin.net.nz/2016/02/26/ejb-stateful-vs-stateless/</link>
		<comments>https://james.lin.net.nz/2016/02/26/ejb-stateful-vs-stateless/#respond</comments>
		<pubDate>Thu, 25 Feb 2016 20:25:38 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[JAVA]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[java ee]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3765</guid>
		<description><![CDATA[I have been studying Java EE and I posted a question on Stackoverflow to verify my understanding of &#8220;SessionBean&#8221;, here is the summary: Stateless bean in client: [crayon-5a655023cd543224122617/] &#160; Stateful bean in client: [crayon-5a655023cd55c437746570/] &#160; A stateless session bean, should not store a state in the bean instance, in order words, it&#8217;s class members should… <span class="read-more"><a href="https://james.lin.net.nz/2016/02/26/ejb-stateful-vs-stateless/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>I have been studying Java EE and I posted a <a href="http://stackoverflow.com/questions/35616897/when-bean-null-does-the-app-server-trash-the-stateful-bean-instance-and-create" target="_blank">question</a> on Stackoverflow to verify my understanding of &#8220;SessionBean&#8221;, here is the summary:</p>
<p>Stateless bean in client:</p><pre class="crayon-plain-tag">public void work(){
    bean.work1();   // this uses instance 1 in App Server
    ...
    bean.work2();   // this can be instance 2 in App Server
}</pre><p>&nbsp;</p>
<p>Stateful bean in client:</p><pre class="crayon-plain-tag">public void work(){
    bean.add_item(item); // this uses instance 1 in App Server
    ....
    bean.checkout();     // this uses instance 1 in App Server
}</pre><p>&nbsp;</p>
<ul>
<li>A stateless session bean, should not store a state in the bean instance, in order words, it&#8217;s class members should not be assigned by external values.</li>
<li>A stateful session bean, maintains it&#8217;s state until it&#8217;s &#8220;flushed&#8221;, a bean method that is annotated by &#8220;@Remove&#8221;</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/02/26/ejb-stateful-vs-stateless/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3765</post-id>	</item>
		<item>
		<title>Your function parameters should be as simple as possible</title>
		<link>https://james.lin.net.nz/2016/02/22/your-function-parameters-should-be-as-simple-as-possible/</link>
		<comments>https://james.lin.net.nz/2016/02/22/your-function-parameters-should-be-as-simple-as-possible/#respond</comments>
		<pubDate>Sun, 21 Feb 2016 20:33:33 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3757</guid>
		<description><![CDATA[Let&#8217;s say you have a function to process a GET parameter from a HTTP request, the bad example to do this is: [crayon-5a655023cdd4b082147261/] The above example has 2 problems: 1. This will become a nightmare to write unit tests on this function, now you need to mock the request object to contain the attribute GET… <span class="read-more"><a href="https://james.lin.net.nz/2016/02/22/your-function-parameters-should-be-as-simple-as-possible/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s say you have a function to process a GET parameter from a HTTP request, the bad example to do this is:</p><pre class="crayon-plain-tag">def process(request):
    value = request.GET.get('name')
    work(value)

process(request)</pre><p>The above example has 2 problems:<br />
1. This will become a nightmare to write unit tests on this function, now you need to mock the request object to contain the attribute GET which is a dictionary has a key &#8216;name&#8217;<br />
2. Person reading the code has to spend the effort of what other usage of &#8220;request&#8221; in the function.</p>
<p>The proper way is to accept a parameter as shallow as possible, in this case, a string</p><pre class="crayon-plain-tag">def process(value):
    work(value)

process(request.GET.get('value'))</pre><p>The above example has 2 advantages:<br />
1. Easy to write unit tests, all you need now is pass in a string<br />
2. The person reading the calling statement knows a string is passed in, no other values are involved.</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/02/22/your-function-parameters-should-be-as-simple-as-possible/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3757</post-id>	</item>
	</channel>
</rss>
