Popular recipes by Jan Kaliszewski http://code.activestate.com/recipes/users/4172762/2011-09-02T01:49:40-07:00ActiveState Code RecipesFast and elegant switch/case-like dispatch (Python) 2011-09-02T01:49:40-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577864-fast-and-elegant-switchcase-like-dispatch/ <p style="color: grey"> Python recipe 577864 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/switch/">switch</a>). Revision 4. </p> <p>My approach to that common issue focuses on <strong>efficiency</strong> and <strong>elegant, declarative style</strong> of definition. That's why:</p> <ul> <li>The way switches work is based on unwrapped defaultdict/list lookup.</li> <li>The way you define switches is based on classes and easy-to-use decorators (note that you can use subclassing in a flexible way -- without any negative impact on efficiency!).</li> <li>Its use cases focus on a-lot-of-keys situations and it does not cover the <em>fall-through</em> feature (though you can reach its semantics if you really need that -- by calling class methods...).</li> </ul> CLOS-like around/before/after auxiliary methods (Python) 2011-08-25T22:59:22-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577859-clos-like-aroundbeforeafter-auxiliary-methods/ <p style="color: grey"> Python recipe 577859 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/auxiliary/">auxiliary</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/clos/">clos</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/super/">super</a>). </p> <p>This module provides an easy way to define and use your own <strong>around/before/after auxiliary methods</strong>, similar to <a href="http://www.aiai.ed.ac.uk/~jeff/clos-guide.html#meth-comb">those used in CLOS</a> (Common Lisp Object System).</p> namedtuple.abc - abstract base class + mix-in for named tuples (Python) 2011-04-02T02:07:00-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577629-namedtupleabc-abstract-base-class-mix-in-for-named/ <p style="color: grey"> Python recipe 577629 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/dry/">dry</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 7. </p> <p>If you need</p> <ul> <li>to define <strong>named tuple subclasses</strong> (including reusable abstract ones), adding/overriding some methods, in a convenient way;</li> <li>to have the named tuple ABC (abstract base class) for <strong>isinstance/issubclass</strong> tests;</li> <li>or simply would like to define your named tuple classes in a <strong>class-syntax-based and DRY way</strong> (without repeating type names...)</li> </ul> <p>-- <strong>this recipe is for you.</strong></p> Tiny trace/debug-logging tool with smart event filtering (Python) 2011-04-29T02:23:37-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577429-tiny-tracedebug-logging-tool-with-smart-event-filt/ <p style="color: grey"> Python recipe 577429 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/trace/">trace</a>). Revision 3. </p> <ul> <li><p>Just import, call and log your selected call/return/exception/etc. events.</p></li> <li><p>You can use a standard Python logger from the <code>logging</code> module.</p></li> <li><p>Quite flexible tool and about 50 efective SLOC only (excluding the example script).</p></li> <li><p>For debugging rather than production environments (programs noticeably slow down).</p></li> </ul> Multiprocess-safe logging file-handler + interprocess RLock (Python) 2010-09-22T17:30:10-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577395-multiprocess-safe-logging-file-handler-interproces/ <p style="color: grey"> Python recipe 577395 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/rlock/">rlock</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threadsafe/">threadsafe</a>). Revision 11. </p> <p>A Python 2.x/3.x-compatibile <strong>multiprocess-safe logging file-handler</strong> (logging.FileHandler replacement, designed for logging to a single file from multiple independent processes) together with a simple <strong>interprocess recursive lock</strong> -- universal abstract classes + Unix/Linux implementation.</p> <p><strong>Update:</strong> It's is a deeply revised version. Especially, now it --</p> <ul> <li>is Python 2.4, 2.5, 2.6, 3.1 -compatibile (previously Py>=2.6 was needed); probably works also with 2.7, 3.0 and 3.2 (but not tested if it does);</li> <li>is multiprocess-safe as well as thread-safe (proviously thread safety within a process was missed);</li> <li>is based on public interfaces only (previously FileHandler._open() was called and overriden);</li> <li>implement full RLock instance interface, as documented for threading.RLock (previously non-blocking mode and context-manager interface were missing).</li> </ul> <p>The module contains:</p> <ul> <li>Unix/Linux-only example implementation (with flock-based locking): <strong>FLockRLock</strong> and <strong>FLockFileHandler</strong> classes.</li> <li>universal abstract classes -- which may be useful at developing implementation for non-Unix platforms: <strong>MultiprocessRLock</strong>, <strong>MultiprocessFileHandler</strong>, <strong>LockedFileHandler</strong>,</li> </ul> <p>Also a quick-and-dirty test was added.</p> <p><strong>It is still an alpha version -- I'll be very grateful for any feedback.</strong></p> <hr /> <p><strong>Further updates:</strong></p> <ul> <li><p>2010-09-20: Some corrections, especially: non-blocking mode bug in MultiprocessRLock.acquire() fixed; _test() function improved; plus fixes in the description below.</p></li> <li><p>2010-09-22: _test() improved and moved to description section. Mistaken copyright-notice removed.</p></li> </ul> fcntl.flock() (Unix file lock) behaviour sampling script (Python) 2010-09-22T00:11:12-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577404-fcntlflock-unix-file-lock-behaviour-sampling-scrip/ <p style="color: grey"> Python recipe 577404 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_descriptor/">file_descriptor</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 3. </p> <p>A quick <em>fcntl.flock(fcntl.LOCK_EX | fcntl.LOCK_NB)</em> call sampling script: with <em>one file object</em> (and descriptor) or <em>separate file objects</em> (and different descriptors) pointing to the same filesystem path -- with/without <strong>threading</strong> or <strong>forking</strong>.</p> <p>It's rather exemplum-and-educational piece of code than utility-script, unless somebody has to few slots in their memory to remember that <strong>flock</strong> is file-descriptor-tacked (then quick run of the script can save web-searching) :)</p> Sorted dictionary (Python) 2010-01-20T17:11:59-08:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/576998-sorted-dictionary/ <p style="color: grey"> Python recipe 576998 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/mapping/">mapping</a>, <a href="/recipes/tags/sorted/">sorted</a>, <a href="/recipes/tags/sorteddict/">sorteddict</a>). Revision 29. </p> <p>A simple implementation of a dictionary which always (when applicable) returns keys, values, items (key-value pairs) sorted by keys (inserting/removing order doesn't matter and only keys are important; so please note that it is something different than OrderedDict in Python 3.1/2.7 or Django's SortedDict).</p>