Most viewed recipes tagged "wrapper"http://code.activestate.com/recipes/tags/wrapper/views/2015-01-29T17:12:10-08:00ActiveState Code RecipesObject wrapper class (Python)
2011-01-28T12:40:58-08:00Nichita Utiuhttp://code.activestate.com/recipes/users/4176755/http://code.activestate.com/recipes/577555-object-wrapper-class/
<p style="color: grey">
Python
recipe 577555
by <a href="/recipes/users/4176755/">Nichita Utiu</a>
(<a href="/recipes/tags/wrapper/">wrapper</a>).
</p>
<p>This is a wrapper class which, it wraps an object which it then proxies unnhandled getattr calls to.</p>
Exception handling in a single line (Python)
2009-10-09T01:07:52-07:00Radek Szklarczykhttp://code.activestate.com/recipes/users/2847011/http://code.activestate.com/recipes/576872-exception-handling-in-a-single-line/
<p style="color: grey">
Python
recipe 576872
by <a href="/recipes/users/2847011/">Radek Szklarczyk</a>
(<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/expression/">expression</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 16.
</p>
<p>The rules of <em>duck typing</em> in python encourage programmers to use the "try...except..." clause. At the same time python with new versions enables to use more powerful list comprehensions (for example Conditional Expressions). However, it is impossible to write the "try...except..." clause in a list comprehension. The following recipe "protects" a function against exception and returns a default value in the case when exception is thrown.</p>
Python Wrapper for Google AJAX Language API (Python)
2009-08-22T12:59:38-07:00Ashish Yadavhttp://code.activestate.com/recipes/users/4171522/http://code.activestate.com/recipes/576890-python-wrapper-for-google-ajax-language-api/
<p style="color: grey">
Python
recipe 576890
by <a href="/recipes/users/4171522/">Ashish Yadav</a>
(<a href="/recipes/tags/google_ajax_api/">google_ajax_api</a>, <a href="/recipes/tags/language_translation/">language_translation</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 3.
</p>
<p>A Python Wrapper for Google AJAX Language API,
Uses Google Language Detection, in cases source language is not provided with the source text,
Splits up text if it's longer then 4500 characters, as a limit put up by the API. Working fine for me, please let me know if i missed something.</p>
Property wrapper (Python)
2010-07-20T14:30:30-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/502243-property-wrapper/
<p style="color: grey">
Python
recipe 502243
by <a href="/recipes/users/4035877/">Louis RIVIERE</a>
(<a href="/recipes/tags/property/">property</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
</p>
<p>This property shortcut doesn't restrict the property's attributes to get, set, del and doc.
Besides these functions can have any name, not limited to fget, fset ...
Note that the default doc is not the getter's one.</p>
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>
Object Wrapper (Python)
2010-05-05T17:02:42-07:00Dmitryhttp://code.activestate.com/recipes/users/4173772/http://code.activestate.com/recipes/577215-object-wrapper/
<p style="color: grey">
Python
recipe 577215
by <a href="/recipes/users/4173772/">Dmitry</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
</p>
<p>There is classical wrapper on Python language. If you have only object and can't change object generation you can use this class.</p>
iter_except -- a useful variant of __builtin__.iter() (Python)
2010-03-27T02:04:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577155-iter_except-a-useful-variant-of-__builtin__iter/
<p style="color: grey">
Python
recipe 577155
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
</p>
<p>Variant of iter(func, sentinel) that looks for an exception rather than for a sentinel value. Good for making iterators from of APIs that advance over a data and return an exception when they are done.</p>
Recipe With No Name Yet (Python)
2012-08-22T17:57:12-07:00Cyrilhttp://code.activestate.com/recipes/users/4182937/http://code.activestate.com/recipes/578221-recipe-with-no-name-yet/
<p style="color: grey">
Python
recipe 578221
by <a href="/recipes/users/4182937/">Cyril</a>
(<a href="/recipes/tags/check/">check</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 9.
</p>
<p>Not a very new recipe, but a short one and (I hope) useful :</p>
<ul>
<li>wrapping any function "f" with two decorators "enter_event" and "exit_event" that will trigger calling of user
functions when, ... hum ... evidently, <strong>just before entering and just after exiting the "f" function</strong>.</li>
</ul>
<p>Typical usages :</p>
<ul>
<li>debugging on a function by function basis :
<ul>
<li>emit a trace in log file to see when functions are called and check sequences correctness
(very usefull when programming by events)</li>
<li>feed a profile analyzer (by fine tuning which functions are enabled)</li>
<li>feed a code coverage analyzer ( " )</li>
</ul></li>
<li>kind of validator on function calling :
<ul>
<li>implement programming by contracts :
<ul>
<li>check that parameters values of "f" function will not have an unexpected value or be of an unexpected type</li>
<li>this allow to increase code robustness by narrowing </li>
</ul></li>
<li>implement invariants (eg. check that returned value is always in the excepted range, ...)</li>
<li>insure that a function follow specifications by meta-checking that has always predictable results
(eg. return the fixed expected value for each possible input value, ...)</li>
</ul></li>
<li>minimum modification of existing code</li>
<li><strong>in the same thinking line as the "monkey patching" concept</strong></li>
</ul>
<p>Notes on usage :</p>
<ul>
<li>recipe works on functions and any kind of methods (methods, class methods,
and static methods)</li>
<li>the usage order of "@enter_event" and "@exit_event" decorators doesn't
matter : the result will be the same</li>
</ul>
<ul>
<li><em>PLEASE VOTE FOR THIS RECIPE if you like it !</em></li>
</ul>
Win Shortcuts wrapper. (Python)
2010-07-23T08:10:44-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/577321-win-shortcuts-wrapper/
<p style="color: grey">
Python
recipe 577321
by <a href="/recipes/users/4035877/">Louis RIVIERE</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 3.
</p>
<p>Reads or Creates Windows shortcuts (links).</p>
Forward iterator (Python)
2015-01-29T17:12:10-08:00Andrea Corbellinihttp://code.activestate.com/recipes/users/4186880/http://code.activestate.com/recipes/578560-forward-iterator/
<p style="color: grey">
Python
recipe 578560
by <a href="/recipes/users/4186880/">Andrea Corbellini</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 2.
</p>
<p>A proxy for iterators that lets you <em>read ahead</em> items without consuming the iterator.</p>
ChainedList and ChainedListView: Exposing Multiple Lists as a Single Sequence (Python)
2012-07-03T21:00:02-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578190-chainedlist-and-chainedlistview-exposing-multiple-/
<p style="color: grey">
Python
recipe 578190
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
</p>
<p>Handy for composing lists without losing the underlying indepedence. The "lists" don't actually have to be lists, though for ChainedList they must be mutable.</p>