Popular recipes tagged "meta:min_python_2=4"http://code.activestate.com/recipes/tags/meta:min_python_2=4/2016-09-01T12:34:17-07:00ActiveState Code RecipesPython2 keyword-only argument emulation as a decorator. Python3 compatible. (Python) 2016-04-15T13:25:20-07:00István Pásztorhttp://code.activestate.com/recipes/users/4189380/http://code.activestate.com/recipes/580639-python2-keyword-only-argument-emulation-as-a-decor/ <p style="color: grey"> Python recipe 580639 by <a href="/recipes/users/4189380/">István Pásztor</a> (<a href="/recipes/tags/args/">args</a>, <a href="/recipes/tags/arguments/">arguments</a>, <a href="/recipes/tags/keyword/">keyword</a>, <a href="/recipes/tags/keyword_only/">keyword_only</a>, <a href="/recipes/tags/kwonly/">kwonly</a>, <a href="/recipes/tags/only/">only</a>, <a href="/recipes/tags/python2/">python2</a>). Revision 4. </p> <p>Provides a very simple decorator (~40 lines of code) that can turn some or all of your default arguments into keyword-only arguments. You select one of your default arguments by name and the decorator turn this argument along with all default arguments on its right side into keyword only arguments. Check the docstring of the decorator or visit the github/pypi page for detailed documentation:</p> <ul> <li><a href="https://github.com/pasztorpisti/kwonly-args" rel="nofollow">https://github.com/pasztorpisti/kwonly-args</a></li> <li><a href="https://pypi.python.org/pypi/kwonly-args" rel="nofollow">https://pypi.python.org/pypi/kwonly-args</a></li> </ul> <p><code>$ pip install kwonly-args</code></p> SortedCollection (Python) 2010-09-01T02:12:33-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577197-sortedcollection/ <p style="color: grey"> Python recipe 577197 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/bisect/">bisect</a>, <a href="/recipes/tags/collection/">collection</a>, <a href="/recipes/tags/sorted/">sorted</a>). Revision 9. </p> <p>Wraps bisect.bisect() in an easy to use class that supports key-functions and straight-forward search methods.</p> defdict (Python) 2015-10-15T15:45:16-07:00userhttp://code.activestate.com/recipes/users/4187240/http://code.activestate.com/recipes/579113-defdict/ <p style="color: grey"> Python recipe 579113 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/dictionary/">dictionary</a>). </p> <p>default dictionary with collision function to handle key collisions.</p> Data (A Class For Arbitrary Data) (Python) 2015-08-03T15:22:40-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/579092-data-a-class-for-arbitrary-data/ <p style="color: grey"> Python recipe 579092 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/lightweight/">lightweight</a>). </p> <p>A class which is designed to be easy to use when one needs a piece of data with a minimum of source required.</p> <p>Usage:</p> <pre class="prettyprint"><code>shop = Data(owner="Homer", address="down the street", ice=Data(flavor="vanilla", amount=3)) print shop Data: owner = 'Homer' ice = Data: amount = 3 flavor = 'vanilla' address = 'down the street' </code></pre> Engineering notation. (Python) 2015-04-15T16:17:12-07:00Paul Sargenthttp://code.activestate.com/recipes/users/4192033/http://code.activestate.com/recipes/579046-engineering-notation/ <p style="color: grey"> Python recipe 579046 by <a href="/recipes/users/4192033/">Paul Sargent</a> (<a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/ipython/">ipython</a>). </p> <p>eng(x) returns a string representing x using the "engineering notation"</p> Call out to an external editor (Python) 2014-09-01T18:26:51-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578926-call-out-to-an-external-editor/ <p style="color: grey"> Python recipe 578926 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/editing/">editing</a>, <a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/external/">external</a>, <a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>Here's a function that lets you use Python to wrap calls to an external editor. The editor can be an command line editor, like venerable old "ed", or something more powerful like nano, vim or emacs, and even GUI editors. After the editor quits, the text you typed in the editor is returned by the function.</p> <p>A simple example, using the (rather cryptic) 'ed' editor on Linux. For the benefit of those unfamiliar with 'ed', I have annotated the editor session with comments.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; status, text = edit('ed') 0 ## ed prints the initial number of lines a ## start "append" mode Hello World! Goodbye now . ## stop appending w ## write the file to disk 25 ## ed prints the number of bytes written q ## quit ed and return to Python &gt;&gt;&gt; status 0 &gt;&gt;&gt; print text Hello World! Goodbye now </code></pre> A simple Matrix class (Python) 2012-05-14T13:34:31-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/578131-a-simple-matrix-class/ <p style="color: grey"> Python recipe 578131 by <a href="/recipes/users/4169530/">Anand B Pillai</a> (<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>). Revision 3. </p> <p>A simple class in Python representing a Matrix with basic operations, operator overloading and class factory methods to make Matrices from different sources.</p> Method chaining or cascading (Python) 2016-09-01T12:34:17-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578770-method-chaining-or-cascading/ <p style="color: grey"> Python recipe 578770 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/cascade/">cascade</a>, <a href="/recipes/tags/cascading/">cascading</a>, <a href="/recipes/tags/chaining/">chaining</a>, <a href="/recipes/tags/method/">method</a>). </p> <p>A frequently missed feature of built-ins like lists and dicts is the ability to chain method calls like this:</p> <pre class="prettyprint"><code>x = [] x.append(1).append(2).append(3).reverse().append(4) # x now equals [3, 2, 1, 4] </code></pre> <p>Unfortunately this doesn't work, as mutator methods return <code>None</code> rather than <code>self</code>. One possibility is to design your class from the beginning with method chaining in mind, but what do you do with those like the built-ins which aren't?</p> <p>This is sometimes called <a href="https://en.wikipedia.org/wiki/Method_cascading">method cascading</a>. Here's a proof-of-concept for an adapter class which turns any object into one with methods that can be chained.</p> Strongly connected components of a directed graph. (Python) 2013-04-03T19:30:32-07:00Mark Dickinsonhttp://code.activestate.com/recipes/users/4172683/http://code.activestate.com/recipes/578507-strongly-connected-components-of-a-directed-graph/ <p style="color: grey"> Python recipe 578507 by <a href="/recipes/users/4172683/">Mark Dickinson</a> (<a href="/recipes/tags/connected/">connected</a>, <a href="/recipes/tags/directed/">directed</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/strong/">strong</a>, <a href="/recipes/tags/tarjan/">tarjan</a>). Revision 3. </p> <p>Two linear-time algorithms for finding the strongly connected components of a directed graph. <code>strongly_connected_components_tree</code> implements (a variant of) Tarjan's well-known algorithm for finding strongly connected components, while <code>strongly_connected_components_path</code> implements a path-based algorithm due (in this form) to Gabow.</p> <p>Edit: I added an iterative function <code>strongly_connected_components_iterative</code>; this is a direct conversion of <code>strongly_connected_components_path</code> into iterative form. It's therefore safe to use on high-depth graphs, without risk of running into Python's recursion limit.</p> Validate XML with schemaLocation (Python) 2013-03-28T10:24:55-07:00Mathias Loeschhttp://code.activestate.com/recipes/users/4185910/http://code.activestate.com/recipes/578503-validate-xml-with-schemalocation/ <p style="color: grey"> Python recipe 578503 by <a href="/recipes/users/4185910/">Mathias Loesch</a> (<a href="/recipes/tags/schema/">schema</a>, <a href="/recipes/tags/schemalocation/">schemalocation</a>, <a href="/recipes/tags/validation/">validation</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 3. </p> <p>Addresses problems described at:</p> <p><a href="http://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml" rel="nofollow">http://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml</a> <a href="https://mailman-mail5.webfaction.com/pipermail/lxml/2011-September/006153.html" rel="nofollow">https://mailman-mail5.webfaction.com/pipermail/lxml/2011-September/006153.html</a></p> Python HTML Stripper (Python) 2013-04-08T13:58:00-07:00Granning Stolinehttp://code.activestate.com/recipes/users/4186069/http://code.activestate.com/recipes/578511-python-html-stripper/ <p style="color: grey"> Python recipe 578511 by <a href="/recipes/users/4186069/">Granning Stoline</a> (<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/stripper/">stripper</a>). </p> <p>Python HTML Stripper</p> Ordered Dictionary for Py2.4 (Python) 2011-04-24T03:20:45-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/ <p style="color: grey"> Python recipe 576693 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/linked_list/">linked_list</a>, <a href="/recipes/tags/ordered/">ordered</a>). Revision 9. </p> <p>Drop-in substitute for Py2.7's new collections.OrderedDict. The recipe has big-oh performance that matches regular dictionaries (amortized O(1) insertion/deletion/lookup and O(n) iteration/repr/copy/equality_testing).</p> Analog stopclock (Python) 2013-03-15T18:30:37-07:00prashanth.chengihttp://code.activestate.com/recipes/users/4185699/http://code.activestate.com/recipes/578493-analog-stopclock/ <p style="color: grey"> Python recipe 578493 by <a href="/recipes/users/4185699/">prashanth.chengi</a> (<a href="/recipes/tags/analog/">analog</a>, <a href="/recipes/tags/chess/">chess</a>, <a href="/recipes/tags/clock/">clock</a>). </p> <p>A very simple tkinter analog stopclock</p> Optional arguments decorator (Python) 2013-01-28T05:03:43-08:00Maxime H Lapointehttp://code.activestate.com/recipes/users/4185033/http://code.activestate.com/recipes/578435-optional-arguments-decorator/ <p style="color: grey"> Python recipe 578435 by <a href="/recipes/users/4185033/">Maxime H Lapointe</a> (<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>If you need your decorator to receive arguments, optional or not, then you should decorate it with the opt_arg_dec decorator to handle all the logic that this implies for you.</p> List comparison, difference and more using set & frozenset (Python) 2012-11-01T10:30:58-07:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578310-list-comparison-difference-and-more-using-set-froz/ <p style="color: grey"> Python recipe 578310 by <a href="/recipes/users/4181178/">Scott S-Allen</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/contain/">contain</a>, <a href="/recipes/tags/difference/">difference</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/remove/">remove</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/union/">union</a>). </p> <p>Python has a powerful suite of tools for comparing lists by way of sets and frozensets. Here are a few examples and conveniences that many newcomers, even a few seasoned developers, are unaware.</p> PseudoStruct (Python) 2012-11-25T03:43:06-08:00Matthew Zipayhttp://code.activestate.com/recipes/users/4183355/http://code.activestate.com/recipes/578349-pseudostruct/ <p style="color: grey"> Python recipe 578349 by <a href="/recipes/users/4183355/">Matthew Zipay</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/record/">record</a>). </p> <p>This is a recipe for a Python "data object." It is similar in function to namedtuple (<a href="http://code.activestate.com/recipes/500261/" rel="nofollow">http://code.activestate.com/recipes/500261/</a>) and recordtype (<a href="http://code.activestate.com/recipes/576555-records/" rel="nofollow">http://code.activestate.com/recipes/576555-records/</a>) in that it is a simple container for data, but is designed to meet three specific goals:</p> <ol> <li>Easy to subclass data objects.</li> <li>Get/set speed comparable to a simple class.</li> <li>Minimal memory consumption per instance.</li> </ol> How to read millions of hexadecimal numbers into a numpy array quickly (Python) 2012-06-27T06:03:40-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578177-how-to-read-millions-of-hexadecimal-numbers-into-a/ <p style="color: grey"> Python recipe 578177 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/numpy/">numpy</a>). </p> <p>The numpy.fromfile() function supports binary formats or decimal text. How do you read millions of hexadecimal numbers quickly?</p> Monotonic local time. And sandwich. (Python) 2013-01-03T08:28:36-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578270-monotonic-local-time-and-sandwich/ <p style="color: grey"> Python recipe 578270 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/daylight/">daylight</a>, <a href="/recipes/tags/dst/">dst</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>A Mutton, Lettuce and Tomato sandwich is generally agreed to be the greatest thing in the world. But a different kind of MLT comes in at a close second: Monotonic Local Time. This is a time value in your local timezone that takes care of that annoying hour that happens once a year where time goes backwards. It works by extending the previous day by a couple of hours (24, 25 etc) until after the switch.</p> Create replica of the existing directory structure with only specified extension files. (Python) 2012-11-16T08:30:31-08:00Achal Rastogihttp://code.activestate.com/recipes/users/4183889/http://code.activestate.com/recipes/578303-create-replica-of-the-existing-directory-structure/ <p style="color: grey"> Python recipe 578303 by <a href="/recipes/users/4183889/">Achal Rastogi</a> (<a href="/recipes/tags/bioinformatics/">bioinformatics</a>, <a href="/recipes/tags/biology/">biology</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/fileextensions/">fileextensions</a>, <a href="/recipes/tags/replica/">replica</a>). Revision 2. </p> <p>The program creates a replica of existing directory structure with specified extension files only. The program maintains the directory-sub-directory architecture while executing. User has to provide two arguments, First the path of existing directory,whose replica has to be created and Second, the path of directory, where the replica has to be created. This program is developed to fetch files with ".pl" (perl) extension, while maintaining the directory-sub-directory architecture. User can modify it, with his/her interested file extension, like ".txt", ".doc", etc.</p> Distributed lock manager for Python (Python) 2012-07-04T21:03:32-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578194-distributed-lock-manager-for-python/ <p style="color: grey"> Python recipe 578194 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/locks/">locks</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/wsgi/">wsgi</a>). Revision 2. </p> <p>Distributed lock manager provides mutex(es) over network. It is used to synchronize processes running on different machines, e.g. WSGI processes in case of web applications. Lock object is compatible with threading.Lock and can be used as a context manager ("with statement"). It can be easily modified to use UNIX sockets instead of TCP/IP. Communication protocol is text based.</p> <p>First start server process:</p> <pre class="prettyprint"><code>$ chmod +x dlm.py $ ./dlm.py </code></pre> <p>Usage:</p> <pre class="prettyprint"><code>from dlm import LockClient client = LockClient('localhost', 27272, 'client_name') lock = client.mkLock('lock_name') lock.acquire() # critical section here... lock.release() # using context manager with lock: # critical section here... </code></pre>