Popular recipes tagged "nested"http://code.activestate.com/recipes/tags/nested/2016-11-10T10:23:11-08:00ActiveState Code RecipesUnit Testing Nested Functions (Python)
2016-11-10T10:23:11-08:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580716-unit-testing-nested-functions/
<p style="color: grey">
Python
recipe 580716
by <a href="/recipes/users/4182236/">Alfe</a>
(<a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/unittests/">unittests</a>).
Revision 3.
</p>
<p>Python allows the declaration of nested functions. These are typically hard to unit test because using just the normal ways of calling they cannot be called from outside their surrounding function. So they cannot be considered a clearly separated unit and thus cannot be unit tested.</p>
<p>This is a drawback of using them, so many developers (especially the ones deep into test driven development who strive to have a high unit test coverage) tend to avoid them in favor for standalone functions which can be called from the unit tests without any hassle.</p>
<p>But not all solutions with nested functions can be written as elegant with standalone functions. Nested functions are powerful insofar that they can access the local variables of the surrounding function without any need to pass them into the nested function, thus the code can in many cases stay neat and tidy while using a standalone function instead might raise the need to pass the complete context in form of a bunch of parameters. Also, using nested functions makes their local usage clear to any reader and keeps the name space tight.</p>
<p>But at least in the standard CPython (i. e. not necessarily in Jython, etc.) the implementation of functions (and methods) allows to find the nested function's code, wrap it properly to give it its needed context and then call it from the outside. I wrote a small module which helps doing exactly this.</p>
wrist friendly dictionary (Python)
2013-08-18T22:54:26-07:00Ariel Keselmanhttp://code.activestate.com/recipes/users/4187559/http://code.activestate.com/recipes/578644-wrist-friendly-dictionary/
<p style="color: grey">
Python
recipe 578644
by <a href="/recipes/users/4187559/">Ariel Keselman</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/nested/">nested</a>).
</p>
<p>this dictionary allows easy manual creation of nested hierarchies, like so:</p>
<p>window.style.width=5</p>
<p>or... </p>
<p>window['background-color'].rgb= 255,255,255</p>
Nested contexts -- a chain of mapping objects (Python)
2010-10-25T02:13:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577434-nested-contexts-a-chain-of-mapping-objects/
<p style="color: grey">
Python
recipe 577434
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/chained/">chained</a>, <a href="/recipes/tags/compiler/">compiler</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/nonlocal/">nonlocal</a>, <a href="/recipes/tags/scopes/">scopes</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 2.
</p>
<p>Easy to use chain of dictionaries for crafting nested scopes or for a tree of scopes. Useful for analyzing AST nodes, XML nodes or other structures with multiple scopes. Can emulate various chaining styles including static/lexical scoping, dynamic scoping and Python's own globals(), locals(), nested scopes, and writeable nonlocals. Can also model Python's inheritance chains: instance dictionary, class dictionary, and base classes.</p>
Easily Write Nested Loops (Python)
2012-02-18T01:34:55-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578046-easily-write-nested-loops/
<p style="color: grey">
Python
recipe 578046
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/iteration/">iteration</a>, <a href="/recipes/tags/nested/">nested</a>).
</p>
<p>The "nest" generator function in this module is provided to make writing
nested loops easier to accomplish. Instead of writing a for loop at each
level, one may call "nest" with each sequence as an argument and receive
items from the sequences correctly yielded back to the caller. A test is
included at the bottom of this module to demonstrate how to use the code.</p>
Nest a flat list (Python)
2010-02-25T03:50:09-08:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/577061-nest-a-flat-list/
<p style="color: grey">
Python
recipe 577061
by <a href="/recipes/users/4173111/">Sander Evers</a>
(<a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/nest/">nest</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/unflatten/">unflatten</a>).
Revision 2.
</p>
<p>Turn a flat list into a nested list, with a specified number of lists per nesting level.</p>
Flattened List (Python)
2009-04-27T18:41:21-07:00marlonamorhttp://code.activestate.com/recipes/users/4169863/http://code.activestate.com/recipes/576719-flattened-list/
<p style="color: grey">
Python
recipe 576719
by <a href="/recipes/users/4169863/">marlonamor</a>
(<a href="/recipes/tags/flat/">flat</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/nested/">nested</a>).
Revision 2.
</p>
<p>This flattenizes any nested level list. I couln't find this in itertools module so I wrote it. Python 3 may be required.</p>