Popular recipes tagged "meta:loc=31"http://code.activestate.com/recipes/tags/meta:loc=31/2016-12-11T13:39:08-08:00ActiveState Code RecipesQuick-and-dirty Windows drive detector (Python) 2016-09-20T17:46:37-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580699-quick-and-dirty-windows-drive-detector/ <p style="color: grey"> Python recipe 580699 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/drives/">drives</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/system_programming/">system_programming</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This is a quick-and-dirty Python script to detect the currently available drives on your Windows PC.</p> Add days to a calendar date- Julian date (Batch) 2016-08-17T10:32:03-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580683-add-days-to-a-calendar-date-julian-date/ <p style="color: grey"> Batch recipe 580683 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/calendar/">calendar</a>). </p> <p>The trick to add days to a date is to convert the date to a serial, do a normal addition then convert the result back to a date. The serial I use here is the Julian day number used by the astronomers because its conversion formulas arewell documented. If you check the julian day number on internet the result can vary by a day, because julian days start at noon and i'm not making this distinction. </p> Permutations by recursive Heap's method (Batch) 2016-06-11T08:42:41-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580678-permutations-by-recursive-heaps-method/ <p style="color: grey"> Batch recipe 580678 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/permutation/">permutation</a>, <a href="/recipes/tags/recursion/">recursion</a>). </p> <p>Setlocal / endlocal allow for recursion in batch. Values can be passed by value and by reference, the later can return a value to the caller.</p> Text widget width and height in pixels (Tkinter) (Python) 2016-12-11T13:39:08-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/578887-text-widget-width-and-height-in-pixels-tkinter/ <p style="color: grey"> Python recipe 578887 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 8. </p> <p>The solution consists in putting the Text widget inside a frame, forcing the frame to a fixed size by deactivating size propagation and configuring the Text widget to expand and fill both directions (to stick to the frame borders).</p> <p><a href="https://stackoverflow.com/questions/14887610/how-to-specify-the-dimensions-of-a-tkinter-text-box-in-pixels" rel="nofollow">https://stackoverflow.com/questions/14887610/how-to-specify-the-dimensions-of-a-tkinter-text-box-in-pixels</a></p> Print selected text pages to PDF with Python, selpg and xtopdf on Linux (Bash) 2014-10-29T17:38:10-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578954-print-selected-text-pages-to-pdf-with-python-selpg/ <p style="color: grey"> Bash recipe 578954 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_files/">text_files</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/unix/">unix</a>). </p> <p>This recipe shows how to use selpg, a Linux command-line utility written in C, together with xtopdf, a Python toolkit for PDF creation, to print only a selected range of pages from a text file, to a PDF file, for display or print purposes. The way to do this is to run the selpg utility at the Linux command line, with options specifying the start and end pages of the range, and pipe its output to the StdinToPDF.py program, which is a part of the xtopdf toolkit.</p> Guess a number (Python) 2014-11-15T09:06:05-08:00Benoithttp://code.activestate.com/recipes/users/4191135/http://code.activestate.com/recipes/578962-guess-a-number/ <p style="color: grey"> Python recipe 578962 by <a href="/recipes/users/4191135/">Benoit</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sample/">sample</a>). </p> <p>This is just a little game to explain some very basic function in Python for beginner. RANDOM, Loop, Try and except... </p> Using pngcanvas, a pure Python PNG library (Python) 2014-01-15T22:45:11-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578811-using-pngcanvas-a-pure-python-png-library/ <p style="color: grey"> Python recipe 578811 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/image_processing/">image_processing</a>, <a href="/recipes/tags/imaging/">imaging</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/pngcanvas/">pngcanvas</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This recipe shows a simple example of how to use pngcanvas, a pure Python library for PNG image creation.</p> Run some command every x seconds (Python) 2013-09-24T13:26:19-07:00Rutger Saalminkhttp://code.activestate.com/recipes/users/4187940/http://code.activestate.com/recipes/578673-run-some-command-every-x-seconds/ <p style="color: grey"> Python recipe 578673 by <a href="/recipes/users/4187940/">Rutger Saalmink</a> (<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/iterative/">iterative</a>, <a href="/recipes/tags/os/">os</a>, <a href="/recipes/tags/run/">run</a>). </p> <p>Sometimes it comes in handy to run some command every minute or hour. For example, have some process check your ip address every minute with 'ifconfig' or run some purgescript at midnight through 'sqlplus.exe @purge_aux_table'.</p> Simple Method to Compute Pi in Python (Python) 2013-06-03T18:06:43-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578547-simple-method-to-compute-pi-in-python/ <p style="color: grey"> Python recipe 578547 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This script was written for an article I wrote about computing Pi with Python. For more information <a href="http://thelivingpearl.com/2013/05/28/computing-pi-with-python/">How to Compute Pi in Python</a></p> Python+OpenCV: Camera frame grab and sobel display (Python) 2012-03-22T07:21:35-07:00J W Jhttp://code.activestate.com/recipes/users/4181154/http://code.activestate.com/recipes/578085-pythonopencv-camera-frame-grab-and-sobel-display/ <p style="color: grey"> Python recipe 578085 by <a href="/recipes/users/4181154/">J W J</a> (<a href="/recipes/tags/opencv/">opencv</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>A couple snippets grabbed from other sources and put together here. Demonstrates: Conversion to greyscale: cv.CvtColor, Sobel operator: cv.Sobel, Smoothing: cv.Smooth</p> Polynomial Factoring Using Rational Root Theorem (Python) 2011-12-03T23:51:07-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577974-polynomial-factoring-using-rational-root-theorem/ <p style="color: grey"> Python recipe 577974 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/poly/">poly</a>, <a href="/recipes/tags/polynomial/">polynomial</a>, <a href="/recipes/tags/polyroots/">polyroots</a>, <a href="/recipes/tags/poly_roots/">poly_roots</a>, <a href="/recipes/tags/rational/">rational</a>, <a href="/recipes/tags/rational_root_theorem/">rational_root_theorem</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/roots/">roots</a>, <a href="/recipes/tags/theorem/">theorem</a>). </p> <p>This algorithm factors a polynomial but will only factor it by giving the rational roots. For instance if one of the roots in the polynomial was irrational, the polynomial would not be factored correctly. </p> Named constant type (Python) 2012-06-13T13:43:47-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577984-named-constant-type/ <p style="color: grey"> Python recipe 577984 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/constant/">constant</a>, <a href="/recipes/tags/named/">named</a>, <a href="/recipes/tags/python/">python</a>). Revision 7. </p> <p>A simple constant type which overrides the base integer type to provide a useful name on str().</p> Turn @some_decorator() into @some_decorator (Python) 2011-08-04T18:48:36-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577820-turn-some_decorator-into-some_decorator/ <p style="color: grey"> Python recipe 577820 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/decorators/">decorators</a>). </p> <p>A decorator factory is a function that returns a decorator based on the arguments you pass in. Sometimes you make a decorator factory to cover uncommon use cases. In that case you'll probably use default arguments to cover your common case.</p> <p>The problem with this, however, is that you still have to use the call syntax (with no arguments) to get the common-use-case decorator, as demonstrated in the recipe title. This recipe effectively makes it so that your decorator factory and your common-use-case decorator have the same name (and actually the same object).</p> Decimal to Binary Conversion (Python) 2010-12-05T22:26:29-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577488-decimal-to-binary-conversion/ <p style="color: grey"> Python recipe 577488 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Unlike bin() function of Python, it can convert floating-point numbers also.</p> Easy XML (Python) 2011-05-03T23:06:30-07:00Yony Kochinskihttp://code.activestate.com/recipes/users/4175703/http://code.activestate.com/recipes/577451-easy-xml/ <p style="color: grey"> Python recipe 577451 by <a href="/recipes/users/4175703/">Yony Kochinski</a> (<a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>Being that I only use XML on occasion for some very basic features, I find myself having to re-learn the appropriate syntax every time. After doing this for the n-th time, I finally decided to wrap this syntax in something that's much more intuitive (to me, at least). The main goal is to make this as simple an API as possible, at the cost of some features.</p> query yes/no (Python) 2010-03-09T17:57:28-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577058-query-yesno/ <p style="color: grey"> Python recipe 577058 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/ask/">ask</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/no/">no</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/raw_input/">raw_input</a>, <a href="/recipes/tags/yes/">yes</a>). Revision 2. </p> <p>Ask the user a question using raw_input() and looking something like this:</p> <pre class="prettyprint"><code>QUESTION [Y/n] ...validate... </code></pre> <p>See also: <a href="http://code.activestate.com/recipes/577096/">Recipe 577096</a> (query custom answers), <a href="http://code.activestate.com/recipes/577097/">Recipe 577097</a> (query yes/no/quit), <a href="http://code.activestate.com/recipes/577098/">Recipe 577098</a> (query long), <a href="http://code.activestate.com/recipes/577099/">Recipe 577099</a> (query)</p> Number To Words Converter (100 => One Hundred) (Java) 2010-07-18T11:17:52-07:00st0lehttp://code.activestate.com/recipes/users/4174421/http://code.activestate.com/recipes/577312-number-to-words-converter-100-one-hundred/ <p style="color: grey"> Java recipe 577312 by <a href="/recipes/users/4174421/">st0le</a> (<a href="/recipes/tags/conversions/">conversions</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/java/">java</a>, <a href="/recipes/tags/number/">number</a>). </p> <p>Converts Integers to Words.</p> AutoRename.py (Python) 2010-08-01T21:39:54-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577342-autorenamepy/ <p style="color: grey"> Python recipe 577342 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/rename/">rename</a>). </p> <p>A friend was trying to open files with Korean names and was finding that Windows 7 was giving him errors. There were well over 22,000 files to deal with, and he did not want to rename them all individually. As a result, this program was quickly written to help him in renaming his vast assortment of files and folders so that he could easily open them in his resource viewer application.</p> xnview backup files remove utility (Python) 2010-07-06T18:09:43-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577281-xnview-backup-files-remove-utility/ <p style="color: grey"> Python recipe 577281 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/backup/">backup</a>, <a href="/recipes/tags/cleanup/">cleanup</a>, <a href="/recipes/tags/remove/">remove</a>, <a href="/recipes/tags/xnview/">xnview</a>). Revision 3. </p> <p>Currently XnView image viewer (versions up to 1.97.6) correctly rotate images only when option "[x] make backup" set.</p> <p>Backup files have added name part '.xnbak' before extension:</p> <pre class="prettyprint"><code>original file: IMG0001.jpg backup file: IMG0001.xnbak.jpg </code></pre> <p>Attached script take a root directory in command line and remove backup files created by XnView only when rotated file present in same directory.</p> Python script main line for graceful exception handling and logging (Python) 2010-09-11T05:46:59-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577258-python-script-main-line-for-graceful-exception-han/ <p style="color: grey"> Python recipe 577258 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/main/">main</a>, <a href="/recipes/tags/mainline/">mainline</a>, <a href="/recipes/tags/script/">script</a>). Revision 5. </p> <p>This is a recipe is often use for the mainline of my Python scripts. With this recipe your Python script will:</p> <ul> <li>gracefully handle <code>Ctrl+C</code> (i.e. <code>KeyboardInterrupt</code>)</li> <li>log an error (using the <code>logging</code> module) for uncaught exceptions, importantly <strong>with the file and line number</strong> in your Python code where the exception was raised</li> <li>gracefully ignore a closed output pipe (common when the user pipes your script through <code>less</code> and terminates that)</li> <li>if your script logger is enabled for <code>DEBUG</code> level logging, a full traceback will be shown for an uncaught exception</li> </ul> <p>Presumptions:</p> <ul> <li><p>you have a global <code>log</code> variable a la:</p> <pre class="prettyprint"><code>import logging log = logging.setLevel("scriptname") </code></pre></li> <li><p>your script's entry point is a function <code>def main(argv): ...</code></p></li> </ul>