Popular recipes tagged "filter" but not "csv"http://code.activestate.com/recipes/tags/filter-csv/2016-03-16T14:45:02-07:00ActiveState Code RecipesCollection Pipeline in Python (Python) 2016-03-16T14:45:02-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/580625-collection-pipeline-in-python/ <p style="color: grey"> Python recipe 580625 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/pipline/">pipline</a>). </p> <p>A powerful functional programming technique is the use of pipelines of functions. If you have used shell scripting languages like <code>bash</code>, you will have used this technique. For instance, to count the number of files or directories, you might say: <code>ls | wc -l</code>. The output of the first command is piped into the input of the second, and the result returned.</p> <p>We can set up a similar pipeline using Lisp-like Map, Filter and Reduce special functions. Unlike the standard Python <code>map</code>, <code>filter</code> and <code>reduce</code>, these are designed to operate in a pipeline, using the same <code>|</code> syntax used by bash and other shell scripting languages:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; data = range(1000) &gt;&gt;&gt; data | Filter(lambda n: 20 &lt; n &lt; 30) | Map(float) | List [21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0] </code></pre> <p>The standard Python functional tools is much less attractive, as you have to write the functions in the opposite order to how they are applied to the data. This makes it harder to follow the logic of the expression.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(map(float, filter(lambda n: 20 &lt; n &lt; 30, data))) [21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0] </code></pre> <p>We can also end the pipeline with a call to <code>Reduce</code> to collate the sequence into a single value. Here we take a string, extract all the digits, convert to ints, and multiply:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; from operator import mul &gt;&gt;&gt; "abcd12345xyz" | Filter(str.isdigit) | Map(int) | Reduce(mul) 120 </code></pre> Setting up a listbox filter in Tkinter(python 2.7) (Python) 2014-04-10T10:25:38-07:00Wyklephhttp://code.activestate.com/recipes/users/4189735/http://code.activestate.com/recipes/578860-setting-up-a-listbox-filter-in-tkinterpython-27/ <p style="color: grey"> Python recipe 578860 by <a href="/recipes/users/4189735/">Wykleph</a> (<a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/listbox/">listbox</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/searchbar/">searchbar</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 3. </p> <p>If you have a listbox and need to filter the contents of the listbox based on the contents of an entry field, this will help you! Shoutout to Nikola-K for the revision. It's much simpler now :D</p> Create and apply filters to lists of file paths (Python) 2012-03-20T21:17:26-07:00eysihttp://code.activestate.com/recipes/users/4177096/http://code.activestate.com/recipes/578084-create-and-apply-filters-to-lists-of-file-paths/ <p style="color: grey"> Python recipe 578084 by <a href="/recipes/users/4177096/">eysi</a> (<a href="/recipes/tags/blacklist/">blacklist</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/walk/">walk</a>, <a href="/recipes/tags/whitelist/">whitelist</a>, <a href="/recipes/tags/yield/">yield</a>). Revision 2. </p> <p>An experiment with filtering lists of files, documentation is written directly into the code.</p> Python Template Engine (Python) 2012-03-31T21:28:12-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/578090-python-template-engine/ <p style="color: grey"> Python recipe 578090 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/engine/">engine</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/template/">template</a>). </p> <p>This is a simple template engine which allows you to replace macros within text. This engine allows for attributes and filters. The default implementation provides the entire string module as filters. Trying to use arguments will of course not work (since the framework supports no other arguments for the filter other than the filtered string itself).</p> Enhancing dir() with globs (Python) 2011-07-01T04:03:46-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577774-enhancing-dir-with-globs/ <p style="color: grey"> Python recipe 577774 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/dir/">dir</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/glob/">glob</a>, <a href="/recipes/tags/globbing/">globbing</a>). </p> <p>dir() is useful for interactively exploring the attributes and methods of objects at the command line. But sometimes dir() returns a lot of information:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; len(dir(decimal.Decimal)) # too much information! 137 </code></pre> <p>It can be helpful to filter the list of names returned. This enhanced version of dir does exactly that, using simple string globbing:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; dir(decimal.Decimal, '*log*') # just attributes with "log" in the name ['_fill_logical', '_islogical', '_log10_exp_bound', 'log10', 'logb', 'logical_and', 'logical_invert', 'logical_or', 'logical_xor'] </code></pre> compare(), making filter() fun again (Python) 2008-10-04T12:40:42-07:00Andreas Nilssonhttp://code.activestate.com/recipes/users/4167183/http://code.activestate.com/recipes/576526-compare-making-filter-fun-again/ <p style="color: grey"> Python recipe 576526 by <a href="/recipes/users/4167183/">Andreas Nilsson</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/lambda/">lambda</a>). Revision 2. </p> <p>compare() takes a function parameter and returns a callable comparator when compared to a value. When called, the comparator returns result of comparing the result of calling the function and the value it was created with.</p> <p>Basic usage:</p> <pre class="prettyprint"><code>items = [1, 2, 3, 4, 5] def double(x): return x * 2 for i in filter(compare(double) &gt; 5, items): print i #Prints 3, 4 and 5 </code></pre> filtered interpolation (Python) 2008-07-29T14:17:02-07:00Pádraig Bradyhttp://code.activestate.com/recipes/users/1890175/http://code.activestate.com/recipes/576389-filtered-interpolation/ <p style="color: grey"> Python recipe 576389 by <a href="/recipes/users/1890175/">Pádraig Brady</a> (<a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/interpolation/">interpolation</a>). </p> <p>I find the following 2 functions useful when programming, when browsing objects interactively like one would do with dir.</p> <p>Importing this module allows one to restrict the output from dir to what's required. Note also the standard pprint module may be useful also.</p>