Popular recipes by Dmitry Dvoinikov http://code.activestate.com/recipes/users/2475216/2016-05-01T13:11:05-07:00ActiveState Code RecipesFunction guards for Python 3 (Python)
2016-05-01T13:11:05-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/580658-function-guards-for-python-3/
<p style="color: grey">
Python
recipe 580658
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/call/">call</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/dispatch/">dispatch</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/guard/">guard</a>).
</p>
<p>This module implements a function guard - facility to redirect the the call to one of several function implementations at run time based on the actual call arguments.</p>
<p>Wrap each of the identically named functions in a @guard decorator and provide a _when parameter with a default value set to guarding expression.</p>
<p>See samples at the top of the module.</p>
Method signature type checking decorator for Python 3 (Python)
2015-05-15T09:25:08-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/572161-method-signature-type-checking-decorator-for-pytho/
<p style="color: grey">
Python
recipe 572161
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/checking/">checking</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/signature/">signature</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/type/">type</a>).
Revision 8.
</p>
<p>This recipe allows nice and clean validation for method parameters/return values. It uses function annotations available in Python 3 for the actual signature specification.</p>
Assertion that a code fragment throws a particular exception (self-test utility, Python 3) (Python)
2010-07-20T13:15:49-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/573452-assertion-that-a-code-fragment-throws-a-particular/
<p style="color: grey">
Python
recipe 573452
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/unittests/">unittests</a>).
Revision 2.
</p>
<p>The class presented in this recipe is useful when you are writing module self-tests and need to assert that a fragment of code throws a particular exception.</p>
Shared lock (aka reader-writer lock) with timeouts and FIFO ordering (Python)
2005-12-20T21:56:24-08:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/465156-shared-lock-aka-reader-writer-lock-with-timeouts-a/
<p style="color: grey">
Python
recipe 465156
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>This shared lock implementation supports timeouts so that an attempt to acquire a lock occasionally times out. It also preserves FIFO ordering for threads waiting for exclusive lock and has other valuable features.</p>
Decorator to make a decorated call in a separate thread with timeout (Python)
2005-10-31T05:15:57-08:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/440569-decorator-to-make-a-decorated-call-in-a-separate-t/
<p style="color: grey">
Python
recipe 440569
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 8.
</p>
<p>Makes it easier to execute async calls or deal with external systems calls to which can block forever or occassionally take long time to complete.</p>
Method signature checking decorators (Python)
2008-01-28T07:13:53-08:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/426123-method-signature-checking-decorators/
<p style="color: grey">
Python
recipe 426123
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 3.
</p>
<p>This recipe adds parameter type checking to each method or function invocation. Not a replacement for static typing, but it makes a nice pair of shackles to me.</p>
Formats current exception with stack trace so that it fits in single line and has known encoding. (Python)
2005-12-01T07:43:44-08:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/444746-formats-current-exception-with-stack-trace-so-that/
<p style="color: grey">
Python
recipe 444746
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 2.
</p>
<p>Have you ever tried to log an exception of unknown type ? What's in it ? How to fetch stack trace ? Will str(e) return plain ascii or international chars ? Is logger ready for it ? This recipe provides a formatting function.</p>
Yet another Design By Contract module for Python (Python)
2006-09-02T08:18:47-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/436834-yet-another-design-by-contract-module-for-python/
<p style="color: grey">
Python
recipe 436834
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 4.
</p>
<p>Implements class invariants, pre/postconditions in a way similar to PyDBC, but significantly better.</p>