IronPython - high order functions as decorators for AOP

by DotNetNerd 20. September 2009 13:58

One of my favorite features from when I was playing around with F# was high order functions. A high order function is quite simply a function that takes a function as an argument and returnes a function. A basic sample just to get the point accross could be:

def Outer():
    innerText = "Hey dude! "
    def Inner(text):
        print innerText + text
    return Inner

myfunc = Outer()
myfunc("My function was called successfully")

This technique can be quite useful when composing functions, but also to wrap code and handle cross cutting concerns (e.g. like logging) through Aspect Oriented Programming. IronPython takes this concept a bit farther through what is called decorators. A decorator is to some extent similar to an attribute, and can be used to do AOP. Using decorators is as simple as defining a high order function and decorating the method that should be wrapped with @myHighOrderFunction like this:

def logging(function):
    def inner():
        print "Do pre call logging"
        function()
        print "Do post call logging"       
    return inner

@logging
def myfunc():
    print "Could do what interesting stuff you like"   
   
myfunc()

As you can see from the code it makes for a very straight foreward and elegant way og doing AOP.

UPDATE: I stumbled across this video with DevHawk where he actually shows a similar example during his talks here in DK.

Tags: ,

Community events and interesting stuff I have stumbled upon

by DotNetNerd 17. September 2009 18:56

Community events 

Over the last couple of months I have spent some time participating at usergroup events and listening to podcasts - to a larger extent than usual. Actually just today I was at "Dev Days - architecture that works", which was the first whole day ANUG event. The turnout was great and the speakers did a good job, so I think the foundation for more similar events was layed. Events like this and the passion that people display for their work is really what makes it so much fun to be a developer.

A few weekends ago I hosted my first codecamp which was about ASP.NET MVC, which will be followed up by a podcast this weekend - which will later be available at anug.dk. It's been a lot of fun and really giving to see how others approach things, and to be able to discuss issues with others that are of a technical nature, and therefore out of scope for everyday discussions with customers and projectmanagers.

Interesting stuff

Well, this post was actually meant to be build around a couple of links I wanted to give some attention, so here it goes.

I am hoping that I'll soon have more time to get back on track with blogging about IronPython - which has been parked for a couple of months because I have been busy moving and doing other stuff. A small but very cool link I found the other day actually gives you a chance to get started with IronPython withput having to install a bunch of stuff. Just go to TryIronPython.org and try it out - with my previous posts as a guide of course :-)

In another category a colleague of mine recommended a tool called Jing, which makes it possible to do short screencasts and either download them or upload them to a server and get a link that you can then pass around. Its actually just what I need in most cases when  need to show how a customer how to perform a task and it is hard to explain in words.

In the utilities category I was also recommended a tool that takes news pages that don't have rss feeds and actually exposes them as an rss feed anyway. The tool which is called feedity is not 100% all the time, but it actually does a pretty good job.

Last but not least I have a short list of podcasts that I listen to, and that I will highly recommend. They all publish casts pretty frequently, and all do a good job conveying discussions about what is happening in .NET

ANUGCast | Hanselminutes | stackoverflow | .Net Rocks! | Software engineering radio

Equinox - min bare røv...

by DotNetNerd 4. September 2009 11:44

Det har været lidt stille med blogindlæg på det seneste, idet jeg har været travlt beskæftiget med at flytte, så jeg kunne komme tættere på Århus. I den forbindelse har jeg haft en virkelig skuffende oplevelse med Equinox, hvor jeg ellers har været en god kunde i 1½ år. For 6 måneder siden ca meldte jeg mig på deres "kommer du betaler vi ordning". Det viser sig nu at der åbenbart stod i den kontrakt man får ved indmeldelse at man binder sig i 12 måneder. Et faktum jeg aldrig var blevet gjort klar over, og derfor var lykkeligt uvidende om.

I mellemtiden har de kørt en ny kampagne, hvor bindingen ikke længere er med. Da jeg var blevet underlagt en række stramninger der var i den forbindelse i forhold til deres fortolkning af hvad det vil sige at træne "en gang om ugen", tænkte jeg at så måtte det være en generel ændring om ikke andet. Det fastholder de imidlertid at det ikke er.

På trods at at jeg har prøvet at forklare dem at jeg er flyttet og aldrig var blevet informeret om at der var en binding insisterer de på at holde mig fast på kontrakten. Jeg har endda tilbudt om ikke de kan komme med et fornuftigt bud på et gebyr de kan tage for at ophæve medlemsskabet - det er de heller ikke interesserede i. I princippet kan jeg "bare" køre forbi Equinox hver 7'ende dag (+ de dage hvor der er 4 dage i en uge inden for slutningen af en måned) og checke ind i det næste halve år. Det virker selvsagt bare fuldstændigt tåbeligt, og jeg synes det er ret usmageligt at de i den grad fastholder en binding de har narret mig ind i. Jeg vil nu undersøge hvor vidt jeg kan komme udenom, da de efter min opfattelse klart er i konflikt med paragraf 3 i markedsføringsloven hvor der står:

"Der må heller ikke anvendes fremgangsmåder eller forretningmetoder, som kan være vildledende eller utilbørlige/urimelige over for forbrugere og andre erhvervsdrivende.Markedsføringen skal i det hele taget være korrekt, hæderlig og sandfærdig, og de erhvervsdrivende skal optræde loyalt og ærligt."

Imellemtiden vil jeg blot råde folk til at gå i en blød bue udenom Equinox.

Who am I?

My name is Christian Holm Nielsen, and I work at Vertica A/S, in Denmark, where I build commerce and other custom solutions.

Some of my primary focus areas are code quality, programming languages and new technologies in .NET.

Month List