User : denis barmenkov

Title User Language Tags Description Date
"tail -f" with inode monitor Denis Barmenkov python

Sometimes tail -f launched for log file miss the point when program recreates log file. Script in this recipe monitors inode changes for specified file and restarts tail if needed.

September 21, 2010
Relative path from one directory to another without explicit string functions (unix only) Denis Barmenkov python

I saw a recipe 208993 messed up with os.sep and '../' and decide to write near-pure-Python version. os.sep used in string expressions only for testing for root directory.

Function deal with Unix paths (root: "/"), Windows systems are not supported (root: "C:\").

August 13, 2010
xnview backup files remove utility Denis Barmenkov python

Currently XnView image viewer (versions up to 1.97.6) correctly rotate images only when option "[x] make backup" set.

Backup files have added name part '.xnbak' before extension:

original file: IMG0001.jpg
backup file: IMG0001.xnbak.jpg

Attached script take a root directory in command line and remove backup files created by XnView only when rotated file present in same directory.

July 6, 2010
Fix mbox files after importing EML into TB using ImportExportTools Denis Barmenkov python

I've found a bug in import EML file into Thunderbird using ImportExportTools addon: when I import eml file into TB there are a 'From' line added to mbox followed with EML file contents. TB maintains right 'From' line for messages fetched from mailservers:

From - Tue Apr 27 19:42:22 2010

ImportExportTools formats this line wrong I suppose that used some system function with default specifier so I saw in mbox file:

From - Sat May 01 2010 15:07:31 GMT+0400 (Russian Daylight Time)

So there are two errors: 1) sequence 'time year' broken into 'year time' 2) extra trash with GMT info along with time zone name

This prevents the mbox file parsing using Python standard library (for sample) because there are a hardcoded regexp for matching From line (file lib/mailbox.py, class UnixMailbox):

_fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
                   r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$"

Attached script fixes incorrect From lines so parsing those mboxes using Python standard library will become ok.

May 2, 2010
Render tables for text interface Denis Barmenkov python

Sometime pprint module is not enough for formatting data for console or log file output. This module provide function which fill the gap.

Sample function call:

nums = [ '1', '2', '3', '4' ]
speeds = [ '100', '10000', '1500', '12' ]
desc = [ '', 'label 1', 'none', 'very long description' ]
lines = format_table( [(nums, ALIGN_RIGHT|PADDING_ALL, 'NUM'), 
                       (speeds, ALIGN_RIGHT|PADDING_ALL, 'SPEED'), 
                       (desc, ALIGN_LEFT|PADDING_ALL, 'DESC')] )

Output:

=======================================
| NUM | SPEED | DESC                  |
=======================================
|   1 |   100 |                       |
|   2 | 10000 | label 1               |
|   3 |  1500 | none                  |
|   4 |    12 | very long description |
=======================================
April 20, 2010
Make unique file name Denis Barmenkov python

Sometimes it is important to save data in the file but the file with the specified name already exists. This function creates a file name that is similar to the original by adding a unique numeric suffix. This avoids the renaming of existing files.

April 18, 2010
File read/write routines Denis Barmenkov python

For small dirty hacks in Perl has a module File::Slurp. I wrote two simple functions when I moving from Perl to Python - one for reading files and second for writing files. Valuable data is list of lines or blob (additionally specified argument binmode=1).

April 18, 2010
Align text filter Denis Barmenkov python

Python 2.3 module textwrap can justify text in three modes: left/right/center. Sometimes 'align' mode become more useful.

March 9, 2010
Precise console progress meter with ETA calculation Denis Barmenkov python

After several attempts to use third-party modules I wrote my own console progress meter.

Bonus list:

  1. calculation of ETA based on last update points. More accuracy when comparing with calculation ETA based on process start time (process can survive after Hibernate, but ETA has lost his accuracy)
  2. ability to write progress meter to sys.stderr
  3. update_left() method for multithreaded programs :)
January 11, 2010
One-word logging.basicConfig wrapper Denis Barmenkov python

Every Python logging manual have this code:

logging.basicConfig(level=logging.DEBUG, filename='debug.log',
                    format='%(asctime)s %(levelname)s: %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

This is a simple function which call it for you. All you need is remember one function name; useful on little scripts.

January 6, 2010
Modified os.walk which return current directory depth Denis Barmenkov python

On some task I need to collect file names under specified directory with distance from it. Standard os.walk function do not return depth value.

One solution -- find function which will calculate relative distance from top directory to file.

Another [presented] solution -- modify os.walk so it returns depth level as fourth tuple's value.

October 18, 2009
simple samefile() for windows (local drive case only) Denis Barmenkov python

A simple replacement of the os.path.samefile() function not existing on the Windows platform. MAC/Unix supported in standard way :).

August 19, 2009
Patch extension binaries compiled for previous (different) Python version Denis Barmenkov python

It is helpful to download and install precompiled binaries of Python modules especially for Windows. Second way is longer: get module sources, build the C compiler environment, compile binaries, beware of compiler warnings, install module.

Often precompiled binaries can be patched to meet used Python version.

See 'discussions' below for details.

August 9, 2009
EXIF-date-based JPEG files rename using PIL Denis Barmenkov python

Rename JPEG files according to EXIF-date using PIL [library].

If global variable CREATE_HARDLINK is set, script creates Windows (XP) batch file for creating hardlink version of source files.

PIL available here: http://www.pythonware.com/products/pil/

February 10, 2009
print statement: battle with UnicodeEncodeError Denis Barmenkov python

I received UnicodeEncodeError when playing with various codepages in source code/files/standard streams. Sometime I receive UnicodeEncodeError when script launched via scheduler or in long running batch when parsing unpredictable [alien ;)] HTML.

Function console() helps avoid this exceptions by converting erroneous charatcters to standard python representation.

to do in future: make a codec-wrapper for safe using in statements like this:

sys.stdout=codecs.getwriter('cp866')(sys.stdout)
July 1, 2007