Other MoonbasesLooking for the German band Alphaville? Their site is called Moonbase too. Sponsored Links |
Older →
(page 1 of 58)
Thursday March 22, 201208:32 PM: A Monkeypatching Decorator for PythonI had to do a bit of Python monkeypatching today — purely in the name of expediency, you understand — and like most things that are discouraged in Python, monkeypatching turns out to be a rather laborious thing. So, I wrote a simple decorator that automates the tedious bits and works more or less along the lines of Phil Hagelberg’s Robert Hooke library in Clojure. The operating principle is pretty straightforward: you define a function and use the decorator to specify the function or method to patch it over. When called, your patch function will receive the original function as its first positional argument, followed by any actual arguments that were intended for the real function.
Typical usage looks like this:
(It doesn’t really matter what you call the decorated patch function, but anonymous functions aren’t the Python Way.) Now this function will get called whenever someone invokes Instead of patching entire classes, you can also patch methods on individual instances. This works exactly the same way, except that you pass an instance rather than a class to the decorator, and (as with bound methods generally), you won’t get an explicit self argument when your patch function is called. Normally, lacking an explicit self argument is not a big inconvenience, since you’ll usually have a variable referring to the patched instance in the enclosing lexical environment of your patch function. The main thing that doesn’t work automagically are methods which use descriptors in
some non-default way, such as class methods and static methods. In those cases, the
You’d need to do this instead:
(This use case is basically the only reason that the
Anyway, there you have it. I’d tell you to use it in good health, but, well—just don’t shoot your eye out. A good heuristic to determine whether it’s appropriate to use this decorator in your code is whether your first thought on reading this article is “Wow, this would be a great way to simplify the code running on my production system.” (Hint: If that’s your first thought, then please don’t.) CommentAll fields are optional. Feedback is private by default; if it's okay for me to post your comment publicly, be sure to say so.
Wednesday December 21, 201110:34 PM: www. and RedirectsI mentioned on Twitter that I think most websites ought to provide service
with both I’d like to take a little more space to unpack my reasoning, and what I personally do about it. Read more... CommentAll fields are optional. Feedback is private by default; if it's okay for me to post your comment publicly, be sure to say so.
Thursday November 03, 201102:35 PM: Asynchronous is not "Fire and Forget": Joiners not QuittersIn all the recent enthusiasm for threaded and asynchronous programming, I’ve noticed people missing something really important: if you fire off an asynchronous task, you will eventually need to wait for it to complete — and you’ll probably need to be able to cancel it as well. At the basic level, this is why Ruby’s These are very important. Let’s begin by looking at Read more... CommentAll fields are optional. Feedback is private by default; if it's okay for me to post your comment publicly, be sure to say so.
Saturday June 18, 201111:55 AM: Sprockets Versus CommonJS: Require for Client-Side JavaScriptOnce you’ve split your client-side JavaScript application into nice, self-contained individual files, you have two new problems:
Sprockets is a tool for concatenating multiple JavaScript files into a single one — in the right order. It’s included with Rails 3.1, and in that environment is the recommended way to approach this. To make sure that a JavaScript file you depend on is included before the
file you’re in, just add a special comment of the form
To me, though, it’s a little sad that we’ve come full-circle back to the
same approach used by the C preprocessor to attain modularity. (Sprockets
require even has similar semantics assigned to the use of quotes and angle
brackets for included filenames — It’s doubly sad, because in the server-side JavaScript world, we’ve got a
perfectly servicible module system,
specified as part of CommonJS. Admittedly,
the CommonJS module API isn’t normally appropriate for client-side use,
because the Implementing the CommonJS Module API itself isn’t that difficult; a simple implementation of the whole thing seems to require about 70 lines of code:
Untested. Anyway, let’s say you’ve got two CommonJS modules, one of which uses an API exported by the other:
A Sprockets-like tool could then concatenate them all together like so:
Ideally, I’d like to see Sprockets support this itself, though the issue of backwards-compatibility needs to be considered. It’d be wonderful to be able to use CommonJS modules to structure client-side JavaScript. CommentAll fields are optional. Feedback is private by default; if it's okay for me to post your comment publicly, be sure to say so.
Thursday March 03, 201101:06 PM: Queueing Theory: Why the Other Line Moves FasterIn this video, Bill Hammack breezes through an introduction to basic queueing theory, the discipline established by Danish mathematician Agner Krarup Erlang. (Yes, the Erlang programming language is named after him.) In this era of parallel and distributed systems, I think queueing theory is actually a very important area for programmers to be familiar with, but Bill’s explanation should be accessible even to a non-technical audience. Why the other line is likely to move faster CommentAll fields are optional. Feedback is private by default; if it's okay for me to post your comment publicly, be sure to say so.
Sunday February 27, 201111:07 PM: Saving Money with Amazon S3 and BittorrentI’m not the biggest fan of Amazon lately, but if you happen to be using S3 for hosting big downloads, or if you want to permanently publish a file using bittorrent without having to maintain your own seed for the rest of time, S3 has a little-used feature that could save you a lot of trouble — and potentially money. Seeding Torrents from S3It turns out that S3 will publish and seed a torrent for any publicly-available file stored in S3. This is pretty easy to set up:
For example, if your uploaded file were available at S3 doesn’t generate the torrent until the first time it’s requested, so you may have to wait a while for the .torrent to be generated if the original file is large. In my experience, when demand outstrips supply, Amazon will actually temporarily spin up additional seeds in order to keep download speeds up (each individual Amazon seed seems to max out around 72kbps). In terms of billing, you’re charged (at the normal S3 rates) for all data downloaded via the Amazon seeds, but peer-to-peer transfers and downloads from other seeds would obviously be free for you. Technical details of working with Bittorrent and the S3 REST API can be found in Amazon’s developer documentation. Saving MoneyThere are basically two scenarios (that I can think of) in which seeding from S3 has the potential to save money:
In the first case, potential savings are going to be largely proportional to how busy the torrent is. If you only ever have one person downloading at a time, costs will be pretty much the same as if people were downloading via HTTP directly. In the second case, any difference is going to depend on the exact pricing structure you’re dealing with — for example, the first gigabyte downloaded from EC2 in a billing cycle is free, so if your EC2 seeds never serve significantly more than that, seeding from S3 is actually the more expensive option. In both cases, savings aren’t guranteed; it’s important to keep an eye on costs and run the numbers. If you aren’t measuring, you’re losing. To sum up: Advantages
Limitations
CommentAll fields are optional. Feedback is private by default; if it's okay for me to post your comment publicly, be sure to say so.
Friday February 11, 201111:12 AM: Neil Gaiman on Copyright, Piracy, and the WebThe Open Rights Group interviews Neil Gaiman about his experiences with online piracy: (Gaiman on Copyright Piracy and the Web) Edit: Also, here’s a journal entry that Gaiman posted during his American Gods experiment, responding to the concerns of an independent bookseller:
CommentAll fields are optional. Feedback is private by default; if it's okay for me to post your comment publicly, be sure to say so.
Thursday February 10, 201103:49 AM: Kitten (and Chipmunk) in Slow Motion |