Popular recipes tagged "meta:loc=44"http://code.activestate.com/recipes/tags/meta:loc=44/2017-01-17T20:05:10-08:00ActiveState Code RecipesHamming Error Correction Code (Python) 2016-07-23T07:58:14-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580691-hamming-error-correction-code/ <p style="color: grey"> Python recipe 580691 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/encoding/">encoding</a>, <a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>Hamming(7,4) Error Correction Code (ECC).</p> Classifying letters as vowels or consonants and counting their frequencies (Python) 2017-01-17T20:05:10-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580749-classifying-letters-as-vowels-or-consonants-and-co/ <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="https://jugad2.blogspot.in/2017/01/classifying-letters-and-counting-their.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/classifying-letters-and-counting-their.html</a></p> Give Python code a web plus command-line interface with hug (Python) 2017-01-05T16:57:15-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580742-give-python-code-a-web-plus-command-line-interface/ <p style="color: grey"> Python recipe 580742 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/hug/">hug</a>, <a href="/recipes/tags/library/">library</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/user_interface/">user_interface</a>, <a href="/recipes/tags/web/">web</a>, <a href="/recipes/tags/web_server/">web_server</a>). </p> <p>This recipe shows how to take a Python function and wrap it with both a web and a command-line interface, somewhat easily, using the hug Python library. The example used shows how to wrap a function that uses the psutil library to get information on disk partitions. So you can see the disk partition info either via the web browser or the command line. The code for the recipe is shown below. It is also possible to wrap multiple functions in the same Python file, and expose all of them via both the web and the command-line.</p> <p>More information and multiple sample outputs are available here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/give-your-python-function-webcli-hug.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/give-your-python-function-webcli-hug.html</a></p> Generate a PDF cheat sheet for converting 0 to 255 between bin / oct / dec / hex (Python) 2016-10-10T20:40:48-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580706-generate-a-pdf-cheat-sheet-for-converting-0-to-255/ <p style="color: grey"> Python recipe 580706 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/hexadecimal/">hexadecimal</a>, <a href="/recipes/tags/octal/">octal</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/utility/">utility</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to generate a PDF cheat sheet, that contains a table for conversion of the numbers 0 to 255 (the numbers that can fit in one byte) between binary, octal, decimal and hexadecimal representations. The table has four columns, one for each of those bases, and 256 rows, for the numbers 0 to 255.</p> <p>TO use the table, you can look for a number, say in decimal, in the Dec(imal) column (or use the search function of your PDF viewer), then when you find it in some row, just look at the other 3 columns in that row, to find the value of that number in binary, octal and hexadecimal. And use the same procedure if starting with a number in any of the other three bases.</p> Pluggable Python generators (Python) 2016-03-18T19:11:48-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580628-pluggable-python-generators/ <p style="color: grey"> Python recipe 580628 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a simple recipe to show that Python generators are pluggable, i.e., they can be passed as arguments into functions, and then used inside those functions.</p> oscylator (Python) 2015-01-23T10:19:26-08:00Paula Baranowskahttp://code.activestate.com/recipes/users/4191523/http://code.activestate.com/recipes/579010-oscylator/ <p style="color: grey"> Python recipe 579010 by <a href="/recipes/users/4191523/">Paula Baranowska</a> . </p> <p>modul 3 kolokwium</p> Python - from nD array to flat array (Python) 2014-03-21T16:09:39-07:00Roberto Bellohttp://code.activestate.com/recipes/users/4189498/http://code.activestate.com/recipes/578854-python-from-nd-array-to-flat-array/ <p style="color: grey"> Python recipe 578854 by <a href="/recipes/users/4189498/">Roberto Bello</a> (<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/nd/">nd</a>). </p> <p>From a multidimensional array to a flat array avoiding numpy. The code could be better?</p> No more UnicodeDecodeErrors when printing (Python) 2013-12-17T23:02:37-08:00Ádám Szieberthhttp://code.activestate.com/recipes/users/4188745/http://code.activestate.com/recipes/578788-no-more-unicodedecodeerrors-when-printing/ <p style="color: grey"> Python recipe 578788 by <a href="/recipes/users/4188745/">Ádám Szieberth</a> (<a href="/recipes/tags/decode/">decode</a>, <a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/print/">print</a>, <a href="/recipes/tags/terminal/">terminal</a>, <a href="/recipes/tags/unicode/">unicode</a>). Revision 2. </p> <p>I hate getting UnicodeDecodeErrors when I use print() for bugtracing or for some other reason. I decided to make a module which spares me the headache.</p> Convert from decimal to base-n for any positive n less than or equal to 10 (Python) 2013-08-09T05:56:16-07:00Samuel James Ericksonhttp://code.activestate.com/recipes/users/4187478/http://code.activestate.com/recipes/578630-convert-from-decimal-to-base-n-for-any-positive-n-/ <p style="color: grey"> Python recipe 578630 by <a href="/recipes/users/4187478/">Samuel James Erickson</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/octal/">octal</a>). </p> <p>Takes in any non-negative integer and converts to the desired base-n for 1&lt;=n&lt;=10.</p> Caesar Ciphers Program in Python (Python) 2013-06-03T17:52:24-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578545-caesar-ciphers-program-in-python/ <p style="color: grey"> Python recipe 578545 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/cipher/">cipher</a>, <a href="/recipes/tags/encryption_decryption/">encryption_decryption</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This script was written for an article I wrote it encrypts and decrypts any plain text into a Caesar Cipher message. For more information <a href="http://thelivingpearl.com/2013/06/03/caesar-ciphers-in-python/">Caesar Ciphers In Python</a></p> Dynamically create a dictionary of ascending paths (Python) 2012-12-03T22:50:47-08:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578354-dynamically-create-a-dictionary-of-ascending-paths/ <p style="color: grey"> Python recipe 578354 by <a href="/recipes/users/4181178/">Scott S-Allen</a> (<a href="/recipes/tags/bootstrap/">bootstrap</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/flexible/">flexible</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/portable/">portable</a>). </p> <p>Ripped from a more elaborate bootstrap script. A sequential set of parent-directories were required for environment variables and subsequent, portable. auto-discovery. </p> Eject\close CDRom door with IronPython using CPython libraries (Text) 2012-12-21T15:54:56-08:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578388-ejectclose-cdrom-door-with-ironpython-using-cpytho/ <p style="color: grey"> Text recipe 578388 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/cdrom/">cdrom</a>, <a href="/recipes/tags/close/">close</a>, <a href="/recipes/tags/eject/">eject</a>). </p> <p>Of course, ActivePython is a great stuff and there are not problems which it can not solve. But I do like experiment with different technologies. The following script demonstrates how you can eject\close CDRom door with IronPython using standard CPython libraries.</p> Using doctests to verify a module's export list (Python) 2012-09-19T19:13:20-07:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578266-using-doctests-to-verify-a-modules-export-list/ <p style="color: grey"> Python recipe 578266 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/doctest/">doctest</a>, <a href="/recipes/tags/export/">export</a>, <a href="/recipes/tags/module/">module</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>If you aren't very careful, modules can end up exporting more symbols than you intend. For example, everything that you import is added to your module's name-space. Then there's scaffolding and other stuff intended for internal use. The usual advice is to name everything with a leading underscore, but that gets complicated fast: "import sys as _sys", "from os import environ as _environ". The alternative is to use "__all__ " to define exactly what you want to export, but then you need to maintain it as you add things to your module. <em>Or do you?</em></p> Multidimensional list (Python) 2012-05-12T06:21:11-07:00Glennhttp://code.activestate.com/recipes/users/4171639/http://code.activestate.com/recipes/578127-multidimensional-list/ <p style="color: grey"> Python recipe 578127 by <a href="/recipes/users/4171639/">Glenn</a> . Revision 2. </p> <p>Pass a tuple of positive integers of length N (any length) as dimensions to MDarray, and a list is created with the length equal to the product NNN of the dimensions specified in the tuple. The items are initialized to None, or to the value of the optional init parameter. As a special case, passing an integer for dims creates a square array of that size (converts the integer to a tuple of length two with the same value for both entries).</p> <p>You can then index the list with single numbers in range(NNN), or by a tuple of the same length N with which the list was created.</p> <pre class="prettyprint"><code>ary = MDarray(( 5, 6, 8 ), 0 ) ix = ary.index_from_tuple(( 2, 4, 3 )) print( ix ) # 87 print( ary[ 2, 4, 2 ], ary[ 2, 4, 3 ], ary[ 2, 4, 4 ]) # 0, 0, 0 print( ary[ 86 ], ary[ 87 ], ary[ 88 ]) # 0, 0, 0 ary[ 2, 4, 3 ] = 25 print( ary[ 2, 4, 2 ], ary[ 2, 4, 3 ], ary[ 2, 4, 4 ]) # 0, 25, 0 print( ary[ 86 ], ary[ 87 ], ary[ 88 ]) # 0, 25, 0 </code></pre> Simplified, highly optimized LRU Cache (Python) 2011-12-01T00:57:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577969-simplified-highly-optimized-lru-cache/ <p style="color: grey"> Python recipe 577969 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>). Revision 3. </p> <p>An O(1) LRU cache. Short, sweet, and fast. No bells and whistles.</p> Splash Screen GTK (Python) 2011-10-24T13:34:29-07:00Nicolas Coelhohttp://code.activestate.com/recipes/users/4179691/http://code.activestate.com/recipes/577919-splash-screen-gtk/ <p style="color: grey"> Python recipe 577919 by <a href="/recipes/users/4179691/">Nicolas Coelho</a> (<a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/screen/">screen</a>, <a href="/recipes/tags/simple/">simple</a>, <a href="/recipes/tags/splash/">splash</a>). </p> <p>This is a simple example showing how to put a splash screen to work in a GTK app.</p> Conversion of PIL Image and numpy array (Python) 2011-03-05T03:47:56-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/ <p style="color: grey"> Python recipe 577591 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/pil/">pil</a>). Revision 2. </p> <p>The utility function that converts PIL image to numpy array and vice versa.</p> dict2xml (Python) 2011-07-19T12:32:57-07:00nuggetierhttp://code.activestate.com/recipes/users/4178225/http://code.activestate.com/recipes/577739-dict2xml/ <p style="color: grey"> Python recipe 577739 by <a href="/recipes/users/4178225/">nuggetier</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 4. </p> <p>convert dictionary / list structures into xml structure</p> Web Testing using Twill (Python) 2010-11-25T08:28:11-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577465-web-testing-using-twill/ <p style="color: grey"> Python recipe 577465 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/qa/">qa</a>, <a href="/recipes/tags/quality_assurance/">quality_assurance</a>, <a href="/recipes/tags/twill/">twill</a>, <a href="/recipes/tags/web_testing/">web_testing</a>). Revision 2. </p> <p>A simple script to demonstrate web testing using Twill.</p> Random numbers with arbitrary probability distribution. (Python) 2010-06-14T17:02:41-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577264-random-numbers-with-arbitrary-probability-distribu/ <p style="color: grey"> Python recipe 577264 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). Revision 2. </p> <p>Generating N random numbers that probability distribution fits to any given function curve.</p>