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):
    @wraps(f)
    def dec(*args, **kwargs):
        # do something useful
        return f(*args, **kwargs)
    return dec

@decorator
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:

@pdecorator('hello')
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

@pdecorator('hello')
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):
    pass

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)
add2(3)
5

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

@pdecorator('hello')
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)
Share
Posted in Software | Tagged | Comments Off

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:

Share
Posted in IT | Tagged | Comments Off

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

Share
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:

Entry

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.

 

Share
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

Share
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).

Share
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 identi.ca 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 identi.ca.

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.

Share
Posted in IT, Social Networking | Tagged | Comments Off

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

Share
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.

Share
Posted in Business, Politics | Tagged | 1 Comment

Abandoning Twidroyd

Twidroyd/Twidroid is a Twitter application for the Android platform.  I really quite liked it, until they were bought by Tweetup.  Why?  Well, during the name change they added a huge EULA.  However, the important part is (highlighted part by me):

CONTENT You shall retain ownership rights in information or other content that you upload, post or otherwise transmit to or via your use of Twidroyd (“Submissions”); however, by making your Submissions through Twidroyd, you grant Licensor a worldwide, non-exclusive, royalty-free license (with the right to sublicense) to use, reproduce, edit, translate, reformat, distribute, modify, transmit, prepare derivative works of, publicly display and produce the Submissions in connection with the enhancement of the Twidroyd service or otherwise in connection with Licensor’s business. You agree that these licenses include the right for the Company to make your Submissions available to other companies, organizations or individuals who partner with the Company for the syndication, broadcast, distribution or publication of such content on other media and services, subject to our terms and conditions for such content use. Such additional uses by the Company, or other companies, organizations or individuals who partner with the Company, may be made with no compensation paid to you with respect to the Submissions. We may modify or adapt your Submissions in order to transmit, display or distribute it over computer networks and in various media and/or make changes to your Submissions as are necessary to conform and adapt that content to any requirements or limitations of any networks, devices, services or media.

This is a bit like Microsoft saying, “If you use Word to write something then you grant us a license to it.”  Or Bic saying if you use their biros then they get a license.  Or perhaps your paper manufacturer.

It’s also really sneaky.  They don’t do it upfront and tell you that they want this right; they hide it in a EULA and in the Terms and Conditions.

So, this was happened when they were bought by Tweetup?  So let’s look at their Terms. Sure enough, hidden in their Terms is:

You agree that these licenses include the right for the Company to make your Submissions and, if applicable, User Content, available to other companies, organizations or individuals who partner with the Company for the syndication, broadcast, distribution or publication of such content on other media and services, subject to our terms and conditions for such content use.

Again they are hiding this.  I guess they want to use all the ‘tweets’ to try and sell them or the intelligence/analysis that they contain.  And they are a commercial company and so want to make money.  I have no problem with that.  My problem is that they aren’t being upfront about it.  And I guess they aren’t being upfront about it because they suspect that most people don’t really like the idea that their stuff (even if it has no individual value) is being sold.

Perhaps we should start paying for these services and really know what is happening to our data, rather than thinking everything is for free, and thus effectively forcing companies to do this type of thing?

Now I just need to find an alternative.  And ideas?

Share
Posted in IT | Tagged , | 2 Comments