Popular Python recipes tagged "meta:requires=array"http://code.activestate.com/recipes/langs/python/tags/meta:requires=array/2017-07-22T15:52:01-07:00ActiveState Code RecipesMandelbrot Set made simple (Python)
2015-12-28T17:56:08-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/579143-mandelbrot-set-made-simple/
<p style="color: grey">
Python
recipe 579143
by <a href="/recipes/users/4076953/">Jack Trainor</a>
(<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/mandelbrot/">mandelbrot</a>, <a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 2.
</p>
<p>This is a mini-framework for exploring the Mandelbrot Set.</p>
<p>It outputs to a Tkinter window or to a PNG file (Pillow package required). It includes a disk caching mechanism for saving and later retrieving Mandelbrot calculations to avoid recalculating the Set on each run.</p>
<p>See module documentation for further information. Compatible with Python 2 and 3.</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>
objectsignature - debugging utility, will work with containers, classes, nested classes ... almost anything (Python)
2012-02-15T23:05:44-08:00Boris Katshttp://code.activestate.com/recipes/users/4180778/http://code.activestate.com/recipes/578039-objectsignature-debugging-utility-will-work-with-c/
<p style="color: grey">
Python
recipe 578039
by <a href="/recipes/users/4180778/">Boris Kats</a>
(<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/objectsignature/">objectsignature</a>, <a href="/recipes/tags/objectview/">objectview</a>).
Revision 3.
</p>
<p>Small debug utility will help to detect unwonted changes of user's class during program execution.</p>
Bloom Filter (Python)
2011-05-05T07:25:01-07:00Sundar Srinivasanhttp://code.activestate.com/recipes/users/4177884/http://code.activestate.com/recipes/577686-bloom-filter/
<p style="color: grey">
Python
recipe 577686
by <a href="/recipes/users/4177884/">Sundar Srinivasan</a>
(<a href="/recipes/tags/big_table/">big_table</a>, <a href="/recipes/tags/bloom_filter/">bloom_filter</a>, <a href="/recipes/tags/sets/">sets</a>, <a href="/recipes/tags/spelling_checker/">spelling_checker</a>).
Revision 2.
</p>
<p>Space efficient, probabilistic set membership tester. Has no False Negatives but allows a rare False Positive.</p>
A script evaluating PSNR metric of two YUV420 frames (Python)
2011-04-08T07:26:45-07:00Denis Gorodetskiyhttp://code.activestate.com/recipes/users/4177587/http://code.activestate.com/recipes/577645-a-script-evaluating-psnr-metric-of-two-yuv420-fram/
<p style="color: grey">
Python
recipe 577645
by <a href="/recipes/users/4177587/">Denis Gorodetskiy</a>
(<a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/difference/">difference</a>, <a href="/recipes/tags/frame/">frame</a>, <a href="/recipes/tags/metric/">metric</a>, <a href="/recipes/tags/psnr/">psnr</a>, <a href="/recipes/tags/video/">video</a>, <a href="/recipes/tags/yuv/">yuv</a>).
Revision 2.
</p>
<p>A script evaluating PSNR metric of two YUV420 frames
<strong>usage: psnr.py filename1.yuv filename2.yuv frame_width frame_height</strong></p>
<p>Alternatively if filename1 contains width and height in the form <strong>file-1200x1600.yuv</strong>,
the script will extract width and height of a frame from the filename. Then usage even simplier:
<strong>psnr.py filename1-1200x1600.yuv filename2.yuv</strong></p>
Simple numeric database (Python)
2011-05-16T08:41:46-07:00Mike Sweeneyhttp://code.activestate.com/recipes/users/4177990/http://code.activestate.com/recipes/577697-simple-numeric-database/
<p style="color: grey">
Python
recipe 577697
by <a href="/recipes/users/4177990/">Mike Sweeney</a>
(<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/numeric/">numeric</a>).
Revision 2.
</p>
<p>A simple in-memory numeric database for Python. Arrays are used to minimise memory consumption.</p>
Simple numeric database (Python)
2011-05-16T12:11:24-07:00Hamidreza Joshaghanihttp://code.activestate.com/recipes/users/4177804/http://code.activestate.com/recipes/577698-simple-numeric-database/
<p style="color: grey">
Python
recipe 577698
by <a href="/recipes/users/4177804/">Hamidreza Joshaghani</a>
(<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/numeric/">numeric</a>).
</p>
<p>A simple in-memory numeric database for Python. Arrays are used to minimise memory consumption.</p>
Search nth smallest element in really big file (Python)
2010-11-30T17:38:38-08:00Teodor Kichatovhttp://code.activestate.com/recipes/users/4176095/http://code.activestate.com/recipes/577478-search-nth-smallest-element-in-really-big-file/
<p style="color: grey">
Python
recipe 577478
by <a href="/recipes/users/4176095/">Teodor Kichatov</a>
(<a href="/recipes/tags/big/">big</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/kth/">kth</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/selection/">selection</a>, <a href="/recipes/tags/smallest/">smallest</a>).
</p>
<p>Search nth smallest float in really big file (more, more and more bigger than available RAM)
in a single pass through the file</p>
<p>if your file more than 150GB - you should use a more appropriate sampling params to the data
more - you can use tempfile to store data(interval) returned from func fill_interval
data:
file with only one float number per line, good shuffled</p>
Size of Python objects (revised). (Python)
2017-07-22T15:52:01-07:00Jean Brouwershttp://code.activestate.com/recipes/users/2984142/http://code.activestate.com/recipes/546530-size-of-python-objects-revised/
<p style="color: grey">
Python
recipe 546530
by <a href="/recipes/users/2984142/">Jean Brouwers</a>
(<a href="/recipes/tags/getsizeof/">getsizeof</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>).
Revision 21.
</p>
<p>This recipe determines the size of Python objects in bytes and has been further enhanced to handle ints, namedtuples, arrays and NumPy types better. Functions <em>alen</em> and <em>itemsize</em> have been updated. Support for Python 2.5 and earlier and the tests/examples have been removed. See project <a href="https://github.com/pympler/pympler">Pympler</a> for unit tests.</p>
<p>See also other, simpler recipes like this <a href="http://code.activestate.com/recipes/577504">Compute memory footprint of an object and its contents</a>.</p>
SimpleCryptSocketExt - SimpleCrypt Wrapper for Easy Socket, Client / Server Encryption (Python)
2010-05-07T15:33:06-07:00AJ. Mayorgahttp://code.activestate.com/recipes/users/4173476/http://code.activestate.com/recipes/577212-simplecryptsocketext-simplecrypt-wrapper-for-easy-/
<p style="color: grey">
Python
recipe 577212
by <a href="/recipes/users/4173476/">AJ. Mayorga</a>
(<a href="/recipes/tags/client_server/">client_server</a>, <a href="/recipes/tags/cryptography/">cryptography</a>, <a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/server/">server</a>, <a href="/recipes/tags/socket/">socket</a>).
Revision 5.
</p>
<p>Lightweight drop-in encryption wrapper for various Client/Server solutions supporting
protocols such as UDP, TCP, HTTP, HTTPS, FTP, RAW Sockets etc.</p>
PluginManager - Extending Project Functionality By Using Custom Modules/Plugins On The Fly. (Python)
2010-05-07T15:32:10-07:00AJ. Mayorgahttp://code.activestate.com/recipes/users/4173476/http://code.activestate.com/recipes/577216-pluginmanager-extending-project-functionality-by-u/
<p style="color: grey">
Python
recipe 577216
by <a href="/recipes/users/4173476/">AJ. Mayorga</a>
(<a href="/recipes/tags/extending/">extending</a>, <a href="/recipes/tags/extensible/">extensible</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/plugin/">plugin</a>).
Revision 4.
</p>
<p>This demo shows how you can create and manage your own custom plugins for extending functionality in your Python projects. There are no safety wrappers in this demo for restricting plugins aside from that fact that plugins are run as an extention of a management class which is run in its own instance only receiving data passed to it by the RumModules method, that said security should ideally be applied to the ModuleAPI class, by restricting __builtins__ on eval calls and/or validating plugin content and parameters which can be done by extending the Prepaser Class. For a great recipe/demo of restricting eval call see <a href="http://code.activestate.com/recipes/496746" rel="nofollow">http://code.activestate.com/recipes/496746</a>. </p>
<p>That aside it was alot of fun to write and use. Enjoy</p>
<p>PS: you will need <a href="http://code.activestate.com/recipes/577144/" rel="nofollow">http://code.activestate.com/recipes/577144/</a> to import Xceptions</p>
Simple Small Tweakable Encryption Solution -- SimpleCrypt (Python)
2010-04-30T21:00:26-07:00AJ. Mayorgahttp://code.activestate.com/recipes/users/4173476/http://code.activestate.com/recipes/577174-simple-small-tweakable-encryption-solution-simplec/
<p style="color: grey">
Python
recipe 577174
by <a href="/recipes/users/4173476/">AJ. Mayorga</a>
(<a href="/recipes/tags/crypt/">crypt</a>, <a href="/recipes/tags/cryptography/">cryptography</a>, <a href="/recipes/tags/encryption/">encryption</a>).
Revision 6.
</p>
<p>Encryption can sometimes be a nightmare, at least is my experience while Python has
some excellent resources for encryption I found myself a lot of times needing an
encryption solution that could port easily between VS C++, .NET, PHP, and Python
projects that would be simple to implement and not rely on large bloated
crypto libs.</p>
Purge Mysql binary logs (Python)
2010-01-12T15:14:10-08:00Umang Gopanihttp://code.activestate.com/recipes/users/4172787/http://code.activestate.com/recipes/577004-purge-mysql-binary-logs/
<p style="color: grey">
Python
recipe 577004
by <a href="/recipes/users/4172787/">Umang Gopani</a>
(<a href="/recipes/tags/automate/">automate</a>, <a href="/recipes/tags/binary_logs/">binary_logs</a>, <a href="/recipes/tags/master_slave/">master_slave</a>, <a href="/recipes/tags/mysql/">mysql</a>, <a href="/recipes/tags/purge/">purge</a>).
Revision 8.
</p>
<p>Being a MySQL DBA , one faces a common issue in replication environment -> Disk space issue on master, since the number of binary logs have increased.
Now, one of the solution to this would be using expire_logs_days parameter in your mysql config file.
But what if, the slave is lagging by few hours or if the slave is broken since few days and the binary logs are removed due to the parameter set. Whenever the salve comes up, it will go bonkers, knowing that the binary log where it last stopped no more exists.</p>
<p>I faced this issue a couple of time until I decided to automate it using a script. Herewith I am attaching a python script which can run regularly in cron.</p>
Interrogating linux /dev/usb/hiddev0 in python (Python)
2009-07-07T01:32:17-07:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/576834-interrogating-linux-devusbhiddev0-in-python/
<p style="color: grey">
Python
recipe 576834
by <a href="/recipes/users/4068698/">Dima Tisnek</a>
(<a href="/recipes/tags/hid/">hid</a>, <a href="/recipes/tags/hiddev/">hiddev</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/usb/">usb</a>).
</p>
<p>What this recipe does:</p>
<p>Maps linux usb hid ioctls and related C structs to python;
Call ioctls, make some sense of output.
Prints all reports for the device with some info.</p>
<p>Works with python 2.4 (tested python 2.4.6 on linux amd64).
Would need changes (e.g. print) for python 3.0.
Might need changes (ioctl signed/unsigned "FIX") for newer python than tested.</p>
Convert text/enriched MIME to text/html (Python)
2009-06-09T15:08:40-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/576800-convert-textenriched-mime-to-texthtml/
<p style="color: grey">
Python
recipe 576800
by <a href="/recipes/users/4076953/">Jack Trainor</a>
(<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/mime/">mime</a>, <a href="/recipes/tags/text_enriched/">text_enriched</a>).
</p>
<p>Converts text stream in text/enriched MIME format from file or stdin to text/html output to file or stdout.</p>
Huffman coding, Encoder/Deconder (Python)
2009-01-04T04:11:37-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576603-huffman-coding-encoderdeconder/
<p style="color: grey">
Python
recipe 576603
by <a href="/recipes/users/4168519/">Shao-chuan Wang</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/compression/">compression</a>, <a href="/recipes/tags/decompression/">decompression</a>, <a href="/recipes/tags/huffman_code/">huffman_code</a>).
Revision 2.
</p>
<p>Please refer to wikipedia: <a href="http://en.wikipedia.org/wiki/Huffman_coding" rel="nofollow">http://en.wikipedia.org/wiki/Huffman_coding</a></p>
<p>Huffman coding is an entropy encoding algorithm used for lossless data compression. The term refers to the use of a variable-length code table for encoding a source symbol (such as a character in a file) where the variable-length code table has been derived in a particular way based on the estimated probability of occurrence for each possible value of the source symbol. It was developed by David A. Huffman while he was a Ph.D. student at MIT, and published in the 1952 paper "A Method for the Construction of Minimum-Redundancy Codes".</p>
get names of all "up" network interfaces (linux only) (Python)
2005-08-11T13:17:36-07:00paul cannonhttp://code.activestate.com/recipes/users/2551140/http://code.activestate.com/recipes/439093-get-names-of-all-up-network-interfaces-linux-only/
<p style="color: grey">
Python
recipe 439093
by <a href="/recipes/users/2551140/">paul cannon</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>Uses the SIOCGIFCONF ioctl to obtain a list of interfaces and extracts those names, returning them in a list of strings.</p>
randomized integer range iterator (Python)
2006-01-29T12:13:35-08:00Dug Songhttp://code.activestate.com/recipes/users/2756255/http://code.activestate.com/recipes/473808-randomized-integer-range-iterator/
<p style="color: grey">
Python
recipe 473808
by <a href="/recipes/users/2756255/">Dug Song</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>iterate over all values of a range in random order.</p>
<p>related to <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/466177" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/466177</a></p>
Sets using a sorted list of integers representing the items (Python)
2005-11-22T15:43:11-08:00Anton Vredegoorhttp://code.activestate.com/recipes/users/2667360/http://code.activestate.com/recipes/452502-sets-using-a-sorted-list-of-integers-representing-/
<p style="color: grey">
Python
recipe 452502
by <a href="/recipes/users/2667360/">Anton Vredegoor</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>This is inspired by Raymond Hettingers recipe for sets, using sorted lists.
However here I'm not sorting the original list but just the indices and the sets share a common universe. This code is not for production use, I just show it in order to explain what I want to do.</p>
Memory compacted list of bits (Python)
2005-10-10T02:08:35-07:00Connelly Barneshttp://code.activestate.com/recipes/users/1859269/http://code.activestate.com/recipes/440658-memory-compacted-list-of-bits/
<p style="color: grey">
Python
recipe 440658
by <a href="/recipes/users/1859269/">Connelly Barnes</a>
.
Revision 5.
</p>
<p>A list of bits, compacted in memory so that 8 elements use 1 byte of storage.</p>