python

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

Advertisements

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