Popular recipes tagged "meta:loc=59"http://code.activestate.com/recipes/tags/meta:loc=59/2017-01-14T22:35:17-08:00ActiveState Code RecipesTwo quick functions for object introspection (Python) 2017-01-14T22:35:17-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580747-two-quick-functions-for-object-introspection/ <p style="color: grey"> Python recipe 580747 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/reflection/">reflection</a>). </p> <p>This recipe shows two quick-and-clean :) utility functions for introspection of Python objects. They are meant to be used while working interactively in the reular Python shell or in the IPython shell. Both of them display attributes of any given object passed as the argument. The first function displays all attributes. The second function only displays atttributes that do not begin and end with a double underscore, so as to filter out "dunder" methods a.k.a. "special" methods - like __len__, __str__, __repr__, etc. The first function - oa(o) , where o is some object - does the same as dir(o), but is useful - in IPython - because, dir(o) output will scroll off the screen if the output is long, since it prints the attributes vertically, one per line, while oa(o) prints them horizontally, so has less chance of the output scrolling off, and the output also occupies fewer lines on the screen, so is easier to scan quickly. The second function - oar(o) - is like oa(o), but filters out attribute names that begin and end with a dunder. So it is useful in both IPython and Python.</p> <p>More information and outputs here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/two-simple-python-object-introspection.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/two-simple-python-object-introspection.html</a></p> States to Regions (Python) 2016-05-09T22:24:26-07:00Jackson Killianhttp://code.activestate.com/recipes/users/4194060/http://code.activestate.com/recipes/580661-states-to-regions/ <p style="color: grey"> Python recipe 580661 by <a href="/recipes/users/4194060/">Jackson Killian</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/regions/">regions</a>, <a href="/recipes/tags/states/">states</a>). </p> <p>Python dictionary mapping two letter state abbreviations to their respective regions of the country (i.e. Midwest, North East, etc.) N - North East W - West M - Mid West S - South O - Other</p> [python3-tk/ttk] Onager Scratchpad (Python) 2016-04-24T02:34:03-07:00Mickey Kocichttp://code.activestate.com/recipes/users/4193984/http://code.activestate.com/recipes/580650-python3-tkttk-onager-scratchpad/ <p style="color: grey"> Python recipe 580650 by <a href="/recipes/users/4193984/">Mickey Kocic</a> (<a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/ttk/">ttk</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 2. </p> <p>I wrote this simple text editor to use for my diary. It's customized the way I like it, but the code is set up so it's easy for other people to change bg, fg, font, etc. I've also compiled a standalone Windows executable (thank you very much ActiveState! without ActivePython the compilation would have been impossible). Anyone who wants a copy of the executable is free to message or email me.</p> <p>NOTE: If you get an error that the theme is not recognized, just comment out line 18 or run the following code in your python3 interpreter:</p> <pre class="prettyprint"><code>&gt;&gt;&gt;from tkinter.ttk import Style &gt;&gt;&gt;s = Style() &gt;&gt;&gt;s.theme_use() </code></pre> <p>You'll get a list of the available themes and can replace the 'alt' in line 18 with any one of them you want.</p> Read all Excel cells in all sheets without telling the range (Python) 2015-12-16T22:28:39-08:00Alfred Wanghttp://code.activestate.com/recipes/users/4193275/http://code.activestate.com/recipes/579137-read-all-excel-cells-in-all-sheets-without-telling/ <p style="color: grey"> Python recipe 579137 by <a href="/recipes/users/4193275/">Alfred Wang</a> (<a href="/recipes/tags/excel/">excel</a>). </p> <p>I constantly need to read an excel file. However, it seems there is no built in functions in xlwings to automatically read all cells without telling xlwings the range you want to read. So I wrote this function to achieve this.</p> Python AST to XML (Python) 2015-02-05T20:18:08-08:00Robert Stewarthttp://code.activestate.com/recipes/users/4191612/http://code.activestate.com/recipes/579019-python-ast-to-xml/ <p style="color: grey"> Python recipe 579019 by <a href="/recipes/users/4191612/">Robert Stewart</a> (<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>Convert Python ASTs to XML files for reading in other languages.</p> Python Dictionary of US States and Territories (Python) 2014-10-11T19:56:18-07:00Jeff vGhttp://code.activestate.com/recipes/users/4190943/http://code.activestate.com/recipes/578949-python-dictionary-of-us-states-and-territories/ <p style="color: grey"> Python recipe 578949 by <a href="/recipes/users/4190943/">Jeff vG</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/states/">states</a>, <a href="/recipes/tags/territories/">territories</a>). </p> <p>Handy for matching state abbreviations with their names. Also for iteration.</p> print qrcode to usb termal RP80 printer from web base app (PHP) 2014-09-09T09:35:32-07:00imam feriantohttp://code.activestate.com/recipes/users/633541/http://code.activestate.com/recipes/578930-print-qrcode-to-usb-termal-rp80-printer-from-web-b/ <p style="color: grey"> PHP recipe 578930 by <a href="/recipes/users/633541/">imam ferianto</a> (<a href="/recipes/tags/php/">php</a>, <a href="/recipes/tags/printing/">printing</a>, <a href="/recipes/tags/qrcode/">qrcode</a>). </p> <p>this script will print qrcode from php script. the printer is attached on webserver usb port, see my other article here: <a href="http://code.activestate.com/recipes/578925-print-directly-from-web-application-to-poseps-ther/" rel="nofollow">http://code.activestate.com/recipes/578925-print-directly-from-web-application-to-poseps-ther/</a></p> <p>and python escpos references: <a href="https://code.google.com/p/python-escpos/wiki/Usage" rel="nofollow">https://code.google.com/p/python-escpos/wiki/Usage</a></p> check_svn_number_for_all_modules (Python) 2014-04-14T07:41:43-07:00Shane Wanghttp://code.activestate.com/recipes/users/4171830/http://code.activestate.com/recipes/578861-check_svn_number_for_all_modules/ <p style="color: grey"> Python recipe 578861 by <a href="/recipes/users/4171830/">Shane Wang</a> . </p> <p>this tool is used to check whether an SVM number is included in current build or not. please note that this is only for internal use.</p> Python AST to XML (Python) 2013-08-07T00:42:35-07:00Ryan Gonzalezhttp://code.activestate.com/recipes/users/4187447/http://code.activestate.com/recipes/578625-python-ast-to-xml/ <p style="color: grey"> Python recipe 578625 by <a href="/recipes/users/4187447/">Ryan Gonzalez</a> (<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>Converts Python ASTs to XML files for reading in other languages.</p> CGI Image Viewer (Python) 2012-07-02T22:52:32-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578186-cgi-image-viewer/ <p style="color: grey"> Python recipe 578186 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/old/">old</a>). </p> <p>If you have ever started a program and never finished it, then you may recognize the code below for what it is: an experiment begun but not completed, meant to allow viewing pictures on a server. It is included here as an exercise for the reader to complete and is committed for archival to be run under Python 2.5 or later versions.</p> Recursively walk Python objects (Python) 2011-12-23T22:10:38-08:00Yaniv Akninhttp://code.activestate.com/recipes/users/4180246/http://code.activestate.com/recipes/577982-recursively-walk-python-objects/ <p style="color: grey"> Python recipe 577982 by <a href="/recipes/users/4180246/">Yaniv Aknin</a> (<a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/recursive_iterator/">recursive_iterator</a>, <a href="/recipes/tags/walk/">walk</a>). Revision 2. </p> <p>A small function that walks over pretty much any Python object and yields the objects contained within (if any) along with the path to reach them. I wrote it and am using it to validate a deserialized data-structure, but you can probably use it for many things.</p> Recursively defined, Haskell-style infinite lists (Python) 2012-05-04T14:09:14-07:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578119-recursively-defined-haskell-style-infinite-lists/ <p style="color: grey"> Python recipe 578119 by <a href="/recipes/users/4181975/">John Crichton</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/recursive/">recursive</a>). Revision 2. </p> <p>A decorator to simplify the creation of recursively defined, Haskell-style infinite lists -- ie. recursive generators -- inspired by Raymond Hettinger's "Technique for cyclical iteration" [*]. </p> <p>[*] <a href="http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/" rel="nofollow">http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/</a> </p> Python Dictionary of US States and Territories (Python) 2010-07-14T19:37:21-07:00Mike Shultzhttp://code.activestate.com/recipes/users/4174394/http://code.activestate.com/recipes/577305-python-dictionary-of-us-states-and-territories/ <p style="color: grey"> Python recipe 577305 by <a href="/recipes/users/4174394/">Mike Shultz</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/states/">states</a>, <a href="/recipes/tags/territories/">territories</a>). </p> <p>Handy for matching state abbreviations with their names. Also for iteration.</p> Async subprocess check_output replacement for Twisted (Python) 2012-01-20T09:48:27-08:00Alan Franzonihttp://code.activestate.com/recipes/users/4169882/http://code.activestate.com/recipes/578021-async-subprocess-check_output-replacement-for-twis/ <p style="color: grey"> Python recipe 578021 by <a href="/recipes/users/4169882/">Alan Franzoni</a> (<a href="/recipes/tags/check_output/">check_output</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/spawnprocess/">spawnprocess</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/twisted/">twisted</a>). </p> <p>As any twisted user knows, the original python subprocess module can yield to interferences with twisted's own reactor - at least unless installSignalHandlers is false, which can lead to other consequences. </p> <p>This recipe simulates a stripped down version of subprocess.check_output() which returns a deferred and is twisted friendly.</p> Destroying Directories (Python) 2011-04-21T02:55:31-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577670-destroying-directories/ <p style="color: grey"> Python recipe 577670 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/delete/">delete</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/recursion/">recursion</a>). </p> <p>When files and directories need to be securely deleted, there are many tools in existence that people can turn to for their needs. While there are many ways one might go about manually deleting files and removing directories, this recipe provides a sample implementation of what may be considered throughout the process. This destructive command line program takes the path to a directory as a command line argument and does its best to render it and its contents unrecoverable. There are much better and inclusive applications than this one, but those wishing to write their own utilities may take some inspiration from this recipe while writing their own tools.</p> <p>If there are any recommendation for this recipe or if anyone wishes to down-vote this recipe, please provide corrective criticism showing the the program's faults and give suggestions on how you would fix any problems that it might have.</p> Maclaurin's_series_tan-1 (Python) 2010-07-07T12:21:44-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577289-maclaurins_series_tan-1/ <p style="color: grey"> Python recipe 577289 by <a href="/recipes/users/4155345/">Fouad Teniou</a> (<a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p> <p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices.</p> <p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p> <p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series. </p> <p>Maclaurin's series for tan-1 function is achieved by integrating the function f(x)= 1/(1+x^2). However, this approach could be used to find Maclaurin's series for others functions.</p> parse a date/time string to a `datetime` instance (Python) 2010-04-02T07:32:17-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/ <p style="color: grey"> Python recipe 577135 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/time/">time</a>). Revision 2. </p> <pre class="prettyprint"><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; str(datetime.datetime.now()) '2010-03-21 21:33:32.750246' &gt;&gt;&gt; str(datetime.date.today()) '2010-03-21' </code></pre> <p>This function goes the other way for date and datetime strings of this format.</p> Henderson and Tilton's formula (Python) 2009-05-08T01:33:33-07:00donyo Ganchevhttp://code.activestate.com/recipes/users/4167704/http://code.activestate.com/recipes/576630-henderson-and-tiltons-formula/ <p style="color: grey"> Python recipe 576630 by <a href="/recipes/users/4167704/">donyo Ganchev</a> . Revision 2. </p> <p>Calculate a effectiveness of zoocides </p> Play a PLS audio stream in Windows Media Player (Python) 2008-05-07T19:38:22-07:00Chris Grebeldingerhttp://code.activestate.com/recipes/users/2404079/http://code.activestate.com/recipes/572191-play-a-pls-audio-stream-in-windows-media-player/ <p style="color: grey"> Python recipe 572191 by <a href="/recipes/users/2404079/">Chris Grebeldinger</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>I don't want to bother installing a better media player, but I want to listen to PLS stations (Shoutcast). So here is a script you can use to play them in Windows Media Player which for some reason doesn't support them.</p> Automation of unix shell or DOS shell programs (Python) 2007-11-14T02:31:49-08:00Peter Dilleyhttp://code.activestate.com/recipes/users/4100123/http://code.activestate.com/recipes/534161-automation-of-unix-shell-or-dos-shell-programs/ <p style="color: grey"> Python recipe 534161 by <a href="/recipes/users/4100123/">Peter Dilley</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 5. </p> <p>Every view months HP provides new SNMP MIB files for their new products (software and hardware). Using the provided HP shell commands mcompile and mxmib, you compile and register each .mib/.cfg file with their management software on a file by file basis. This script automates the process.</p> <p>Shows: o Automation of shell commands o Using script/shell arguments o Error checking o OS independent file, path, directory handling o Sequential programming</p> <p>Prerequisites: This script assumes you have HP Systems Insight Manager installed to provide the mcompile and mxmib programs if you wish to run it directly on your system.</p>