skien.cc

a mathematician/economist/statistician by schooling, a data scientist/python hacker by trade, and a homebrewer by night, skien.cc (pronounced skīəns) is my blog aimed to track explorations in all of the above.

Behind the Scenes of the Startup Podcast

I’m a huge fan of podcasts: I never miss an episode of Marketplace; I am near giddy when a new episode of Radiolab is released; I credit Planet Money with preparing me for a successful first interview out of college at Freddie Mac. The hottest new podcast is undoubtedly Startup (Serial is a close second.) Created and narrated by Alex Blumberg (of Planet Money and This American Life), it tracks Alex and his cofounder Matt Lieber on their journey to launch Gimlet Media, a podcasting company.

This podcast offers a rare look into the startup/VC/Silicon Valley tech scene and the process of launching a company from the ground up. While popularity of this topic has certainly grown in recent years, rarely do we get to see such a raw and personal take on it. Certainly, any method of storytelling that condenses months of work into half hour pieces cannot escape an editorial aspect, but Alex manages to capture his story in an unsensational yet insightful way.

Unpythonic Python

FizzBuzz

When I applied to Hacker School, one of the application question was FizzBuzz:

Write a program that prints out the numbers 1 to 100 (inclusive). If the number is divisible by 3, print Fizz instead of the number. If it’s divisible by 5, print Buzz. If it’s divisible by both 3 and 5, print FizzBuzz. You can use any language.

(Hacker School has since updated the question slightly, likely to make it more difficult to solve via Google. I’ve left out the updated version intentionally to minimize my effect on Googlability.)

This problem is fairly straight forward, and a good bite size problem to use as an example for different languages and programming styles; it’s similar to “Hello, World!” and “Fibonacci”.

Some Unpythonic Python

I looked at this problem today while showing a friend the Hacker School application, and started thinking about the many ways to tackle the problem just in Python alone. Python, specifically with PEP 8, lays out the ideal way to write Pythonic Python. But Python doesn’t enforce Pythonic Python, so I started thinking, what other kinds of Python could I write this in.

Warning: Very Unpythonic Python ahead. However, all code should work (with Python 2.7.5).

Feel free to tweet me or comment with suggested fixes, or new additions.

SQLAlchemy and Race Conditions: Follow Up on Commits and Flushes

In an earlier post, I talked about my adventures in creating a get_one_or_create() function for SQLAlchemy to use with Flask and Postgres.

There was an excellent question asked in the comments about weather session.flush() could be used on line 13 instead of session.commit(). I didn’t fully understand the answer, but explained that I had landed on this for safety of efficiency. The author of SQLAlchemy also echoed that it’s not a simple answer. I spent some time with a small test app on Heroku to figure out how this all works, and what the best approach really was.

Adding Unique Constraints After the Fact in SQLAlchemy

In a previous post, I wrote about writing a get_one_or_create() function for SQLAlchemy. Turns out that I had inadvertently created a race condition while horizontally scaling on Heroku. That post explains, in detail, the solution I came up with for moving forward, however I still had duplicate objects in my database that needed to remove. Since I’m on Heroku, I’m using Postgres (although I’m highly opinionated towards using Postgres often), so some of this may be Postgres specific, and I will do my best to note when that’s the case.

I’ll use the same example models that I used in my previous post. We had Game and GameBeat models:

On Markdown

In the beginning, there was LaTeX

As a former mathematics student, I have an unusual soft spot for LaTeX. Despite it’s obscenely verbose and quirky syntax, it was more than a breath of fresh air compared to Word’s Equation Editor. (It required a highlighting, 4 clicks, and keyboard input to create an exponent, if I remember correctly.) But not only does LaTeX keep your fingers on the keyboard, it’s output is the most beautiful thing I’ve ever laid eyes on. I mean, just look at this inequality:

SQLAlchemy and Race Conditions: Implementing `get_one_or_create()`

Note: Examples here are built with respect to Flask SQLAlchemy, and while some notation may match that convention, the concepts should apply to use of SQLAlchemy in general.

Motivation

Suppose we have a Flask app that interacts with an API on a site that hosts webgames. Our users have OAuth’ed our application into the API so we can see their activity, and we want to track which games they have beat. (We’ll assume that users only beat a game once or more generally we are only concerned with the first time they beat it.) We create the Game and GameBeat models like so:

Flask-Twitter-OEmbedder and Flask Extension Testing

Flask-Twitter-OEmbedder

The Twitter API V1.1 provides an endpoint for easily embedding tweets into your webpages. However, Twitter rate limits this endpoint and requires authorization. Flask Twitter OEmbedder gets your Twitter credentials from your app.config and exposes the oembed_tweet function to the Jinja templates. Flask Twitter OEmbedder also manages the caching of the tweets, given an arbitrary cache using Flask-Cache.

You can check it out and fork it on GitHub. Feature requests, issues, and anything else are encouraged! This is very much alpha and I plan to continue working on it.

Flask Extension Testing

Flask-Twitter-OEmbedder was the first Flask extension I have written (and really the first proper Python package I’ve ever written). This summer I am lucky enough to be participating in Hacker School and one of my goals for the batch was to be able to write unit tests for my code that I develop. Since this was my first proper package, I figured it was a great place to start.

Unlocking Technology

I recently read an opinion piece on Wired, Beyond Unlocking: Don’t Let Them Kill the First Decent Copyright Reform by Kyle Wiens. The author touts the bill as “not a band-aid,” but “a solution” because

[it] would allow all consumers to circumvent the digital locks on their mobile devices. Anyone could access and modify software on their devices, in the same way they already modify and repair hardware.

On the surface, this seems like pretty clear position. I bought my hardware, I own it, I should be able to take it apart and use it however I want! The only reason tech companies would want to put digital locks on whatever devices they sell is because they want to maintain complete control of their platform and make more money. Those damn corporate fat cats! How could you not want to support such a clear and open policy?

But I argue (in homage to Freakonomics) that there is a hidden side here.

Print Can Be Deceiving

This is more of a short musing than a blog post, but maybe some insight that can help a beginner. This is written specifically with respect to Python, but I think some of the basic ideas translate to other object oriented languages.

when print was your best friend

When someone is new to Python, and more so when new to programming in general, print is your best friend. Learning a new language is done by comparing what you expect happen and what actually happen. The prototypical example in any language is

print 'Hello World!'

and a new comer to Python going through something like Learn Python the Hard Way (LPTHW) will spend a number of lessons where a script ends with a print statement revealing the answer. This is notable because in a production system, print isn’t used much at all, unless something goes wrong.

Using the Facebook Javascript SDK and jQuery to Create User Accounts With a Flask App

Before we get started, the full code is available on GitHub.

Getting Started with developers.facebook.com

To work with any of the Facebook APIs, you need to be a Facebook developer. Just go to developers.facebook.com and sign up with your Facebook account. Once you’re signed in, click on Apps and create a new app. In order to actually communicate with the app, you will need to register the app with a domain name where the app will live. One option for this is to use (heroku)[http://www.heroku.com/], although I used AWS on a subdomain of my existing domain. The repo in it’s current state isn’t set up for heroku, and may require a few changes to run there.

Using the Facebook JavaScript SDK

I should first point out that Facebook has fantastic documentation for using this SDK. This section is mostly the same as the their content, with a few Flask specific things.