Linting coffee-script in Emacs

I’ve been doing a lot of node.js recently, and I started editing coffee-script in my favourite editor which is Emacs. Now one thing I use regularly in Emacs is flymake-mode which essentially provides a set of hooks that are activated when you type into a buffer in Emacs.

So, I did a bit of investigation and discovered coffeelint. Then, all I needed to do was adapt lintnode (or rather the fork I use) and make it work with coffeeline. And coffeelintnode is the result.

You need a bit of emacs configuration to make it work. I use:

;; make coffeelinenode work nicely!
(add-to-list 'load-path "path-to-coffeelintnode")
(require 'flymake-coffeelint)
;; Make sure we can find the lintnode executable
(setq coffeelintnode-location "path-to-coffeelintnode")
(setq coffeelintnode-node-program "path-to-node-executable")
(setq coffeelintnode-coffeelint-excludes (list 'max_line_length))
(setq coffeelintnode-coffeelint-includes '())
(setq coffeelintnode-coffeelint-set "")
;; Start the server when we first open a coffee file and start checking
(setq coffeelintnode-autostart 'true)
(add-hook 'coffee-mode-hook
  (lambda ()
    (unless (eq buffer-file-name nil) (flymake-mode 1)) ;dont invoke flymake on temporary buffers for the interpreter
    (local-set-key [f2] 'flymake-goto-prev-error)
    (local-set-key [f3] 'flymake-goto-next-error)))
Posted in Uncategorized | Comments Off on Linting coffee-script in Emacs

Exploring Python @decorators

Today I’ve been using Python decorators to factor out common functionality in test cases. I ran into the slightly (!) interesting problem of how to define a @decorator(like_this). That is, a decorator that takes a parameter.

It’s fairly trivial to define a decorator that doesn’t take a parameter:

from functools import wraps

def decorator(f):
    def dec(*args, **kwargs):
        # do something useful
        return f(*args, **kwargs)
    return dec

def hello(string):
    print string

Okay, it doesn’t do much! @wraps(f) from functools does the magic. The end effect is that the function hello ends up pointing to the decfunction that is dynamically created when the file or module is loaded. That is, @decorator runs when the file is loaded, creates a dec instance and then @wraps ‘renames’ things so that hello points to dec and the f in dec points to the original hello definition (the one with the print string)!

But, what if we want to have a decorator that takes a parameter.  i.e. we want to do this:

def hello2(string, string2='optional'):
    hello2 doc string.
    print string, string2

So, as usual, one goes off and does some research. My answer was found here in a very useful post by Elf Sternberg. The example on the linked page has a bit more detail than I needed to understand so I simplified it as:

def pdecorator(name):
    def inner(f):
        def wrapped(*args, **kwargs):
            return f(name, *args, **kwargs)
        return wraps(f)(wrapped)
    return inner

def hello2(string, string2='optional'):
    My doc string.
    print string, string2

So, what’s going on here? PEP-318 explains what’s going on. Essentially, the syntax of a decorator with arguments like this:

@decorator(arg1, arg2, arg3)
def func(*args):

is actually transformed into:

func = decorator(arg1, arg2, arg3)(func)

That is, decorator has to return a function that can decorate func. So in the above example, wrapped is the function we want to wrap around f. So what does

return wraps(f)(wrapped)

actually do?  For that we have to look at wraps from functools.

wraps(f) returns a functools.partial object. A partial object is sort of like a function that is partially called, i.e. it isn’t ‘finished’ yet. e.g. Take the complete function f(a,b) -> a+b. f(2,3) -> 5. However, now imagine that g(f,2) returns a function h(b) which takes a single parameter b such that h(3) -> 5. Effectively h(b) (as we designed it) has partially completed or frozen f() such that the first parameter a is now 2 in the add. In Python it looks like this:

from functools import partial
add2 = partial(lambda x,y: x+y, 2)

i.e. the 2 was captured in the in the x position in the function. Cool, huh!

Anyway, what wraps(...) returns is a partially applied functools.update_wrapper(...) function which has captured the __name__, __doc__ and __dict__ of the function passed as an argument. i.e. wraps(f) returns a partially applied update_wrapper function with f.__name__, f.__doc__ and f.__dict__ frozen into it. However, the wrap hasn’t yet been applied to a function. In our example above, wraps(f)(wrapped) thus then wraps f.__name__, f.__doc__ and f.__dict__ on to the wrapped function.

So coming back to the example (repeated again to stop you scrolling up and down!):

def pdecorator(name):
    def inner(f):
        def wrapped(*args, **kwargs):
            return f(name, *args, **kwargs)
        return wraps(f)(wrapped)
    return inner

def hello2(string, string2='optional'):
    My doc string.
    print string, string2

inner(f) returns a function which is basically wrapped(f(…)) but has the __name__, __doc__ and __dict__ of f.

i.e. the nice equivalent of

def hello2(...): pass
hello2 = wrapped(hello2)
Posted in Software | Tagged | Comments Off on Exploring Python @decorators

Ubuntu 10.04 LTS – experimenting with overcommit

I currently running Ubuntu 10.04 LTS as my desktop. Neither Unity nor GNOME Shell (in GNOME 3) really interest me, but that’s for another post! This one is about the exasperation I feel when Linux overcommits too much memory and then goes into disk-thrashing hell.

It happens when I open just ‘one-too-many’ tabs in Chrome. The desktop becomes unresponsive as the memory manager in Linux tries to find pages for Chrome to use for the new tab. Unfortunately, it’s so overcommitted the pages that it can’t find any. Every process wants to run and Linux essentially runs out of pages. Hence disk-thrashing starts, the desktop slows to a crawl or simply becomes unresponsive, and I try to Ctrl-Alt-F1 to get to a TTY so I can kill something (usually ‘killall chrome’).

I’d rather the box just didn’t allocate the memory if it doesn’t have it – i.e. don’t open the new tab if there isn’t memory for it.

My experiment is going to be to run for a few weeks using some sysctls of:

  • vm.overcommit_memory = 2
  • vm.overcommit_ratio = 120

This should give me a maximum commit of 8GB (4Gb swap and 4GB of memory). I wonder how it will go?

Useful information from:

Posted in IT | Tagged | Comments Off on Ubuntu 10.04 LTS – experimenting with overcommit

So Microsoft is buying Skype …

Yesterday (10 May 2011), news broke that Microsoft is paying $8.5B for Skype (or roughly £5.2B in English).  Skype is a rather good, if proprietary, telephony/conferencing app that is multi-platform. It currently works on Windows, of course, but also on Macs, Linux, iThings (iPhones, iPads and iPods), and on Android devices. And there’s the potential problem.

Continue reading

Posted in Business, IT | Tagged | 2 Comments

Rant: Just because something is hard …

<rant>I’m doing a beginner’s French course at the Open University. It’s great. It’s also hard work and takes lots of time.

However, the forums seem to attract many posts about how it is “suitable for beginner’s” or that “it’s not a beginner’s course”. Apart from being annoying, they are simply incorrect.

Fact: the course, L192, assumes no previous knowledge of French before starting on the course:


No prior knowledge of French is required to study this course.

This is a Level 1 course. Level 1 courses provide core subject knowledge and study skills needed for both higher education and distance learning. If you have any doubt about the suitability of the course, please contact our Student Registration & Enquiry Service.

The course is for beginner’s; it just isn’t easy.</rant>

The problem, of course, is that when something is hard or takes a little bit of work or time, some people instantly assume that the problem lies outside of themselves, and therefore, surely the problem is with the course/person/situation. Work harder and/or smarter; stop complaining.


Posted in Uncategorized | 3 Comments

The facebook problem

When a for-profit company gives you something for free, with little chance of ever charging, you have to ask, “who is the customer and what is the product?” Facebook has (reputedly) 500 million users, none of whom pay a penny for the service. Twitter has (possibly)  175 million users, again, none of whom pay anything for the service.

Who is the product? Who is the customer? Do you even care?

Continue reading

Posted in Life, Politics, Social Networking, Technology | Tagged , , , , | 11 Comments

Call Centre PIN Codes

With the recent snowy weather in December, my roof sustained a fair amount of damage. Enough damage, in fact, to require a claim on the insurance. All well and good. In fact the insurance company have been very good and all is proceeding to plan.

However, they called me today, somewhat randomly, to inform me that the claim was proceeding normally. They called me and then asked me to provide details to ‘prove’ who I was. I said, before I do that, can you prove who you are? And they couldn’t. There’s no information they could give me over the phone to demonstrate that they were who they said they were.

Why am I so cautious? Well, why not. It’s trivially easy to spoof a phone number on ID, and this was ‘withheld’ anyway. I ended the call, looked up their number on my policy document and rang back in – it was the only way to (mostly) ensure that I was talking to who I thought I would be.

So it made me think: if we need to prove who we are when we call them, why shouldn’t they prove who they are when they call us. And the easiest way to do that is to give a piece of information that only they could know, essentially, something that you’ve given them as a password only to be given back to you – not for you to access the account.

Of course, it will never happen … (sigh).

Posted in Uncategorized | 2 Comments

MyIET – not another networking silo, please

The  IET, or Institution of Engineering and Technology, is introducing ‘MyIET’, which is a member’s personal portal on the IET website. However, it’s also got status updates like twitter, a discussions and comments forum, etc. It looks like they have tagged some social networking stuff onto the standard account type stuff.

Some context is probably required: The IET is a relatively large professional organisations serving mostly electrical and electronics engineers with a smattering of other technology people. There will, of course, be some networking that takes place within the confines of the Institution.

However, do I really want my activity stream to be confined in MyIET, rather than at or twitter? No, is the honest answer, but I also think that what the IET have added to MyIET is symptomatic of a wider issue. Actually, I don’t really want it in twitter either, except that it has the network effect. I’d rather it was in

The issue is silos. MyIET is yet another social networking silo. If you put your data into the MyIET silo can you easily get it out? In this case, the answer is a resounding ‘no’. There doesn’t appear to be any options to export your status feed, comments or other data that you put into the site.

My ideal would be that my activity stream (or status updates) are held at a location of my choosing. They would probably be split into a number of categories, one of which might be IET related or business/professional related. And then I would configure the IET portal to pull my activity stream to it. Then, I would control the activity stream and what I do with it.

This is, of course, a federated social network. Oh, and OneSocialWeb are doing just that.

Posted in IT, Social Networking | Tagged | Comments Off on MyIET – not another networking silo, please

Putting CM6 (Android 2.2 Froyo) on a UK Vodafone HTC Magic

I’ve got an HTC Magic by Vodafone in the UK. It’s a very nice phone. Unfortunately, it is also abandonware. It is now virtually out of contract. And before I rooted it and put CM6 on it, it was stuck at Android 1.6 which was released on the 15 September 2009. That is over a year ago. Android 2.2 was released on the 20 May 2010 over 4 months ago (source). This is the story of how I got CM6 on to my HTC Magic.

Continue reading

Posted in IT, Technology | Tagged | 9 Comments

Banks: the problem with ‘too big to fail’

We hear it time and again.  Such-and-such bank is too big to fail. To me, that simply means too big. A bank failing needs to be like a ripple in a pond not like a tsunami crashing all before it and leaving a wasteland behind it.

There’s just something very wrong with the banking industry. In virtually every other industry corporate failure (or basically going bust) is the market punishing poor decision making by that organisation’s management. It’s a good thing. A company makes some bad decisions and the people in charge get feedback about those decisions in their company losing customers, money, both or even just going bust. A company making good decisions is rewarded in the market and keeps going.

And a company going bust is actually good feedback; it’s doing something wrong in the market. The people working in it should be released back into the labour pool where they can be employed by other companies that are making the right decisions in the market. This, of course, requires a near-perfect market where lots of little companies are providing services in the market.

However, in the UK, Northern Rock, RBS, Lloyds et al. made incredibly poor decisions about how to invest their money.  The lost staggering amounts of money on their little pyramid scheme when over-inflated property prices crashed and mortgages were defaulted on by people who should never have been given the mortgage in the first place.  Pure Greed.  But it would’ve been okay if their gambling had paid off.  They didn’t, and the banks didn’t get to feel the pain of their mistakes.  And that’s because, we, the tax payer bailed every-last-one-of-them out.

So they’ve learnt nothing. In fact they’ve been rewarded for their incompetence. Not only do the banks get to lose bigger-than-telephone-number amounts of money, but they now know, if they do, they’ll simply get their customers to bail them out one way or another.

What’s the solution? I don’t know, but one method might be to make them small enough so that when they fail, we get a ripple and not a tsunami. And maybe regulate them so that the gamblers don’t get to take down the mortgage providers, day-to-day business providers, and the consumer/retail banks. Obviously, it’s more complex than this, but it might be a step in the right direction.

Posted in Business, Politics | Tagged | 1 Comment