Popular recipes by Denis Barmenkov http://code.activestate.com/recipes/users/57155/popular/2011-04-11T13:02:32-07:00ActiveState Code Recipes"tail -f" with inode monitor (Python) 2010-09-21T07:37:38-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577398-tail-f-with-inode-monitor/ <p style="color: grey"> Python recipe 577398 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/inode/">inode</a>, <a href="/recipes/tags/monitor/">monitor</a>, <a href="/recipes/tags/tail/">tail</a>). Revision 3. </p> <p>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.</p> Relative path from one directory to another without explicit string functions (unix only) (Python) 2011-04-11T13:02:32-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577356-relative-path-from-one-directory-to-another-withou/ <p style="color: grey"> Python recipe 577356 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/relative/">relative</a>, <a href="/recipes/tags/unix/">unix</a>). Revision 4. </p> <p>I saw a <a href="http://code.activestate.com/recipes/208993/">recipe 208993</a> 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.</p> <p>Function deal with Unix paths (root: "/"), Windows systems are not supported (root: "C:\").</p> xnview backup files remove utility (Python) 2010-07-06T18:09:43-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577281-xnview-backup-files-remove-utility/ <p style="color: grey"> Python recipe 577281 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/backup/">backup</a>, <a href="/recipes/tags/cleanup/">cleanup</a>, <a href="/recipes/tags/remove/">remove</a>, <a href="/recipes/tags/xnview/">xnview</a>). Revision 3. </p> <p>Currently XnView image viewer (versions up to 1.97.6) correctly rotate images only when option "[x] make backup" set.</p> <p>Backup files have added name part '.xnbak' before extension:</p> <pre class="prettyprint"><code>original file: IMG0001.jpg backup file: IMG0001.xnbak.jpg </code></pre> <p>Attached script take a root directory in command line and remove backup files created by XnView only when rotated file present in same directory.</p> Render tables for text interface (Python) 2010-04-20T18:02:51-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577202-render-tables-for-text-interface/ <p style="color: grey"> Python recipe 577202 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/table/">table</a>, <a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>Sometime pprint module is not enough for formatting data for console or log file output. This module provide function which fill the gap.</p> <p><strong>Sample function call:</strong></p> <pre class="prettyprint"><code>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')] ) </code></pre> <p><strong>Output:</strong></p> <pre class="prettyprint"><code>======================================= | NUM | SPEED | DESC | ======================================= | 1 | 100 | | | 2 | 10000 | label 1 | | 3 | 1500 | none | | 4 | 12 | very long description | ======================================= </code></pre> Make unique file name (Python) 2010-04-18T21:07:52-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577200-make-unique-file-name/ <p style="color: grey"> Python recipe 577200 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/rename/">rename</a>). Revision 2. </p> <p>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.</p> File read/write routines (Python) 2010-04-18T17:18:50-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577199-file-readwrite-routines/ <p style="color: grey"> Python recipe 577199 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/read/">read</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/write/">write</a>). Revision 2. </p> <p>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).</p> Fix mbox files after importing EML into TB using ImportExportTools (Python) 2010-05-02T13:21:00-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577214-fix-mbox-files-after-importing-eml-into-tb-using-i/ <p style="color: grey"> Python recipe 577214 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/eml/">eml</a>, <a href="/recipes/tags/from/">from</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/importexporttools/">importexporttools</a>, <a href="/recipes/tags/mbox/">mbox</a>, <a href="/recipes/tags/tb/">tb</a>, <a href="/recipes/tags/thunderbird/">thunderbird</a>). Revision 2. </p> <p>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:</p> <pre class="prettyprint"><code>From - Tue Apr 27 19:42:22 2010 </code></pre> <p>ImportExportTools formats this line wrong I suppose that used some system function with default specifier so I saw in mbox file:</p> <pre class="prettyprint"><code>From - Sat May 01 2010 15:07:31 GMT+0400 (Russian Daylight Time) </code></pre> <p>So there are two errors: 1) sequence 'time year' broken into 'year time' 2) extra trash with GMT info along with time zone name</p> <p>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):</p> <pre class="prettyprint"><code>_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*$" </code></pre> <p>Attached script fixes incorrect From lines so parsing those mboxes using Python standard library will become ok.</p> Modified os.walk which return current directory depth (Python) 2009-11-06T16:16:42-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576933-modified-oswalk-which-return-current-directory-dep/ <p style="color: grey"> Python recipe 576933 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/directory_depth/">directory_depth</a>, <a href="/recipes/tags/os_walk/">os_walk</a>, <a href="/recipes/tags/relative_path/">relative_path</a>). Revision 2. </p> <p>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.</p> <p>One solution -- find function which will calculate relative distance from top directory to file.</p> <p>Another [presented] solution -- modify os.walk so it returns depth level as fourth tuple's value.</p> Align text filter (Python) 2010-03-09T07:34:43-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577093-align-text-filter/ <p style="color: grey"> Python recipe 577093 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/align/">align</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/wrap/">wrap</a>). Revision 4. </p> <p>Python 2.3 module textwrap can justify text in three modes: left/right/center. Sometimes 'align' mode become more useful.</p> Precise console progress meter with ETA calculation (Python) 2010-04-18T21:11:40-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577002-precise-console-progress-meter-with-eta-calculatio/ <p style="color: grey"> Python recipe 577002 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/console/">console</a>, <a href="/recipes/tags/eta/">eta</a>, <a href="/recipes/tags/meter/">meter</a>, <a href="/recipes/tags/progress/">progress</a>). Revision 13. </p> <p>After several attempts to use third-party modules I wrote my own console progress meter.</p> <p>Bonus list:</p> <ol> <li>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)</li> <li>ability to write progress meter to sys.stderr</li> <li>update_left() method for multithreaded programs :)</li> </ol> One-word logging.basicConfig wrapper (Python) 2010-01-06T06:54:09-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576994-one-word-loggingbasicconfig-wrapper/ <p style="color: grey"> Python recipe 576994 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/basicconfig/">basicconfig</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). Revision 2. </p> <p>Every Python logging manual have this code:</p> <pre class="prettyprint"><code>logging.basicConfig(level=logging.DEBUG, filename='debug.log', format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') </code></pre> <p>This is a simple function which call it for you. All you need is remember one function name; useful on little scripts.</p> Patch extension binaries compiled for previous (different) Python version (Python) 2010-03-12T15:50:04-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576873-patch-extension-binaries-compiled-for-previous-dif/ <p style="color: grey"> Python recipe 576873 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/binaries/">binaries</a>, <a href="/recipes/tags/dll/">dll</a>, <a href="/recipes/tags/patch/">patch</a>, <a href="/recipes/tags/precompiled/">precompiled</a>, <a href="/recipes/tags/pyd/">pyd</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 4. </p> <p>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.</p> <p>Often precompiled binaries can be patched to meet used Python version. </p> <p>See 'discussions' below for details.</p> simple samefile() for windows (local drive case only) (Python) 2009-09-09T02:26:35-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576886-simple-samefile-for-windows-local-drive-case-only/ <p style="color: grey"> Python recipe 576886 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/os_path_samefile/">os_path_samefile</a>, <a href="/recipes/tags/samefile/">samefile</a>, <a href="/recipes/tags/win32/">win32</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 4. </p> <p>A simple replacement of the os.path.samefile() function not existing on the Windows platform. MAC/Unix supported in standard way :).</p> EXIF-date-based JPEG files rename using PIL (Python) 2009-02-10T07:28:48-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576646-exif-date-based-jpeg-files-rename-using-pil/ <p style="color: grey"> Python recipe 576646 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/exif/">exif</a>, <a href="/recipes/tags/jfif/">jfif</a>, <a href="/recipes/tags/jpeg/">jpeg</a>, <a href="/recipes/tags/jpg/">jpg</a>, <a href="/recipes/tags/photo/">photo</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/rename/">rename</a>). Revision 4. </p> <p>Rename JPEG files according to EXIF-date using PIL [library].</p> <p>If global variable CREATE_HARDLINK is set, script creates Windows (XP) batch file for creating hardlink version of source files.</p> <p>PIL available here: <a href="http://www.pythonware.com/products/pil/" rel="nofollow">http://www.pythonware.com/products/pil/</a></p> binascii.crc32 result to hex (Python) 2007-06-14T06:25:57-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/521907-binasciicrc32-result-to-hex/ <p style="color: grey"> Python recipe 521907 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Function becomes useful when we need hex value of crc32() result, even if it is negative.</p> print statement: battle with UnicodeEncodeError (Python) 2009-02-10T07:22:46-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/523011-print-statement-battle-with-unicodeencodeerror/ <p style="color: grey"> Python recipe 523011 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 5. </p> <p>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.</p> <p>Function console() helps avoid this exceptions by converting erroneous charatcters to standard python representation.</p> <p>to do in future: make a codec-wrapper for safe using in statements like this:</p> <pre class="prettyprint"><code>sys.stdout=codecs.getwriter('cp866')(sys.stdout) </code></pre> Saving [logging.] disk log fragment into separated file (Python) 2007-05-08T07:26:44-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/519624-saving-logging-disk-log-fragment-into-separated-fi/ <p style="color: grey"> Python recipe 519624 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Sometime I want extract part of disk-based log file, created by standard logging module) into separated file on disk. This recipe shows simple technique to acquire this.</p> <p>Sample: processing tasks in loop, so on exit I have few logs: on log per task.</p> How to disablŠµ debug logging in release version (Python) 2006-07-12T08:34:36-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/496870-how-to-disabl-debug-logging-in-release-version/ <p style="color: grey"> Python recipe 496870 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 3. </p> <p>When you release your program to client, its a good idea to disable all the debug messages. It is possible via custom configuring debug levels at all modules, but may be implemented using simple wrapper around logging.getLogger() function.</p> Getting SYSTEM environment variable under Windows (Python) 2009-02-10T07:24:02-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/473800-getting-system-environment-variable-under-windows/ <p style="color: grey"> Python recipe 473800 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>As you can see in Windows Control Panel 'System' applet there are two groups of environment variables: USER and SYSTEM. Here presents function for retrieve SYSTEM variable value.</p> Align text string using spaces between words to fit specified width (Python) 2010-03-09T07:04:19-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/414870-align-text-string-using-spaces-between-words-to-fi/ <p style="color: grey"> Python recipe 414870 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/text/">text</a>). Revision 5. </p> <p>Python 2.3 module textwrap can justify text in three modes: left/right/center. Sometimes 'align' mode become more useful.</p>