Archive for June, 2007

Simple easygui File Dialogs

Tuesday, June 19th, 2007

Did you know that you can have a file-open or -save dialog in just 2 lines of Python? Stephen Ferg’s excellent easygui module makes it possible. No other modules are needed, easygui is cross-platform. See also the enterbox example.

Two lines of code:

import easygui
fileNameAndPath = easygui.fileopenbox(title = "Choose your file", argInitialFile = "*.txt")

Resulting dialog with a custom title and a ‘*.txt’ file filter:

Full code:

# fileopenbox example, 20070619 Ian Ozsvald
# documentation from: http://www.ferg.org/easygui/easygui.txt
# For file-opening:
# fileopenbox(title=None, argInitialFile=None):
# For file-saving:
# filesavebox(title=None, argInitialFile=None)
import easygui
fileNameAndPath = easygui.fileopenbox(title = "Choose your file", argInitialFile = "*.txt")
# fileNameAndPath will look like
# 'C:/easygui/someDocument.txt'
# if the user selects a file
# or None if the user presses Cancel
print fileNameAndPath

To see easygui’s fileopenbox, filesavebox, choicebox, enterbox and ynbox in action and learn how to write a Python program from scratch, visit: Python 101 - easygui and csv.

Request for Reviewers

Monday, June 18th, 2007

We are looking for reviewers for our first two commercial ShowMeDo series. Both series are aimed at new Python coders and show everything needed to learn new Python skills.

One series shows how to write a complete application from scratch and the other shows three ways to develop code (PyDev, IDLE, text-editor) on Windows. Both cover unit-tests, refactoring and give lots of background material.

Kyran and I would like to offer a free license for each series in exchange for a review. Send me a mail if you’d like to know more (ian AT showmedo DOT com).

The two series are Python 101 - easygui and csv and Python Development on XP, part of our large (and mostly free) Python category.

Suggestions for topics? We’re keen to receive suggestions for other topics that you’d like to see covered.

Optimising Genshi Imports

Monday, June 18th, 2007

We’ve just moved from Kid templates to Genshi (v0.4). Kyran’s first approach was to put all the helper div-functions into one of two separate (big) Genshi files to simplify the translation process.

We had several of these files, each about 10k in size, each imported into every main Genshi template.

This approach made it easy for Kyran to move us to Genshi but made for slower-than-desirable render times. Whilst speed-testing the pages I noted that the several 10k Genshi imports per page was the real burden.

The solution was to extract the necessary functions (div-blocks) from the helper files, put each of them into their own short Genshi file, and import only the necessary helper functions.

Now our main Genshi files need only import 3-10 helper functions (each file is about 10-30 lines long) rather than importing the several 10k files.

The speed-testing shows that each page’s imports took around 2 seconds per page before this process and 0.34 seconds after. The imports often dominated our render-times so this 6* speed-up is a welcome improvement.

For those who are new to optimisation we’re following the classical approach:

  1. Make measurements to identify the real bottleneck (very important!)
  2. Make changes one at a time to test their effect
  3. Keep the beneficial changes

Speed-testing Genshi pages for optimisation

Monday, June 18th, 2007

To optimise some of our slower Genshi pages (e.g. this one) I needed a reliable way of extracting render-times from different sections of the templates. Python’s datetime module came to the rescue.

Below is a snippet of a Genshi template showing how I embedded calls to datetime and how I rendered the time-differences in the resulting page.

This is a quick and hacky solution, useful for noting performance numbers (I used 4 refreshes of each page to get average values). Once I was finished with my optimisations I just deleted the datetime code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude">

<!-- Bring datetime into our Python namespace -->
<?python import datetime ?>
<!-- Put the current time into t1 -->

<?python t1=datetime.datetime.now() ?>
...<!-- imports I want to speed-test -->
<?python t2=datetime.datetime.now() ?>

...<!-- main body of the template -->
</body>

<?python t3=datetime.datetime.now() ?>
<!-- Show in the rendered page the time spent in the imports section -->
<p>${"imports ",t2-t1}</p>
<!-- Same for body section -->

<p>${"body ",t3-t2}</p>

</html>

ShowMeDo RSS Feeds

Sunday, June 17th, 2007

We have just implemented the first round of RSS feeds for ShowMeDo (using the very simple TurboGears FeedController). This feature is about a year overdue…if only I’d known it was so straight-forward. Look for the big RSS icons in the site.

The latest-video feed is tag-based so to track the latest Python videos subscribe to:
http://showmedo.com/latestVideoFeed/rss2.0?tag=python

To track all the latest videos (not tag-specific) just use:
http://showmedo.com/latestVideoFeed/rss2.0

To follow all the new comments across the site (no Python-specific feed just yet) use:
http://showmedo.com/latestCommentFeed/rss2.0
and then you can follow and join the conversations.

Want to share your knowledge? We’d love to have more Python videos, full instructions are here.

SQLObject 0.8.4 with TurboGears 1.0.1

Saturday, June 16th, 2007

We wanted to move to SQLObject 0.8.4 from 0.7.7 (bugfix branch) as it handles more MySQL errors.

I couldn’t find documentation saying that the transition would be ok (TG is setup to use the 0.7 branch). Easy_install was great and the switch works just fine. Maybe this post will save someone else a bit of head-scratching.

Learn GUI and File-Handling techniques

Wednesday, June 13th, 2007

This is our second in-depth pay-to-own video series for Python Programming, aimed at new-to-intermediate Python programmers (the first was our Python Newbies series).

Python 101 - easygui and csv is a fully worked series lasting 1 hour 20 minutes which shows you how to write a simple user-interface, process files and use Test-Driven Development. This series has been planned and created professionally.

We start with blank source files and build a fully-working useful program with a unit-test suite using a module we download from the web (easygui).

Audience - Beginner and intermediate Python coders who want to learn new modules, skills and techniques. To see what is covered take a look at the episode list.

What you get - Ownership of all 17 episodes (total: 1 hour 20 minutes) forever, the option to ask me questions (this is encouraged!), all source-code and background material (including wikipedia references), the option to download the episodes for off-line viewing.

Other benefits - We reserve 10% of the price as a donation to the Python Software Foundation. You have a full money-back guarantee (for 30 days), for whatever reason.

Exercises - Several are included ranging from ‘expand this pseudo-code’ to ‘write your own unit-tests that test this new function’. Each exercise is worked-through in the following episode so everything is explained for you.

Platforms - Windows, Linux and Mac.

Topics:

  • easygui module - how to download and use a new Python module, how to write a very simple GUI
  • File i/o - using files and the built-in csv module
  • Unit-testing - how to do it and demonstrations of why it is necessary
  • Test-driven development - a powerful development practice to make you a stronger coder
  • Exception handing - giving the user a useful error message

See all of our 112 Python tutorial videos here.

New ShowMeDo: Practical Episode - User Interface Basics

Monday, June 11th, 2007

“In this episode, you’ll learn the basics of working with Ubuntu’s (GNOME) user interface.” - video here, Lucas Holland

New ShowMeDo: Addons

Monday, June 11th, 2007

“This episode includes an introduction to addons in mozilla programs. I also show you 2 Extensions, that I’m using: Adblock Plus and Minimize to tray.” - video here, Lucas Holland,Marius Meinert