Popular recipes tagged "meta:loc=157"http://code.activestate.com/recipes/tags/meta:loc=157/2014-08-01T08:28:07-07:00ActiveState Code RecipesHandle exit context manager (Python) 2014-08-01T08:28:07-07:00Giampaolo RodolĂ http://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577997-handle-exit-context-manager/ <p style="color: grey"> Python recipe 577997 by <a href="/recipes/users/4178764/">Giampaolo RodolĂ </a> (<a href="/recipes/tags/atexit/">atexit</a>, <a href="/recipes/tags/contextlib/">contextlib</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/exit/">exit</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/sigint/">sigint</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sigterm/">sigterm</a>). Revision 23. </p> <p>A context manager which properly handles SIGTERM (SystemExit) and SIGINT (KeyboardInterrupt) signals, registering a function which is always guaranteed to be called on interpreter exit. Also, it makes sure to execute previously registered functions as well (if any).</p> Color Study 1 (Python) 2011-04-05T23:35:32-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577636-color-study-1/ <p style="color: grey"> Python recipe 577636 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/experiment/">experiment</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/sandbox/">sandbox</a>). </p> <p>The following program was written to understand how two different color systems related to each other, RGB and HSV. The recipe could be better written and extended further to support other color systems in addition to the two it already uses, but it is a simple example of how one might go about to understand what others speak of when talking about color. The GUI is simple to use and responsive, and the code is a good demonstration of what may need to be written to produce a demonstration such as this one.</p> <p>If you have suggestions or would like to vote this recipe down, please provide an explanation of the problem along with a solution that you would propose and implement to improve upon what is presented.</p> Converting XML to Dictionary and back (Python) 2009-01-15T11:42:18-08:00Cory Fabrehttp://code.activestate.com/recipes/users/4158087/http://code.activestate.com/recipes/573463-converting-xml-to-dictionary-and-back/ <p style="color: grey"> Python recipe 573463 by <a href="/recipes/users/4158087/">Cory Fabre</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 7. </p> <p>This code converts an XML ElementTree.Element into a dict and back into XML. It attempts to retain as much information as possible during the conversions while still being convenient to access.</p> stock prices historical data bulk download from internet (Python) 2007-04-01T14:05:47-07:00gian paolo cicerihttp://code.activestate.com/recipes/users/631476/http://code.activestate.com/recipes/511444-stock-prices-historical-data-bulk-download-from-in/ <p style="color: grey"> Python recipe 511444 by <a href="/recipes/users/631476/">gian paolo ciceri</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>Courtesy of Yahoo finance, it is possible to bulk download historical prices data. This script, borrowed from pycurl retriever-multi.py example, fetch series for several tickers at a time. It uses urllib to fetch web data, so it should work with a plain vanilla python distro.</p> TryFinally (Python) 2005-10-17T22:37:51-07:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/442419-tryfinally/ <p style="color: grey"> Python recipe 442419 by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>This is a convenient way to deeply nest try/finally statements.</p> Unique stable-unstable (Python) 2005-07-27T18:23:55-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/438599-unique-stable-unstable/ <p style="color: grey"> Python recipe 438599 by <a href="/recipes/users/2403049/">bearophile -</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </p> <p>Python needs a unique function, this is my best try so far. It uses the faster algorithm (among five) for each situation.</p> pyM3U is a very simple Python script (usable also as a module) allowing the generation of M3U playlists (Python) 2004-07-13T03:23:40-07:00Foo Bearhttp://code.activestate.com/recipes/users/181960/http://code.activestate.com/recipes/286234-pym3u-is-a-very-simple-python-script-usable-also-a/ <p style="color: grey"> Python recipe 286234 by <a href="/recipes/users/181960/">Foo Bear</a> . </p> <p>With this script you can walk into tree subdirectories and generate a playlist (m3u format) that can be read with players like xmms, winamp and so on.</p> <p>The description of the format is here: <a href="http://hanna.pyxidis.org/tech/m3u.html" rel="nofollow">http://hanna.pyxidis.org/tech/m3u.html</a></p> xmlrpc parameter mapping for OO-style programming. (Python) 2004-12-17T06:18:46-08:00Diez Roggischhttp://code.activestate.com/recipes/users/2216384/http://code.activestate.com/recipes/359874-xmlrpc-parameter-mapping-for-oo-style-programming/ <p style="color: grey"> Python recipe 359874 by <a href="/recipes/users/2216384/">Diez Roggisch</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>This recipe automates the creation and lookup of objects passed from and to xmlrpc server methods so that incoming calls arguments are automatically replaced by their respective objects, and returned objects get mapped and their ids returned.</p> Handling Deeply Nested/Recursive Data (Python) 2006-04-26T06:15:08-07:00Simon Burtonhttp://code.activestate.com/recipes/users/1214028/http://code.activestate.com/recipes/302535-handling-deeply-nestedrecursive-data/ <p style="color: grey"> Python recipe 302535 by <a href="/recipes/users/1214028/">Simon Burton</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Common python services such as pickle, deepcopy and comparison tests either fail entirely or do not scale for highly recursive data structures. This recipe presents a reversible "flatten" transformation that allows for such operations.</p> Stateful Objects use Mix-ins to define behaviour (Python) 2002-11-02T06:41:05-08:00Dave Hayneshttp://code.activestate.com/recipes/users/116882/http://code.activestate.com/recipes/161173-stateful-objects-use-mix-ins-to-define-behaviour/ <p style="color: grey"> Python recipe 161173 by <a href="/recipes/users/116882/">Dave Haynes</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>You want to implement stateful objects, which have a different set of behaviours according to what state they are in.</p> <p>This requirement can be achieved with the use of mix-ins. A mix-in is a class which is dynamically inherited by an object. The methods of the mix-in class are thus accessible through the object. This is a clean way of providing objects with standard interfaces.</p> <p>Chuck Esterbrook has written a discussion of mix-ins, and their implementation in Python, which can be found here: <a href="http://www.linuxjournal.com/article.php?sid=4540" rel="nofollow">http://www.linuxjournal.com/article.php?sid=4540</a></p> <p>The recipe draws from the famous "Lumberjack Song". If you're not familiar with it, all you need to know is that the passage of time modifies the Lumberjack's behaviour.</p>