Fix “TypeError–enable-jpeg requested but jpeg not found” Pillow

I got some problem when installing Pillow for my Django project via pip (I’m using Elementary OS that based on Ubuntu).

ValueError: TypeError--enable-jpeg requested but jpeg not found, aborting.

My problem solved after install external library.

$ sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev \
libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk

Reference: https://pillow.readthedocs.org/en/latest/installation.html#building-on-linux

Load and Parse JSON Data with Hook in Python

I have JSON data from my API, and I need comsume those data.
Python have an awesome built in function json and fully integrated with json data. Here is my format data of my json response.

'[{"date": "12-10-2015", "title": "Python 101"}, 
{"date": "12-07-2012", "title": "Vacation"}, 
{"date": "11-10-2013", "title": "Cooking Master"}, 
{"date": "10-01-2011", "title": "Data Science in Nutshell"}, 
{"date": "17-07-2009", "title": "Sublime Text Review"}]'

The problem is, “date” object in my json is a String and I have to parse it to datetime while I’m parsing the data. And here is it, how to parse json response to python dictionary and parse “date” key to datetime.

import json
from datetime import datetime

json_string = '[{"date": "12-10-2015", "title": "Python 101"}, 
{"date": "12-07-2012", "title": "Vacation"}, 
{"date": "11-10-2013", "title": "Cooking Master"}, 
{"date": "10-01-2011", "title": "Data Science in Nutshell"}, 
{"date": "17-07-2009", "title": "Sublime Text Review"}]'

def parse_date(data):
    if 'date' in data:
        # Convert String date to python datetime object.
        data['date'] = datetime.strptime(data['date'], '%d-%B-%Y')
    return data

# Parse JSON data including parse date string key.
parse_data = json.loads(json_string, object_hook=parse_date)

Referece: https://docs.python.org/2/library/json.html

Group List of Dictionary Data by Particular Key in Python

I have python list that contains dictionary paired by “name” and “class” key to represent student data. I need to display all the data group by the “class” key.

Here is the data:

students = [
    {'name': 'alex','class': 'A'},
    {'name': 'richard','class': 'A'},
    {'name': 'john','class': 'C'},
    {'name': 'harry','class': 'B'},
    {'name': 'rudolf','class': 'B'},
    {'name': 'charlie','class': 'E'},
    {'name': 'budi','class': 'C'},
    {'name': 'gabriel','class': 'B'},
    {'name': 'dessy', 'class': 'B'}
]

Python already have a cool built in function called itertools.groupby to solve this kind of problem.

Before we group the data, we have to sort our data by the key. You can refer to my previous post how to sort dictionary by key.

And this is how to group by key:

import itertools
from operator import itemgetter

students = [
    {'name': 'alex','class': 'A'},
    {'name': 'richard','class': 'A'},
    {'name': 'john','class': 'C'},
    {'name': 'harry','class': 'B'},
    {'name': 'rudolf','class': 'B'},
    {'name': 'charlie','class': 'E'},
    {'name': 'budi','class': 'C'},
    {'name': 'gabriel','class': 'B'},
    {'name': 'dessy', 'class': 'B'}
]

# Sort students data by `class` key.
students = sorted(students, key=itemgetter('class'))

# Display data grouped by `class`
for key, value in itertools.groupby(students, key=itemgetter('class')):
    print key
    for i in value:
        print i.get('name')

"""
results:

A
alex
richard
-------
B
harry
rudolf
gabriel
dessy
-------
C
john
budi
-------
E
charlie
-------
"""

Reference: https://docs.python.org/2/library/itertools.html#itertools.groupby

Sort List of Dictionary by Key in Python

Here is example code to perform sorting list of dictionary by dictionary key.
For example I need to sort my list by level.

from operator import itemgetter

tmp = [
    {'name': 'foo', 'age': 35, 'level': 1},
    {'name': 'bar', 'age': 11, 'level': 5},
    {'name': 'john', 'age': 28, 'level': 3},
    {'name': 'doe', 'age': 74, 'level': 9},
    {'name': 'alex', 'age': 12, 'level': 7},
]

print sorted(tmp, key=itemgetter('level'))                # Sort ascending
print sorted(tmp, key=itemgetter('level'), reverse=True)  # Sort descending

Reference: https://wiki.python.org/moin/SortingListsOfDictionaries

Create Alphabet List in Python

Just found trick to create list of alphabet in python.

import string

alphabet = string.ascii_lowercase # you can also call .ascii_uppercase
print alphabet # 'abcdefghijklmnopqrstuvwxys'

list_char = list(alphabet)
print list_char # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Reference: http://stackoverflow.com/a/16060908/1936697

Python Debugging with Traceback

I have problem when dealing with chaining function that throws error exception in each method calls like example below.

def func_a():
    try:
        # Error occured in here.
    except ValueError, e:
        raise e

def func_b():
    try:
        func_a()
    except ValueError, e:
        raise e

def func_c():
    try:
        func_b()
    except ValueError, e:
        raise e

if __name__ == '__main__':
    try:
        func_c()
    except Exception, e:
        raise e

From example above, I get used to use ipdb for debugging. Usuallly I find out one by one from func_c() until func_a() to locate the error.

I just found most simple way to deal with it without any extensions. We can use built in function traceback. Traceback will show in which line the error came from without interrupting your program.

Here is example how to use it:

import sys
import traceback

if __name__ == '__main__':
    try:
        func_c()
    except Exception, e:
        traceback.print_exc(file=sys.stdout)
        raise e

And the error stack trace example.

Traceback (most recent call last):
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/views.py", line 155, in create_stpicklist_post_handler
    instance = STPicklist.objects.create(**picklist_meta)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/query.py", line 372, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/models.py", line 210, in save
    super(STPicklist, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/utils/text_processing.py", line 247, in save
    super(ModelDiffMixin, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/utils/text_processing.py", line 207, in save
    super(StripModelMixin, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/base.py", line 589, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/base.py", line 626, in save_base
    update_fields=update_fields, raw=raw, using=using)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/signals.py", line 119, in signal_create_stactivity
    raise e
InvalidOperation: 0 / 0

Basic Authentication in Python urllib2

Yesterday I’m playing around with github api with urllib and found a problem to perform basic authentication when I sent request to API. I’m not using requests because too lazy to install third party dependency. 😀

Here is it..

import urllib2
import base64

request = urllib2.Request("https://api.github.com/user/starred")
credential = base64.encodestring('{user}:{pwd}'.format(user=username, pwd=password)).replace('\n', '')
request.add_header('Authorization', 'Basic {credential}'.format(credential=credential))   
result = urllib2.urlopen(request)

Reference: http://stackoverflow.com/a/2955687/1936697