Archive for the ‘python’ Category

reversing a string in python

Monday, October 15th, 2007

This beautiful construction was not obvious for me.


>>>  'foo'[::-1]
'oof'

inplace, a new xslt-based CMS

Thursday, October 4th, 2007

I’ve released a content management system for static pages:

InPlace CMS
http://inplace.sourceforge.net/

Use InPlace CMS to maintain a bundle of HTML pages in an uniform appearance. Adopt the light and agile approach to document a product or to create a mini-site. Target users are the developers of open source projects.

(more…)

python, re-encoding incorrected encoded string

Friday, August 17th, 2007

Python has a quite decent internal support of unicode. But sometimes it’s hard to find how to exploit the support.

I’ve lost some time solving a simple problem:

* A program reads a file, which is in the iso-8859-1 encoding.
* The text is converted to Unicode
* At some moment, program detects that some strings are actually were in windows-1251 encoding
* The task: how to re-interpret the string?

The only solution I found is hacky:

(more…)

downloading log files

Thursday, August 16th, 2007

The best place to analyze log files is the local computer, not the server:
1) Log files grow fast, taking all the server space
2) Queries might be CPU-intensite
If you like me, you prefer that the log files appear on the local computer automatically.

(more…)

automating GUI tasks in Python

Friday, July 20th, 2007

I have a program which is an example of usability nightmare. It’s easier to create data in Excel and somehow migrate them to that program. Unfortunately, the only access to data is through GUI.

Well, I decided to find some automation tool for GUI testing, with Python as a scripting language. The need of Python is essential, because I don’t want to learn yet another scripting language, especially if it is claimed to be user-friendly.

The first try was TestComplete, but the license costs too much for me ($600), therefore I didn’t even consider it. Then I skipped several tools because of unknown scripting languages. Finally, I found something promising: Ranorex.

(more…)

breaking expectations

Tuesday, May 22nd, 2007

How do you thing, what the following Python/libxml2 code does?

for kid in node.get_children():
  print kid.name

(more…)

shut up, you dummy 7-bit Python

Friday, March 23rd, 2007

I’m working on an unicode-aware application. I like to use print to debug programs, but in this case it was nightmare. The most popular result of print was:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xXX in position 0: ordinal not in range(128)

I spent two hours fixing it, and I hope it’s done. The solution is one of the ugliest hack I ever written, but it solves the pain.

(more…)

execute an XPath and get the line numbers

Monday, March 5th, 2007

I’m investigating the structure of an XML file. The best tool is an usual text editor (vim), but I need to look at some specific tags in some specific contexts. The idea is to execute an XPath and somehow to get to the location in the editor. Remembering that I saw something interesting while playing with libxml internals, I re-checked… And bingo! I’ve written a script which executes an XPath and prints the result together with the line number of the source XML file.

(more…)

plasTeX

Wednesday, October 25th, 2006

Seems interesting: plasTeX is a LaTeX document processing framework written entirely in Python. It currently comes bundled with an XHTML renderer (including multiple themes), as well as a way to simply dump the document to a generic form of XML. Other renderers can be added as well and are planned for future releases.

scons signatures for Python actions

Thursday, August 24th, 2006

Finally, I traced out why time-to-time my functional tests stop passing.
* Some steps are associated with Python functions.
* Signatures of such steps depend on the code of the functions.
* If the code of a function is changed, the signature is changes, and the step is considered out-of-date.
Therefore, small changes in code lead to changes in the build process.

(more…)

forcing python mode in vim

Wednesday, August 23rd, 2006

To force the python mode for a file, add the following comment at the beginning:

# -*- mode: python -*-

It’s also possible to add other options as well. Unfortunately, I don’t remember the term for such comment, so I can’t find its descrition in the vim documentation.

currently executing python file

Wednesday, August 16th, 2006

Here is a way to get the currently executing Python file:

print (lambda x:x).func_code.co_filename

(more…)

parsing latex log files

Thursday, August 10th, 2006

In mary cases, LaTeX should be run several times to get the correct result (for example, to resolve cross-references). The only way to detect if re-run is required is to analyze the log file. I haven’t found anything ready to use, so I’ve written it myself.

(more…)

python trap

Wednesday, August 2nd, 2006

What will this code print?

for item in ('aaa'): print item

(more…)

Python for Lisp Programmers

Thursday, January 5th, 2006

A small Lisp vs Python article: “Python for Lisp Programmers“. The title is very untraditional, one expects Python and Lisp swapped.