Archive for January, 2014

One of my friends on the wxPython Google Group asked how to catch any exception that happens in wxPython. The problem is complicated somewhat because wxPython is a wrapper on top of a C++ library (wxWidgets). You can read about the issue on the wxPython wiki. Several wxPython users mentioned using Python’s sys.excepthook to catch the errors. So I decided to write up an example showing how that worked based on something that Andrea Gavana posted on the aforementioned thread. We will also look at the solution that is in that wiki link. (more…)

Sometimes when you are coding, you want to know how long it takes for a particular function to run. This topic is known as profiling or performance tuning. Python has a couple of profilers built into its Standard Library, but for small pieces of code, it’s easier to just use Python’s timeit module. Thus, timeit will be the focus of this tutorial. The timeit module uses a platform-specific method to get the most accurate run time. Basically, the timeit module will do a setup once, run the code n number of times and returns the time it took to run. Usually it will output a “best of 3″ score. Oddly enough, the default number of times it runs the code is for 1,000,000 loops. The timeit does timing with time.time() on Linux / Mac and time.clock() on Windows to get the most accurate readings, which is something most people don’t think about.

You can run the timeit module from the command line or by importing it. We will look at both use cases. (more…)

I’ve been thinking about creating a simple downloading script with wxPython for quite a while. Then I saw someone on StackOverflow asking about how to do it and I decided it was time to figure it out. Downloading a file with Python is trivial. I wrote about a couple of different ways to do that in a previous article. The big question was how to do it in such a way that I could update the UI as the file was downloading. It’s actually pretty easy and this article will show you how! (more…)

A few years ago, a fellow named Zed Shaw created a website called Learn Python the Hard Way that a lot of people praised. The website is made up of lots of short exercises that help beginner programmers learn the various nuances of Python but in bit-sized chunks. He kept updating it from time to time and eventually Addison-Wesley turned it into a book by the same name. I was recently given a copy of the PDF version of the book to review. Here’s the quick version:

Quick Review

  • Why I picked it up: I received this book specifically to review it, although I was interested in reading it anyway just from what I’d heard about the site
  • Why I finished it: The book’s chapters are short…and technically, I skimmed a lot of it
  • I’d give it to: Someone who wants to learn Python and who has no previous experience with any other language

(more…)

The topic of generators has been covered numerous times before. However, it’s still a topic that a lot of new programmers have trouble with and I would hazard a guess that even experienced users don’t really use them either.

Python generators allow developers to lazily evaluate data. This is very helpful when you are dealing with so-called “big data”. Their main use is for generating values and for doing so in an efficient manner. In this article, we will go over how to use a generator and take a look at generator expressions. Hopefully by the end you will comfortable using generators in your own projects. (more…)

Editor’s note: This is a guest post from Yasoob Khalid who authors the Free Python Tips blog

So hi there guys! I hope you are fine. So what is in this post? Today we will be writing a cleanup script. The idea for this post came from Mike Driscoll who recently wrote a very useful post about writing a cleanup script in python. So how is my post different from his post? (more…)

The wxPython toolkit provides an alternative to using Sizers for layout that is known as “sized_controls”. These controls or widgets are basically top-level widgets (like frame, panel, dialog, etc) that have sizing logic built into them. This article will cover all four types of sized_controls. They are as follows:

  • SizedPanel
  • SizedScrolledPanel
  • SizedFrame
  • SizedDialog

The SizedScrolledPanel widget is new as of wxPython 2.9.3.1, but the other 3 types of controls are available in wxPython 2.8.8 and possibly older versions (see Trac for more info). Just keep that in mind if you are on wxPython 2.8. If you are ready, we can get started! (more…)

wxPython 2.9 introduced the world to a new type of sizer that can take widgets and automatically make them “wrap” around as you resize the frame. That sizer is known as wx.WrapSizer. For some reason, it is relatively unknown, so we’ll spend a few minutes going over how to use it in this article.

To follow along with this tutorial, you will need to have wxPython 2.9 or greater. Once you’ve got that, we can continue. (more…)

The Python programming language added the super() type back in version 2.2. For some reason, it’s still a topic that a lot of beginners don’t understand. One of my readers recently asked me about it and since I don’t really use it, I decided to do some research in the hopes of understanding its usage myself so I could explain what super is and why you would use it. We’ll spend some time looking at various people’s definitions of super and then look at some examples to try to figure this out. (more…)

Python has a neat little concept called a property that can do several useful things. In this article, we will be looking into how to do the following:

  • Convert class methods into read-only attributes
  • Reimplement setters and getters into an attribute

In this article, you will learn how to use the builtin class property in several different ways. Hopefully by the end of the article, you will see how useful it is. (more…)