Some Python Tip / Tricks

Here are some of the Python tips and tricks that I have learnt over time. Some of these might be too simple and known, but I chose to document them anyway.

Note: This blog post is a WIP.

Convert Strings to Python datetime objects

To perform operation related to date and time, you can convert your Python strings to datetimeobjects. For this, you can use strptime function. An example code below:

>>> from datetime import datetime
>>> s = "Tue Mar 24 2015 17:00:00"
>>> pattern = "%a %b %d %Y %H:%M:%S"
>>> dt = datetime.strptime(s, pattern)

Now you can perform various operations like calculating time delta, converting to other time formats, separating various fields, etc using this object.

A detailed list of supported format specifier can be found here.


Scrolling Webpage using Selenium Python

Sometimes, there's a need to scroll a webpage to load more content. For example, you need to load more tweets on your feed, and for that you need to scroll all the way down. Using selenium for Python, this can be achieved by following:

from selenium import webdriver  
driver = webdriver.Firefox()  
driver.get("https://twitter.com/c0dist")  
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")  

Socks Proxy in Selenium for Python

Sometimes, you need to request page using Socks proxy in selenium-python. I wrote about this on Garage4Hackers forums, some time back. The post demonstrates how you can use either Firefox or PhantomJS webdriver with Socks proxy.

You can find the post here.