Monday, August 17, 2009

Very Simple RESTful Web Services in Python

I was trying to find something for easy implementation of web services in any CGI language. Firstly I considered PHP. A lot of negative emotions, nothing really simple and a wasted day. Perl has some better support for the stuff, but I've chosen Python (mostly because I've got less experience in it and wanted to give it a try).

Python has a CGI-like standard called WSGI, which makes Web Services implementation much easier. But still not that easy, as I want it. So, spent a day writing my own "library", which you can find here, hosted on Google Code. Now you can write RESTful Web Services like this:
@url_pattern("/users/${username}/plans/${year}", ['GET', 'PUT'])
def get_plans (username, year, request):
return "Inside get_plans('%s', '%s')" % (username, year)
I'm gonna use it to implement a very first version of Pomodoro Server. Some brief impressions:
  • Python is very easy to learn and has a lot of great metaprogramming capabilities, very similar to Ruby. Great language, it's a pleasure to code it!
  • Python's documentation is just awesome!
  • Some Web Frameworks provide the similar functionality, but it's painful to install and comes with a lot of other heavyweight features, such as MVC and ORM.
  • Google Code is a nice place to host your projects, though there are some limitations (for example, wiki is very basic and not compatible with anything else).
Update: I've just found a similar thing in Java, defined in JSR-311: JAX-RS - Java API for RESTful Web Services and implemented as Jersey. Just a short sample from its tutorial:
@Path("/users/{username}")
public class UserResource {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
}
}
Update2: found a very similar solution for Python called Bottle. Sample code:
@route('/hello/:name')
def hello_name(name):
return 'Hello %s!' % name

run(host='localhost', port=8080)

Wednesday, August 5, 2009

DSL Killer Application

Now I want to point at a really mind-blowing tool called JetBrains Meta Programming System. You see, some time ago to implement your own Domain-Specific Language it was necessary to develop a grammar, implement a parser (or use such tools as GNU Bison for example) which normally operates in XML SAX manner and finally do something with the results obtained. Nowadays these new tools enable you to do virtually all this stuff in kinda type-safe way. It strictly watches at all your manipulations and alerts violations.

The whole process somehow resembles XDoclet and XSLT a bit, but it's much more reliable, because of the advanced IDEA editor, which takes into account all the necessary constraints, disallowing to misprint something. Still (due to verbose syntax) logical errors at source generation stage are highly possible. Good news is that you can use the whole power of modularity in this process to reduce such risks. Maybe the most striking thing about it is that its own language to define grammar and generate artifacts is also a DSL implemented in the same environment!

You can download JetBrains MPS here for free and try a tutorial. It will take some time to get used to the tool's UI. Its strangeness is in fact that there's a pretty complex tree representation behind a text you see on the screen, and it contains a lot of meta-information displayed as special markup signs, different colors, etc. So you shouldn't think of it as of a plain old source code anymore (to see what I'm talking about, just look at XML files inside a simple test project).

There are some alternatives to this environment, namely Microsoft Visual Studio Domain-Specific Language Tools and MetaEdit+, though I haven't tried any.

P.S.: Thanks to Martin Fowler's bliki for the link - it's a really visionary source of information! Here you can also watch his video on this topic.