Latest recipes tagged "function" but not "amiga"http://code.activestate.com/recipes/tags/function-amiga/new/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> Function guards for Python 3 (Python) 2016-05-01T13:11:05-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/580658-function-guards-for-python-3/ <p style="color: grey"> Python recipe 580658 by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a> (<a href="/recipes/tags/call/">call</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/dispatch/">dispatch</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/guard/">guard</a>). </p> <p>This module implements a function guard - facility to redirect the the call to one of several function implementations at run time based on the actual call arguments.</p> <p>Wrap each of the identically named functions in a @guard decorator and provide a _when parameter with a default value set to guarding expression.</p> <p>See samples at the top of the module.</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> Two Versions Of Bash One Liner INKEY$ Functions... (Bash) 2013-03-28T17:51:32-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578502-two-versions-of-bash-one-liner-inkey-functions/ <p style="color: grey"> Bash recipe 578502 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/inkey/">inkey</a>, <a href="/recipes/tags/shell/">shell</a>). Revision 2. </p> <p>This is probably common knowledge to the professionals but not so much for amateurs like myself.</p> <p>This is a code snippet for the equivalent of BASIC's...</p> <p>LET char$=INKEY$</p> <p>As the timeout parameter cannot be less than 1 second then this is the only limitation...</p> <p>It is a single line function which has a variable "char"...</p> <p>Read the code for more information...</p> <p>There are now two versions, edit out and choose which is best for you...</p> All [iter] is Instance of [class/type/tuple] (Python) 2012-07-26T18:09:30-07:00Jonathan Frerehttp://code.activestate.com/recipes/users/4182985/http://code.activestate.com/recipes/578226-all-iter-is-instance-of-classtypetuple/ <p style="color: grey"> Python recipe 578226 by <a href="/recipes/users/4182985/">Jonathan Frere</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/instance/">instance</a>, <a href="/recipes/tags/isinstance/">isinstance</a>). Revision 2. </p> <p>So it turned out that I needed a way of ensuring that all members of an iterable are instances of a certain class. Here, therefore, is the <code>isallinstance()</code> function.</p> Smarter Default Arguments (Python) 2011-08-12T23:06:57-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577786-smarter-default-arguments/ <p style="color: grey"> Python recipe 577786 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/default_arguments/">default_arguments</a>, <a href="/recipes/tags/deferred/">deferred</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/functions/">functions</a>). Revision 3. </p> <p>Improved handling of mutable and deferred default arguments. After the recipe you'll find an explanation of what that means.</p> <p>Works for 2.7 with minor tweaks (getfullargspec --> getargspec).</p> Change a Function's Closure! (Python) 2011-08-12T23:25:55-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577760-change-a-functions-closure/ <p style="color: grey"> Python recipe 577760 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/closure/">closure</a>, <a href="/recipes/tags/function/">function</a>). Revision 2. </p> <p>This is a hack to get around the read-only nature of __closure__ on function objects. Watch your step!</p> Simple recursive function to non-recursive function (Python) 2011-05-27T01:47:37-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577724-simple-recursive-function-to-non-recursive-functio/ <p style="color: grey"> Python recipe 577724 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/recursive/">recursive</a>, <a href="/recipes/tags/substitute/">substitute</a>). </p> <p>This recipe is a simple solution for turning a recursive function into a non-recursive function.</p> Simple 1KHz Audio Function Generator Using Standard Python In Linux... (Python) 2011-03-01T19:37:16-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/577592-simple-1khz-audio-function-generator-using-standar/ <p style="color: grey"> Python recipe 577592 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/program/">program</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/source/">source</a>). </p> <h5 id="simple-1khz-audio-function-generator-using-standard-python-in-linux-afgpy">Simple 1KHz Audio Function Generator Using Standard Python In Linux - afg.py</h5> <p>This code is a stand alone program to generate a signal, at the earphone sockets, of 1KHz. It is a basic audio signal generator and can be used as a starter test signal source for amateur electronics enthusiasts testgear suite(s).</p> <p>It needs /dev/audio to work; if you haven't got it then install oss-compat from your distro's repository.</p> <p>Ensure the audio system is NOT in use for this to work.</p> <p>Sine, Square, Triangle, Sawtooth+, Sawtooth-, Pulse+ and Pulse- signals are generated in 10 second bursts. The waveforms generated are unfiltered and therefore not "pure", but hey, an audio function generator signal source, for free, without external hardware, AND, using standard Python, what more do you want... :) An oscilloscope will show the waveforms generated at the earphone socket.</p> <p>Noise is not included but that is SO easy that I left it out FTTB. (This will be a future upload. ;o)</p> <p>All that is required to make this a piece of audio test equipment is a cable plugged into to the earphone socket.</p> <p>Assuming it is copied into the module(s) drawer just type:-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import afg[RETURN/ENTER] </code></pre> <p>And away you go...</p> <p>This is Public Domain and you may do with it as you like.</p> <p>Read the program for more information. (There will be more to come in the future... :)</p> A simple function benchmarking module (Python) 2010-08-27T09:26:48-07:00Timothee Cezardhttp://code.activestate.com/recipes/users/4174785/http://code.activestate.com/recipes/577377-a-simple-function-benchmarking-module/ <p style="color: grey"> Python recipe 577377 by <a href="/recipes/users/4174785/">Timothee Cezard</a> (<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/function/">function</a>). </p> <p>This module enable its user to monitor the amount of time spend in between two commands start and stop. The module is fairly imprecise if the monitored task is quick as the start and stop commands are fairly slow (2e-07 - 5e-07 second)</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> Decorator to expose local variables of a function after execution (Python) 2010-07-07T22:01:23-07:00Pietro Berkeshttp://code.activestate.com/recipes/users/4174299/http://code.activestate.com/recipes/577283-decorator-to-expose-local-variables-of-a-function-/ <p style="color: grey"> Python recipe 577283 by <a href="/recipes/users/4174299/">Pietro Berkes</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 2. </p> <p>Decorator to expose the local variables defined in the inner scope of a function. At the exit of the decorated function (regular exit or exceptions), the local dictionary is copied to a read-only property, <code>locals</code>.</p> <p>The main implementation is based on injecting bytecode into the original function, and requires the lightweight module <code>byteplay</code> (available <a href="http://code.google.com/p/byteplay/">here</a>). See below for an alternative implementation that only uses the standard library.</p> Random numbers with arbitrary probability distribution. (Python) 2010-06-14T17:02:41-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577264-random-numbers-with-arbitrary-probability-distribu/ <p style="color: grey"> Python recipe 577264 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). Revision 2. </p> <p>Generating N random numbers that probability distribution fits to any given function curve.</p> Numerical Integration using Monte Carlo method (Python) 2010-06-16T19:31:17-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577263-numerical-integration-using-monte-carlo-method/ <p style="color: grey"> Python recipe 577263 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). Revision 2. </p> <p>Numerical Integration using Monte Carlo method.</p> Area Under the Curve using Monte Carlo method (Python) 2010-06-17T03:54:27-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577262-area-under-the-curve-using-monte-carlo-method/ <p style="color: grey"> Python recipe 577262 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>). Revision 5. </p> <p>Calculating area under the curve using Monte Carlo method for any given function.</p> A new method for computing the hypergeometric function 1F1(a,b,t) (Python) 2010-03-22T05:00:20-07:00Fernando Nieuwveldthttp://code.activestate.com/recipes/users/4172088/http://code.activestate.com/recipes/577133-a-new-method-for-computing-the-hypergeometric-func/ <p style="color: grey"> Python recipe 577133 by <a href="/recipes/users/4172088/">Fernando Nieuwveldt</a> (<a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/hypergeometric/">hypergeometric</a>, <a href="/recipes/tags/laplace_inversion/">laplace_inversion</a>). Revision 3. </p> <p>I present a method of computing the 1F1(a,b,x) function using a contour integral. The method is based on a numerical inversion, basically the Laplace inversion. Integral is 1F1(a,b,x) = Gamma(b)/2\pi i \int_\rho exp(zx)z^(-b)(1+x/z)^(-a)dz, \rho is taken as a Talbot contour. The Talbot method is applied with the use of the midpoint rule for numerical integration. Here the user must give the number of function evaluations and this may vary from problem to problem. It is very easy to implement with only a few lines of code and it is very accurate even for large arguments.</p> Type-Checking Function Overloading Decorator (Python) 2010-04-11T11:16:48-07:00Ryan Liehttp://code.activestate.com/recipes/users/4171032/http://code.activestate.com/recipes/577065-type-checking-function-overloading-decorator/ <p style="color: grey"> Python recipe 577065 by <a href="/recipes/users/4171032/">Ryan Lie</a> (<a href="/recipes/tags/checkng/">checkng</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/overloading/">overloading</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/type/">type</a>). Revision 2. </p> <p>The recipe presents a function overloading decorator in python that do type check. The type signature is marked with the @takes and @returns decorator, which causes the function to raise an InputParameterError exception if called with inappropriate arguments. The @overloaded function searches for the first overloads that doesn't raise TypeError or InputParameterError when called. Alternative overloads are added to the overloads list by using the @func.overload_with decorator. The order of function definition determines which function gets tried first and once it founds a compatible function, it skips the rest of the overloads list.</p> Simple Function Overloading with Decorator (Python) 2010-04-06T00:59:52-07:00Ryan Liehttp://code.activestate.com/recipes/users/4171032/http://code.activestate.com/recipes/577064-simple-function-overloading-with-decorator/ <p style="color: grey"> Python recipe 577064 by <a href="/recipes/users/4171032/">Ryan Lie</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/overloading/">overloading</a>, <a href="/recipes/tags/python/">python</a>). Revision 3. </p> <p>The recipe presents a simple decorator for function overloading in python. The @overloaded function searches for the first overloads that doesn't raise TypeError when called. Overloads are added to the overloads list by using the @func.overload_with decorator. The order of function definition determines which function gets tried first and once it founds a compatible function, it skips the rest of the overloads list.</p> Exception handling in a single line (Python) 2009-10-09T01:07:52-07:00Radek Szklarczykhttp://code.activestate.com/recipes/users/2847011/http://code.activestate.com/recipes/576872-exception-handling-in-a-single-line/ <p style="color: grey"> Python recipe 576872 by <a href="/recipes/users/2847011/">Radek Szklarczyk</a> (<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/expression/">expression</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). Revision 16. </p> <p>The rules of <em>duck typing</em> in python encourage programmers to use the "try...except..." clause. At the same time python with new versions enables to use more powerful list comprehensions (for example Conditional Expressions). However, it is impossible to write the "try...except..." clause in a list comprehension. The following recipe "protects" a function against exception and returns a default value in the case when exception is thrown.</p> Reevaluate functions when called, v3 (Python) 2009-05-14T15:18:41-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576754-reevaluate-functions-when-called-v3/ <p style="color: grey"> Python recipe 576754 by <a href="/recipes/users/4170000/">geremy condra</a> (<a href="/recipes/tags/annotations/">annotations</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>). Revision 3. </p> <p>This small snippet came about as a result of this discussion on python-ideas, requesting a new syntax for dynamically reevaluating a function each time it is called.</p> <p>It is a minor alteration of version 2 of this recipe that, instead of calling eval() on string annotations, simply requires that the annotations be callable and calls them at runtime.</p>