Popular recipes tagged "meta:min_python_2=2"http://code.activestate.com/recipes/tags/meta:min_python_2=2/2015-10-15T21:33:32-07:00ActiveState Code RecipesUse PDFWriter with context manager support (Python) 2013-12-16T21:43:27-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578790-use-pdfwriter-with-context-manager-support/ <p style="color: grey"> Python recipe 578790 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/with_statement/">with_statement</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>PDFWriter - a core class of the xtopdf toolkit - can now be used with a Python context manager, a.k.a. the Python <strong>with</strong> statement.</p> <p>Code example below. More details here:</p> <p><a href="http://jugad2.blogspot.in/2013/12/xtopdf-pdfwriter-now-has-context.html" rel="nofollow">http://jugad2.blogspot.in/2013/12/xtopdf-pdfwriter-now-has-context.html</a></p> Fixed Lengh List (Python) 2014-01-24T15:27:25-08:00Hirohttp://code.activestate.com/recipes/users/4184239/http://code.activestate.com/recipes/578813-fixed-lengh-list/ <p style="color: grey"> Python recipe 578813 by <a href="/recipes/users/4184239/">Hiro</a> (<a href="/recipes/tags/length/">length</a>, <a href="/recipes/tags/list/">list</a>). Revision 2. </p> <p>In some applications, it's advantageous to be able to define a fixed length list.</p> <p>The class provides all features as python internal type: list</p> <p>The main focus of <strong>fixed length</strong> list is only keep certain number of items. "overflow" items will be discarded.</p> Create a Table of Contents for an ebook with XMLtoPDFBook (Python) 2013-12-16T21:49:33-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578791-create-a-table-of-contents-for-an-ebook-with-xmlto/ <p style="color: grey"> Python recipe 578791 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/ebooks/">ebooks</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/publishing/">publishing</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>XMLtoPDFBook is a tool that allows you to create simple PDF ebooks from XML content. It is a part of the xtpodf toolkit.</p> <p>You can now create PDF ebooks with XMLtoPDFBook, that include a Table of Contents (TOC).</p> Bag class (Python) 2013-07-19T21:08:46-07:00userhttp://code.activestate.com/recipes/users/4187240/http://code.activestate.com/recipes/578611-bag-class/ <p style="color: grey"> Python recipe 578611 by <a href="/recipes/users/4187240/">user</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). Revision 8. </p> <p>A bag: a set-like container that simply counts the number of same items held within it. </p> graph (Python) 2015-10-15T21:33:32-07:00userhttp://code.activestate.com/recipes/users/4187240/http://code.activestate.com/recipes/578615-graph/ <p style="color: grey"> Python recipe 578615 by <a href="/recipes/users/4187240/">user</a> (<a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/graph/">graph</a>). Revision 4. </p> <p>Abstract Graph Class: A Graph is a very abstract and powerful data structure to hold many different kinds of data relationships. </p> Python list of files and their path from folders and subfolders (Python) 2013-06-10T19:07:46-07:00Sam Khanhttp://code.activestate.com/recipes/users/4185025/http://code.activestate.com/recipes/578557-python-list-of-files-and-their-path-from-folders-a/ <p style="color: grey"> Python recipe 578557 by <a href="/recipes/users/4185025/">Sam Khan</a> (<a href="/recipes/tags/file_io/">file_io</a>). </p> <p>Look into a folder along with subfolders for a file starting with or ending with certain characters. </p> Pi Circle (Python) 2012-05-13T04:35:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578130-pi-circle/ <p style="color: grey"> Python recipe 578130 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/art/">art</a>, <a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/fun/">fun</a>, <a href="/recipes/tags/pi/">pi</a>). Revision 5. </p> <p>Computes Pi to many decimal places and prints the digits in a circle.</p> Tracking Child Classes (Python) 2012-05-30T10:00:21-07:00Evert van de Waalhttp://code.activestate.com/recipes/users/4182118/http://code.activestate.com/recipes/578137-tracking-child-classes/ <p style="color: grey"> Python recipe 578137 by <a href="/recipes/users/4182118/">Evert van de Waal</a> (<a href="/recipes/tags/child/">child</a>, <a href="/recipes/tags/classes/">classes</a>). </p> <p>This recipe allows a base class to keep track of the child classes that inherit from it using a metaclass.</p> Fast Indexing functions (greater than, less than, equal to, and not equal to) (Python) 2012-03-13T16:21:36-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578071-fast-indexing-functions-greater-than-less-than-equ/ <p style="color: grey"> Python recipe 578071 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>Oftentimes you want to find the index of a list-like object. Numpy arrays, for example, do not have a index member function. These get the job done quickly.</p> <p>Note: these do not raise exceptions, instead they return -1 on error. You should change that if you want different behavior.</p> Bless Classes into Singletons (Python) 2011-09-21T05:34:52-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/577875-bless-classes-into-singletons/ <p style="color: grey"> Python recipe 577875 by <a href="/recipes/users/4169530/">Anand B Pillai</a> (<a href="/recipes/tags/metaclasses/">metaclasses</a>, <a href="/recipes/tags/patterns/">patterns</a>, <a href="/recipes/tags/singleton/">singleton</a>). </p> <p>A pattern using metaclasses for "blessing" classes into Singletons.</p> Josephus problem (Python) 2011-08-13T23:17:27-07:00Giannis Fysakishttp://code.activestate.com/recipes/users/4174072/http://code.activestate.com/recipes/577840-josephus-problem/ <p style="color: grey"> Python recipe 577840 by <a href="/recipes/users/4174072/">Giannis Fysakis</a> . </p> <p>In computer science and mathematics, the Josephus problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game.</p> <p>There are people standing in a circle waiting to be executed. After the first person is executed, certain number of people are skipped and one person is executed. Then again, people are skipped and a person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.</p> <p>The task is to choose the place in the initial circle so that you are the last one remaining and so survive. If 2 are the number of people that are skipped</p> Managment command to deploy a Django project (Python) 2011-06-21T22:10:31-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577767-managment-command-to-deploy-a-django-project/ <p style="color: grey"> Python recipe 577767 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/django/">django</a>). </p> <p>This recipe shows a management command for django project that enables automatic synchronization between the local and remote project. Of course, the deploy exclude to overwrite the settings.py located in the remote server.</p> <p>This is a clean way to deploy your own project without calling external commands, although this command needs rsync to work.</p> Generator of combinations without replacement for a sequence using dynamic programming (Python) 2011-06-21T09:11:19-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577766-generator-of-combinations-without-replacement-for-/ <p style="color: grey"> Python recipe 577766 by <a href="/recipes/users/4174931/">Filippo Squillace</a> . </p> <p>From the previous recipe <a href="http://code.activestate.com/recipes/577764-combinations-of-a-sequence-without-replacement-usi/" rel="nofollow">http://code.activestate.com/recipes/577764-combinations-of-a-sequence-without-replacement-usi/</a> we can easily build a generator that return all the K-combinations without replacement.</p> Get a list from a ConfigParser option (Python) 2011-05-13T13:53:56-07:00Georges Martinhttp://code.activestate.com/recipes/users/4177973/http://code.activestate.com/recipes/577694-get-a-list-from-a-configparser-option/ <p style="color: grey"> Python recipe 577694 by <a href="/recipes/users/4177973/">Georges Martin</a> (<a href="/recipes/tags/configparser/">configparser</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/option/">option</a>). Revision 2. </p> <p>Return a list from a ConfigParser option. By default, split on a comma and strip whitespaces.</p> Arduino Diecimila Board Access Inside Windows 32 Bit DEMO. (Python) 2011-04-10T11:02:37-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/577648-arduino-diecimila-board-access-inside-windows-32-b/ <p style="color: grey"> Python recipe 577648 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/access/">access</a>, <a href="/recipes/tags/arduino/">arduino</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>Arduino-Windows2x.py</p> <p>Finally the Windows 32 bit version of the other recipes of similar design. (The AMIGA/WinUAE and Linux versions are already on this site.)</p> <p>To get more recognisable characters displayed it is best to use a potentiometer wired as one end to +5V, the other end to Gnd and the wiper to ANALOG IN 0. This has been tested on various Linux Distros and kept as simple as possible so that anyone can understand it.</p> <p>The required ?.pde file for the Arduino Board can be found here:-</p> <p><a href="http://code.activestate.com/recipes/577625-arduino-diecimila-board-access-inside-winuae-demo/?in=lang-python" rel="nofollow">http://code.activestate.com/recipes/577625-arduino-diecimila-board-access-inside-winuae-demo/?in=lang-python</a></p> <p>Similar assumptions are made as in the URL above.</p> <p>Enjoy finding simple solutions to often very difficult problems... ;o)</p> <p>Bazza, G0LCU.</p> query yes/no (Python) 2010-03-09T17:57:28-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577058-query-yesno/ <p style="color: grey"> Python recipe 577058 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/no/">no</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>, <a href="/recipes/tags/yes/">yes</a>). Revision 2. </p> <p>Ask the user a question using raw_input() and looking something like this:</p> <pre class="prettyprint"><code>QUESTION [Y/n] ...validate... </code></pre> <p>See also: <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p> A simple function benchmarking module (Python) 2010-08-27T09:26:48-07:00Timothee Cezardhttp://code.activestate.com/recipes/users/4174785/http://code.activestate.com/recipes/577377-a-simple-function-benchmarking-module/ <p style="color: grey"> Python recipe 577377 by <a href="/recipes/users/4174785/">Timothee Cezard</a> (<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/function/">function</a>). </p> <p>This module enable its user to monitor the amount of time spend in between two commands start and stop. The module is fairly imprecise if the monitored task is quick as the start and stop commands are fairly slow (2e-07 - 5e-07 second)</p> Non-recursive Flatten, leaves Strings and Dicts alone, minimal type assumptions, with tests (Python) 2010-09-10T00:10:47-07:00Manuel Garciahttp://code.activestate.com/recipes/users/2827266/http://code.activestate.com/recipes/577387-non-recursive-flatten-leaves-strings-and-dicts-alo/ <p style="color: grey"> Python recipe 577387 by <a href="/recipes/users/2827266/">Manuel Garcia</a> (<a href="/recipes/tags/nonrecursive/">nonrecursive</a>). </p> <p>I have written Flatten a few dozen times, and also searched the interwebs - I don't feel good about a heavily recursive function for Python, and some of the "type-sniffing" I saw in some codes seemed fishy - so I coded up this version. At least this increases the options. Handles strings, dictionaries, generators, sequences, lists, tuples -- in sensible ways. Handles arbitrarily deep levels of nesting. Does the bare minimum of type-sniffing. With Doctest tests.</p> Python script main line for graceful exception handling and logging (Python) 2010-09-11T05:46:59-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577258-python-script-main-line-for-graceful-exception-han/ <p style="color: grey"> Python recipe 577258 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/main/">main</a>, <a href="/recipes/tags/mainline/">mainline</a>, <a href="/recipes/tags/script/">script</a>). Revision 5. </p> <p>This is a recipe is often use for the mainline of my Python scripts. With this recipe your Python script will:</p> <ul> <li>gracefully handle <code>Ctrl+C</code> (i.e. <code>KeyboardInterrupt</code>)</li> <li>log an error (using the <code>logging</code> module) for uncaught exceptions, importantly <strong>with the file and line number</strong> in your Python code where the exception was raised</li> <li>gracefully ignore a closed output pipe (common when the user pipes your script through <code>less</code> and terminates that)</li> <li>if your script logger is enabled for <code>DEBUG</code> level logging, a full traceback will be shown for an uncaught exception</li> </ul> <p>Presumptions:</p> <ul> <li><p>you have a global <code>log</code> variable a la:</p> <pre class="prettyprint"><code>import logging log = logging.setLevel("scriptname") </code></pre></li> <li><p>your script's entry point is a function <code>def main(argv): ...</code></p></li> </ul> Simple polynomial class (Python) 2014-02-13T20:55:55-08:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/576953-simple-polynomial-class/ <p style="color: grey"> Python recipe 576953 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/arithmetic/">arithmetic</a>, <a href="/recipes/tags/calculator/">calculator</a>, <a href="/recipes/tags/polynomial/">polynomial</a>). Revision 7. </p> <p>This implements polynomial functions over a single variable in Python. It represents the polynomial as a list of numbers and allows most arithmetic operations, using conventional Python syntax. It does not do symbolic manipulations. Instead, you can do things like this:</p> <pre class="prettyprint"><code>x = SimplePolynomial() eq = (x-1)*(x*1) print eq # prints 'X**2 - 1' print eq(4) # prints 15 </code></pre>