Popular recipes tagged "meta:requires=cpickle"http://code.activestate.com/recipes/tags/meta:requires=cpickle/2014-08-02T02:56:12-07:00ActiveState Code RecipesYet Another NamedTuple (Python)
2014-08-02T02:56:12-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578918-yet-another-namedtuple/
<p style="color: grey">
Python
recipe 578918
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>Yet another look at Raymond Hettinger's excellent "namedtuple" factory. Unlike Raymond's version, this one minimizes the use of exec. In the original, the entire inner class is dynamically generated then exec'ed, leading to the bulk of the code being inside a giant string template. This version uses a regular inner class for everything except the __new__ method.</p>
Easy Bit Arrays using Long Integers (Python)
2013-12-01T02:21:14-08:00Mike Sweeneyhttp://code.activestate.com/recipes/users/4177990/http://code.activestate.com/recipes/578777-easy-bit-arrays-using-long-integers/
<p style="color: grey">
Python
recipe 578777
by <a href="/recipes/users/4177990/">Mike Sweeney</a>
(<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/bit/">bit</a>, <a href="/recipes/tags/bitwise/">bitwise</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/long/">long</a>).
</p>
<p>Some simple techniques for working with long integers as bit arrays. Python support for long integers allows fast bitwise operations on large bit arrays. Bitwise operations on 100 million bit arrays happen in the blink of an eye. They are also fast and compact when saving and loading to disk. Unfortunately bit set, get, append, pop etc are slow so another bit array technique may be preferred.</p>
Treat the Win32 Registry like a Python dict -- pure python (Python)
2012-10-20T13:54:02-07:00Grizzly Nyohttp://code.activestate.com/recipes/users/4183996/http://code.activestate.com/recipes/578296-treat-the-win32-registry-like-a-python-dict-pure-p/
<p style="color: grey">
Python
recipe 578296
by <a href="/recipes/users/4183996/">Grizzly Nyo</a>
(<a href="/recipes/tags/registery/">registery</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/win32/">win32</a>).
</p>
<p>This class wraps most of the win32api functions for accessing a registry. It will read and write all win32 registry types, and will de/serialize python objects to registry keys when a string or integer representation is not possible.</p>
<p>This is an update of <a href="http://code.activestate.com/recipes/573466/">recipe 573466</a>, which is an update of 551761, which is in turn an update of 174627, folding in the enhancements listed in the discussion there to allow registry value types to be read and written within the dictionary metaphor if required. It doesn't change how it worked before, it adds a new capability, and shouldn't break existing code using the 551761 version.</p>
<p>Altered to use _winreg to avoid dependency on PyWin32 (not part of Python).</p>
CGI Image Viewer (Python)
2012-07-02T22:52:32-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578186-cgi-image-viewer/
<p style="color: grey">
Python
recipe 578186
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/old/">old</a>).
</p>
<p>If you have ever started a program and never finished it, then you may recognize the code below for what it is: an experiment begun but not completed, meant to allow viewing pictures on a server. It is included here as an exercise for the reader to complete and is committed for archival to be run under Python 2.5 or later versions.</p>
Page Counter (Python)
2012-07-02T22:41:43-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578184-page-counter/
<p style="color: grey">
Python
recipe 578184
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/achive/">achive</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/counter/">counter</a>, <a href="/recipes/tags/old/">old</a>, <a href="/recipes/tags/page/">page</a>).
</p>
<p>This page counter was a CGI experiment in what it would take to implement such a simple concept. This is committed for archival to be run under Python 2.5 or later versions.</p>
Manage hashes in database files. (Python)
2012-03-24T14:40:24-07:00eysihttp://code.activestate.com/recipes/users/4177096/http://code.activestate.com/recipes/578081-manage-hashes-in-database-files/
<p style="color: grey">
Python
recipe 578081
by <a href="/recipes/users/4177096/">eysi</a>
(<a href="/recipes/tags/caching/">caching</a>, <a href="/recipes/tags/hashes/">hashes</a>).
Revision 4.
</p>
<p>This module can be used to calculate file hashes, store them in a database file
and retrieve them at a later date.</p>
<p>It uses the files modify time stamp to know if it can use the hash stored in
the db or if it has to re-calculate it. So the user will not have to worry
about the hash being incorrect if the file changes in between runs.</p>
Manage hashes in database files. (Python)
2012-03-20T01:08:15-07:00cathy parkhttp://code.activestate.com/recipes/users/4181365/http://code.activestate.com/recipes/578083-manage-hashes-in-database-files/
<p style="color: grey">
Python
recipe 578083
by <a href="/recipes/users/4181365/">cathy park</a>
(<a href="/recipes/tags/caching/">caching</a>, <a href="/recipes/tags/hashes/">hashes</a>).
</p>
<p>This module can be used to calculate file hashes, store them in a database file
and retrieve them at a later date.</p>
<p>It uses the files modify time stamp to know if it can use the hash stored in
the db or if it has to re-calculate it. So the user will not have to worry
about the hash being incorrect if the file changes in between runs.</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>
Fibonacci Number Generator (Python)
2011-04-13T00:00:59-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577651-fibonacci-number-generator/
<p style="color: grey">
Python
recipe 577651
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/fibonacci/">fibonacci</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/memorizing/">memorizing</a>, <a href="/recipes/tags/number/">number</a>).
</p>
<p>This recipe allows for individual number generation, and allows for generations of number ranges as well. </p>
<p>The recipe automatically memorizes numbers generated before and keeps them through out the running of the script. This recipe cannot handle too large of numbers, but does act as a simple solution for most cases.</p>
Python Sessions (Python)
2011-01-08T03:23:05-08:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577524-python-sessions/
<p style="color: grey">
Python
recipe 577524
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/cookie/">cookie</a>, <a href="/recipes/tags/management/">management</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/sessions/">sessions</a>).
Revision 2.
</p>
<p>I think this script should now be functional enough to post here at ActiveState. When you reply or vote down, please post some information on your problem with the code, and if you can, maybe something about the solution. This interface works, and is designed especially for Python. If you like it, don't be afraid to post that either, constructive criticism is all good, but compliments are even better! ;)</p>
<p>Get the latest version of this code here:
<a href="http://wiki.sunjay.ca/Python:Contents/Python_Session" rel="nofollow">http://wiki.sunjay.ca/Python:Contents/Python_Session</a></p>
<p><strong>Latest Changes:</strong></p>
<ul>
<li>Added a new function for making the session cookie last longer. See code for <code>set_expires</code>. Note: This new function is just in case you need to include a session for longer than one brower session (for example, one day instead). </li>
</ul>
LoggingWebMonitor - a central logging server and monitor. (Python)
2010-02-02T01:56:42-08:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/577025-loggingwebmonitor-a-central-logging-server-and-mon/
<p style="color: grey">
Python
recipe 577025
by <a href="/recipes/users/924636/">Gabriel Genellina</a>
(<a href="/recipes/tags/client_server/">client_server</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/remote/">remote</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/web/">web</a>).
Revision 3.
</p>
<p>LoggingWebMonitor listens for log records sent from other processes running in the same box or network. Collects and saves them concurrently in a log file. Shows a summary web page with the latest N records received.</p>
Authenticated encryption with PyCrypto (Python)
2010-01-26T16:05:50-08:00Daniel Millerhttp://code.activestate.com/recipes/users/4016391/http://code.activestate.com/recipes/576980-authenticated-encryption-with-pycrypto/
<p style="color: grey">
Python
recipe 576980
by <a href="/recipes/users/4016391/">Daniel Miller</a>
(<a href="/recipes/tags/aes/">aes</a>, <a href="/recipes/tags/encrypt/">encrypt</a>, <a href="/recipes/tags/hmac/">hmac</a>, <a href="/recipes/tags/pycrypto/">pycrypto</a>).
Revision 3.
</p>
<p>PyCrypto-based authenticated encryption using AES-CBC and HMAC-SHA256. This class only supports shared secret encryption. Look elsewhere for public key encryption.</p>
Named Tuples (Python)
2009-05-26T22:44:39-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/500261-named-tuples/
<p style="color: grey">
Python
recipe 500261
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 15.
</p>
<p>Fast, lightweight attribute-style access to tuples.</p>
sqlite message bus (Python)
2010-01-11T09:36:21-08:00André Bjärbyhttp://code.activestate.com/recipes/users/97994/http://code.activestate.com/recipes/577001-sqlite-message-bus/
<p style="color: grey">
Python
recipe 577001
by <a href="/recipes/users/97994/">André Bjärby</a>
(<a href="/recipes/tags/message_bus/">message_bus</a>, <a href="/recipes/tags/sqlite/">sqlite</a>).
</p>
<p>Let several (unix) processes communicate via a shared sqlite database.</p>
Debugging a running python process by interrupting and providing an interactive prompt (Python)
2008-09-25T11:23:29-07:00Brian McErleanhttp://code.activestate.com/recipes/users/111980/http://code.activestate.com/recipes/576515-debugging-a-running-python-process-by-interrupting/
<p style="color: grey">
Python
recipe 576515
by <a href="/recipes/users/111980/">Brian McErlean</a>
(<a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/interactive/">interactive</a>, <a href="/recipes/tags/remote/">remote</a>, <a href="/recipes/tags/signal/">signal</a>).
Revision 2.
</p>
<p>This provides code to allow any python program which uses it to be interrupted at the current point, and communicated with via a normal python interactive console. This allows the locals, globals and associated program state to be investigated, as well as calling arbitrary functions and classes.</p>
<p>To use, a process should import the module, and call listen() at any point during startup.
To interrupt this process, the script can be run directly, giving the process Id of the process to debug as the parameter.</p>
TwitterCmd - Interactive console for twittering (Python)
2009-10-08T02:15:19-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/576923-twittercmd-interactive-console-for-twittering/
<p style="color: grey">
Python
recipe 576923
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/microblogging/">microblogging</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/twitter/">twitter</a>).
Revision 4.
</p>
<p>TwitterCmd is an interactive console for twitter built on top of python-twitter library. It provides short, easy to remember commands for most twitter actions and provides a one-way interaction with the Python interpreter, exposing its objects to further inspection by the python-twitter API.</p>
Chat server & client using select.select (Python)
2007-09-28T08:57:53-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/531824-chat-server-client-using-selectselect/
<p style="color: grey">
Python
recipe 531824
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 3.
</p>
<p>This recipe demos how to write a simple command line chat server & client using multiplexing using select.select. The server uses select call to multiplex multiple clients and the client uses it to multiplex command line & socket I/O.</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>
Multithreaded Multiplexer/Demultiplexer to allow multiple transactions to be performed in parallel on a single channel (Python)
2008-10-04T00:12:00-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/576520-multithreaded-multiplexerdemultiplexer-to-allow-mu/
<p style="color: grey">
Python
recipe 576520
by <a href="/recipes/users/4129454/">david decotigny</a>
(<a href="/recipes/tags/demultiplexer/">demultiplexer</a>, <a href="/recipes/tags/multiplexer/">multiplexer</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/transaction/">transaction</a>).
Revision 8.
</p>
<p>Imagine you have one wire and you want to send a message from a machine A to another machine B, and wait for an answer from B (a "request/response transaction" in the following). Now imagine you have many threads on A which can make this kind of transaction "simultaneously", then you have to be cautious, otherwise you might get the wrong responses for your transactions. One solution is to serialize the transactions: the threads on A will have to wait for their turn to make their request/response transaction.</p>
<p>Another solution is to map a thread requesting a transaction on A, to a thread handling it on B. This allows for several transactions to be in flight at the same time on the same wire. This is what this module does: on A lives a multiplexer and threads call its transaction() method to initiate the request/response. On B, process_transaction() of a demultiplexer will get called by worker threads. All this in a multi-threaded way on a single wire. The module takes care of managing the interleaving requests/responses.</p>