-
posts
-
Cron only decorator for App Engine
For a recent project I recently I have been using App Engine’s cron feature to aggregate data and perform maintenance tasks. However, since cron is a simple web request, if a user accesses that url then the cron job will run. In order to prevent normal users from being able to run cron jobs I created a decorator that specifies a view as cron only. This decorator is for use with the kay framework but it should be easy to port to your application. def cron_only(func): from werkzeug import Response def inner(request, *args, **kwargs): import os from kay.conf import settings...
-
Dynamically Adding a Method to Classes or Class Instances in Python
In Python you sometimes want to dynamically add methods to classes or class instances (objects). In Python code, the most obvious way to accomplish this might be something like the following but it has one caveat, class MyObj(object): def __init__(self, val): self.val = val def new_method(self, value): return self.val + value obj = MyObj(3) obj.method = new_method … you can’t use self. Let’s try to actually call the method. >>> obj.method(5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: new_method() takes exactly 2 arguments (1 given) The new_method() function is added to the class as a...
-
Data and instinct driven decision making
I just read a piece called We Are All Talk Radio Hosts which mentions some studies saying that the more we humans think about things, the more we make bad decisions. The rationale is that we try to come up with reasons why we like or dislike something rather than simply choosing it based on our experience. It extends to almost all of our choices from jams, to cars, to apartments. This sort of phenomenon is why you can’t ask people what they want. They don’t know. Even if they think they know, they’ll really tell you something they don’t...
-
Key Value Storage Systems ... and Beyond ... with Python
Google docs wouldn’t let me share the presentation publicly with people outside
our company’s domain and it gave me an error when I tried to download it as a
PowerPoint file or PDF so I was forced to recreate my presentation locally.
Anyway, I placed the slides to my talk at PyCon Asia
online please check it out on Slideshare.
-
Google Chrome Background Connections
Today I found that Google Chrome was making lots of background connections to 1e100.net. I was a little worried at first but this seems to be a google owned domain and these connections are used for their anti-phishing/malware feature. But even after disabling the feature I still noticed lots of connections open to 1e100.net: ian@macbook-ian:~$ lsof -i4 | grep chrome chrome 5294 ian 70u IPv4 82734 0t0 TCP macbook-ian.local:54753->nrt04s01-in-f99.1e100.net:www (ESTABLISHED) chrome 5294 ian 81u IPv4 82735 0t0 TCP macbook-ian.local:54754->nrt04s01-in-f99.1e100.net:www (ESTABLISHED) chrome 5294 ian 82u IPv4 82736 0t0 TCP macbook-ian.local:54755->nrt04s01-in-f99.1e100.net:www (ESTABLISHED) chrome 5294 ian 89u IPv4 83365 0t0 TCP macbook-ian.local:56139->tx-in-f100.1e100.net:www (ESTABLISHED)...
-
Shady Harvard Puzzle Facebook App Disassembled
Today I got an email from a friend on Facebook that suggested that I try out this application called “Only 4% of harvard grads can solve this riddle..”. Being curious I took a look at it. The app starts out innocently enough, posing a riddle that supposedly only 4% of Harvard grads can guess. But in order to see the answer it asks you to type “Ctrl-C” to copy some javascript, “Alt-D” to select your address bar, and “Ctrl-V Enter” to run the javascript. Here is the beautified packed javascript. javascript: (function () { a = "app112010525500764_jop"; b = "app112010525500764_jode";...
-
Caching Permanent Redirects
Since I’ve started using Chrome as my main browser for surfing I have noticed that some web applications seem to redirect to 404 pages. After some investigation it seems that the browser caches 301 responses (they are permanent after all) and when going to that URL again automatically redirects you. This is a standard and correct thing to do since it saves HTTP requests but many websites/webapps haven’t used redirects and permanent redirects properly. Many return permanent redirects when redirecting after a login page or user action. I suppose many people think that it’s the better thing to do since...
-
python-openvcdiff and Cython
I started a project today to implement an interface for the open-vcdiff using Cython. I’m not a C++ master and the Python C libraries are pretty new to me but I managed to expose and implement a few methods of the VCDiffEncoder class. The hardest part so far has been trying to figure out how to use the C++ standard library types like std::string. I’m also not sure how I can interface with python in such a way as to allow fast processing of potentially large binary data. Normally I would use a file-like object in Python to create a...
-
Modipyd with Growl Notifications and Test Driven Development
Recently at work my coworker Shinya Okano came across Modipyd written by Takanori Ishikawa. Modipyd is a module dependency monitoring framework which can build module dependency trees and monitor when modules have been changed. But most interesting feature it provides is the pyautotest tool. pyautotest is a small daemon that will monitor modules in a project and automatically run tests that depend on a particular module when the module changes. This comes in really handy when writing python libraries and tools. When run on the console it looks something like this: ian@macbook-ian:~/src/mylib$ pyautotest .... ---------------------------------------------------------------------- Ran 4 tests in 0.002s...
-
'self' ForeignKeys always result in a JOIN
I came across a little annoyance in Django today. I found that a ForeignKey that reference self, i.e. points to the same table, always results in a join in a filter. Take this normal foreign key reference. class Customer(models.Models): user = models.ForeignKey(User) >>> Customer.objects.filter(user__isnull)._as_sql() ('SELECT U0."id" FROM "accounts_customer" U0 WHERE U0."customer_id" IS NULL', ()) Now lets look at a version of the customer model with a self reference. class Customer(models.Models): user = models.ForeignKey(User) other_cust = models.ForeignKey('self') >>> Customer.objects.filter(user__isnull)._as_sql() ('SELECT U0."id" FROM "accounts_customer" U0 LEFT OUTER JOIN "accounts_customer" U1 ON (U0."other_cust_id" = U1."id") WHERE U1."id" IS NULL', ()) Hmm, yuck. That...