Popular recipes tagged "function" but not "linux" and "math"http://code.activestate.com/recipes/tags/function-linux-math/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> 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> 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> 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> 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> 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> 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> 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> C function decorator (Python) 2009-04-29T23:55:03-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576731-c-function-decorator/ <p style="color: grey"> Python recipe 576731 by <a href="/recipes/users/4170000/">geremy condra</a> (<a href="/recipes/tags/cpp/">cpp</a>, <a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>). Revision 6. </p> <p>This recipe provides an easy-to-use, decorator-based solution to the problem of using functions from other languages, especially C, in Python. It takes advantage of Python's new annotations to provide simple type checking and automatic conversion. If you like it, you may also want to check out my <a href="http://code.activestate.com/recipes/576734/">C structure decorator</a></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> Reevaluate functions when called (Python) 2009-05-13T15:53:28-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576751-reevaluate-functions-when-called/ <p style="color: grey"> Python recipe 576751 by <a href="/recipes/users/4170000/">geremy condra</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>). Revision 2. </p> <p>This small snippet came about as a result of <a href="http://groups.google.com/group/python-ideas/browse_thread/thread/92cf3f55919a8510/9e39950144daa9bb">this discussion</a> on python-ideas, requesting a new syntax for dynamically reevaluating a function each time it is called.</p> <p>This snippet implements a simple decorator to do so without added syntax.</p> Reevaluate functions when called, v2 (Python) 2009-05-14T15:10:51-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576753-reevaluate-functions-when-called-v2/ <p style="color: grey"> Python recipe 576753 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>). </p> <p>As with version 1 of this recipe, it was sparked by a discussion on python-ideas about adding a special syntax to function signatures for reevaluating the arguments to a function at runtime. The below is a decorator and annotation based solution to this problem which stores the code to be evaluated as a string in the annotations, rather than reevaluating the entire function every time it is called.</p> Parse call function for Py2.6 and Py2.7 (Python) 2009-02-28T20:13:15-08:00Jervis Whitleyhttp://code.activestate.com/recipes/users/4169341/http://code.activestate.com/recipes/576671-parse-call-function-for-py26-and-py27/ <p style="color: grey"> Python recipe 576671 by <a href="/recipes/users/4169341/">Jervis Whitley</a> (<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/call/">call</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/nodevisitor/">nodevisitor</a>, <a href="/recipes/tags/parsing/">parsing</a>). Revision 14. </p> <p>In some cases it may be desirable to parse the string expression "f1(*args)" and return some of the key features of the represented function-like call. </p> <p>This recipe returns the key features in the form of a namedtuple. </p> <p>e.g. (for the above)</p> <pre class="prettyprint"><code>&gt;&gt;&gt; explain("f1(*args)") [ Call(func='f1', starargs='args') ] </code></pre> <p>The recipe will return a list of such namedtuples for <code>"f1(*args)\nf2(*args)"</code> Note that while the passed string expression must evaluate to valid python syntax, names needn't be declared in current scope.</p>