DRY recipe on Django pagination template

Here is a quick recipe for Django templates, let’s say you are building a Django web which has many list views, and which has pagination, it’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… Read More »

Django Rest Framework QuerySet

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 

Posting image to thumbor using requests package

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’s time

 

Suppressing imported but unused warnings from flake

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… Read More »

Make sure you are closing the DB connections after accessing Django ORM in your threads

I was writing a service which import various feeds and then post to another RESTful API, simple enough as below:

Then the feed model:

Then run this feed in trivial code:

And you will quickly notice the threads complaining

What it’s happening is Django will create a new connection per thread… Read More »

EJB stateful vs stateless

I have been studying Java EE and I posted a question on Stackoverflow to verify my understanding of “SessionBean”, here is the summary: Stateless bean in client:

  Stateful bean in client:

  A stateless session bean, should not store a state in the bean instance, in order words, it’s class members should… Read More »

Your function parameters should be as simple as possible

Let’s say you have a function to process a GET parameter from a HTTP request, the bad example to do this is:

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… Read More »

Writing static typed style code in dynamic type language

I am not sure who else is doing this but I have a static typed language background and I tend to write code in static type style. Don’t assign different value types to the same variable Let’s see what dynamic type in the simplest form in Python:

While this is perfectly legitimate, but it’s… Read More »

PHP developers should try Python.

PHP and Python both are dynamic typed language, so there is no point the discuss anything related in this aspect. Exception is first class citizen

Prints ‘Caught’ Let’s look at PHP:

Better functions Imagine you can write functions like this

Short-lived vs Long-lived Almost all PHP scripts are short-lived, including most frameworks, which… Read More »