reversing a string in python
Monday, October 15th, 2007This beautiful construction was not obvious for me.
>>> 'foo'[::-1]
'oof'
This beautiful construction was not obvious for me.
>>> 'foo'[::-1]
'oof'
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.
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:
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.
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.
How do you thing, what the following Python/libxml2 code does?
for kid in node.get_children():
print kid.name
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.
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.
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.
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.
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.
Here is a way to get the currently executing Python file:
print (lambda x:x).func_code.co_filename
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.
A small Lisp vs Python article: “Python for Lisp Programmers“. The title is very untraditional, one expects Python and Lisp swapped.