Looking for some contracting work

So I’m planning to quit my current job (don’t worry, it’s only a year placement thing); I can’t take any more of the hacking I have to do every day to get around their crappy code. Their attitude towards software development sucks too, and quite frankly is pretty scary.

I have about 5 months before my course starts again, so if anyone is looking for a contractor that actually gives a damn about the code they write please e-mail me at: port001 ~AT~ gmail.com

Harmonious goes aspect oriented

(Harmonious is a web framework I’m working on for my degree, see previous blog entries for more info)

So after mulling over various problems (I’m in no hurry so I’m not forcing myself to make decisions about design issues on the spot. Instead I’m letting them sit in my head for however long it takes for the best solution to make itself known) related to implementing Aspect-Oriented Programming techniques elegantly, I’ve finally come to a conclusion and laid out the basic code needed.

So, wtf? It goes like this…

URLs are mapped to methods (see previous blog entries) and these methods can be decorated. The new ‘aspect’ decorator lets you decorate your method with, umm.. aspects. I like to call them Operational Aspects, as they relate to the various aspects of operation required to process a request.

e.g:

@public
@aspect(MyAspect)
def mypage(self):

return “hi!”

The aspects to which you decorate your method with are grouped, this is so that the framework knows what to expect from the aspect and what the aspect is expecting from the framework. There are also 3 execution modes: before your method is executed, after and in parallel with your method.

The reasons for implementing this all stem from my ponderings about how best to implement CRUD (Create Read Update Delete) support. My main qualm was that your method may only want the framework to handle some of these operations for you, the others you may want to handle yourself. So instead of an all or nothing situation you can now decorate your method with aspects (operations) of the CRUD model 🙂

Creating custom aspects is pretty easy too, here is a simple aspect:

class MyAspect:

class aspectmeta:

execution = AspectExecution.AFTER
keep_instance = True # Cache an instance of this aspect as we’re going to use it for more than one execution mode.
returns = AspectKeyword.RESPONSE
group = AspectGroup.OUTPUT

def __init__(self, harm, response):

self.harm = harm
self.response = response

def run(self, event):

notice(“MyAspect got event %s:” % event)

self.response.set_header(‘Status’, ‘groovy’)

return response

As you can probably tell, this aspect is executed after the requested method is executed and it returns a Response object. The framework determines what arguments __init__ accepts using introspection and supplies them from the current scope, most things will be imported though.

There are a number of things these aspects are going to make implementing really easy, input/output filters are pretty much a done job already 🙂

By the by, I’m looking for a new name. The reason being that harmonious.(org,net,com) are all taken 🙁

b2evo’s code tag sucks!

Ciao

Of URLs and Methods

For a long time now I’ve been hacking around on a web framework I started from scratch (mainly because they’re fun to code), but recently I started from scratch again using all I’d learnt from trying things out in the earlier prototype and what I’d learnt from other frameworks. So suddenly it’s all got a bit serious and I’m slowly coming together with something that’s looking very promising and for which I have big ambitions for.

Yeah, I know what you’re all thinking, but I think I understand the framework “scene” pretty well now and what it takes for a framework to stand out from the crowd. Time will tell 😉

Anyway, today I’m going to show you how the URL to method resolver works and what you can do with it.

First of all a little example:

class MyTestController:

@public
def index(self, a, b, c):

return “I like twigs”

def page1(self, a, b, c=”w00t”):

return “PHIL COLLINS IS GOD” # I actually got an anonymous letter in the post containing a badge with this on it! o_O

OK, pretty basic stuff and you can probably tell already what the URLs are going to look like for such mappings:

www.mysite.co.uk/MyTest/arg_a/arg_b/arg_c/ maps to the index method
www.mysite.co.uk/MyTest/page1/arg_a/arg_b/arg_c/ maps to the page1 method, so does
www.mysite.co.uk/MyTest/page1/arg_a/arg_b/

Joy.

Time for full complexity mode:

class MyTestController:

@public
def index(self, a, b, *varargs):

return “Hi mum!”

@public
def page1(self, a, b, c=”t00t”, *varargs, **kwargs):

return “burzum”

@public
@constraint(‘a’, r’^[a-zA-Z]*$’)
@constraint(‘c’, r'(some|thing|other)’)
def page2(self, a, b, c=”123″, **kwargs):

return ” ”

Look! you can accept variable arguments(an unknown number of) and keyword arguments(key/value pairs); groovy.

and some URL examples for those:
www.mysite.co.uk/MyTest/arg_a/arg_b/on/and/on/with/ariston maps to the index method
www.mysite.co.uk/MyTest/page1/arg_a/arg_b/arg_c/blah/a_key/a_value/another_key/another_value maps to the page1 method

(I’ll come to page2 in just a moment)

Well done if you noticed that for the page1 URL we’ll only ever have 1 or no variable in varargs as we need the unmapped URL parts (i.e not arg_a, b or c) to be a multiple of 2 so that we can satisfy the key/value pair requirement of the **kwargs. So, chances are you probably wont want to use *varargs and **kwargs in the same method.

page2…. I hope the decorator name (the @foo thing) and the regular expressions gave away what’s going on here. You can choose to constrain some of your arguments if you so whish, it’ll save you having to do some input validation at least.

So there you have it, you can use Python to its full extent as you would in any normal method and the framework does the leg work for you. I _am_ aware that using regular expressions to constrain URL mappings is standard stuff for frameworks these days, yet I think my using them in decorators is far nicer then having a separate messy mappings file.

Oh and by the way… using **kwargs effectively allows you to never create a URL containing ?foo=bar&ugly=yes again! I’m not saying something like www.mysite.co.uk/blog/06/01/2006/sessionid/df7d7sa5hf7g/size/100/ makes much sense to the user (maybe it’s less confusing for the average Joe?) but however you design your URLs is _totally_ up to you 🙂

p.s Please don’t kill me for any grammar/spelling mistakes — I’ve literally been looking at code all day long (the C apps at work were coded by the devil himself, I’m positive)

p.p.s b2evo’s tags don't seem to work too great and I'm too tired to play around with it.