Spread Firefox Affiliate Button

Pages

Validating credit card numbers in python

For various reasons I’ve needed to validate some credit card numbers in Python. For future reference, here’s what I’ve come up with:

import re
def validate_cc(s):
    """
    Returns True if the credit card number ``s`` is valid,
    False otherwise.
 
    Returning True doesn't imply that a card with this number has ever been,
    or ever will be issued.
 
    Currently supports Visa, Mastercard, American Express, Discovery
    and Diners Cards.  
 
    >>> validate_cc("4111-1111-1111-1111")
    True
    >>> validate_cc("4111 1111 1111 1112")
    False
    >>> validate_cc("5105105105105100")
    True
    >>> validate_cc(5105105105105100)
    True
    """
    # Strip out any non-digits
    # Jeff Lait for Prime Minister!
    s = re.sub("[^0-9]", "", str(s))
    regexps = [
            "^4\d{15}$",
            "^5[1-5]\d{14}$",
            "^3[4,7]\d{13}$",
            "^3[0,6,8]\d{12}$",
            "^6011\d{12}$",
            ]
 
    if not any(re.match(r, s) for r in regexps):
        return False
 
    chksum = 0
    x = len(s) % 2
 
    for i, c in enumerate(s):
        j = int(c)
        if i % 2 == x:
            k = j*2
            if k >= 10:
                k -= 9
            chksum += k
        else:
            chksum += j
 
    return chksum % 10 == 0

Father’s Day Run 2008

Me at Father's Day Run last year

In what is becoming something of an annual tradition, I’m going to be participating in the 2008 Father’s Day Run in support of prostate cancer research.

Both my father and father-in-law have had prostate cancer, so this is a cause that’s very close to my heart.

Please consider supporting me in my run by clicking the image above, or this link.

BTW, that’s me and Thomas in the picture at last year’s Father’s Day Run – the first time I ran as a new father!

a confession

I don’t get lolcats. or lotcatz.

whatever.

People keep sending me links to them. People keeping making them. They show up in my RSS feeds with frightening frequency.

I have to admit, however, that these were pretty funny: http://blog.rominet.net/2008/05/debianopenssl-debacle.html. Mmmm…antropi….

Also, did you know that apparently there can be improper lolcat grammar!?

Re: Tolkien didn’t like Narnia?

Holly,

I’ve heard this before as well. I came across a much much much longer article earlier this year: Letters to Malcolm and the trouble with Narnia (by way Mark Shea’s blog)

This hasn’t really affected my enjoyment of Lewis’ works. Mel and I also love Narnia and we can’t wait to see Prince Caspian in the theatres (subject to babysitting too!)

It just means that I wouldn’t use Narnia as a way to teach concepts about Christianity to somebody. You could still point out ideas in Narnia that draw from Christianity. But I wouldn’t use the Narnia stories to draw conclusions about Christianity – I think this could be a natural inclination since in many ways the Narnia stories are very similar to ideas and events from Christianity.

So, we enjoy Narnia for what it is, a fantastic set of stories set in a wonderful world with characters that we grow to love.

nmudiff is awesome

Man, I wish I had known about this before!

nmudiff is a program to email an NMU diff to the Debian Bug Tracking System.

I often make quick little changes to debian packages to fix bugs or typos, and it’s always been a bit of a pain to generate a patch to send to the maintainer.

nmudiff uses debdiff (another very useful command I just learned about) to generate the patch, and email it to the bug tracking system with the appropriate tags.

Kangaroo Courts in Canada

The /. crowd isn’t known for its love of the political right, so I was surprised to see the reactions to this story which came up in my RSS reader over the weekend:

Author Faces Canadian Tribunal For Hate Speech.

The “Human Rights Commissions” here in Canada don’t have a great track record in terms of treating people as innocent until proven guilty, or requiring actual evidence, or anything else that you normally expect from a system that has the power to pass legal judgement on people.

The gist of the story is that a well known Canadian Author, Mark Steyn, will be put “on trial” before one of these “Human Rights Commissions” for his book, America Alone. I haven’t read the book, so I won’t speak to its claims or accuracy. It could certainly be considered controversial, and I’m sure many people wouldn’t like what he was trying to say.

What this issue is all about is a person’s right to freedom of speech and our distorted notion of tolerance. Are people offended by Mark Steyn’s book? Most definitely. Do people have a right to not be offended by the free speech of others? I don’t think so. We (here in Canada anyway) have gone a bit overboard in our promotion of “tolerance.” We’ve abandoned a healthy attitude of tolerating behaviour that while not illegal or immoral is somehow distasteful to us (maybe just because it’s different?) and embraced tolerance as a sort of prime directive, or 0th commandment, “Thou shalt not offend thy neighbour.” The trouble is, my neighbour being offended has more to do with my neighbour’s values and state of mind than it does with my actions, so in the name of “tolerance”, the state has begun to legislate what is and is not offensive.

Evolution or Darwinism or Dawkinsism or Creationism or whatever its called these days…

There’s a great new piece up on What You Ought to Know called Darwin’s Intelligent Design.

I think he does a pretty good job to distinguishing between natural selection / evolution / intelligent design / creationism. He also takes a few shots at Dawkins, which is always a good thing :)

It’s part of their Open Your Mind week. Very good stuff, you should check it out!

I’ve been subscribed to their RSS feed for a few weeks now, just to find out all the stuff that I ought to know. So far I’ve really enjoyed the videos. Keep it up guys! (Sorry, I don’t know the guy’s name)