Popular recipes tagged "meta:loc=28"http://code.activestate.com/recipes/tags/meta:loc=28/2015-09-18T15:04:09-07:00ActiveState Code Recipes10 lines for a beginner to try out the Spyder IDE for python programming (Python) 2015-09-18T15:04:09-07:00mai3ahttp://code.activestate.com/recipes/users/4192857/http://code.activestate.com/recipes/579100-10-lines-for-a-beginner-to-try-out-the-spyder-ide-/ <p style="color: grey"> Python recipe 579100 by <a href="/recipes/users/4192857/">mai3a</a> (<a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/snippet/">snippet</a>, <a href="/recipes/tags/spyder/">spyder</a>, <a href="/recipes/tags/stuffthatworks/">stuffthatworks</a>). </p> <p>This python code generates no lint errors and just print out a few numbers.</p> <p>Useful if you use the Spyder IDE for the first time, which provides NOTHING to get you started.</p> Quasicrystal Pattern Generator (Python) 2015-08-08T20:50:23-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579094-quasicrystal-pattern-generator/ <p style="color: grey"> Python recipe 579094 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Quasicrystal Pattern Generator</p> Get all installed Windows hotfixes (Perl) 2014-07-31T17:33:19-07:00Brett Carrollhttp://code.activestate.com/recipes/users/4174322/http://code.activestate.com/recipes/578917-get-all-installed-windows-hotfixes/ <p style="color: grey"> Perl recipe 578917 by <a href="/recipes/users/4174322/">Brett Carroll</a> (<a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/fix/">fix</a>, <a href="/recipes/tags/hotfix/">hotfix</a>, <a href="/recipes/tags/hotfixes/">hotfixes</a>, <a href="/recipes/tags/ole/">ole</a>, <a href="/recipes/tags/perl/">perl</a>, <a href="/recipes/tags/quick/">quick</a>, <a href="/recipes/tags/win32/">win32</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/wmi/">wmi</a>). </p> <p>This script uses WMI (via Win32::OLE) to retrieve and print out a comma separated list of all installed Windows hotfixes.</p> Guess the number, Game (Python) 2013-07-10T17:49:30-07:00superducktoxichttp://code.activestate.com/recipes/users/4187037/http://code.activestate.com/recipes/578600-guess-the-number-game/ <p style="color: grey"> Python recipe 578600 by <a href="/recipes/users/4187037/">superducktoxic</a> . </p> <p>you think of the number and then guess it.</p> Prime Number Generator in Python (half effcient) (Python) 2013-01-06T20:20:49-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578401-prime-number-generator-in-python-half-effcient/ <p style="color: grey"> Python recipe 578401 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/prime_generator/">prime_generator</a>, <a href="/recipes/tags/prime_number/">prime_number</a>). </p> <p>This is a simple prime number generator in python that I put together for an article I wrote <a href="http://thelivingpearl.com/2013/01/06/how-to-find-prime-numbers-in-python/">How To Find Prime Numbers In Python</a>. In this one we cut the numbers in half so it is only slightly more efficient than the simplest prime number generator. </p> <p>Note: There are more efficient ways to do this. Please look at my other recipes. </p> Directory Cleaner (Python) 2012-12-05T20:56:05-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578357-directory-cleaner/ <p style="color: grey"> Python recipe 578357 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/demonstration/">demonstration</a>). </p> <p>If there is a particular directory that you want purged of its files and want a solution in Python, this simple program is designed to do just that. This recipe is just meant as a starting point for those wondering how to delete all files and sub-folders from a central folder.</p> File Path Logger (Python) 2012-12-05T20:59:38-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578358-file-path-logger/ <p style="color: grey"> Python recipe 578358 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/demonstration/">demonstration</a>). </p> <p>If you want a list of directories and the files they contain, this program will log such information to a file for you. For those wondering how to walk directories and their contents, this little recipe provides an <code>engine</code> function that provides a simple demonstration of the <code>os.walk</code> function.</p> Sharing-aware tree transformations (Python) 2012-05-07T08:20:58-07:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578117-sharing-aware-tree-transformations/ <p style="color: grey"> Python recipe 578117 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/fold/">fold</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/sharing/">sharing</a>, <a href="/recipes/tags/tree/">tree</a>, <a href="/recipes/tags/yaml/">yaml</a>). Revision 2. </p> <p>The function <code>foldsh</code> in this recipe is a general purpose tool for transforming tree-like recursive data structures while keeping track of shared subtrees.</p> <pre class="prettyprint"><code># By default, a branch is encoded as a list of subtrees; each subtree can be a # branch or a leaf (=anything non-iterable). Subtrees can be shared: &gt;&gt;&gt; subtree = [42,44] &gt;&gt;&gt; tree = [subtree,[subtree]] # We can apply a function to all leaves: &gt;&gt;&gt; foldsh(tree, leaf= lambda x: x+1) [[43, 45], [[43, 45]]] # Or apply a function to the branches: &gt;&gt;&gt; foldsh(tree, branch= lambda t,c: list(reversed(c))) [[[44, 42]], [44, 42]] # The sharing is preserved: &gt;&gt;&gt; _[0][0] is _[1] True # Summing up the leaves without double counting of shared subtrees: &gt;&gt;&gt; foldsh(tree, branch= lambda t,c: sum(c), shared= lambda x: 0) 86 </code></pre> <p>In particular, it is useful for transforming YAML documents. An example of this is given below.</p> Cycle-aware tree transformations (Python) 2012-06-20T08:09:13-07:00Sander Evershttp://code.activestate.com/recipes/users/4173111/http://code.activestate.com/recipes/578118-cycle-aware-tree-transformations/ <p style="color: grey"> Python recipe 578118 by <a href="/recipes/users/4173111/">Sander Evers</a> (<a href="/recipes/tags/cyclic/">cyclic</a>, <a href="/recipes/tags/fold/">fold</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/yaml/">yaml</a>). </p> <p>A variation on <a href="http://code.activestate.com/recipes/578117/">Recipe 578117</a> that can deal with cycles. A cycle means that a tree has itself as a subtree somewhere. A fold over such a data structure has a chicken-and-egg-problem: it needs its own result in order to construct its own result. To solve this problem, we let <code>branch</code> construct a <em>part</em> of its result before going into recursion. After the recursion, <code>branch</code> gets a chance to complete its result using its children's results. Python's support for coroutines (using <code>yield</code>) provides a nice way to define such a two-stage <code>branch</code> function.</p> Random fractal curve (Python) 2011-07-09T22:09:23-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577783-random-fractal-curve/ <p style="color: grey"> Python recipe 577783 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Random fractal curve (non-deterministic).</p> Linux cat command backward (Python) 2011-05-18T12:54:15-07:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577699-linux-cat-command-backward/ <p style="color: grey"> Python recipe 577699 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/cat/">cat</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/log/">log</a>, <a href="/recipes/tags/output/">output</a>). Revision 2. </p> <p>Utility to output file from backward. Useful for logs output.</p> webcheck: site to csv (Python) 2011-03-09T06:37:08-08:00Jervis Whitleyhttp://code.activestate.com/recipes/users/4169341/http://code.activestate.com/recipes/577602-webcheck-site-to-csv/ <p style="color: grey"> Python recipe 577602 by <a href="/recipes/users/4169341/">Jervis Whitley</a> (<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/linkcheck/">linkcheck</a>, <a href="/recipes/tags/sitemap/">sitemap</a>, <a href="/recipes/tags/webcheck/">webcheck</a>). Revision 3. </p> <p>An extension to Arthur de Jong's excellent webcheck tool (a website link checker) (<a href="http://arthurdejong.org/webcheck" rel="nofollow">http://arthurdejong.org/webcheck</a>) that will read in the resultant webcheck.dat file and create a csv formatted file.</p> Decorator to expose local variables of a function after execution [alternative implementation] (Python) 2010-07-08T09:44:28-07:00Andrea Maffezzolihttp://code.activestate.com/recipes/users/4171157/http://code.activestate.com/recipes/577295-decorator-to-expose-local-variables-of-a-function-/ <p style="color: grey"> Python recipe 577295 by <a href="/recipes/users/4171157/">Andrea Maffezzoli</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/inner/">inner</a>, <a href="/recipes/tags/local/">local</a>, <a href="/recipes/tags/scope/">scope</a>, <a href="/recipes/tags/variables/">variables</a>). Revision 6. </p> <p>Please note that the present is a fork of the <a href="http://code.activestate.com/recipes/577283/">recipe 577283</a> "Decorator to expose local variables of a function after execution" of Pietro Berkes, available at <a href="http://code.activestate.com/recipes/577283-decorator-to-expose-local-variables-of-a-function-/" rel="nofollow">http://code.activestate.com/recipes/577283-decorator-to-expose-local-variables-of-a-function-/</a> , and aiming only to report the alternative implementation "persistent_locals2", whose I'm co-author with Pietro Berkes, and which was submitted together to the original recipe. Refer to the latter for an exhaustive description and discussion.</p> Total ordering class decorator (Python) 2010-09-07T05:47:25-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576685-total-ordering-class-decorator/ <p style="color: grey"> Python recipe 576685 by <a href="/recipes/users/178123/">Raymond Hettinger</a> . Revision 11. </p> <p>Given a class defining one or more ordering methods, this decorator supplies the rest. This simplifies and speeds-up the approach taken in <a href="http://code.activestate.com/recipes/576529/">recipe 576529</a>.</p> REAL case insensitive string replace (Python) 2009-04-25T15:48:50-07:00Jonas Haaghttp://code.activestate.com/recipes/users/4169206/http://code.activestate.com/recipes/576715-real-case-insensitive-string-replace/ <p style="color: grey"> Python recipe 576715 by <a href="/recipes/users/4169206/">Jonas Haag</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/case_insensitive/">case_insensitive</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/str/">str</a>). Revision 2. </p> <p>REAL case insensitive version of <code>str.replace</code> that keeps the letter case of the original expression (Doesn't only replace <code>Foo</code> and <code>foo</code> to <code>...foo...</code> but to <code>...Foo..</code> and <code>...foo...</code>).</p> The Poem ( in Python) (Python) 2008-08-08T10:39:50-07:00Marcello(Harry) Bontempo Salgueirohttp://code.activestate.com/recipes/users/4166255/http://code.activestate.com/recipes/576413-the-poem-in-python/ <p style="color: grey"> Python recipe 576413 by <a href="/recipes/users/4166255/">Marcello(Harry) Bontempo Salgueiro</a> (<a href="/recipes/tags/in/">in</a>, <a href="/recipes/tags/poem/">poem</a>, <a href="/recipes/tags/the/">the</a>). </p> <p>This is my first poem writing in python. Why i do that? I do because i saw some perl poems and not so much in python, just it! =) Python developers let's go express your art, and domain of langague Python...!!!! ihulllllllllllllllll!!! see ya!</p> <p>marcello.</p> Convenient Utility for Making Dictionaries (Python) 2008-07-23T23:55:00-07:00Andrew Konstantarashttp://code.activestate.com/recipes/users/4141807/http://code.activestate.com/recipes/576373-convenient-utility-for-making-dictionaries/ <p style="color: grey"> Python recipe 576373 by <a href="/recipes/users/4141807/">Andrew Konstantaras</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>This function creates a dictionary containing all of the variables passed as parameters with the variable name being the key and the value of the variable as the value. I use it frequently when passing parameters to another function.</p> sane tab completion in pdb (Python) 2009-07-06T04:35:33-07:00Stephen Emsliehttp://code.activestate.com/recipes/users/4004606/http://code.activestate.com/recipes/498182-sane-tab-completion-in-pdb/ <p style="color: grey"> Python recipe 498182 by <a href="/recipes/users/4004606/">Stephen Emslie</a> (<a href="/recipes/tags/complete/">complete</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/pdb/">pdb</a>, <a href="/recipes/tags/tab/">tab</a>). Revision 6. </p> <p>I make frequent use of python's built-in debugger, but one obvious feature seems to be missing - the bash-like tab completion that you can add to the interpreter. Fortunately pdb's interactive prompt is an instance of Cmd, so we can write our own completion function.</p> <p>Note: this uses rlcompleter, which isn't available on windows</p> <p>Edit (6 Jul 2009): import rlcompleter early and force output to stdout to ensure monkeypatch sticks Edit: updated to handle changes in local scope Edit: Fixed start via 'python -m pdb ...'. Check the comments for details.</p> ISBN-13 converter (Python) 2006-09-15T18:53:01-07:00Sanghyeon Seohttp://code.activestate.com/recipes/users/990805/http://code.activestate.com/recipes/498104-isbn-13-converter/ <p style="color: grey"> Python recipe 498104 by <a href="/recipes/users/990805/">Sanghyeon Seo</a> . </p> <p>ISBN, the International Standard Book Number, will migrate to 13-digits version from current 10-digits version on 2007-01-01. This recipe converts ISBN-10 to ISBN-13 by regenerating the check digit.</p> Keyed Dictionary -- strict key values in a dictionary (Python) 2006-12-04T21:52:58-08:00Mike Hostetlerhttp://code.activestate.com/recipes/users/138739/http://code.activestate.com/recipes/499296-keyed-dictionary-strict-key-values-in-a-dictionary/ <p style="color: grey"> Python recipe 499296 by <a href="/recipes/users/138739/">Mike Hostetler</a> (<a href="/recipes/tags/extending/">extending</a>). Revision 2. </p> <p>The KeyedDict object keeps a list of approved key values. If you try to assigned an unapproved key to a value, an exception is thrown.</p> <p>When you create the object, you give it a sequence (tuple, list, etc.) of key objects and then, when you set an item with a key, it will make sure that the key is in the "approved" list. If you try to set an item with a key that isn't in your approved list, you get an TypeError exception.</p>