Archive for the ‘python’ Category

python libxml2 dita

Thursday, January 21st, 2016

python libxml2 dita

For correct transformation of DITA files (XML-standard for modular documentation), it is necessary to pull information from DTD (document type definition). In my python code, sometimes I did get this information and sometimes not. Now I’ve tracked the source of instability and corrected the code.

(more…)

Generating Excel XML, avoiding “found unreadable content”

Wednesday, September 30th, 2015

In theory, changing content of an Excel file is easy:

* Parse XML from the zip-file
* Change XML
* Save XML into the zip

In practice I got the error: >>Von Excel wurde unlesbares Inhalt in … gefunden. Möchten Sie den Inhalt dieser Arbeitsmappe wiederherstellen?< < (English: "Excel found unreadable content...") (more…)

Tkinter sample application for a long operation

Thursday, July 9th, 2015

In the year 2001 I started to experiment with GUI applications in Python. The experience was summarized and published in the article “A complete Python Tkinter sample application for a long operation“. Now, in the year 2015, a programmer have sent me an updated code. With the minimal changes, which mostly are update of the names of Tkinter modules, the code works.

(more…)

Workaround for: CTR mode needs counter parameter, not IV

Friday, February 20th, 2015

After upgrading the local Linux system, my python paramiko (ssh protocol implementation) program stopped working, with the error message:

CTR mode needs counter parameter, not IV

(more…)

using freebidi from python using ctypes

Monday, December 8th, 2014

GNU FriBidi is an implementation of the Unicode Bidirectional Algorithm (bidi). There is a Python binding PyFribidi, but it is not complete. What I need is not a visual presentation of a string, but information where direction is changing. This function is not provided by the binding, therefore I’ve made an alternative using ctypes.

(more…)

python libxml2: save XML as HTML

Thursday, October 31st, 2013

HTML is the main output format for XML transformations. Every XSLT-processor, including libxslt/libxml2, supports it. But if you transform a libxml2 tree manually, you are in trouble. You can save XML only as XML, not as HTML. A solution is required. My version is not elegant, but works.

(more…)

using xlrd and formatting Excel numbers

Tuesday, October 22nd, 2013

The number (and dates) in Excel are float numbers. How these numbers are displyed to an user — as an integer, or with two digits after a point, etc — are defined by the cell format. Unfortunately, xlrd does not support number formatting. It is your task to interpret the format and display the number as expected. My code can probably help. Download xlrd-format-excel-number

(more…)

pyinstaller, wxpython, _core_ not found

Wednesday, July 31st, 2013

Sometimes an error and the error message are different things. One of the examples is that my wxpython-program did not want to start after converting to exe using pyinstaller:

ImportError: No module named _core_

(more…)

wxPython GIFAnimationCtrl: embed animation gif in the source code

Wednesday, March 27th, 2013

In some cases it is useful to store media files inside the python code itself. For images, PyEmbeddedImage and the script img2py.py work well. But for GIFAnimationCtrl no obvious solution is available, therefore I had to investigate the source code of wx.animate to find one.

(more…)

xml.etree.ElementTree and processing instructions

Tuesday, June 19th, 2012

Python standard library xml.etree.ElementTree is convenient to work with a simple subset of XML. Unfortunately for me, this subset does not include processing instructions, therefore an workaround is required.
(more…)

python wtf: strip() eats too much

Tuesday, June 1st, 2010

Many of python-xml code is probably wrong. Tracing a bug, I found an interesting WTF. A minimal example:

import string
s1 =  "\xa0x\xa0"
s2 = u"\xa0x\xa0"
print repr(s1.strip())
print repr(s2.strip())
print repr(s2.strip(string.whitespace))

And what we see in the output?

(more…)

importing as module when a file has a non-identifier name

Friday, June 12th, 2009

I need to reuse a function in a file. The usual way is just to import this file as a module. But what to do if the name is “bad”? The following doesn’t work:


import foo-bar as foo_bar
foo_bar.callme()

After trials and errors, the desired magic found:

(more…)

escape a TeX string in Python

Friday, October 17th, 2008

Do you generate [La]TeX files using Python? If yes, do you escape the special characters. Haven’t you forgotten to escape also empty strings?

To avoid reinventing the wheel, I tried to use TeXML libraries. This is the right choice, but one has to use a little trick.

(more…)

be carefull with libxml2 in python

Tuesday, May 27th, 2008

Unpleasant surprise from libxml2 bindings for Python: one must care for encoding conversion.

(more…)

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…)