Archive for the 'python' Category

Splitting queries into search terms with Python

I'm currently writing a site to host the archives for the Honyaku mailing list. I needed to split a search query into individual terms, so for example the query "懸念 risk" would retrieve all posts with the words "懸念" and "risk", not necessarily in that sequence or order. I also wanted to support quoted [...]

Python introspection with the inspect module

Here are some fun and potentially useful tricks you can do using the inspect module.
Get a list of currently imported modules:

import inspect
myglobals = dict()
myglobals.update(globals())
modules = [value
           for key, value in myglobals.items()
           if inspect.ismodule(value)]
Get a list of classes in a module (by Marc 'BlackJack' Rintsch):

import module
from inspect import [...]

Clumsy regex syntax in Python is a feature

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
–Jamie Zawinski
A common complaint about Python is that its regular-expression syntax is clumsy compared to some other languages. But actually, library support rather than core-language support is a feature, not a bug: it keeps the core [...]

My solution to the localization horror story

The localization horror story in this CPAN article about the Locale::Maketext module tells of the combinatory explosion of translation "rules" required when localizing text with variables (placements) into multiple languages.
Since I translate (and localize) Japanese to English, this is a problem that really strikes home with me. Here's a simple example of this problem in [...]

Six GoF design patterns, Python style

Although it usually requires some adaptation, Python makes implementing most of the GoF design patterns trivial. In some cases, they're actually part of the core language; in most of the others, they're a lot simpler in Python than C++ et al. Here I have implemented examples of the following common design patterns in Python:

Iterator
Decorator
Abstract factory
Factory
State
Template

Please [...]

Counting words, characters, and Asian characters with Python

As a translator, I often need to get word counts. As a Japanese-to-English translator, I need to get Japanese character counts as well.
These are the kinds of counts that MS Word gives:

Characters (with spaces)
Characters (without spaces)
Words
Asian characters
Non-Asian words

Let's calculate each of them. Say that the name text points to the text we want.
Characters (with spaces)
This [...]

How learning Python made me a better C++ programmer

This post on the Raganwald blog spurred me to write about something I've been thinking about for a while: how learning Python has made me a better C++ programmer.
The short answer is, it convinced me to start using the boost libraries (and the Loki library). I'd seen these libraries before, and thought that they looked [...]

Classes considered overused

We all love classes. When I write a class, I get a warm and fuzzy feeling, because I know I'm doing OOP.
A class represents a bundle of data and behavior. A classic example is a BankAccount class, which maintains a "balance" state and various methods for manipulating and querying the balance.
This is a great concept. [...]

Setting up agile development with Python/Komodo (part 4)

This is the fourth and final part in a series of tutorials on setting up a Python project in Komodo Edit for agile development.
In part 1, we set up automated testing, and started our automated build script. In part 2, we added building an executable and setup program to our build script. In part 3, [...]

Setting up agile development with Python/Komodo (part 3)

This is the third part in a series of tutorials on setting up a Python project for agile development in Komodo Edit.
In part one, we set up unit testing and started our automated build script. In part two, we created scripts to build an executable and installer. In this part, we're going to use pywinauto [...]

« Previous PageNext Page »