Most viewed recipes tagged "meta:loc=12"http://code.activestate.com/recipes/tags/meta:loc=12/views/2017-07-11T18:57:54-07:00ActiveState Code RecipesGetting a value from a dictionary (Python)
2001-05-29T09:51:54-07:00andy mckayhttp://code.activestate.com/recipes/users/92886/http://code.activestate.com/recipes/59866-getting-a-value-from-a-dictionary/
<p style="color: grey">
Python
recipe 59866
by <a href="/recipes/users/92886/">andy mckay</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>Want to get a value from a dictionary but want to make sure that the value exists in the dictionary? Then use the incredibly useful get method.</p>
How to handle PDF embedded files with PyMuPDF (Python)
2017-07-11T18:57:54-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580796-how-to-handle-pdf-embedded-files-with-pymupdf/
<p style="color: grey">
Python
recipe 580796
by <a href="/recipes/users/4193772/">Jorj X. McKie</a>
(<a href="/recipes/tags/embedded_files/">embedded_files</a>, <a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>).
Revision 3.
</p>
<p>Version 1.11.0 (based on MuPDF v1.11) allows exporting, importing and interrogating files embedded in a PDF.</p>
<p>PDF "/EmbeddedFiles" are similar to ZIP archives (or the Microsoft OLE technique), allowing arbitrary data to be incorporated in a PDF and benefit from its unique features.</p>
The Simplest CGI Program (Python)
2001-03-12T06:36:58-08:00Jeff Bauerhttp://code.activestate.com/recipes/users/98055/http://code.activestate.com/recipes/52220-the-simplest-cgi-program/
<p style="color: grey">
Python
recipe 52220
by <a href="/recipes/users/98055/">Jeff Bauer</a>
(<a href="/recipes/tags/cgi/">cgi</a>).
</p>
<p>The simplest CGI program. This script displays the
current version of Python and the environment values.</p>
sample with replacement (Python)
2004-03-08T00:21:23-08:00Sean Rosshttp://code.activestate.com/recipes/users/761068/http://code.activestate.com/recipes/273085-sample-with-replacement/
<p style="color: grey">
Python
recipe 273085
by <a href="/recipes/users/761068/">Sean Ross</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>For taking k random samples (with replacement) from a population, where k may be greater than len(population).</p>
Extract text from XML document (Python)
2001-06-14T19:42:23-07:00Paul Prescodhttp://code.activestate.com/recipes/users/11203/http://code.activestate.com/recipes/65128-extract-text-from-xml-document/
<p style="color: grey">
Python
recipe 65128
by <a href="/recipes/users/11203/">Paul Prescod</a>
(<a href="/recipes/tags/xml/">xml</a>).
Revision 2.
</p>
<p>People often ask how to extract the text from an XML document. This small program does it.</p>
Class with default method handler (Python)
2004-10-08T06:21:21-07:00mark andrewhttp://code.activestate.com/recipes/users/2085251/http://code.activestate.com/recipes/307618-class-with-default-method-handler/
<p style="color: grey">
Python
recipe 307618
by <a href="/recipes/users/2085251/">mark andrew</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>A class whose objects can handle undefined method calls, passing them on to a
default handler.</p>
First n primes numbers (Python)
2014-09-20T19:33:41-07:00juanhttp://code.activestate.com/recipes/users/4190606/http://code.activestate.com/recipes/578923-first-n-primes-numbers/
<p style="color: grey">
Python
recipe 578923
by <a href="/recipes/users/4190606/">juan</a>
.
Revision 6.
</p>
<p>Using the Sieve of Eratosthenes find the first n primes numbers.</p>
Draw a diamond with asterisks using recursion (Python)
2014-11-07T17:51:32-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578959-draw-a-diamond-with-asterisks-using-recursion/
<p style="color: grey">
Python
recipe 578959
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/drawing/">drawing</a>).
</p>
<p>Given the following function header:</p>
<pre class="prettyprint"><code>def triangles(n):
</code></pre>
<p>If n for example is 5, then the drawing would be like this:</p>
<pre class="prettyprint"><code> *
* * *
* * * * *
* * *
*
</code></pre>
Running linux command inside your python script (Python)
2012-09-09T08:08:46-07:00Ahmed Kamelhttp://code.activestate.com/recipes/users/4183490/http://code.activestate.com/recipes/578254-running-linux-command-inside-your-python-script/
<p style="color: grey">
Python
recipe 578254
by <a href="/recipes/users/4183490/">Ahmed Kamel</a>
(<a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>Running any linux command line inside your python script.</p>
Dicts from lists (Python)
2003-11-28T03:09:30-08:00Richard Philipshttp://code.activestate.com/recipes/users/98125/http://code.activestate.com/recipes/252176-dicts-from-lists/
<p style="color: grey">
Python
recipe 252176
by <a href="/recipes/users/98125/">Richard Philips</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Simple oneliner to built a dictionary from a list</p>
More accurate sum (Python)
2004-08-05T10:25:59-07:00Yaroslav Bulatovhttp://code.activestate.com/recipes/users/1915780/http://code.activestate.com/recipes/298339-more-accurate-sum/
<p style="color: grey">
Python
recipe 298339
by <a href="/recipes/users/1915780/">Yaroslav Bulatov</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>Built-in "sum" function, as well as add.reduce functions in Numeric/numarray introduce a large error when summing large arrays of like elements. I got relative error of about 1e-9 after summing 10 million doubles between 0 and 1. Function below has error less than 1e-15, doesn't use any additional memory (although it destroys the data array), and also runs asymptotically faster for unlimited precision numbers.</p>
Quickly remove or order columns in a list of lists (Python)
2003-03-28T20:27:26-08:00Jason Whitlarkhttp://code.activestate.com/recipes/users/302240/http://code.activestate.com/recipes/192401-quickly-remove-or-order-columns-in-a-list-of-lists/
<p style="color: grey">
Python
recipe 192401
by <a href="/recipes/users/302240/">Jason Whitlark</a>
.
</p>
<p>I've often needed to take out or reorder the columns in a list of lists. I'm embarrased to say it took me a while to think of this, it's so simple, but I looked around and have not found any examples of it, so...</p>
How to Connect to an Already Running Instance of Internet Explorer (Python)
2004-06-13T23:03:49-07:00Bill Bellhttp://code.activestate.com/recipes/users/98151/http://code.activestate.com/recipes/269345-how-to-connect-to-an-already-running-instance-of-i/
<p style="color: grey">
Python
recipe 269345
by <a href="/recipes/users/98151/">Bill Bell</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>Various sources (but principally Hammond and Robinson) show how to instantiate Internet Explorer in order to have access to its interfaces. However, few places show how to connect to an instance that is already running, which is so easy in VB.</p>
<p>Thanks are due to someone called "gcash" for this idea. See <a href="http://dbforums.com/t867088.html." rel="nofollow">http://dbforums.com/t867088.html.</a></p>
<p>It just seemed to me that this is a good recipe to have in this collection as a reference to a certain approach, rather than as a full solution.</p>
quick Python profiling with hotshot (Python)
2009-02-20T22:35:07-08:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/576656-quick-python-profiling-with-hotshot/
<p style="color: grey">
Python
recipe 576656
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/hotshot/">hotshot</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/profile/">profile</a>).
Revision 2.
</p>
<p>This is a quick snippet that I use occasionally to profile some pure-Python code, using <a href="http://docs.python.org/library/hotshot.html#module-hotshot"><code>hotshot</code></a>. Basically it is this:</p>
<ol>
<li>Put this <code>@hotshotit</code> decorator on the function you want to profile.</li>
<li>Run your code through some representative paces. The result will be a <code><functionname>.prof</code> in the current directory.</li>
<li>Process the <code>.prof</code> file and print the top 20 hotspots with the given "show_stats.py" script.</li>
</ol>
<p>Props to <a href="http://blogs.activestate.com/toddw/">Todd</a> for slapping this code together.</p>
<p>Hotshot <a href="http://docs.python.org/library/profile.html">is a little out of favour</a> now, so I should -- or Todd :) -- should really come up with an equivalent that uses <code>cProfile</code>.</p>
lazy property (Python)
2009-04-26T18:10:55-07:00Sridhar Ratnakumarhttp://code.activestate.com/recipes/users/4169511/http://code.activestate.com/recipes/576720-lazy-property/
<p style="color: grey">
Python
recipe 576720
by <a href="/recipes/users/4169511/">Sridhar Ratnakumar</a>
(<a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/lazy_evaluation/">lazy_evaluation</a>).
Revision 6.
</p>
<p>Python does not have lazy evaluation syntax features built-in, but fortunately decorators can be used with new-style classes to emulate such a feature. There are cases where one wants <code>foo.property</code> to return the actual property whose calculation takes significant amount of time.</p>
<p>This recipe adapts the existing <code>property</code> to provide a <code>lazypropery</code> decorator that does this.</p>
<p>See the first comment below for an example usage.</p>
<p>Also see: <a href="http://en.wikipedia.org/wiki/Lazy_initialization">lazy initialization</a></p>
Calculating PI using random numbers (Monte Carlo method) (Python)
2010-03-27T00:58:24-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577154-calculating-pi-using-random-numbers-monte-carlo-me/
<p style="color: grey">
Python
recipe 577154
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/random/">random</a>).
</p>
<p>Calculating PI using random numbers (Monte Carlo method)</p>
Quick Test If Strings Are Identifiers (Python)
2005-05-08T10:42:59-07:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/413487-quick-test-if-strings-are-identifiers/
<p style="color: grey">
Python
recipe 413487
by <a href="/recipes/users/2400454/">Zoran Isailovski</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 3.
</p>
<p>Sometimes you need strings to be true identifiers, for ex. to represent symbolic names. Smalltalk offers the type 'Symbol' for this purpose. In python, you need to test this explicitly. Here is a quick way.</p>
User List Subclass (Python)
2008-08-18T09:26:07-07:00nosklohttp://code.activestate.com/recipes/users/4166478/http://code.activestate.com/recipes/576428-user-list-subclass/
<p style="color: grey">
Python
recipe 576428
by <a href="/recipes/users/4166478/">nosklo</a>
(<a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/slices/">slices</a>, <a href="/recipes/tags/subclass/">subclass</a>, <a href="/recipes/tags/__getitem__/">__getitem__</a>).
Revision 2.
</p>
<p>As subclassing list has a problem when using __getitem__, __delitem__ and __setitem__ methods with slices (they don't get called because parent implements __getslice__, __delslice__ and __setslice__ respectively), I've coded this UserList class that is a subclass of list, but overwrites these methods.
By subclassing this class, you can overwrite __getitem__ and it will be called correctly for slices.</p>
touch (Python)
2009-09-28T16:19:23-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/576915-touch/
<p style="color: grey">
Python
recipe 576915
by <a href="/recipes/users/4173505/">Trent Mick</a>
(<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/touch/">touch</a>, <a href="/recipes/tags/unix/">unix</a>).
</p>
<p>Python function a la the Unix <code>touch</code> program (<a href="http://www.manpagez.com/man/1/touch/">man touch</a>).</p>
"safely" defining __setattr__ (Python)
2006-09-01T08:06:29-07:00Ori Peleghttp://code.activestate.com/recipes/users/2056315/http://code.activestate.com/recipes/497009-safely-defining-__setattr__/
<p style="color: grey">
Python
recipe 497009
by <a href="/recipes/users/2056315/">Ori Peleg</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>Overriding __setattr__ in classes requires care when setting attributes yourself. Here's an idea for safely setting attributes in __init__.</p>
<p>Update: this idea doesn't work. See Mike Foord's recipe for one that does:</p>
<p><a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389916" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389916</a></p>