Top-rated recipes tagged "python2" Code RecipesLines Of Code (LOC) (Python) 2016-10-25T17:53:01-07:00Jean Brouwers <p style="color: grey"> Python recipe 580709 by <a href="/recipes/users/2984142/">Jean Brouwers</a> (<a href="/recipes/tags/count/">count</a>, <a href="/recipes/tags/lines/">lines</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/source/">source</a>). Revision 3. </p> <p>Count the number of lines (code, comment, blank) in one or several Python source files.</p> Find the arity of a Python function (Python) 2017-01-30T14:09:47-08:00Vasudev Ram <p style="color: grey"> Python recipe 580753 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/reflection/">reflection</a>). </p> <p>This recipe shows how to find the arity of a given Python function. The arity of a function is the number of arguments the function takes. The recipe uses the inspect module of Python.</p> <p>More details and sample output (including some limitations) here:</p> <p><a href="" rel="nofollow"></a></p> Classifying letters as vowels or consonants and counting their frequencies (Python) 2017-01-17T20:05:10-08:00Vasudev Ram <p style="color: grey"> Python recipe 580749 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/assertions/">assertions</a>, <a href="/recipes/tags/comprehension/">comprehension</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionaries/">dictionaries</a>, <a href="/recipes/tags/dict_comp/">dict_comp</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/tuple/">tuple</a>, <a href="/recipes/tags/unpack/">unpack</a>). </p> <p>This recipe shows how to take a string as input and classify the characters in it as vowels, consonants or neither. The frequency of each vowel is calculated and the frequency of all the consonants in total is calculated. The program logic is fairly simple, and uses a dictionary comprehension and a dict; the more interesting thing about it, is that it illustrates 8 Python language features in under 35 lines of code.</p> <p>More details and sample output here:</p> <p><a href="" rel="nofollow"></a></p> Put Peewee ORM data to PDF with xtopdf (Python) 2016-09-29T18:04:57-07:00Vasudev Ram <p style="color: grey"> Python recipe 580704 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/formats/">formats</a>, <a href="/recipes/tags/orm/">orm</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/peewee/">peewee</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/sqlite/">sqlite</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how some basics of how to fetch data from database tables managed by the Peewee ORM (a lightweight expressive ORM for Python) and write that data, formatted, to a PDF file. The recipe uses Python, the Peewee ORM and the xtopdf toolkit for PDF creation.</p> Emulating super() in Python 3.x using Python 2.7 (Python) 2016-07-31T04:03:29-07:00sunqingyao <p style="color: grey"> Python recipe 580694 by <a href="/recipes/users/4194518/">sunqingyao</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python2/">python2</a>). </p> <p>Depending on the name of the first argument, <code>self.__sup</code> or <code>cls.__sup</code> behaves like <code>super()</code> in Python 3, while this code is written in Python 2.7.</p> <p>It works for both ordinary methods and class methods(static methods don't use <code>super()</code>). See my code for detailed examples:</p> Finding the sizes of various Python data types (Python) 2016-04-28T18:28:59-07:00Vasudev Ram <p style="color: grey"> Python recipe 580655 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/type/">type</a>). </p> <p>This recipe shows how to find the sizes of various common data types in Python, both built-in and user-defined. It uses the sys.getsizeof() function and also discusses a few other points of interest.</p> Python2 keyword-only argument emulation as a decorator. Python3 compatible. (Python) 2016-04-15T13:25:20-07:00István Pásztor <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="" rel="nofollow"></a></li> <li><a href="" rel="nofollow"></a></li> </ul> <p><code>$ pip install kwonly-args</code></p> Computing simple list functions recursively (Python) 2016-03-01T20:41:13-08:00Vasudev Ram <p style="color: grey"> Python recipe 580617 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/recursive/">recursive</a>). </p> <p>This recipe shows how to compute some simple list functions (functions that operate on lists) using simple recursive algorithms. Simple non-recursive algorithms exist for these tasks; this recipe is just for illustrative purposes. However, the principles described can be used for more complex recursive algorithms, for which non-recursive algorithms might be more complex than the recursive ones.</p> Simple drawing tool with Python turtle graphics (Python) 2016-01-27T22:22:01-08:00Vasudev Ram <p style="color: grey"> Python recipe 580544 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/drawing/">drawing</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/turtle/">turtle</a>). </p> <p>A simple drawing program that lets you use your keyboard to draw figures on screen, using the turtle graphics module built into Python.</p> Python-controlled Unix pipeline to generate PDF (Python) 2016-01-07T18:02:52-08:00Vasudev Ram <p style="color: grey"> Python recipe 579146 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/pipelining/">pipelining</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/unix/">unix</a>). </p> <p>This recipe shows how to create a Unix pipeline that generates PDF output, under the control of a Python program. It is tested on Linux. It uses nl, a standard Linux command that adds line numbers to its input, and selpg, a custom Linux command-line utility, that selects only specified pages from its input, together in a pipeline (nl | selpg). The Python program sets up and starts that pipeline running, and then reads input from it and generates PDF output.</p> Validate data easily with JSON Schema (Python) 2015-12-06T21:23:48-08:00Vasudev Ram <p style="color: grey"> Python recipe 579135 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/jsonschema/">jsonschema</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>). </p> <p>This recipe shows how to use the jsonschema Python library, which implements the JSON Schema specification, to easily validate your Python data. It sends good output to stdout and bad output to stderr, for demo purposes.</p> <p>jsonschema Python library: <a href="" rel="nofollow"></a></p> <p>JSON Schema: <a href="" rel="nofollow"></a></p> PYQT TOUCH INPUT (Python) 2015-10-26T19:14:01-07:00jshah <p style="color: grey"> Python recipe 579118 by <a href="/recipes/users/4193036/">jshah</a> (<a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/qt4/">qt4</a>, <a href="/recipes/tags/touch/">touch</a>). </p> <p>PYQT TOUCH INPUT WIDGET </p> Use PyAudio to play a list of WAV files (Python) 2015-10-22T18:24:08-07:00Vasudev Ram <p style="color: grey"> Python recipe 579116 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/music/">music</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/sound/">sound</a>). </p> <p>This recipe shows how to use PyAudio, a 3rd-party Python audio toolkit, to play a list of WAV files on your computer. This is an enhanced version of a basic WAV code example on the PyAudio site. You can specify either one WAV filename on the command line, like this:</p> <p>py chimes.wav</p> <p>or specify a text file containing names of WAV files to play, like this:</p> <p>py -f wav_fil_list.txt</p> <p>The only dependency is PyAudio, which you can install with pip.</p> Recognizing speech (speech-to-text) with the Python speech module (Python) 2015-10-22T13:09:02-07:00Vasudev Ram <p style="color: grey"> Python recipe 579115 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/language_translation/">language_translation</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/speech_recognition/">speech_recognition</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe shows how to use the 'speech' (or 'pyspeech' - it seems to have two names) Python library to make the computer recognize what you say and convert it to text. Note: This library did not always give correct results for me, so it may not be advisable to use it in production. Also, the pyspeech site says that the library is no longer being maintained. Use at your own risk.</p> Generate a PDF invoice with xtopdf and Python (Python) 2015-10-07T18:02:46-07:00Vasudev Ram <p style="color: grey"> Python recipe 579108 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows the basics of how to generate invoices as PDF documents, using xtopdf, a Python toolkit for PDF creation. An example of creating a simple invoice through Python and xtopdf code is shown. </p> <p>xtopdf is available on Bitbucket at:</p> <p><a href="" rel="nofollow"></a></p> <p>and you can find many links with examples of using xtopdf, how to install it, etc., from this Google search:</p> <p><a href="" rel="nofollow"></a></p> <p>This sub-feed of my blog:</p> <p><a href="" rel="nofollow"></a></p> <p>gives access to all my blog posts labelled xtopdf.</p> Converting numeric strings to integers (Python) 2015-08-04T20:08:29-07:00Vasudev Ram <p style="color: grey"> Python recipe 579093 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/formats/">formats</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/numerical/">numerical</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/representation/">representation</a>). </p> <p>This recipe shows how to convert numeric strings into integers, i.e. it emulates the Python int() function - partially. It does not handle negative numbers, floating point numbers, or numbers in other bases than decimal. It is only meant as a simple demo of the steps by which a string containing an integer value, is converted into an actual integer, in Python (and similarly, in other languages).</p> Decorator for defining functions with keyword-only arguments (Python) 2015-07-10T15:25:04-07:00Oscar Byrne <p style="color: grey"> Python recipe 579079 by <a href="/recipes/users/4192487/">Oscar Byrne</a> (<a href="/recipes/tags/arguments/">arguments</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/keyword/">keyword</a>, <a href="/recipes/tags/metadecorator/">metadecorator</a>, <a href="/recipes/tags/positional/">positional</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/python3/">python3</a>). Revision 2. </p> <p>Python2.x implementation of python3's keyword-only arguments (aka arguments that must be specified as keywords, and are not automatically filled in by positional arguments - see PEP 3102).</p> Python 2 nonlocal (Python) 2015-09-08T01:27:04-07:00Ryan Gonzalez <p style="color: grey"> Python recipe 578965 by <a href="/recipes/users/4187447/">Ryan Gonzalez</a> (<a href="/recipes/tags/backport/">backport</a>, <a href="/recipes/tags/nonlocal/">nonlocal</a>, <a href="/recipes/tags/python2/">python2</a>). Revision 2. </p> <p>This implements nonlocal in Python 2...albeit in a slightly ugly way. Tested with CPython 2.7 and PyPy.</p> Peek at Python value stack (Python) 2013-01-08T22:15:21-08:00Dima Tisnek <p style="color: grey"> Python recipe 578412 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/stack/">stack</a>). </p> <p>Poking at value stack, a.k.a. evaluation stack is not allowed in Python, however everything is doable with ctypes.</p> <p>I think this could be useful in debugging complex statements.</p> The many uses of randomness - Part 1 (Python) 2016-06-01T19:36:28-07:00Vasudev Ram <p style="color: grey"> Python recipe 580674 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_number/">random_number</a>). </p> <p>This recipe shows some of the many uses of random numbers, using the random function from the random module from Python's standard library. A subsequent recipe or two will show other uses, both of other functions from the module, and for other purposes.</p> <p>The uses shown in this recipe have to do with using random float values, and scaling them and offsetting them, and also how to get a repeated/predictable series of random numbers.</p>