Don’t overuse classes in Python

Unlike some mainstream languages like Java, you don't have to package everything into a class in Python. A class is a good tool when you want to package up state and behavior, but when all you've got is a bundle of related functionality, the module is the natural unit of packaging in Python. In my [...]

wchartype: Python module to get full-width (double-byte) character types

When dealing with full-width (especially CJK) characters, you'll often want to know the type of a particular character — Kanji/Hanzi/Hanja, hiragana, katakana, and so on. wchartype is a Python module that will determine the type of a full-width character. The functions all expect Unicode strings of length one. Usage: import wchartype if wchartype.is_asian(u'\u65e5′):     [...]

Retrieving common Windows folder names with Python

Often using Windows, you'll need to get the names of special folders, like "App Data," "Local App Data," "My Documents," and "My Pictures." This is especially true in Vista, where the recommended practice of writing app-specific data to the (Local) App Data directory is being enforced. You can do this using the win32com.shell.shell module in [...]

Getting selected items from a ListCtrl in wxPython

I seem to be on a roll with getting selected items from various wxPython controls. This time, I needed to get the selected items from a list control. wx.ListCtrl doesn't have a simple method for getting the selection, but building on this post by Robin Dunn, I was able to cobble my own together using [...]

Getting the selected cells from a wxPython grid

Getting the selected cells from a wxPython grid is a little tricky, because you have to use a different method depending on which of the following scenarios applies: A single cell has been selected Multiple cells have been selected via Control+Clicking A range of cells has been selected via dragging The following two functions will [...]

Using custom functions with SQLAlchemy and SQLite

I recently developed a Web-based translation memory (TM) application in Python. One thing the application does is fuzzy glossary matching: given a source sentence, it'll find all terms in the glossary that are fuzzy substrings of that sentence (using my fuzzy substring matching module, which is based on the Levenshtein distance algorithm), and return the [...]

Book Review: CherryPy Essentials

I recently created a server application to share Felix memories/glossaries over a local network. After a simple test application, I was confident that CherryPy would serve my needs, so I bought CherryPy Essentials (Sylvain Hellgegouarch) and started hacking. The story of a website The book uses a practical-minded, show-me-the-code style that I personally like. The [...]

Python is for people who want to program

Saw a great quote the other day on comp.lang.python, in response to a troll questioning Python's usefulness in the "real" world: Python is for people who want to program, not REAL WORLD programmers. By Mensanator in comp.lang.python (Google groups link) (Python encourages a sense of fun, and people on the comp.lang.python group tend to like [...]

The mysterious “ImportError: cannot import name cache”

The scenario: I'm packaging a CherryPy server with py2exe, using Mako as my template engine. So I create my exe file, and fire up the app, and get this 500 Mako error: Traceback (most recent call last):   File "cherrypy\_cprequest.pyo", line 551, in respond   File "cherrypy\_cpdispatch.pyo", line 24, in __call__   File "main.py", line 210, in index [...]

Counting words (etc.) in an HTML file with Python

In a previous post, I wrote about how to count words, characters, and Asian characters using python. In this post I want to pull that together with code to get a word count from an HTML file. What needs counting What needs counting depends to some extent on what you need the word count for, [...]