Categories: Portfolio, Software Development

Speaking at PyCon Canada 2017 on “Python as a Philosophy”

I’m going to be in Montreal this weekend for PyCon Canada to give a talk on “Python as a Philosophy”. It will explore some of the ideas that are part of Python, built into its core and affect its community and how those ideas have spread to other software and software developers:

A number of programming languages that have not been used quite as frequently as others contain “profound enlightenment” that a developer learns through their use. Languages such as Lisp and Smalltalk are in this category.

This presentation suggests that Python is also one of those languages. Python and PEP20 The Zen Of Python can affect how a developer writes code and then translates their practice into using other programming languages.

And more:

For example, my own hands-on programming in Python has allowed me to carry on a number of practices into JavaScript and Ruby. The talk will specifically cover “explicit rather than implicit” code and the value of documentation as a developer-user experience.

I will outline several examples and counter-examples of Python-inspired code practices within the context of JavaScript and Ruby.

Slides for the presentation:

Categories: Software Development

Dimming JavaScript curly braces and brackets in Emacs

When coding in Lisp or Scheme, some Emacs users prefer to have the parenthesis/brackets dimmed. The reason for this is that it makes it easier to see the things that matter like variable names.


If you are looking for a JavaScript mode for Emacs, please check out the following projects:


I’ve been coding more JavaScript lately and wondered if this would work for JS and indeed it does. While it doesn’t turn JS into CoffeeScript and you still have to type in in the brackets and curly braces and semi-colons, it does let you adjust the colour of them and let’s you dim them so that you can see the important parts.

Download parenface.el and then paste in the following snippet and you’re good to go. Don’t forget to customize the paren-face colour by doing this: M-x customize-group faces

https://gist.github.com/omouse/9394679

Here’s a before and after shot (I’m using snow3 as the paren-face colour, the default provided by parenface.el is DimGray):

before and after enabling parenface for js-mode
before and after enabling parenface for js-mode

I’m not sure how useful it is to hide the ‘var’ and ‘function’ keywords. The cool thing is that all you have to do is alter the regex to suit your preferences.

If you like these kinds of snippets, check out Emacs-SOS, a StackOverflow search for emacs.

Categories: Software Development

Composable Regular Expressions and Fields

Martin Fowler wrote a brief article about composing regular expressions in order to make it easier to deal with individual “tokens” and to give them structure.

As noted by a reddit commenter, string constants can be a bit unwieldly to deal with and will break. Scheme Shell solves this problem with an s-expression-based structure syntax of regular expressions called SRE. You can click that link to read more about it, but what I’ll do here is convert Fowler’s example to some SREs to show how much more useful structured regular expressions are when you use them in a Lisp-like language.

His structured pattern in the Java language looks like this:

const string scoreKeyword = @"^scores+";
const string numberOfPoints = @"(d+)";
const string forKeyword = @"s+fors+";
const string numberOfNights = @"(d+)";
const string nightsAtKeyword = @"s+nights?s+ats+";
const string hotelName = @"(.*)";
const string pattern =  scoreKeyword + numberOfPoints +
  forKeyword + numberOfNights + nightsAtKeyword + hotelName;

He also has an alternative method that joins strings with whitespace:

private String composePattern(params String[] arg) {
  return "^" + String.Join(@"s+", arg);
}
const string numberOfPoints = @"(d+)";
const string numberOfNights = @"(d+)";
const string hotelName = @"(.*)";
const string pattern =  composePattern("score", numberOfPoints, 
  "for", numberOfNights, "nights?", "at", hotelName);

Here is what that looks like in Scheme Shell:

(define number-of-points (rx (submatch (+ digit))))
(define number-of-nights (rx (submatch (+ digit))))
(define hotel-name (rx (submatch (* any))))
(define s+ (rx (+ whitespace)))
(define pattern (rx "score" ,s+ ,@number-of-points ,s+ "for" ,s+ ,@number-of-nights ,s+ "nights?" ,s+ "at" ,s+ ,@hotel-name))

Now that I’ve written that out, it looks cumbersome. It wasn’t a huge pain to type out, but all of that whitespace matching looks redundant and I can see why Fowler wrote up the composePattern function.

Let’s try treating this problem differently by assuming that the important data is separated by whitespace, and let’s call the stuff that’s in between the whitespace a “field”. By doing this, we no longer have to use regular expressions. We can now use a tool like AWK or something else to split each string into some fields and then look at each field and turn it into the appropriate data type.

Here is my Python solution:

# fields:    0    1   2  3   4     5   6     7       8
example = "score 400 for 2 nights at Minas Tirith Airport"
fields = example.split() # Python assumes whitespace as the delimiter
numberOfPoints = int(fields[1]) # 400
numberOfNights = int(fields[3]) # 2
hotelName = ' '.join(fields[6:])   # 'Minas Tirith Airport'

There might be a slightly better solution that treats the hotel name differently, but it doesn’t really matter. The point is that this is no longer a problem that requires the use and composition of regular expressions.

Categories: Utilities

Emacs Tip: Word counting with a regular expression

Here’s a word counting function for Emacs that counts the number of words in a buffer by using a regular expression:

The regular expression matches against one or more word characters. It doesn’t move the current point of the buffer so you don’t have to keep re-positioning the cursor every time you run this function.

The reason I’m posting this is because I was searching for “emacs word counting” and the forums and StackOverflow suggested using the “wc” shell command while in Linux, but at the moment was developing in Windows! How can I use it? (Update in 2018: Windows now has an Ubuntu Linux subsystem that allows the use of the wc word counting command). Another thing I found is that people write up functions that loop through words and count them. I’m guilty of doing that, but using the above function is much much easier.

Emacs has most everything that you can think of. The problem is that it’s all hidden away somewhere and you have to take the time to dig around.

Update: The how-many function is defined in replace.el. To find out more about it, press C-h f how-many RET.

Categories: Software Development

TaskScheduler Domain Specific Language converted to Lisp

Oren Eini has written a DSL that uses the Boo language and does a bit of sub-classing. This was in response to a reply to one of his posts by a blogger named Tim Wilde. The purpose of a domain-specific language is to make it easier to talk about the domain. The DSL written is written for the domain of scheduling tasks. Instead of typing out the following:

myTask = new Task("warn if website not alive", (3 * 60), ... )
myTask.start()

You would type out:

task "warn if website not alive" every 3 minutes starting now ...

It’s more readable for other users and it hides details that are unnecessary when talking about the domain.

Tim came up with a C# variant which had roughly 250 lines of code, 8 classes and 6 interfaces. Over-kill in my opinion. Oren came up with something that makes the DSL look cleaner and it uses the Boo language.

Oren says,

There are several interesting things that are going on in the DSL. For a start, we are using Boo’s ability to drop parenthesizes if needed, which gives us a more “keyword” feeling. Another interesting approach is that when the last parameter of a method is a delegate, we can use just create a block and use that as a way to create the delegate.

The keyword feeling is fantastic. But it can be taken much further. Much much further. Lisp comes with very powerful macros that can be used to write DSLs and DSLs have been written in Lisp since before I was born in 1987. Being able to create a DSL by extending a language is nothing new. This is why I’ve implemented my own version of the above DSL in Lisp

My Implementation

Warning: I’m using the Scheme dialect and the MzScheme implementation. I’m using the MzScheme thread library instead of the official multi-threading extension to Scheme.

My implementation uses Scheme and the syntax-rules pattern language. I didn’t much like it but now I am getting used it and may use it more often than the Common Lisp defmacro method. I have no idea exactly what the differences are since I am Lisp beginner, but I was able to use Scheme to write an implementation of this DSL in less than 30 minutes.

The code consists of 3 functions: 2 are placeholders, the other one converts whatever time is given into the equivalent amount in seconds. So, 1 minute turns into 60 seconds and 3 minutes turns into 180 seconds. This is used with the sleep function in each task’s thread which controls how long to wait until the task is executed again.

The 2 Patterns Detected

The code also includes 1 syntax definition that has 2 similar patterns that it can match. The 1st pattern is triggered when the task is not starting immediately. An example:

(task "warn if website is not alive"
      every 3 seconds
      starting in 5 seconds
      when (not (website-alive? "http://example.org"))
      then (notify "admin@example.org" "server down!"))

The 2nd pattern is triggered when the task is starting immediately. This is triggered when the keyword “starting” is followed by the word “now”. The above example re-written to start immediately.

2 Interesting Things

Two interesting things that you must know about this code: literals and named let. Literals are using in the syntax-rules pattern language and are the 1st argument to the syntax-rules call. Literals allow us to include keywords in the pattern, such as now and when.

The other interesting thing is the used of the named let. This is when the 1st argument of a let call, a literal, is the name of the “function” created. The 2nd argument is the list of parameters which can include default values that the “function” will be called with. A named let is typically used to add a recursive loop in the middle of another function.

Conclusion

The download is available here. The code for this domain specific language is very short: 59 lines including comments. It uses no classes and no interfaces. Some ideas for improvements include hooking into a task/thread list so that tasks/threads can be killed or retrieved later on.

Lisp can be used as well and can be more concise than other languages. Domain specific languages are not a new idea and I am happy that software developers are finding great uses for them.

Categories: Software Development

Automating Simple Tasks with Scheme (Competing with Perl, Python and Ruby)

Racket/PLT Scheme Logo

reddit user by the name of alanshutko stated what was necessary to make Scheme, Common Lisp, Haskell, and other non-mainstream languages more appealing to the average programmer.

Compare that with the types of simple programs we see in Perl and Python. “I have a bunch of files, and I want to rename them all according to some pattern.” Common problem, easy solution. “I’ve got a log file full of email addresses, I need to strip them out from the log entries, remove duplicates, and add them to a database.” Again, fairly simple, fairly small, really useful. When Haskell can compete on those types of problems, it’ll be easier to induce people to learn it. (Same with CL, my fav language….)

So here is a Scheme program that does this. It is written to use MzScheme because that’s the only Scheme I have installed in Windows at the moment. Thus, it takes advantage of PLaneT and the other libraries that come with MzScheme.

Hopefully this can convince others that Scheme is a good language for common tasks.
Continue reading “Automating Simple Tasks with Scheme (Competing with Perl, Python and Ruby)”