Popular Python recipes tagged "meta:requires=collections"http://code.activestate.com/recipes/langs/python/tags/meta:requires=collections/2016-09-18T20:39:20-07:00ActiveState Code RecipesEqually-spaced numbers (linspace) (Python)
2015-01-12T22:16:37-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/579000-equally-spaced-numbers-linspace/
<p style="color: grey">
Python
recipe 579000
by <a href="/recipes/users/4184316/">Andrew Barnert</a>
(<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/linspace/">linspace</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/spread/">spread</a>).
</p>
<p>An equivalent of <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html"><code>numpy.linspace</code></a>, but as a pure-Python lazy sequence.</p>
<p>Like NumPy's <code>linspace</code>, but unlike the <a href="http://code.activestate.com/recipes/577068/"><code>spread</code></a> and <a href="http://code.activestate.com/recipes/577068/"><code>frange</code></a> recipes listed here, the <code>num</code> argument specifies the number of values, not the number of intervals, and the range is closed, not half-open.</p>
<p>Although this is primarily designed for floats, it will work for <code>Fraction</code>, <code>Decimal</code>, NumPy arrays (although this would be silly) and even <code>datetime</code> values.</p>
<p>This recipe can also serve as an example for creating lazy sequences.</p>
<p>See the discussion below for caveats.</p>
Compute Memory footprint of an object and its contents (Python)
2012-11-23T23:57:31-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577504-compute-memory-footprint-of-an-object-and-its-cont/
<p style="color: grey">
Python
recipe 577504
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>).
Revision 3.
</p>
<p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p>
DBF reader and writer -- selective fields and nullreplace (Python)
2016-09-18T20:39:20-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/580696-dbf-reader-and-writer-selective-fields-and-nullrep/
<p style="color: grey">
Python
recipe 580696
by <a href="/recipes/users/4189558/">Tomas Nordin</a>
(<a href="/recipes/tags/database/">database</a>).
</p>
<p>This fork assumes a desire for limited selection of field names. With
huge files this might be necessary on some machines.</p>
<p>Also, assuming that the meaning of null in a dbf file means zero might
be a mistake, so the fork adds an argument nullreplace as way to
choose what to replace null with. Null is sometimes used to mean
missing value. This change is decoupled from the selective names
feature.</p>
JSON Formatted Logging (Python)
2016-05-27T01:07:42-07:00Michael Blan Palmerhttp://code.activestate.com/recipes/users/4194130/http://code.activestate.com/recipes/580667-json-formatted-logging/
<p style="color: grey">
Python
recipe 580667
by <a href="/recipes/users/4194130/">Michael Blan Palmer</a>
(<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>I have created a package that outputs JSON formatted lines to a log file. It can make use of the standard logging parameters and/or take custom input. The use of JSON in the log file allows for easy filtering and processing.</p>
Configurable JSON Extensions for Python (Python)
2016-05-22T19:00:54-07:00Michael Blan Palmerhttp://code.activestate.com/recipes/users/4194130/http://code.activestate.com/recipes/580664-configurable-json-extensions-for-python/
<p style="color: grey">
Python
recipe 580664
by <a href="/recipes/users/4194130/">Michael Blan Palmer</a>
(<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>The concept behind the package is to build a single class per type you want to add to json in Python. The new class will have a method for encoding to json and a method for decoding from json. The classes are then loaded into an encoder object and a decoder object that are hooked into the standard json loads
and dumps functions.</p>
LRU dictionary (Python)
2016-04-17T01:22:01-07:00Felixhttp://code.activestate.com/recipes/users/4193957/http://code.activestate.com/recipes/580644-lru-dictionary/
<p style="color: grey">
Python
recipe 580644
by <a href="/recipes/users/4193957/">Felix</a>
(<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/expiration/">expiration</a>, <a href="/recipes/tags/expiring/">expiring</a>, <a href="/recipes/tags/lru/">lru</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>, <a href="/recipes/tags/python3/">python3</a>).
</p>
<p>For most cases where you want to store a limited amount of data, functools.partial is sufficient. However, if you want or need more control over your data, especially specifying an expiration date for your entries, this can come in handy.</p>
<p>Read the docstrings for implementation details.</p>
LRU dictionary (Python)
2016-04-17T01:22:51-07:00Felixhttp://code.activestate.com/recipes/users/4193957/http://code.activestate.com/recipes/580645-lru-dictionary/
<p style="color: grey">
Python
recipe 580645
by <a href="/recipes/users/4193957/">Felix</a>
(<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/expires/">expires</a>, <a href="/recipes/tags/expiring/">expiring</a>, <a href="/recipes/tags/lru/">lru</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>).
</p>
<p>For most cases where you want to store a limited amount of data, functools.partial is sufficient. However, if you want or need more control over your data, especially specifying an expiration date for your entries, this can come in handy.</p>
<p>Read the docstrings for implementation details.</p>
How to use super() effectively -- Python 3.2 version (Python)
2015-12-01T16:23:09-08:00Lance E Sloanhttp://code.activestate.com/recipes/users/4188012/http://code.activestate.com/recipes/579130-how-to-use-super-effectively-python-32-version/
<p style="color: grey">
Python
recipe 579130
by <a href="/recipes/users/4188012/">Lance E Sloan</a>
(<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>).
</p>
<p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p>
How to use super() effectively -- Python 2.7 version (Python)
2015-12-01T16:20:26-08:00Lance E Sloanhttp://code.activestate.com/recipes/users/4188012/http://code.activestate.com/recipes/579129-how-to-use-super-effectively-python-27-version/
<p style="color: grey">
Python
recipe 579129
by <a href="/recipes/users/4188012/">Lance E Sloan</a>
(<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>).
</p>
<p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p>
Simple FIFO trading model for pnl (Python)
2015-02-10T13:34:48-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/579024-simple-fifo-trading-model-for-pnl/
<p style="color: grey">
Python
recipe 579024
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/pnl/">pnl</a>, <a href="/recipes/tags/trading/">trading</a>).
Revision 2.
</p>
<p>Simple approach to calculating FIFO pnl.</p>
Publish SQLite data to PDF using named tuples (Python)
2015-02-24T22:08:11-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579027-publish-sqlite-data-to-pdf-using-named-tuples/
<p style="color: grey">
Python
recipe 579027
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/sql/">sql</a>, <a href="/recipes/tags/sqlite/">sqlite</a>, <a href="/recipes/tags/sqlite3/">sqlite3</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>).
</p>
<p>This recipe shows how to publish SQLite data to PDF, using named tuples from the collections module of Python, the sqlite3 library, and the xtopdf library for PDF generation.</p>
Hivemind (Python)
2015-07-15T12:37:13-07:00Oscar Byrnehttp://code.activestate.com/recipes/users/4192487/http://code.activestate.com/recipes/579082-hivemind/
<p style="color: grey">
Python
recipe 579082
by <a href="/recipes/users/4192487/">Oscar Byrne</a>
(<a href="/recipes/tags/borg/">borg</a>, <a href="/recipes/tags/defaultdict/">defaultdict</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/hashing/">hashing</a>, <a href="/recipes/tags/hivemind/">hivemind</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/singleton/">singleton</a>, <a href="/recipes/tags/state/">state</a>).
</p>
<p>Inspired by the ever-popular Borg pattern, objects inheriting from Hivemind share state if initialised with the same arguments</p>
OrderedSet (Python)
2015-06-25T17:33:13-07:00Sanderhttp://code.activestate.com/recipes/users/4192426/http://code.activestate.com/recipes/579071-orderedset/
<p style="color: grey">
Python
recipe 579071
by <a href="/recipes/users/4192426/">Sander</a>
(<a href="/recipes/tags/ordered/">ordered</a>, <a href="/recipes/tags/set/">set</a>).
</p>
<p>Set that remembers original insertion order.</p>
OrderedSet (Python)
2012-12-19T07:12:32-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576694-orderedset/
<p style="color: grey">
Python
recipe 576694
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/ordered/">ordered</a>, <a href="/recipes/tags/set/">set</a>).
Revision 9.
</p>
<p>Set that remembers original insertion order.</p>
Monitor Progress of File Descriptors of Another Process (Python)
2014-05-30T01:10:53-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/578882-monitor-progress-of-file-descriptors-of-another-pr/
<p style="color: grey">
Python
recipe 578882
by <a href="/recipes/users/4182236/">Alfe</a>
(<a href="/recipes/tags/file_descriptor/">file_descriptor</a>, <a href="/recipes/tags/monitor/">monitor</a>, <a href="/recipes/tags/prediction/">prediction</a>, <a href="/recipes/tags/proc/">proc</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/watch/">watch</a>).
</p>
<p>This tool (inspired by azat@stackoverflow, see <a href="http://stackoverflow.com/a/16082562/1281485" rel="nofollow">http://stackoverflow.com/a/16082562/1281485</a>) allows to watch the progress of the file descriptors of another process. This can be used, for example, if you transfer a file to another host and the transferring program does not show any progress indication itself. Instead of waiting blindly until the routine is done, with this tool you can use Linux's proc file system to monitor the progress of the other process while it walks through the file.</p>
<p>The tool continuously monitors the position in and the size of the files the given process's file descriptors point to. For growing (or shrinking, but that's very unusual) files, a time when it was (or will be) empty is computed ("emptyTime"), for moving file descriptors (the typical case), the time when it started at position 0 ("startTime"), the time when it will reach the current size of the file ("reachTime") and when it will meet with the end of a growing (or shrinking) file is computed ("meetTime").</p>
<p>For fixed-size files the meetTime will be the same as the reachTime of course. The meetTime only makes sense in case a file is growing and at the same time read (e. g. when a movie is downloaded to a file by one process and converted by a different process; using this tool can tell you when the converter process might run dry on the input because the download wasn't fast enough, and in this case you maybe can pause the converter to prevent this situation).</p>
<p>The tool is designed as a library; the display of the information is independent from the gathering of the data. Please feel free to create more fancy displays, add percentage output etc.</p>
Proof-of-concept for a more space-efficient, faster-looping dictionary (Python)
2013-01-17T09:28:24-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578375-proof-of-concept-for-a-more-space-efficient-faster/
<p style="color: grey">
Python
recipe 578375
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/compact/">compact</a>, <a href="/recipes/tags/dictionary/">dictionary</a>).
Revision 20.
</p>
<p>Save space and improve iteration speed by moving the hash/key/value entries to a densely packed array keeping only a sparse array of indices. This eliminates wasted space without requiring any algorithmic changes.</p>
Create on-the-fly class adapters with functools.partial (Python)
2014-05-29T18:48:28-07:00Christoph Schuelerhttp://code.activestate.com/recipes/users/4174094/http://code.activestate.com/recipes/578884-create-on-the-fly-class-adapters-with-functoolspar/
<p style="color: grey">
Python
recipe 578884
by <a href="/recipes/users/4174094/">Christoph Schueler</a>
(<a href="/recipes/tags/adapter/">adapter</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/partial/">partial</a>, <a href="/recipes/tags/pattern/">pattern</a>).
</p>
<p>functools.partial could not only applied to functions it also works with classes.
This opens some interesting perspectives, like on-the-fly creation of class
adapters, as the following code illustrates.</p>
Compute Memory footprint of an object and its contents (Python)
2014-05-15T13:44:15-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578880-compute-memory-footprint-of-an-object-and-its-cont/
<p style="color: grey">
Python
recipe 578880
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>).
</p>
<p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p>
How to use super() effectively -- Python 2.7 version (Python)
2011-06-01T19:19:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577721-how-to-use-super-effectively-python-27-version/
<p style="color: grey">
Python
recipe 577721
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>).
Revision 8.
</p>
<p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p>
Py2.6+ and Py3.0+ backport of Python 3.3's LRU Cache (Python)
2013-03-06T05:38:15-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/
<p style="color: grey">
Python
recipe 578078
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>).
Revision 6.
</p>
<p>Full-featured O(1) LRU cache backported from Python3.3. The full Py3.3 API is supported (thread safety, maxsize, keyword args, type checking, __wrapped__, and cache_info). Includes Py3.3 optimizations for better memory utilization, fewer dependencies, and fewer dict lookups.</p>