Fetch Current Bitcoin Price with Python

Using the MtGox API, you can easily fetch the current Bitcoin price using a very small Python script. import requests url = ‘http://data.mtgox.com/api/2/BTCUSD/money/ticker’ r = requests.get(url, headers={‘Accept’: ‘application/json’}) print r.json()[‘data’][‘avg’][‘display_short’] If you want to support for multiple currencies and more information, here’s an extended version: # -*- coding: utf-8 -*- from __future__ import print_function, unicode_literals […]

Read More

Heroku Buildpack for Numpy, Scipy and Scikit-Learn

(TLDR: https://github.com/dbrgn/heroku-buildpack-python-sklearn) Background At Webrepublic we just launched a Python based system that among other things does comparison of large texts using tf-idf vectors in a multi-dimensional vector space and measuring the cosine similarity between them (see http://stackoverflow.com/a/8897648/284318). For this, we needed scikit-learn. During the deployment process, I discovered that one does not simply deploy […]

Read More

Using Jedi with YouCompleteMe

(In case you’re not familiar with it yet, Jedi is an awesome autocompletion library for Python that tries to understand your code. There are editor plugins available for Vim, Emacs and Sublime Text 2.) The “official” Jedi plugin for vim is jedi-vim, but there’s another vim autocompletion plugin that supports Jedi: YouCompleteMe. In contrast to […]

Read More

Enums in Python

Until now, if you wanted to use enumeration types in Python you had to fall back to a class-attribute approach: >>> class Color(object): … RED = 1 … GREEN = 2 … BLUE = 3 … >>> print(Color.RED) 1 This has different downsides, for example with representation (Color.RED is represented as an integer, not as […]

Read More

Testing Dajaxice Views in Django

If you want to test Dajaxice views from the Django test client, this might be your first approach: url = ‘/dajaxice/apps.front.add_vote/’ data = {‘vote’: ‘yes’, ‘primary_key’: ‘1’} response = self.client.post(url, data=data) This doesn’t work for several reasons. First of all, we need to simulate an AJAX request. Therefore the HTTP_X_REQUESTED_WITH header needs to be set. […]

Read More

Programming a Perceptron in Python

At HSR, I’m currently enrolled in a course about neural networks and machine learning. One of the simplest forms of a neural network model is the perceptron. Background Information A perceptron classifier is a simple model of a neuron. It has different inputs ($x_1$…$x_n$) with different weights ($w_1$…$w_n$). $$s = sum_{i=0}^n w_i cdot x_i$$ The […]

Read More

d3.js and X-Requested-With

Most JavaScript frameworks set the X-Requested-With HTTP Header to XMLHttpRequest when sending non-cross-domain XHR requests. Many web frameworks like Django or Flask use this to detect AJAX requests. Because of issues with X-Requested-With and cross-domain XHR requests, d3.js does not set that header by default. Therefore Django’s request.is_ajax() and Flask’s request.is_xhr() break. In order to […]

Read More

Recompile Vim with Python/Ruby Support on Arch Linux

Update 2014-08-21: The vim packages in Arch Linux have been reorganized: https://www.archlinux.org/news/reorganization-of-vim-packages/. The standard vim package should now contain Python support, so the recompile is not necessary anymore. For my vim configuration, I need a version of Vim that was built with Python and Ruby support. Unfortunately, the default version of Vim that is installed […]

Read More

Virtualenv Quickstart Guide

I was searching for a nice virtualenv quickstart guide today, but couldn’t find one that I liked. Either they were outdated and still relied on easy_install, or they were too complicated. So here’s my own. Why use virtualenv? Virtualenv (http://www.virtualenv.org/) basically provides you with a full Python environment (and/or versions) inside a single folder. This […]

Read More

Mail Loop From Hell

Found in #django on freenode, Jul 12, 2012. All names are edited. 11:16 < abrt> since it’s quiet in here I’ll tell you a story. 11:16 < abrt> back in 1992, I had just graduated university and was interning at a government facility in newport news 11:16 < abrt> along with some friends from college. […]

Read More

Goodbye WordPress, hi rstblog!

Since having switched from the PHP world to the Python world about 2 years ago, I thought about relaunching my Blog using Python instead of PHP. At first, I thought about creating an application with Django and PostgreSQL, but never really had the time and motivation to finally implement it. But today I stumbled over […]

Read More