Mini PyCon MY 2014    Posted:


This is the first mini PyCon held in Malaysia so congratulation to the team that finally make it happened. It just 1 day event filled with tracks on various topic related to python. Due to the space limitation, all sessions being held serially in a single room. Having attend a number of PyCon APAC last time in SG, I'm quite happy that I managed to follow all the sessions in this mini pycon. It quite frustrating when you have to decide between two interesting sessions because it run in parallel in two different places. Below are some quick notes from the sessions that I managed to recall.

  • Sphinx and Read The Docs - Eric Holscher

    • Defacto documentation tools in Python Community.
    • While RTD and Sphinx are both valuable tools that we have in Python, I can't really stand RST for writing. It kind of Latex where everytime I need to write, I have to read the documentation again to look for all the syntax.
    • I have only used RTD once in the past, for writing my unfinished book on django.
  • Python on Google App Engine - Desmond Lua

    • Quite an interesting talk.
    • Everything you did probably would cost money so you have to give extra attention on how you write your app.
    • I had a thought of moving Cari Harga Barang to App Engine in order to save some money on the hosting and also the time to manage it but look like it will cost more with App Engine since there's lot of write operations to store all the items and keep it updated daily.
  • Boosting Productivity with IPython - Boey Pak Cheong

    • Finally, I had a visual on what Ipython Notebook really is. In fact, in the later talk, the presenter was using IPython Notebook to show and demonstrate his python code snippet.
  • Python Powered Cloud - Joseph Ziegler

    • This imo the most interesting talk.
    • I have long heard about Elastic Beanstalk (EB) but never had a chance to give it a try.
    • EB probably what I have been looking on how to deploy our apps.
    • The only downside of EB is that it based on RHEL which mean for organization that been using Ubuntu, it will be a huge leap to migrate.
    • Another cool stuff shown in this talk, the bees army. It being used to DDOS the website in order to demo how EB auto scaling work. Still wondering how this thing went out of my radar ;).
  • Python in Embedded and Robotics Development - Yap Wen Jiun

    • I'm not into robotic (yet), maybe until my kid ready to play around with computer and all the stuff.
    • There's a kind of OS for robotic stuff call ROS.
  • Creating Powerful RESTful API Services with Django Tastypie - Leong Seh Hui

    • We used Django Rest Framework for one of our app in development.
    • The speaker however never used DRF so unable to get his insight on the differences between both framework.
    • Looking at Why Tastypie section which say 'You DON'T want to have to write your own serializer to make the output right.', ok, that sound true.
    • Tastypie way of hooking the Resource into urls.py also captured my attention since it look similar to what I'm doing with my little library django-viewrouter.

Comments

PHP: Headers already sent and HTTP basic    Posted:


If there's one error that must be encountered by all PHP developers, this:-

Warning: Cannot modify header information - headers already sent by ...

probably one of it. Anyone who developing in PHP must at least once having issue involving this. Lot of articles and answers have been written on this issue, and this answer on stackoverflow is one of the most comprehensive write up explaining the issue, why it happen and how to fix it. So I don't plan to talk much on the PHP part in this post but more on the HTTP part.

What is HTTP ?

It's a protocol that govern how a piece of information can be exchanged over the Internet. There are lot of protocols defined and being used on the Internet but HTTP definitely one of the widely used protocol in form of what layman known as web, or World Wide Web (WWW). The protocol is being defined as RFC 2616 and as we will later realized, it is a text protocol. Being a text based protocol make it much easier to study and understand and test it in practical manner since no special tools needed to actually implement the protocol. Before we delved deeper into the protocol, let also clear up what actually meant by the word protocol itself.

Read more…

Comments

When to comment ?    Posted:


One of the question we faced up when writing code is when to put comment for our code. It''s already pretty well known that we should comment on the 'why' part, not the 'how'. While looking through our code for some code review, I found these few lines of code:-

# make requests to the API and process response
resp = requests.post(url, data=json.dumps(payload), auth=auth, headers=headers)
if resp.status_code == 200:
    ...
...

Above I think is an example of unnecessary comment since it obvious from the code that we're making some request to a url and processing it's response. Good comment is something along this line:-

# doing POST here since the API does not yet support PUT
resp = requests.post(url, data=json.dumps(payload), auth=auth, headers=headers)
if resp.status_code == 200:
    ...
...

In above illustrative situation, the request logically should be a PUT request but due to some REASON it has to be a POST. The REASON that is worthy for comment since it explain the WHY part. It help the next developer coming up to maintain this code when he probably has in mental state that the request in this section of code should be a PUT not POST. The comment help him to not start raising suspicious that this section of code now has a bug.

So this just one example of when to comment our code. I'll try to add more as I encountered more examples.

Comments

Python: Custom Interactive Console    Posted:


While working on new features that consist of web frontend and a backend API, I found a constant need to manipulate the backend API through python interactive console. Ideally I should implement a command line interface that I can use while waiting for the web frontend to finish but since the API (a class) already has structured methods to access all the API functionalities, a cli interface will just duplicating most of the class API methods.

What tedios when accessing the API through python interactive console is having to re-import all the objects that you need every time you start the interactive console. So an idea pop up - why not create my own custom interactive console with all the objects that I need pre-imported. In python, creating your own interactive console is trivial. The code module has all that you need.

import code

# SomeClass will be available in the interactive console
from yourmodule import SomeClass

vars = globals()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()

While simple and just few lines of code, above is not really useful. It doesn't have auto complete and no history, you can't use up/down arrow key to repeat the previous command. A better version:-

import code
import readline
import rlcompleter

# SomeClass will be available in the interactive console
from yourmodule import SomeClass

vars = globals()
vars.update(locals())
readline.set_completer(rlcompleter.Completer(vars).complete)
readline.parse_and_bind("tab: complete")
shell = code.InteractiveConsole(vars)
shell.interact()

Above is much nicer. Now you have auto complete (type Some<Tab> and you got SomeClass). Cool. But I immediately noticed that the history was not preserved, mean that when I closed the console and start again, it's in blank slate. I can't repeat command in previous session. This actually how the default python console work but I'd already have ~/.pythonrc.py that I defined as PYTHONSTARTUP to setup the tab completion and command history. It seem that my pythonrc.py not being processed by this custom console.

Noticing that the python console from django manage.py shell working correctly with my pythonrc.py, I'd take a look at the implementation code. Look like django has to process PYTHONSTARTUP manually. I'm not really keen on copy-pasting the code from django but there's no way to pass your current enviroment into the console that django created.

In the end, I have to copy the django code into my console function:-

import code
import readline
import rlcompleter

# SomeClass will be available in the interactive console
from yourmodule import SomeClass

vars = globals()
vars.update(locals())
readline.set_completer(rlcompleter.Completer(vars).complete)
readline.parse_and_bind("tab: complete")

# copied from django.core.management.commands.shell
for pythonrc in (os.environ.get("PYTHONSTARTUP"),
                 os.path.expanduser('~/.pythonrc.py')):
    if pythonrc and os.path.isfile(pythonrc):
        try:
            with open(pythonrc) as handle:
                exec(compile(handle.read(), pythonrc, 'exec'))
        except NameError:
            pass

code.interact(local=vars)

I found that working directly in the console like this motivate me to write better docstring for each functions and methods as I'd constantly using the help() function to figure out what each function/class/methods are doing. This seem to be much better than having to write dedicated cli interface for your API.

Comments

HackerNews/Reddit/Stackoverflow like communities    Posted:


Being a developer/programmer, site such as Reddit (especially r/programming) and HackerNews are my main source of latest news and updates. Next definitely Stackoverflow when looking for answers on specific programming problems. But from time to time, I'd also discovered few other small communities, mostly country focused while going through the google search results. So I've decided to start collecting link to these communities in this blog posts.

So here are the lists so far:-

While not an Q&A site, I listed also here site with domain of similar theme:-

Comments

Javascript Self Invoking Function    Posted:


While peeking into Twitter Bootstrap js code, I noticed function being defined like this:-

+function ($) { "use strict";

    // ALERT CLASS DEFINITION
    // ======================

    var dismiss = '[data-dismiss="alert"]'
    var Alert   = function (el) {
      $(el).on('click', dismiss, this.close)
}
...

Noticed the + sign before the keyword function. At first I thought it could some typo slipping through the commit but after some google, it turn out that just another way to define self invoking function in JavaScript. Those who has been writing some JavaScript, or deal with some JS library probably already familiar with a common idiom to write code like below:-

(function($) {
    $('#some_dom_id').click( ....);
})(jQuery);

Read more…

Comments

Angularjs: Event handler attached to all anchor tag 'a'    Posted:


Noticed today that angular modify the behavior of 'a' tag to include some event handler by default. Discover this after getting error Object #<g> has no method 'on' error in my dev console. The error actually showing up after I added jquery to the page but the fact that angular attaching event to all 'a' tag really suprising. I have some 'normal' links I don't want any fancy behavior, just a link. Found a post on how to create new directive to disable the behavior but for now I manage to escape from this by simply not using angular together with jquery.

Comments

Django: @transaction.commit_manually and accessing db from template    Posted:


Bumped into this issue twice and because the first time it happened was almost a year ago, in different project, I'd already lost my mental note about it when it happened again for second time. Our views functions are wrapped inside @transaction.commit_manually, which look like below:-

@transaction.commit_manually
def send_message(request):
    context = {}
    # do some stuff
    transaction.commit()
    return render(request, 'path/to/template.html', context)

This work flawlessly until recently, I need to add some logic in the template that need to check certain user's permission, before displaying the part. In the template, it just some function call that access db like:-

{% if user.can_see_this %}
<a href="#">Some link</a>
{% endif %}

Django now raise TransactionManagementError complaining I have pending commit/rollback. It doesn't make sense because I can verify that the line that executed transaction.commit() was reached in the views above. Only after reading through this ticket on django issue tracker, I'd immediately remember the real issue.

Turn out this caused by new behavior of django transaction in 1.3 and above. Before 1.3, transaction only marked as dirty if we did any write database operation but since 1.3 and above, any database operation will mark the transaction as dirty. So the following code will work before 1.3:-

@transaction.commit_manually
def sendmessage(request):
    transaction.rollback()
    user = User.objects.get(pk=1)
    return HttpResponse('OK')

All database operation now will mark the transaction as dirty and since we access the db in template - which happened during render after we called commit(), it obvious now why django complain we unclosed transaction.

It should be written like below:-

@transaction.commit_manually
def send_message(request):
    context = {}
    # do some stuff
    response = render(request, 'path/to/template.html', context)
    transaction.commit()
    return response

Comments

Django From Scratch    Posted:


There are a lot of tutorials out there on Django and the official documentation also has one. For this post, I decided not to go through the typical route on how to get started with django. Let's 'ignore' the best practices and focus on what actually work and hopefully we can learn something along the way. So let's get started by downloading Django itself from the website.

Read more…

Comments

About me

Web developer in Malaysia. Currently work at MARIMORE Inc building internet services using Python and Django web framework.

ImportError is an error message emitted by Python when it failed to load certain module as requested by programmer. It's a very common error when someone new to the language trying it out. This website on the same theme, will try provide help for newcomers on any technologies to overcome their first hurdle.

Others

I can also be found at the following sites:-

  • http://k4ml.blogspot.com/
  • http://k4ml.github.io/
  • http://metak4ml.blogspot.com/
  • http://www.mydev.my/
  • http://github.com/k4ml/

Disclaimers

The postings on this site are my own and don't necessarily represent my employer's positions, strategies or opinions.

Share