Python: Fix getting “about:blank” in Selenium with PhantomJS

Having an issue with selenium when loading some URL with selenium using PhantomJS.
When I get the current_url some URL returned “about:blank” instead of the valid URL.

After several workarounds, the issue because PhantomJS by default using “SSLv3” and many websites had a bug in SSL migrate to TLS and caused got a blank page.

Here is how I solve my issue.

from selenium import webdriver

driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])


Create Empty Object in Python

I need to create dummy object in python for testing purpose. Here is how to create dummy object in python on the fly using “type”.

request = type('request', (), {})()  # Create request object on the fly
print request is None                # False
print request                        # <__main__.request at 0x1073f8590>


Atom for Python Development

Here is atom plugins that I used for my python development, because I’m no longer using PyCharm. 😀


For installation plugins above, you can refer to link that I have provided, because the docs pretty straightforward. 😀

Addition, maybe previously you are using sublime text and you are using gitgutter, you can add this snippet to your atom config to highlight all your changes on your project. Kinda look like gitgutter. 😀

Fix “openssl/aes.h file not found” on OS X

I got an issue when installing pusher-http-python, python library for on OS X.
My problem solved just run this single line in terminal.

env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" pip install cryptography


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


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


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']


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():
        # Error occured in here.
    except ValueError, e:
        raise e

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

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

if __name__ == '__main__':
    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__':
    except Exception, e:
        raise e

And the error stack trace example.

Traceback (most recent call last):
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/", 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/", 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/", line 372, in create, using=self.db)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/", line 210, in save
    super(STPicklist, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/utils/", line 247, in save
    super(ModelDiffMixin, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/utils/", line 207, in save
    super(StripModelMixin, self).save(*args, **kwargs)
  File "/home/adiyatmubarak/.virtualenvs/vasham/lib/python2.7/site-packages/django/db/models/", 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/", 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/", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/adiyatmubarak/Polatic/Projects/vasham-erp/stocktracks/", line 119, in signal_create_stactivity
    raise e
InvalidOperation: 0 / 0