Most viewed recipes tagged "meta:loc=33"http://code.activestate.com/recipes/tags/meta:loc=33/views/2017-04-19T18:03:11-07:00ActiveState Code RecipesImplementing function-based callbacks in Python (Python) 2017-04-19T18:03:11-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/ <p style="color: grey"> Python recipe 580787 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/techniques/">techniques</a>). </p> <p>This recipe shows a simple way of implementing callbacks in Python. There are a few ways this can be done. The way shown here uses a simple function-based approach.</p> <p>In a nutshell, a callback can be informally described like this: function <strong>a</strong> calls function <strong>b</strong>, and wants to make <strong>b</strong> run a specific independent chunk of code at some point during <strong>b</strong>'s execution. We want to be able to vary which chunk of code gets called in different calls to <strong>b</strong>, so it cannot be hard-coded inside <strong>b</strong>. So function <strong>a</strong> passes another function, <strong>c</strong>, to <strong>b</strong>, as one argument, and <strong>b</strong> uses that parameter <strong>c</strong> to call the functionality that <strong>a</strong> wants <strong>b</strong> to call. (Function <strong>b</strong> may pass some parameters to the function represented by <strong>c</strong>, when it calls it. These could be either internally generated, passed from <strong>a</strong>, or a combination of both). So, by changing the value of the function <strong>c</strong> that gets passed to <strong>b</strong> (on different calls to <strong>b</strong>), <strong>a</strong> can change what chunk of code <strong>b</strong> calls.</p> <p>More details and full code, description and output here:</p> <p><a href="https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html</a></p> ODE Solver using Euler Method (Python) 2011-04-10T00:30:58-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577647-ode-solver-using-euler-method/ <p style="color: grey"> Python recipe 577647 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>First and Second Order Ordinary Differential Equation (ODE) Solver using Euler Method.</p> printing a banner/title line (Python) 2010-07-07T17:20:35-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/306863-printing-a-bannertitle-line/ <p style="color: grey"> Python recipe 306863 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Sometimes it is nice to print a banner line in the output of a command line script to group a section of output (say, in a log file). This little banner() function will center a given string in a line (using a character and length you can specify).</p> <p><strong>Update</strong>: Just use <code>string.center(...)</code> (<a href="http://docs.python.org/library/string.html#string.center">docs</a>) as suggested in the comments below.</p> Levenshtein-Distance (Python) 2009-08-10T06:54:57-07:00Martin Schimmelshttp://code.activestate.com/recipes/users/4171400/http://code.activestate.com/recipes/576874-levenshtein-distance/ <p style="color: grey"> Python recipe 576874 by <a href="/recipes/users/4171400/">Martin Schimmels</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>calculates the Levenshtein-Distance of two strings. </p> <p>see: <a href="http://en.wikipedia.org/wiki/Levenshtein_distance" rel="nofollow">http://en.wikipedia.org/wiki/Levenshtein_distance</a> (english) and: <a href="http://de.wikipedia.org/wiki/Levenshtein-Distanz" rel="nofollow">http://de.wikipedia.org/wiki/Levenshtein-Distanz</a> (deutsch)</p> Parsing Excel XML (Python) 2003-04-01T03:33:22-08:00Thomas Guettlerhttp://code.activestate.com/recipes/users/1077859/http://code.activestate.com/recipes/192914-parsing-excel-xml/ <p style="color: grey"> Python recipe 192914 by <a href="/recipes/users/1077859/">Thomas Guettler</a> (<a href="/recipes/tags/xml/">xml</a>). </p> <p>This script parses an MS-Excel spreadsheet saved as XML.</p> <p>The spreadsheet: a1, b1 a2, b2</p> <p>will be stored like this:</p> <p>[[u'a1', u'b1'], [u'a2', u'b2']]</p> Frozen dictionaries (Python) 2005-05-16T06:20:36-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/414283-frozen-dictionaries/ <p style="color: grey"> Python recipe 414283 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>A frozendict is a dictionary that cannot be modified after being created - but it is hashable and may serve as a member of a set or a key in a dictionary.</p> PyOOCalc - Python Libre/Open Office Calc Interface API (UNO) (Python) 2016-01-10T13:12:53-08:00Yuriihttp://code.activestate.com/recipes/users/4193384/http://code.activestate.com/recipes/579147-pyoocalc-python-libreopen-office-calc-interface-ap/ <p style="color: grey"> Python recipe 579147 by <a href="/recipes/users/4193384/">Yurii</a> (<a href="/recipes/tags/openoffice/">openoffice</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>Create Libre/Open Office Calc documents, reports on Python</p> Ruby-style DSL's in Python 2.5 (Python) 2007-11-04T10:35:19-08:00Miklos Hollenderhttp://code.activestate.com/recipes/users/4097594/http://code.activestate.com/recipes/534150-ruby-style-dsls-in-python-25/ <p style="color: grey"> Python recipe 534150 by <a href="/recipes/users/4097594/">Miklos Hollender</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>The with statement in Python 2.5 can be used similarly to the anonymous blocks of code in Ruby.</p> Simple read only attributes with meta-class programming (Python) 2003-05-03T02:57:27-07:00Stephan Diehlhttp://code.activestate.com/recipes/users/774251/http://code.activestate.com/recipes/197965-simple-read-only-attributes-with-meta-class-progra/ <p style="color: grey"> Python recipe 197965 by <a href="/recipes/users/774251/">Stephan Diehl</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>This recipe is a rewrite of a portion of <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157768" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157768</a> . It shows an easy way to write read-only attributes with the help of meta classes.</p> Dragon Fractal Curve (Python) 2009-12-09T20:55:01-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/576982-dragon-fractal-curve/ <p style="color: grey"> Python recipe 576982 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>). </p> <p>Levy Dragon Fractal Curve using Turtle graphics module of Python.</p> How to use Python to convert a web page to PDF with a POST request to SelectPdf Online API and save it on the disk (Python) 2015-11-16T14:52:17-08:00SelectPdfhttp://code.activestate.com/recipes/users/4193129/http://code.activestate.com/recipes/579126-how-to-use-python-to-convert-a-web-page-to-pdf-wit/ <p style="color: grey"> Python recipe 579126 by <a href="/recipes/users/4193129/">SelectPdf</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/htmltopdf/">htmltopdf</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/selectpdf/">selectpdf</a>). </p> <p>This code converts an url to pdf in Python using SelectPdf HTML To PDF REST API through a POST request. The parameters are JSON encoded. The content is saved into a file on the disk.</p> Rock,Paper,Scissors (Python) 2014-12-19T05:39:38-08:00Burak Tandoganhttp://code.activestate.com/recipes/users/4191373/http://code.activestate.com/recipes/578983-rockpaperscissors/ <p style="color: grey"> Python recipe 578983 by <a href="/recipes/users/4191373/">Burak Tandogan</a> (<a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/paper/">paper</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/rock/">rock</a>, <a href="/recipes/tags/scissors/">scissors</a>). Revision 2. </p> <p>A simple game :)</p> Get full caller name (package.module.function) (Python) 2012-11-30T21:54:46-08:00anatoly techtonikhttp://code.activestate.com/recipes/users/4168147/http://code.activestate.com/recipes/578352-get-full-caller-name-packagemodulefunction/ <p style="color: grey"> Python recipe 578352 by <a href="/recipes/users/4168147/">anatoly techtonik</a> (<a href="/recipes/tags/caller/">caller</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/logging/">logging</a>). </p> <p>This function allows to get fully qualified name of the calling function. I expected this field to be available from logging module, but it is not here <a href="http://docs.python.org/2/library/logging.html#logrecord-attributes" rel="nofollow">http://docs.python.org/2/library/logging.html#logrecord-attributes</a> It might be that it is too expensive for performance or just doesn't play well in some situations. I don't know. I use it solely when debugging and it is convenient.</p> <p>Also here: <a href="https://gist.github.com/2151727" rel="nofollow">https://gist.github.com/2151727</a></p> Sorting a list of dictionary based on a key value (Python) 2008-08-20T03:30:37-07:00riteshnhttp://code.activestate.com/recipes/users/4166555/http://code.activestate.com/recipes/576433-sorting-a-list-of-dictionary-based-on-a-key-value/ <p style="color: grey"> Python recipe 576433 by <a href="/recipes/users/4166555/">riteshn</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>Here is a quick and dirty way to sort a list of dictionary based on a key value.</p> Fastest way to list all primes below N in python (Python) 2010-07-27T01:57:20-07:00robert.william.hankshttp://code.activestate.com/recipes/users/4174481/http://code.activestate.com/recipes/577331-fastest-way-to-list-all-primes-below-n-in-python/ <p style="color: grey"> Python recipe 577331 by <a href="/recipes/users/4174481/">robert.william.hanks</a> (<a href="/recipes/tags/fast/">fast</a>, <a href="/recipes/tags/fastest/">fastest</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/python/">python</a>). Revision 6. </p> <p>I was looking for a fast way to list all primes below n, so far i came up to this with the numpy solution the fastest. It does primes up to 10e6 in 15ms in my old machine, and it is capable of reaching 10e9. </p> calling a superclass's implementation of a method (Python) 2001-03-12T08:06:55-08:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52235-calling-a-superclasss-implementation-of-a-method/ <p style="color: grey"> Python recipe 52235 by <a href="/recipes/users/97991/">Alex Martelli</a> (<a href="/recipes/tags/oop/">oop</a>). </p> <p>Python has no equivalent to Java's "super" keyword (so that some part of a method can be delegated to the superclass), but it's easy to get similar convenience despite Python's multiple-inheritance generality.</p> Use PrettyTable and xtopdf to create PDF tables with borders, alignment and padding (Python) 2015-01-24T21:21:27-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579012-use-prettytable-and-xtopdf-to-create-pdf-tables-wi/ <p style="color: grey"> Python recipe 579012 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/prettytable/">prettytable</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/tabular/">tabular</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to create tabular data in PDF format, supporting neat borders, and alignment and padding of columns, using the Python libraries called PrettyTable and xtopdf.</p> Easy string interpolation in Python 2.4 (Python) 2004-11-12T02:46:30-08:00Michele Simionatohttp://code.activestate.com/recipes/users/1122360/http://code.activestate.com/recipes/335308-easy-string-interpolation-in-python-24/ <p style="color: grey"> Python recipe 335308 by <a href="/recipes/users/1122360/">Michele Simionato</a> (<a href="/recipes/tags/text/">text</a>). </p> <p>Regular string interpolation in Python requires the user to pass an explicit keyword dictionary. This recipe adds a little bif of magic, so that if a name is not found in the passed dictionary, it is looked up in the locals and globals dictionaries. It is also possible not to pass any explicit dictionary, then the names is searched in the locals and globals dictionaries only.</p> File encryption using stream cipher (Python) 2010-03-20T14:53:48-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ <p style="color: grey"> Python recipe 577090 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/math/">math</a>). Revision 2. </p> <p>File encryption/decryption using stream cipher. It can encrypt/decrypt any type of file.</p> Waitable, cross-process threading.Event class. (Python) 2006-10-10T11:18:34-07:00David Wilsonhttp://code.activestate.com/recipes/users/1822824/http://code.activestate.com/recipes/498191-waitable-cross-process-threadingevent-class/ <p style="color: grey"> Python recipe 498191 by <a href="/recipes/users/1822824/">David Wilson</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>This class implements an interface similar to threading.Event using os.pipe() as the mechanism for signalling. This allows the class to be used to wake up select.select() and select.poll() loops without having to peridiodically resume execution to check the status of an event.</p>