Popular Python recipes tagged "meta:requires=types"http://code.activestate.com/recipes/langs/python/tags/meta:requires=types/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>
Python 2 nonlocal (Python)
2015-09-08T01:27:04-07:00Ryan Gonzalezhttp://code.activestate.com/recipes/users/4187447/http://code.activestate.com/recipes/578965-python-2-nonlocal/
<p style="color: grey">
Python
recipe 578965
by <a href="/recipes/users/4187447/">Ryan Gonzalez</a>
(<a href="/recipes/tags/backport/">backport</a>, <a href="/recipes/tags/nonlocal/">nonlocal</a>, <a href="/recipes/tags/python2/">python2</a>).
Revision 2.
</p>
<p>This implements nonlocal in Python 2...albeit in a slightly ugly way. Tested with CPython 2.7 and PyPy.</p>
Analog Clock (Python)
2014-05-14T20:53:39-07:00Paulo Cavalcantihttp://code.activestate.com/recipes/users/4189949/http://code.activestate.com/recipes/578875-analog-clock/
<p style="color: grey">
Python
recipe 578875
by <a href="/recipes/users/4189949/">Paulo Cavalcanti</a>
(<a href="/recipes/tags/clock/">clock</a>).
Revision 3.
</p>
<p>A python-tkinter analog clock</p>
where() function for generator expressions (Python 3) (Python)
2014-10-05T04:10:21-07:00Alan Cristhian Ruizhttp://code.activestate.com/recipes/users/4186199/http://code.activestate.com/recipes/578946-where-function-for-generator-expressions-python-3/
<p style="color: grey">
Python
recipe 578946
by <a href="/recipes/users/4186199/">Alan Cristhian Ruiz</a>
(<a href="/recipes/tags/generators/">generators</a>).
Revision 3.
</p>
<p>Function that work like an "where statement" for generator expression. The code below</p>
<pre class="prettyprint"><code>x, y, z = 1, 2, 3
((x, y, z) for _ in range(5))
</code></pre>
<p>Is equivalent to:</p>
<pre class="prettyprint"><code>((x, y, z) for _ in range(5)) < where(x=1, y=2, z=3)
</code></pre>
Pyqt / Pyside: thread-safe callbacks + main loop integration (Python)
2013-10-12T08:43:09-07:00Justin Israelhttp://code.activestate.com/recipes/users/4187487/http://code.activestate.com/recipes/578634-pyqt-pyside-thread-safe-callbacks-main-loop-integr/
<p style="color: grey">
Python
recipe 578634
by <a href="/recipes/users/4187487/">Justin Israel</a>
(<a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/pyqt/">pyqt</a>, <a href="/recipes/tags/pyside/">pyside</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/thread/">thread</a>).
Revision 4.
</p>
<p>A mechanism for communication from any thread to the main thread.</p>
<p>It uses the same custom Event, but adds a classmethod convenience for taking a callback and params, wrapping it into an event and posting it to the receiver. Also adds in support for weak method references to the callback, in case the source object gets deleted before its callback is actually called.</p>
<p>Merges forks of both:
<a href="http://code.activestate.com/recipes/81253/#c5" rel="nofollow">http://code.activestate.com/recipes/81253/#c5</a>
<a href="http://code.activestate.com/recipes/578299-pyqt-pyside-thread-safe-global-queue-main-loop-int/" rel="nofollow">http://code.activestate.com/recipes/578299-pyqt-pyside-thread-safe-global-queue-main-loop-int/</a></p>
lndir.py (short python version of the BSD/X11 lndir utility) (Python)
2013-05-28T07:21:28-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578535-lndirpy-short-python-version-of-the-bsdx11-lndir-u/
<p style="color: grey">
Python
recipe 578535
by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a>
(<a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/os_walk/">os_walk</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/walk/">walk</a>).
Revision 2.
</p>
<p>This should be valid for Python 2.6 and up, including the 3.x series.</p>
car.py (Ctypes ARchiver in PYthon [libarchive ffi wrapper]) (Python)
2013-05-28T04:02:15-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578531-carpy-ctypes-archiver-in-python-libarchive-ffi-wra/
<p style="color: grey">
Python
recipe 578531
by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a>
(<a href="/recipes/tags/bzip2/">bzip2</a>, <a href="/recipes/tags/cpio/">cpio</a>, <a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/ffi/">ffi</a>, <a href="/recipes/tags/gzip/">gzip</a>, <a href="/recipes/tags/lzma/">lzma</a>, <a href="/recipes/tags/pax/">pax</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tar/">tar</a>).
Revision 3.
</p>
<p>CTypes libarchive wrapper, handles extraction only with no arguments.</p>
slurp.py (Regex based simple parsing engine) (Python)
2013-05-26T18:00:58-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578532-slurppy-regex-based-simple-parsing-engine/
<p style="color: grey">
Python
recipe 578532
by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a>
(<a href="/recipes/tags/parser/">parser</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/text_processing/">text_processing</a>).
</p>
<p>A parsing engine that allows you to define sets of patterns and callbacks, and process any I/O object in Pyton that has a readline() method.</p>
Analog stopclock (Python)
2013-03-15T18:30:37-07:00prashanth.chengihttp://code.activestate.com/recipes/users/4185699/http://code.activestate.com/recipes/578493-analog-stopclock/
<p style="color: grey">
Python
recipe 578493
by <a href="/recipes/users/4185699/">prashanth.chengi</a>
(<a href="/recipes/tags/analog/">analog</a>, <a href="/recipes/tags/chess/">chess</a>, <a href="/recipes/tags/clock/">clock</a>).
</p>
<p>A very simple tkinter analog stopclock</p>
Using ChainMap for embedded namespaces (Python)
2012-10-03T18:12:50-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578279-using-chainmap-for-embedded-namespaces/
<p style="color: grey">
Python
recipe 578279
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/chainmap/">chainmap</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/namespace/">namespace</a>).
</p>
<p>The Zen of Python tells us:</p>
<p><em>Namespaces are one honking great idea -- let's do more of those!</em></p>
<p>Python already has an excellent namespace type, the module, but the problem with modules is that they have to live in a separate file, and sometimes you want the convenience of a single file while still encapsulating your code into namespaces. That's where classes are the usual solution, but classes need to be instantiated and methods need to be defined with a <code>self</code> parameter.</p>
<p>C++ has "namespaces" for encapsulating related objects and dividing the global scope into sub-scopes. Can we do the same in Python?</p>
<p>With a bit of metaclass trickery and the new ChainMap type from Python 3.3, we can!</p>
Bunch class created from attributes in class (Python)
2011-12-31T18:03:02-08:00Fabio Zadroznyhttp://code.activestate.com/recipes/users/4180406/http://code.activestate.com/recipes/577999-bunch-class-created-from-attributes-in-class/
<p style="color: grey">
Python
recipe 577999
by <a href="/recipes/users/4180406/">Fabio Zadrozny</a>
(<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/bunch/">bunch</a>, <a href="/recipes/tags/metaclass/">metaclass</a>).
Revision 3.
</p>
<p>Provide a way to construct class __init__, __slots__, __eq__, __ne__, __repr__ for the class and makes explicit which attributes each instance will have (and providing defaults).</p>
<p>The __main__ session shows an example of how it should be used.</p>
Rudimentary Database Engine (Python)
2013-09-02T01:51:53-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577825-rudimentary-database-engine/
<p style="color: grey">
Python
recipe 577825
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/engine/">engine</a>, <a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/experiment/">experiment</a>).
Revision 2.
</p>
<p>This module was written for self-academic purposes in an attempt to understand databases better. Just as in college where students are required to reinvent "wheels" like linked lists, trees, binary searches, et cetera, developing this program helped me understand some of the things that goes on behind the scenes in a database while also helping to learn what the desired output of each operation should be. The code in this module provides support for running a simple database engine that runs completely in memory and allows usage of various concepts available in a structured query language to get and set data that may be saved to file.</p>
Apply decorators to all functions in a module (Python)
2011-06-09T22:51:28-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577742-apply-decorators-to-all-functions-in-a-module/
<p style="color: grey">
Python
recipe 577742
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/modules/">modules</a>).
Revision 2.
</p>
<p>Use my modulehacker recipe (<a href="http://code.activestate.com/recipes/577740/">recipe 577740</a>) to apply decorators to any number of modules, or even (nearly) all of them.</p>
Cache decorator in python 2.4 (Python)
2011-08-08T00:10:51-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577827-cache-decorator-in-python-24/
<p style="color: grey">
Python
recipe 577827
by <a href="/recipes/users/4178890/">sivarama sarma</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>The latest version of Python introduced a new language feature, function and method decorators (PEP 318, <a href="http://www.python.org/peps/pep-0318.html" rel="nofollow">http://www.python.org/peps/pep-0318.html</a>). This recipe show a common callable transformation that can benefit from the new syntax, often referred to as Memoization pattern.</p>
to change instance to immutable (Python)
2011-08-08T05:52:18-07:00KATO Kanryuhttp://code.activestate.com/recipes/users/4178894/http://code.activestate.com/recipes/577831-to-change-instance-to-immutable/
<p style="color: grey">
Python
recipe 577831
by <a href="/recipes/users/4178894/">KATO Kanryu</a>
(<a href="/recipes/tags/immutable/">immutable</a>, <a href="/recipes/tags/instance/">instance</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>make a class instance to immutable one.
we can call fields, methods, and properties.
This airms to make easier to develop multi thread object-oriented programming.</p>
Trace decorator for debugging (Python)
2011-01-24T18:40:51-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577551-trace-decorator-for-debugging/
<p style="color: grey">
Python
recipe 577551
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/trace/">trace</a>).
Revision 2.
</p>
<p>This package provides a decorator for tracing function and method calls in your applications. The tracing capabilities are managed through the logging package, and several mechanisms are provided for controlling the destination of the trace output.</p>
<p>It also provides functionality for adding decorators to existing classes or modules.</p>
Poor Man unit tests (Python)
2011-01-08T18:57:18-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577538-poor-man-unit-tests/
<p style="color: grey">
Python
recipe 577538
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/assertions/">assertions</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/unittests/">unittests</a>).
</p>
<p>When building unit tests for modules many times using PyUnit feels like overkill. This is a simple implementation for testing single file modules.</p>
Better quote module for bash shells (Python)
2010-12-03T09:16:45-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577483-better-quote-module-for-bash-shells/
<p style="color: grey">
Python
recipe 577483
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/escape/">escape</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/quote/">quote</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/shell/">shell</a>).
</p>
<p>This Python module quotes a Python string so that it will be treated as a single argument to commands ran via os.system() (assuming bash is the underlying shell). In other words, this module makes arbitrary strings "command line safe" (for bash command lines anyway, YMMV if you're using Windows or one of the (less fine) posix shells).</p>
Easy property creation and control (Python)
2010-12-01T17:22:49-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577482-easy-property-creation-and-control/
<p style="color: grey">
Python
recipe 577482
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/property/">property</a>, <a href="/recipes/tags/property_creation/">property_creation</a>, <a href="/recipes/tags/tools/">tools</a>, <a href="/recipes/tags/unittests/">unittests</a>).
</p>
<p>The Property class provides basic functionality that allows class level control over how a particular attribute is managed. In its simplest form a Property attribute works exactly like a regular attribute on an instance while providing documentation details about the attribute accessible via the declaring class.</p>
Named Sequences for environments containing large numbers of POD instances (Python)
2010-11-27T13:55:18-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577473-named-sequences-for-environments-containing-large-/
<p style="color: grey">
Python
recipe 577473
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/slot/">slot</a>, <a href="/recipes/tags/unittests/">unittests</a>).
</p>
<p>Generate classes with named data attributes that can be sequenced.
Useful for POD classes for which many records will exist concurrently.</p>
<p>Compare the feature set to NamedTuples by Raymond Hettinger:
<a href="http://code.activestate.com/recipes/500261-named-tuples/" rel="nofollow">http://code.activestate.com/recipes/500261-named-tuples/</a></p>