Most viewed recipes tagged "meta:license=bsd"http://code.activestate.com/recipes/tags/meta:license=bsd/views/2017-04-27T21:26:00-07:00ActiveState Code RecipesImplementing function-based callbacks in Python (Python) 2017-04-19T18:03:11-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/ <p style="color: grey"> Python recipe 580787 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/techniques/">techniques</a>). </p> <p>This recipe shows a simple way of implementing callbacks in Python. There are a few ways this can be done. The way shown here uses a simple function-based approach.</p> <p>In a nutshell, a callback can be informally described like this: function <strong>a</strong> calls function <strong>b</strong>, and wants to make <strong>b</strong> run a specific independent chunk of code at some point during <strong>b</strong>'s execution. We want to be able to vary which chunk of code gets called in different calls to <strong>b</strong>, so it cannot be hard-coded inside <strong>b</strong>. So function <strong>a</strong> passes another function, <strong>c</strong>, to <strong>b</strong>, as one argument, and <strong>b</strong> uses that parameter <strong>c</strong> to call the functionality that <strong>a</strong> wants <strong>b</strong> to call. (Function <strong>b</strong> may pass some parameters to the function represented by <strong>c</strong>, when it calls it. These could be either internally generated, passed from <strong>a</strong>, or a combination of both). So, by changing the value of the function <strong>c</strong> that gets passed to <strong>b</strong> (on different calls to <strong>b</strong>), <strong>a</strong> can change what chunk of code <strong>b</strong> calls.</p> <p>More details and full code, description and output here:</p> <p><a href="https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html</a></p> Simple command-line alarm clock in Python (Python) 2015-10-25T18:27:27-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579117-simple-command-line-alarm-clock-in-python/ <p style="color: grey"> Python recipe 579117 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe shows how to create a simple alarm clock in Python, that can be run from the command line in a terminal. It lets you specify the alarm time in minutes as a command line argument, and prints a wake-up message and beeps a few times, after that time arrives. You can use 0 for the minutes to test it immediately, including to adjust the volume using your speaker controls.</p> Python text-to-speech with pyttsx (Python) 2014-03-02T21:35:15-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578839-python-text-to-speech-with-pyttsx/ <p style="color: grey"> Python recipe 578839 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/pyttsx/">pyttsx</a>, <a href="/recipes/tags/speech/">speech</a>, <a href="/recipes/tags/text_to_speech/">text_to_speech</a>). </p> <p>This recipe shows how to generate computer speech from text using the pyttsx Python library. Text-to-speech, or speech synthesis, has many useful applications.</p> Convert Microsot Excel (XLSX) to PDF with Python and xtopdf (Python) 2015-11-22T22:15:25-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579128-convert-microsot-excel-xlsx-to-pdf-with-python-and/ <p style="color: grey"> Python recipe 579128 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/excel/">excel</a>, <a href="/recipes/tags/formats/">formats</a>, <a href="/recipes/tags/openpyxl/">openpyxl</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/xlsx/">xlsx</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how the basics of to convert the text data in a Microsoft Excel file (XLSX format) to PDF (Portable Document Format). It uses openpyxl to read the XLSX file and xtopdf to generate the PDF file.</p> Validate data easily with JSON Schema (Python) 2015-12-06T21:23:48-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579135-validate-data-easily-with-json-schema/ <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="https://pypi.python.org/pypi/jsonschema" rel="nofollow">https://pypi.python.org/pypi/jsonschema</a></p> <p>JSON Schema: <a href="http://json-schema.org/" rel="nofollow">http://json-schema.org/</a></p> Recognizing speech (speech-to-text) with the Python speech module (Python) 2015-10-22T13:09:02-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579115-recognizing-speech-speech-to-text-with-the-python-/ <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> Classifying characters using nested conditional expressions (Python) 2017-04-27T21:26:00-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580792-classifying-characters-using-nested-conditional-ex/ <p style="color: grey"> Python recipe 580792 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/characters/">characters</a>, <a href="/recipes/tags/classification/">classification</a>, <a href="/recipes/tags/conditional_expressions/">conditional_expressions</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/map/">map</a>). </p> <p>Python has a feature called conditional expressions, similar to C's ternary operator. For example:</p> <p>print n, 'is odd' if n % 2 == 1 else 'is even'</p> <p>Here, the conditional expression is this part of the print statement above:</p> <p>'is odd' if n % 2 == 1 else 'is even'</p> <p>This expression evaluates to 'is odd' if the condition after the if keyword is True, and evaluates to 'is even' otherwise.</p> <p>The Python Language Reference section for conditional expressions shows that they can be nested. This recipe shows that we can use nested conditional expressions (within a return statement in a user-defined function) to classify characters into lowercase letters, uppercase letters, or neither.</p> <p>It also shows how to do the same task using map, lambda and string.join, again with a nested conditional expression, but without using return or a user-defined function.</p> PyDbLite, a small in-memory database engine (Python) 2011-07-18T19:36:04-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/496770-pydblite-a-small-in-memory-database-engine/ <p style="color: grey"> Python recipe 496770 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/database/">database</a>). Revision 4. </p> <p>A small, fast, in-memory database management program</p> <p>The database object supports the iterator protocol, so that requests can be expressed with list comprehensions or generator expressions instead of SQL. The equivalent of :</p> <pre class="prettyprint"><code>cursor.execute("SELECT name FROM table WHERE age=30") rows = cursor.fetchall() </code></pre> <p>is :</p> <pre class="prettyprint"><code>rows = table(age=30) </code></pre> <p>The module stores data in a cPickled file. Records are indexed by a unique record identifier, that can be used for direct access. Since operations are processed in memory they are extremely fast, nearly as fast as SQLite in the few tests I made, and MUCH faster than other pure-Python modules such as Gadfly or KirbyBase. An index can be created on a field to even speed up selections</p> <p>Concurrency control is supported by a version number set for each record</p> <p>Complete documentation is <a href="http://www.pydblite.net">here</a></p> HTTPS httplib Client Connection with Certificate Validation (Python) 2011-01-18T18:30:45-08:00Marcelo Fernándezhttp://code.activestate.com/recipes/users/4173551/http://code.activestate.com/recipes/577548-https-httplib-client-connection-with-certificate-v/ <p style="color: grey"> Python recipe 577548 by <a href="/recipes/users/4173551/">Marcelo Fernández</a> (<a href="/recipes/tags/certificate/">certificate</a>, <a href="/recipes/tags/client/">client</a>, <a href="/recipes/tags/client_server/">client_server</a>, <a href="/recipes/tags/httplib/">httplib</a>, <a href="/recipes/tags/https/">https</a>, <a href="/recipes/tags/networking/">networking</a>, <a href="/recipes/tags/ssl/">ssl</a>, <a href="/recipes/tags/validation/">validation</a>). </p> <p>Despite httplib.HTTPSConnection lets the programmer specify the client's pair of certificates, it doesn't force the underlying SSL library to check the server certificate against the client keys (from the client point of view).</p> <p>This class allows to force this check, to ensure the python client is connecting to the right server.</p> Implementing class-based callbacks in Python (Python) 2017-04-20T23:34:50-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580788-implementing-class-based-callbacks-in-python/ <p style="color: grey"> Python recipe 580788 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/callbacks/">callbacks</a>, <a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a follow-on to this recently posted recipe:</p> <p>Implementing function-based callbacks in Python: <a href="https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351" rel="nofollow">https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351</a></p> <p>This new recipe shows how to create and use callbacks in Python, using classes with methods, instead of plain functions, as was done in the recipe linked above. All other points such as reasons and benefits for using callbacks, are more or less the same as mentioned in the previous recipe, except that class instances can carry state around, so to that extent, the two approaches are different.</p> <p><a href="https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html</a></p> Convert JSON to PDF with Python and xtopdf (Python) 2014-12-10T18:02:14-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578979-convert-json-to-pdf-with-python-and-xtopdf/ <p style="color: grey"> Python recipe 578979 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/sys/">sys</a>). </p> <p>This recipe show the basic steps needed to convert JSON input to PDF output, using Python and xtopdf, a PDF creation toolkit. xtopdf is itself written in Pytho, and uses the ReportLab toolkit internally.</p> <p>We set up some needed values, such as the output PDF file name, the font name and size, the header and footer, and the input lines for the body of the PDF output; all these values are passed in JSON format (in a single dictionary) to a function that uses those values to generate a PDF file with the desired content.</p> <p>The code is intentionally kept simple so as to require the least amount of code needed to demonstrate the techniques involved. But it can be generalized or extended to more complex situations.</p> Convert Microsoft Word files to PDF with DOCXtoPDF (Python) 2013-12-24T23:03:43-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578795-convert-microsoft-word-files-to-pdf-with-docxtopdf/ <p style="color: grey"> Python recipe 578795 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/docx/">docx</a>, <a href="/recipes/tags/microsoft/">microsoft</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/word/">word</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>Yhis recipe shows how to convert Microsoft Word .DOCX files to PDF format, using the python-docx library and my xtopdf toolkit for PDF creation.</p> <p>Note: The recipe has some limitations. E.g. fonts, tables, etc. from the input DOCX file are not preserved in the output PDF file.</p> DDE Client (Python) 2011-04-14T16:33:19-07:00David Naylorhttp://code.activestate.com/recipes/users/4177661/http://code.activestate.com/recipes/577654-dde-client/ <p style="color: grey"> Python recipe 577654 by <a href="/recipes/users/4177661/">David Naylor</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/dde/">dde</a>, <a href="/recipes/tags/win32/">win32</a>). </p> <p>A DDE Client using ctypes for Windows. Unlike the pywin32 implementation this code supports advice requests. </p> Simple drawing tool with Python turtle graphics (Python) 2016-01-27T22:22:01-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580544-simple-drawing-tool-with-python-turtle-graphics/ <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> How a Python function can find the name of its caller (Python) 2015-09-30T15:12:17-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579105-how-a-python-function-can-find-the-name-of-its-cal/ <p style="color: grey"> Python recipe 579105 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sys/">sys</a>). </p> <p>This recipe shows how a Python function can find out the name of its caller, i.e. which other Python function has called it.</p> Converting numeric strings to integers (Python) 2015-08-04T20:08:29-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579093-converting-numeric-strings-to-integers/ <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> Expectation-Maximization (Python) 2011-06-04T11:02:18-07:00Gabriel Synnaevehttp://code.activestate.com/recipes/users/4178198/http://code.activestate.com/recipes/577735-expectation-maximization/ <p style="color: grey"> Python recipe 577735 by <a href="/recipes/users/4178198/">Gabriel Synnaeve</a> (<a href="/recipes/tags/data_mining/">data_mining</a>, <a href="/recipes/tags/machine_learning/">machine_learning</a>). Revision 3. </p> <p>Quick and simple implementation of Gaussian mixture model (with same covariance shapes) based expectation-maximization algorithm.</p> Use PyAudio to play a list of WAV files (Python) 2015-10-22T18:24:08-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579116-use-pyaudio-to-play-a-list-of-wav-files/ <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 pyaudio_play_wav.py chimes.wav</p> <p>or specify a text file containing names of WAV files to play, like this:</p> <p>py pyaudio_play_wav.py -f wav_fil_list.txt</p> <p>The only dependency is PyAudio, which you can install with pip.</p> slugify: make a string usable in a URL or filename (Python) 2010-06-07T04:11:55-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577257-slugify-make-a-string-usable-in-a-url-or-filename/ <p style="color: grey"> Python recipe 577257 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/django/">django</a>, <a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/slug/">slug</a>, <a href="/recipes/tags/slugify/">slugify</a>, <a href="/recipes/tags/url/">url</a>). Revision 2. </p> <p>"Slugify" a string so it is ascii, has only alphanumeric and hyphen characters. Useful for URLs and filenames. This is heavily based on the slugify in Django.</p> <p>Note: presumes that you've <code>import re</code>d higher up in your module.</p> Python method chaining examples (Python) 2016-02-25T19:40:33-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580616-python-method-chaining-examples/ <p style="color: grey"> Python recipe 580616 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/chaining/">chaining</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This recipe shows a few examples of doing method chaining in Python.</p>