User : denis barmenkov
|"tail -f" with inode monitor||Denis Barmenkov||python||inode monitor tail||
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||path relative unix||
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||backup cleanup remove xnview||
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:
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||eml from import importexporttools mbox tb thunderbird||
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:
ImportExportTools formats this line wrong I suppose that used some system function with default specifier so I saw in mbox file:
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):
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||format table text||
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:
|April 20, 2010|
|Make unique file name||Denis Barmenkov||python||file name rename||
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||binary file read text write||
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||align pipe text wrap||
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||console eta meter progress||
After several attempts to use third-party modules I wrote my own console progress meter.
|January 11, 2010|
|One-word logging.basicConfig wrapper||Denis Barmenkov||python||basicconfig logging wrapper||
Every Python logging manual have this code:
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||directory_depth os_walk relative_path||
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||os_path_samefile samefile win32 windows||
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||binaries dll patch precompiled pyd windows||
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||date exif jfif jpeg jpg photo pil rename||
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||print statement: battle with UnicodeEncodeError sysadmin||
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:
|July 1, 2007|