Popular recipes tagged "meta:loc=14"http://code.activestate.com/recipes/tags/meta:loc=14/2017-03-27T13:20:41-07:00ActiveState Code RecipesA pseudo-echo, (or printf), function for any Python version. (Python) 2017-01-20T22:17:23-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/580750-a-pseudo-echo-or-printf-function-for-any-python-ve/ <p style="color: grey"> Python recipe 580750 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/amiga/">amiga</a>, <a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/echo/">echo</a>, <a href="/recipes/tags/fs_uae/">fs_uae</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/printf/">printf</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/winuae/">winuae</a>). </p> <p>A simple example of having a pseudo-echo using sys.stdout.write...</p> <p>This gives exactly the same results from Python Versions, 1.4.0, 2.0.1, 2.5.6, 2.6.9, 3.4.3 and 3.5.2 on various platforms including the classic AMIGA A1200.</p> <p>Enjoy finding simple solutions to often very difficult problems...</p> <p>Bazza.</p> File selector dialog in batch (Batch) 2017-03-27T13:20:41-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580665-file-selector-dialog-in-batch/ <p style="color: grey"> Batch recipe 580665 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/directory/">directory</a>). Revision 2. </p> <p>Runs mshta hidden to open a file dialog and get the path of the file selected. No parameters can be passed to the dialog. EDITED: I did a subroutine with the snippet</p> Swapping two variables without using a third (temporary) variable (Python) 2015-09-19T19:51:22-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579101-swapping-two-variables-without-using-a-third-tempo/ <p style="color: grey"> Python recipe 579101 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/swap/">swap</a>). </p> <p>This recipe shows how to swap the values of two variables without making use of a third, temporary variable. The traditional method of swapping two variables is using a temp variable.</p> <p>The first method shown here, swaps two variables an and b without using a temp variable. The variables a and b are integers:</p> <p>a = 1, b = 2</p> <h4 id="prints-original-values-of-a-and-b-ie-1-and-2">Prints original values of a and b, i.e. 1 and 2:</h4> <p>print a, b a = a + b b = a - b a = a - b</p> <h4 id="prints-swapped-values-of-a-and-b-ie-2-and-1">Prints swapped values of a and b, i.e. 2 and 1:</h4> <p>print a, b</p> <p>The above swap method, using arithmetic expressions, will not work for non-numeric data types, and may also not work (at least in some cases) for floats. But the method below should work for any type of Python variable:</p> <p>It even works for function objects. If you have:</p> <p>def foo(): print "This is foo()." def bar(): print "This is bar()."</p> <p>and then:</p> <p>foo(), bar()</p> <p>and then:</p> <p>foo, bar = bar, foo</p> <p>then see what happens to values foo and bar, when you do:</p> <p>foo() bar()</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> search file extensions in directory (Python) 2014-04-17T22:33:39-07:00Keisuke URAGOhttp://code.activestate.com/recipes/users/668964/http://code.activestate.com/recipes/578862-search-file-extensions-in-directory/ <p style="color: grey"> Python recipe 578862 by <a href="/recipes/users/668964/">Keisuke URAGO</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/finder/">finder</a>). </p> <p>This code is find and print file extensions from directory.</p> Factorial (Python) 2014-03-08T13:09:41-08:00Johnhttp://code.activestate.com/recipes/users/4189390/http://code.activestate.com/recipes/578848-factorial/ <p style="color: grey"> Python recipe 578848 by <a href="/recipes/users/4189390/">John</a> (<a href="/recipes/tags/factorial/">factorial</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/loops/">loops</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/simple/">simple</a>). </p> <p>Just a simple factorial program I made in Python 3.</p> Formating strings (print a table) (Batch) 2013-06-18T07:52:03-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578562-formating-strings-print-a-table/ <p style="color: grey"> Batch recipe 578562 by <a href="/recipes/users/4184115/">greg zakharov</a> (<a href="/recipes/tags/format/">format</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/table/">table</a>). </p> <p>As you know there are no escape characters such as "\t" in the windows command language but it does not mean that we can not format text. Command prompt has its own tricks. At firstly, you need declare enabledelayedexpansion after setlocal command in your batch file to get access for some interesting things; secondly, use &lt;code&gt;&lt;nul set /p "str=[string]"&lt;/code&gt; construction which is equal print function in C language. OK, next batch file print multiplication table.</p> Basic Multiplication in Python (Python) 2013-06-18T15:21:08-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578564-basic-multiplication-in-python/ <p style="color: grey"> Python recipe 578564 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/multiplication/">multiplication</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Here is a very simple program in Python to multiply 2 numbers. I worte for my blog <a href="http://thelivingpearl.com/basic-multiplication-in-python/">Captain DeadBones Chronicles</a></p> Enable Clear Type font smoothing on Windows (pywin32 version) (Python) 2013-03-22T11:14:44-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578499-enable-clear-type-font-smoothing-on-windows-pywin3/ <p style="color: grey"> Python recipe 578499 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/fontsmoothing/">fontsmoothing</a>, <a href="/recipes/tags/pywin32/">pywin32</a>, <a href="/recipes/tags/windows_api/">windows_api</a>). Revision 2. </p> <p>Running the script without parameters will enable Clear Type font smoothing. Pass <code>0</code>, <code>false</code>, <code>off</code>, or <code>disable</code> to turn off Clear Type.</p> <p>This version requires the <code>pywin32</code> module from <a href="http://sourceforge.net/projects/pywin32/files/pywin32/Build%2520218/" rel="nofollow">http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/</a></p> <p>For a <code>ctypes</code>-based version see: <a href="http://code.activestate.com/recipes/578500-enable-clear-type-font-smoothing-on-windows-ctypes/" rel="nofollow">http://code.activestate.com/recipes/578500-enable-clear-type-font-smoothing-on-windows-ctypes/</a></p> Python subprocess: hide console on Windows (Python) 2013-07-29T05:33:49-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578300-python-subprocess-hide-console-on-windows/ <p style="color: grey"> Python recipe 578300 by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>It creates a new <em>hidden</em> window, so it will work in frozen apps (.exe).</p> Page Counter (Python) 2012-07-02T22:41:43-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578184-page-counter/ <p style="color: grey"> Python recipe 578184 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/achive/">achive</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/counter/">counter</a>, <a href="/recipes/tags/old/">old</a>, <a href="/recipes/tags/page/">page</a>). </p> <p>This page counter was a CGI experiment in what it would take to implement such a simple concept. This is committed for archival to be run under Python 2.5 or later versions.</p> Bash Prompt Rainbow Color Chart (Bash) 2011-10-02T21:39:21-07:00userendhttp://code.activestate.com/recipes/users/4179007/http://code.activestate.com/recipes/577889-bash-prompt-rainbow-color-chart/ <p style="color: grey"> Bash recipe 577889 by <a href="/recipes/users/4179007/">userend</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/prompt/">prompt</a>, <a href="/recipes/tags/ps1/">ps1</a>, <a href="/recipes/tags/rainbow/">rainbow</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/terminal/">terminal</a>). </p> <p>This is a simple loop that displays a rainbow of different colors.</p> Directory Deleter (Python) 2011-07-27T15:23:29-07:00mJackhttp://code.activestate.com/recipes/users/4178791/http://code.activestate.com/recipes/577809-directory-deleter/ <p style="color: grey"> Python recipe 577809 by <a href="/recipes/users/4178791/">mJack</a> (<a href="/recipes/tags/delete/">delete</a>, <a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/directory/">directory</a>). </p> <p>This small program lets a user delete a directory on their Windows system. Also good for learning from.</p> Komodo JS Macro: colorize the output console with a different color scheme (JavaScript) 2011-06-24T17:52:48-07:00Todd Whitemanhttp://code.activestate.com/recipes/users/2666241/http://code.activestate.com/recipes/577770-komodo-js-macro-colorize-the-output-console-with-a/ <p style="color: grey"> JavaScript recipe 577770 by <a href="/recipes/users/2666241/">Todd Whiteman</a> (<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/scintilla/">scintilla</a>, <a href="/recipes/tags/toddw/">toddw</a>). </p> <p>It's possible to colorize any Scintilla based widget (the editor part) in Komodo using a color scheme. In Komodo there is the "Fonts and Color" preferences that get applied to all scintilla widgets, but you can manually change individual scintilla widgets as you wish.</p> <p>This macro changes the color scheme of the Command Output tab to use the "Dark" scheme.</p> BitSort (Python) 2011-06-19T14:03:56-07:00wong2http://code.activestate.com/recipes/users/4178345/http://code.activestate.com/recipes/577763-bitsort/ <p style="color: grey"> Python recipe 577763 by <a href="/recipes/users/4178345/">wong2</a> (<a href="/recipes/tags/sort/">sort</a>). </p> <p>BitSort in python. using bitarray library</p> Split Strings w/ Multiple Separators (Python) 2011-03-20T20:33:13-07:00Kenneth Reitzhttp://code.activestate.com/recipes/users/4177394/http://code.activestate.com/recipes/577616-split-strings-w-multiple-separators/ <p style="color: grey"> Python recipe 577616 by <a href="/recipes/users/4177394/">Kenneth Reitz</a> (<a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/string_parsing/">string_parsing</a>). Revision 5. </p> <p>Splits strings with multiple separators instead of one (e.g. <code>str.split()</code>).</p> map_longest and map_strict (Python) 2011-05-06T17:35:17-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577687-map_longest-and-map_strict/ <p style="color: grey"> Python recipe 577687 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/iteration/">iteration</a>, <a href="/recipes/tags/map/">map</a>). Revision 2. </p> <p>In Python 3, the map builtin silently drops any excess items in its input:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; b = [2, 3, 4] &gt;&gt;&gt; c = [3, 4, 5, 6, 7] &gt;&gt;&gt; list(map(lambda x,y,z: x*y+z, a, b, c)) [5, 10, 17] </code></pre> <p>In Python 2, map pads the shorter items with None, while itertools.imap drops the excess items. Inspired by this, and by itertools.zip_longest, I have map_longest that takes a value to fill missing items with, and map_strict that raises an exception if a value is missing.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(map_longest(lambda x,y,z: x*y+z, a, b, c, fillvalue=0)) [5, 10, 17, 6, 7] &gt;&gt;&gt; list(map_strict(lambda x,y,z: x*y+z, a, b, c)) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "&lt;stdin&gt;", line 9, in map_strict ValueError: too few items in iterable </code></pre> naive natural sort (Python) 2011-08-13T16:47:36-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577679-naive-natural-sort/ <p style="color: grey"> Python recipe 577679 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/sort/">sort</a>). </p> <p>I wrote this after reading The Alphanum Algorithm (<a href="http://www.davekoelle.com/alphanum.html" rel="nofollow">http://www.davekoelle.com/alphanum.html</a>) by David Koelle a few years ago; my goal was to improve the performances of the Python version of his scripts.</p> <p>My version is approximatly 10 times faster than it's <code>alphanum.py</code> and about 3 times faster than the <code>alphanum.py_v2.4</code> on my computer, yielding the same results (for non-unicode at least).</p> <p><strong>Note</strong>: see the version of wizkid in the comments which is even faster.</p> Infinite list of primes! Yay! (Python) 2010-07-20T04:05:00-07:00Alejandro Peraltahttp://code.activestate.com/recipes/users/4174433/http://code.activestate.com/recipes/577318-infinite-list-of-primes-yay/ <p style="color: grey"> Python recipe 577318 by <a href="/recipes/users/4174433/">Alejandro Peralta</a> (<a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/primes/">primes</a>). </p> <p>It's an iterator that returns prime numbers. </p> <p>Got the idea from here: <a href="http://www.cs.hmc.edu/%7Eoneill/papers/Sieve-JFP.pdf" rel="nofollow">www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf</a></p> Using a getter for a one-time calculation of a JavaScript object attribute (JavaScript) 2010-07-16T18:10:51-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577310-using-a-getter-for-a-one-time-calculation-of-a-jav/ <p style="color: grey"> JavaScript recipe 577310 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/getter/">getter</a>, <a href="/recipes/tags/performance/">performance</a>). </p> <p>This is a technique for using a JavaScript getter to calculate the value of an attribute <strong>just the first time</strong>. Rather than caching the value in some private variable and returning it, you just delete the getter and put the calculated value in its place.</p> <p>Note: I'd read about this technique ages ago, but forgot the details and had to look-up "delete" for removing the getter. :)</p>