Posts about coding

All about coding

Re-implementing the Kubernetes Guestbook Example with Flask and NGINX

The official Kubernetes walkthrough guides often points to the guestbook application as a quintessential example of how a simple, but complete multi-tier web application can be deployed with Kubernetes. As described in the README, it consists of a web frontend, a redis master (for storage), and a replicated set of redis 'slaves'.

//cloud.google.com/container-engine/images/guestbook.png

This seemed like an ideal starting point for deploying my Flask applications with a similar stack, and also makes use of redis master/slaves. The difficulty I found with readily making use of this example as a starting point is that the frontend is implemented in PHP, which is considerably different to modern paradigms (Node.js, Flask/Django, Rails, etc.) As described in the README:

A frontend pod is a simple PHP server that is configured to talk to either the slave or master services, depending on whether the client request is a read or a write. It exposes a simple AJAX interface, and serves an Angular-based UX. Again we'll create a set of replicated frontend pods instantiated by a Deployment — this time, with three replicas.

I figured re-implementing the frontend pod in with Flask would require minimal changes - the UI would remain mostly the same, and the actual interaction with the redis master/slaves is quite trivial.

Read more…

Visualizing and Animating Optimization Algorithms with Matplotlib

In this series of notebooks, we demonstrate some useful patterns and recipes for visualizing animating optimization algorithms using Matplotlib.

In [1]:
%matplotlib inline
In [2]:
import matplotlib.pyplot as plt
import autograd.numpy as np

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import LogNorm
from matplotlib import animation
from IPython.display import HTML

from autograd import elementwise_grad, value_and_grad
from scipy.optimize import minimize
from collections import defaultdict
from itertools import zip_longest
from functools import partial

We shall restrict our attention to 3-dimensional problems for right now (i.e. optimizing over only 2 parameters), though what follows can be extended to higher dimensions by plotting all pairs of parameters against each other, effectively projecting the problem to 3-dimensions.

The Wikipedia article on Test functions for optimization has a few functions that are useful for evaluating optimization algorithms. In particular, we shall look at Beale's function:

$$ f(x, y) = (1.5 - x + xy)^2 + (2.25 - x + xy^2)^2 + (2.625 - x + xy^3)^2 $$
In [3]:
f  = lambda x, y: (1.5 - x + x*y)**2 + (2.25 - x + x*y**2)**2 + (2.625 - x + x*y**3)**2

Installing Cartopy on Mac OSX (10.11)

So you develop on Mac OS X (10.11) and have installed the external dependencies (geos 3.5.0, proj 4.9.2) of Cartopy with Homebrew:

$ brew install geos
$ brew install proj

Moreover, you have created a virtualenv and installed the Python dependencies (Cython, NumPy):

$ mkvirtualenv cartopy_venv
(cartopy_venv)$ pip install cython
(cartopy_venv)$ pip install numpy

Yet, when you finally go install Cartopy, you still encounter the following seemingly inexplicable error:

(cartopy_venv)$ pip install cartopy
[...]
clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/include -I./lib/cartopy -I-I/usr/local/Cellar/proj/4.9.2/include -I/usr/local/Cellar/geos/3.5.0/include -I/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c lib/cartopy/trace.cpp -o build/temp.macosx-10.11-x86_64-2.7/lib/cartopy/trace.o
lib/cartopy/trace.cpp:249:10: fatal error: 'proj_api.h' file not found
#include "proj_api.h"
         ^
1 error generated.
error: command 'clang' failed with exit status 1

----------------------------------------

Read more…

Workflow for keeping Nikola config file updated

For most, keeping Nikola up-to-date is usually a simple matter of running something like:

$ pip install --upgrade nikola

The same goes for its dependencies. However, one important thing that can get overlooked is the Nikola configuration file for your site (the conf.py file sitting at the root of your Nikola site directory), which is almost always updated with each major Nikola release.

Read more…

Creating custom (Unix) fortunes

Like others, I include fortune in my .bash_profile so that I am greeted with a random, witty and thought-provoking epigram upon starting a new shell session. Eventually, however, I found most of the epigrams to be mostly trite and vapid, and quickly grew tired of them. Instead of tinkering with the probabilities of sampling from the various datafiles available, I decided to roll with my own epigram datafile.

Read more…

Louis does dotfiles

../../images/bash.thumbnail.png

For a long time, I kept a small set of minimalistic dotfiles (.bash_profile, .gitconfig, .vimrc). You probably couldn't find anything in my .bash_profile other than exports and path definitions that were absolutely necessary to get tools such as Homebrew and virtualenvwrapper to work. Furthermore, I was dismissive towards spending any significant amount of time down the rabbit hole of perpetual dotfile customization and optimization.

Recently, while digging through my bookmarks saved from months ago, I came across GitHub does dotfiles. Needless to say, after perusing through some of the most popular dotfiles, dotfile utilities and tutorials, I came away quite impressed. It disabused me of my previous misbelief that dotfile calibration is ultimately a fruitless and ceaseless task, and I decided to give it another shot.

Read more…