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

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

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>

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

Atom for Python Development

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

hyperclick: https://atom.io/packages/hyperclick
autocomplete-python: https://atom.io/packages/autocomplete-python
linter: https://github.com/atom-community/linter
linter-flake8: https://github.com/AtomLinter/linter-flake8

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 https://gist.github.com/Keda87/118f19c78286ca845d4d 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 https://pusher.com/ 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

Reference:
https://github.com/pyca/cryptography/issues/2350
http://masnun.com/2015/12/01/fixing-fatal-error-opensslaes-h-file-not-found-on-os-x.html

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

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