# Python SimpleHTTPServer Recipe: Enable CORS

Create a file, let's call it cors_http_server.py, with the code below:

#! /usr/bin/env python

from SimpleHTTPServer import SimpleHTTPRequestHandler, test

class CORSHTTPRequestHandler(SimpleHTTPRequestHandler):

if __name__ == '__main__':
test(HandlerClass=CORSHTTPRequestHandler)


# Installing lxml on Mac OSX (10.11) inside a virtualenv with pip

The lxml library is a dependency for many popular Python projects such as Scrapy and Nikola. If you are a Mac OSX user, it's highly likely that you have encountered the following error when trying to install lxml or its dependants with pip:

In file included from src/lxml/lxml.etree.c:346:
$WORKON_HOME/venv_name/build/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found #include "libxml/xmlversion.h" ^ 1 error generated. error: command 'clang' failed with exit status 1  Read more… # Quotes: "One broken window starts the process toward decay" A building with broken windows looks like nobody cares about it. So other people stop caring. They allow more windows to become broken. Eventually they actively break them. They despoil the facade with graffiti and allow garbage to collect. One broken window starts the process toward decay. —Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship # Quotes: "It's harder to read code than to write it" We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by incremental renovation: tinkering, improving, planting flower beds. There's a subtle reason that programmers always want to throw away the code and start over. The reason is that they think the old code is a mess. And here is the interesting observation: they are probably wrong. The reason that they think the old code is a mess is because of a cardinal, fundamental law of programming: It’s harder to read code than to write it. —Joel Spolsky, Things You Should Never Do, Part I # Creating a Nikola theme with Sass-compiled Bootstrap ## Initializing the Theme First, create a new Nikola theme. I prefer to do this by creating a new repository on Github so that it can be initialized with a README, LICENSE, .gitignore, etc. Once that's done, clone the newly created repo into <site-root>/themes. Read more… # Installing NLOpt with Python support on Mac OS X (10.10) NLOpt is a popular open-source library for nonlinear optimization. It can be somewhat tricky to install it with Python support on Mac OS X, especially isolated under a virtual environment. This article outlines the different ways of making NLopt play nicely with your Python development environment, whatever that may be. Read more… # Project Euler Problem 42: Coded triangle numbers The nth term of the sequence of triangle numbers is given by,$T_n = \frac{n(n+1)}{2}$; so the first ten triangle numbers are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 =$T_{10}$. If the word value is a triangle number then we shall call the word a triangle word. Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words? Read more… # Project Euler Problem 43: Sub-string divisibility The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. Let$d_1$be the 1st digit,$d_2$be the 2nd digit, and so on. In this way, we note the following:$d_2d_3d_4=406$is divisible by 2$d_3d_4d_5=063$is divisible by 3$d_4d_5d_6=635$is divisible by 5$d_5d_6d_7=357$is divisible by 7$d_6d_7d_8=572$is divisible by 11$d_7d_8d_9=728$is divisible by 13$d_8d_9d_{10}=289$is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. Read more… # Project Euler Problem 44: Pentagon numbers Pentagonal numbers are generated by the formula,$P_n=\frac{n(3n−1)}{2}$. The first ten pentagonal numbers are: 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... It can be seen that$P_4 + P_7 = 22 + 70 = 92 = P_8$. However, their difference,$70 − 22 = 48$, is not pentagonal. Find the pair of pentagonal numbers,$P_j$and$P_k$, for which their sum and difference are pentagonal and$D = |P_k − P_j|$is minimised; what is the value of$D\$?

In [1]:
def polygonal(s):
c = s - 2
a = b = 1
while True:
yield a
b += c
a += b

In [2]:
def sum_diff_polygonal(s):
seen = set()
for i in polygonal(s):
for j in seen:
p = i - j
q = j
# we already know p+q=i is s-gonal
# since i must be; just need to check
# that p and p-q are as well
if p in seen and p-q in seen:
yield p, q

it = sum_diff_polygonal(5)

next(it)

(7042750, 1560090)