McCarthy’s Lisp

So, I got no sleep last night due to a client project. Profitable, but exhausting. Not wanting to have a Monday go by without a post, however, I thought I would comment on McCarthy’s original definition of Lisp, which is sited here.

There are few times I have felt the privilege of reading code which is awe-inspiringly brilliant. This is one of those times. It is remarkably simple. Take the “and” function, for example:

(defun and. (x y)
  (cond (x (cond (y 't) ('t '())))
        ('t '())))

Translated:

  1. Define and
  2. cond is basically a switch, it looks for the first “car” which is true. So (cond (x ...) means if x then …
  3. Repeat cond with y
  4. 't is a catch-all so that the cond will definitely hit something. It is the “default” in the C-style switch. In both case ('t '()) means, “if nothing before this is true, return false.

You never get to see a definition of the logic behind “and”, “and” just works, however in this case, it is clearly a beautiful and clear construct built out of the language itself. Truly, this is a thing of beauty.

Posted in Uncategorized | 5 Comments

PyFram week 8: Refactor!

PyFram was in need of a bit of a refactor. I mean, all of the view classes were in the same file, and if something went wrong with that, then you couldn’t even get a proper error message. Well, this is simply unacceptable.

So, I refactored.
Continue reading

Posted in PyFram, PyFram tutorial | Tagged , , | Leave a comment

Treat your people well — or else

Treating your people well is cheaper than treating them badly. Let these words be on your heart; repeat them to your children, meditate upon them in your house and when away…
Continue reading

Posted in Business | Tagged , | 2 Comments

Benchmarx continued: what is the fastest way to append to a list in Python

Going with the last post, I thought I would continue with some Python list and set benchmarks. I started with lists: Continue reading

Posted in PyFram, Python | Tagged , , , , , , , | Leave a comment

Efficient Python concatenation: str

I realized recently that I actually have no idea whether my string concatenation is efficient or not. I mean, this site, suggested that it was, but I had the thought, “But that’s Python 2… what about Python 3″. So, I though I would completely re-create his tests and see what happened. (Download the actual source here)

The long and the short:

  1. method1: raw concatenation s = ”; s += ‘foo’.
  2. method2: uses a character array (array.array).
  3. method3: create a loop followed by a join.
  4. method4: uses io.StringIO (creates a “file” buffer and outputs it).
  5. method5: uses a list comprehension

Continue reading

Posted in Best Practices, PyFram, Python | Tagged , , , , , , | 1 Comment

When you decide that your employee’s job has reached the end of life

So, you’ve decided it is time for layoffs. You’ve figured out who you’re going to get rid of and now all you have to do is let them know. But have you figured out how?

“How will I fire someone” is something which everyone in the entire history of management has had to answer at some point. Somewhere along the line it has to happen, but there are implications here which you HAVE to take into account. Because if you screw this point up, your employees will HATE you.
Continue reading

Posted in Business | Tagged , , , , | Leave a comment

PyFram Week7: Oops, we need a <head> element!

So, you might have noticed that there is something… wrong with the renderer class at the moment. Sure, it can handle appending the body of a request, but it really has weakness when it comes to the head. To demonstrate:

<html><head>
<!-- we need to add stuff here! -->
</head><body>
<!-- we can already add stuff here -->
</body></html>

This is a problem especially if we want to add <script> tags to the head of the HTML document.

Now, at first, it might be tempting to simply add another renderer and have a “head” and a “body” property on the HTMLRenderer. Well, there is a problem with that: you then have dangling methods which really are undefined. “getHeader” and “setHeader” start becoming useless on these new renderer instances — after all, there really isn’t a “header” to a <body>.

So, basically, we need some form of document object which has a head and a body. It still needs to have a “render” method, but it needs a “head” and “body” property. In order to do this, I pulled the headers out of the renderer and created an intermediary class between the BasicRenderer and the HTMLRenderer. This is, once again, because it does not make sense to have an HTML document have a header — it has a head.

In addition to that, I created an HTMLDocumentRenderer. As expected, this is a renderer which has both a head and a body, both of which can be easily appended individually. To help ensure that people are appending as expected, and to sort of encorage best practices, I’ve placed a warning in the “append” method of the HTMLDocumentRenderer which will encourage people to either append the body or the head directly. Finally, I’ve added a “doctype” property (which will need to be talked about later).

All told, it can be found here.

Posted in PyFram, PyFram tutorial | Leave a comment

Cleaner HTML (strip_tags in Python)

So, in PHP there is a function strip_tags. It’s pretty neat. It lets you remove all HTML tags from a given string except for the specified ones. Python doesn’t have this and, based on what we saw on Monday, it might be a good idea to have something like it.

Now, most attempts to implement this in Python are some regular expression. But, I don’t trust regular expressions with HTML[1], and I want something a little more… verbose.

I would like, eventually, to allow users to do things like leave their own <code> tags, maybe an <a>, or even an <em>. I can even see a use for allowing <div> in some circumstance. Of course, if I let them just go-willy-nilly, then someone can simply leave an <em> unclosed and leave my site with a bit of a slanted perspective.

So, I created the HTMLCleaner class. For now, it will reside in the view.py file (subject to change). It’s job? Exactly what I just talked about. A demonstration (from the unittest code): Continue reading

  1. [1] This is the best explanation I know of for why it is a bad idea.
Posted in PyFram, PyFram tutorial, Python, Uncategorized | Tagged , , , , , , , | Leave a comment

Stackoverflow moderators [migrated]

Earlier today, I had a post about moderators on Stackoverflow. Because of the length and breadth of the topic, it has been moved to the articles section. It can be found here: http://www.allen-poole.com/wordpress/articles/stackoverflow-moderators/

Posted in Business, StackOverflow | Tagged | Leave a comment

John McCarthy is dead. Long live Lisp.

Jobs died and it was profound for we lost an innovator.
Ritchie died and I was sad for we lost a mentor.
McCarthy died and I lost a hero.

Continue reading

Posted in Personal | Leave a comment