Category Archives: Python

Linear Algebra Review and numpy

I’ve signed up for the Machine Learning course from Stanford. One of the first week’s subject areas is a Linear Algebra Review, and the recommended software is GNU Octave. However, I’d prefer to use numpy and Python. Here’s my notes … Continue reading

Posted in Python | 5 Comments

Tuple Trivia

A couple of minor points regarding tuples. When you take a slice of a tuple, there are 3 parameters: – the start index – the end index – the step which produces: It doesn’t matter that, in the second instance, … Continue reading

Posted in Python, Software | Leave a comment

globals and __main__: a Python gotcha

One of the good features is the lack of gotchas. I define gotchas as traps for the unwary programmer, where something unexpected happens. Thankfully these are rare in Python. But the following is one that is worth knowing about. Consider … Continue reading

Posted in Python, Software | Leave a comment

Edge case for split / explode

During recent coding, using PHP, I found that the explode function behaves in a non-ideal way (for me) when trying to split an empty string: produces: I was expecting (needing?) an empty array. After thinking about it a bit more, … Continue reading

Posted in Python, Software | Leave a comment

Reading gzip files in Python – fast!

I have been doing lots of parsing of Apache log files recently with Python. Speed is of the essence, so I was interested to know if the speed issues mentioned in http://bugs.python.org/issue7471 are still relevant. So I set out to … Continue reading

Posted in Python, Software | 7 Comments

Python Standard Library

There are some gems in the Python standard library which, once found, get regular use – at least by me. Here are some of them. 1. enumerate A built-in function, which allows you to replace code like this: with the … Continue reading

Posted in Python, Software | Leave a comment

Python regular expression surprise

I came across this regular expression anomaly recently in Python. The regular expression was more complex, but this example illustrates the idea. Try and predict the outcome (should work with Python 2.5 – Python 3.1) This produces: a, bc, d … Continue reading

Posted in Python, Software | 1 Comment

PHP and Python

My new job involves a lot more PHP, which seems closely related to Perl. There are a number of observations that I would make in the comparison between PHP and Python. 1. PHP is more verbose. In particular, PHP requires … Continue reading

Posted in Python, Uncategorized | Leave a comment

Strange behaviour using ctypes in IronPython

I came across some slightly unexpected behaviour with ctypes in IronPython. Maybe this is the wrong way to look at it – I should be amazed that ctypes works at all in IronPython. But I thought I would document it … Continue reading

Posted in Python, Uncategorized | Leave a comment

ctypes and addressof

Here’s a gotcha I came across in ctypes a while ago. Have a look at the code below, and see if you can work out what it will print: If you’re using Python 2.x or Python 3.x, then you might … Continue reading

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