I’m not sure why I haven’t seen anybody using this since Surlex came out in 2009. Surlex is especially powerful when using it to match Django URLs.
I’ve written several Django apps recently that had relatively complex URLs and Surlex has really saved the day for me. There are two main features that I’d like to point out:
- from surlex.dj import surl
from surlex.dj import surl
This function allows one to use surlex syntax in the urlpatterns and simply put surl() around it instead of url() like so:
This is a real URL pattern that I’ve put here. The ‘normal’ way to do this would have been:
That may make perfect sense to anyone who understands named groups in regex, but I would rather not have to type such a complicated syntax for such a common task in Django.
This one is really useful for all sorts of macros. I recently wrote a yardbird-based IRC bot for work and needed a bunch of macros for the irc message resolving portion:
name = settings.YARDBIRD_NAME
register_macro(‘botmsg’,'(?:chanmsg/%s[:,. ] ?|privmsg/)’ % name)
To be honest, I’ve severely modified Yardbird and will eventually send the patches back, so this stuff doesn’t make as much sense without my patches that increase the flexibility of Yardbird.
The ‘ch’ macro is for matching IRC channels, ‘nm’ is for nicknames, ‘botmsg’ is for messages directed to the bot, and ‘msg’ is for messages in the channel or to the bot versus other types of messages, e.g. nick changes come in as ‘nick/’ so we don’t want to act on those as possible commands.
The patterns for these macros are really simply and much easier to read as well:
surl(r’^<:botmsg>tell <nick:nm> <message=.*>$’, ‘tell’),
That command is for telling the bot to later tell a user a message when they log in again. It checks if the command was to the bot through ‘botmsg’, sees the command as ‘tell’ and the command has two parameters, ‘user’ and ‘message’ the latter of which matches the rest of the statement. All of this is accessible from a very quick glance at the pattern instead of attempting to read this:
‘^(?:chanmsg/bot_name[:,. ] ?|privmsg/)tell (?P<nick>[\\w_-]+) (?P<message>.*)$’
The length of that regex is 77 characters, more than twice the 38 characters of the surlex pattern.
As I said before, Surlex is Awesome.