Python Programming Snippets

Uninstall Python Package from OS X

I have been using Python 3 on my OS X provided by the installation package from Python.org for some time, and recently I have decided to switch to Python installation from Homebrew for easier management.

In order to uninstall the Python from package, you must remove the files and symbolic links to them.

WARNING: This commands will remove all Python versions installed with packages. Python provided from the system will not be affected.

sudo rm -rf /Library/Frameworks/Python.framework
cd /usr/local/bin
ls -l . | grep '../Library/Frameworks/Python.framework' | awk '{print $9}' | xargs sudo rm

You should also remember to remove the application folder, where x.y is the version number of your installation (ie. 2.7 or 3.4).

sudo rm -rf "/Applications/Python x.y"
Linux Python Programming Snippets

Quick Guide to D-Bus with Python

Recent weeks, I dealt with D-Bus for my Google Summer of Code project. I was trying to control system services, and systemd has a D-Bus interface. In the beginning, D-Bus seemed a little confusing for me, but I believe I understand it now. In this post I will try to explain it in simple terms and an example.

D-Bus is a interprocess communication tool. It delivers messages to applications. The most confusing thing for me was that taxonomy of D-Bus having several terms such as bus names, objects, paths, methods, signals, properties, interfaces, proxies… But when you learn their meanings, the bigger picture gets more clear.

  • Bus names are applications. They often have well known names in a format similar to namespaces in Java (e.g “com.onurguzel.MyIntergalacticApplication”).
  • Objects are synonymous to the objects in programming languages, and paths are the object instances.
  • Methods are functions that is run on the object, and signals are the broadcasts from the object. Properties are the constants or variables in the object.
  • Interfaces are the types of the object. Thus, some object may support several interfaces. Their format is the same as bus names, but they should not be mixed with each other.
  • Proxies are higher level objects. When a method is invoked on the proxy, it is converted to D-Bus method call message, sent, and reply message is returned. They offer easier operation with D-Bus then manually creating messages, sending them and waiting for a reply.

Since all major Linux distributions use NetworkManager for network connections, I preferred to create this tutorial with it.

import dbus

bus = dbus.SystemBus()

# Warning: Bus names and interfaces are different terms.
# Just because they contain same format or even same data
# does not mean they are the same thing.
# I used these variables to denote the diffrance where bus names and
# interfaces are used.
NM_BUSNAME = 'org.freedesktop.NetworkManager'
NM_IFACE = 'org.freedesktop.NetworkManager'

# Create Python object from /org/freedesktop/NetworkManager instance
# in org.freedesktop.NetworkManager application.
nm = bus.get_object(NM_BUSNAME, '/org/freedesktop/NetworkManager')

# Get list of active connections from the properties
# "Get" method is in "org.freedesktop.DBus.Properties" interface
# It takes the interface name which has the property and name of
# the property as arguments.

connections = nm.Get(NM_BUSNAME, 'ActiveConnections',

# While invoking a method on the object, dbus_interface keyword
# argument is provided to specify which interface has the method.

# Now, lets disconnect. This time we are using the NetworkManager
# interface: "org.freedesktop.NetworkManager"
for path in connections:
    nm.DeactivateConnection(path, dbus_interface=NM_IFACE)

If you have questions about this tutorial, you can ask in the comments section below. For more information about D-Bus, my references:

Linux Python Programming Web Programming

How to Run Flask Applications with Nginx Using Gunicorn

We have recently bought a VPS for İTÜ24, the online newsletter of Istanbul Technical University. The server is running on Ubuntu Server 12.04 operating system. Due to limited memory resources and performance concerns, we preferred to setup nginx as web server.

Our server will serve several web pages and applications developed in various programming languages, such as PHP, Python, Ruby (on Rails). Currently, we have one Python application, which is using Flask framework.

How we run Flask application with nginx, step by step…

Python Programming Snippets

WikiLeaks Cablegate Count v2

Since the official WikiLeaks page has changed, the previous script needs to be updated. Here it is:

import urllib, piksemel, re

wleaks = int(re.search(r"(d+) / [0-9,]+", piksemel.parseString(urllib.urlopen("http://www.wikileaks.ch/cablegate.html").read()).getTag("body").getTag("div").getTag("div").getTag("a").nextTag().getTag("p").toString()).groups()[0])

Please remember to install piksemel module.


Show forecast using Yahoo! Weather

Note: This snippet uses piksemel module to parse XML files. If you don’t have it, read this article to install it.

First of all, you need the WOEID (Where on Earth Identification) of the city. In order to learn it, go to http://weather.yahoo.com and search for the city. You’ll find the WOEID at the end of the url of the page.

# for Istanbul, TR
woeid = 2344116

Use this id in Yahoo! Weather API url, for w parameter.

weatherUri = "http://weather.yahooapis.com/forecastrss?w=%d" % woeid

If you want the results to be in metric units, add u=c parameter into your query.

weatherUri = "http://weather.yahooapis.com/forecastrss?w=%d&u=c" % woeid

Using urllib, fetch the XML file:

import urllib

xml = urllib.urlopen(weatherUri).read()

We can parse this XML easily using piksemel:

import piksemel

temp = piksemel.parseString(xml).getTag('channel').getTag('item').getTag('yweather:condition').getAttribute('temp')

You can also view XML file and look for other resources such as: humidity, visibility, wind, etc.