Popular recipes tagged "meta:loc=49"http://code.activestate.com/recipes/tags/meta:loc=49/2016-09-22T12:25:30-07:00ActiveState Code RecipesSimple Matlab/Ocave like arrays conversion to numpy.arrays in python interpreter (Python) 2016-09-22T12:25:30-07:00Przemyslaw Podczasihttp://code.activestate.com/recipes/users/4179716/http://code.activestate.com/recipes/580700-simple-matlabocave-like-arrays-conversion-to-numpy/ <p style="color: grey"> Python recipe 580700 by <a href="/recipes/users/4179716/">Przemyslaw Podczasi</a> (<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/interpreter/">interpreter</a>, <a href="/recipes/tags/matlab/">matlab</a>). </p> <p>Matlab/Octave syntax for 1D/2D arrays is more packed and doesn't require putting extra ',' and extra '[', ']' between dimensions. For this I wrote a parser that intercepts python interpreter and using numpy functionality parses Matlab's style arrays 1D and 2D into numpy.arrays.</p> least square fitting (Python) 2015-10-03T15:32:28-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579106-least-square-fitting/ <p style="color: grey"> Python recipe 579106 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/fitting/">fitting</a>). Revision 2. </p> <p>a generic python code to fit points to a given curve, was made for a paraboloid, but can be easily expanded to many kind of curves</p> Longest common subsequence of 3+ strings (Python) 2015-06-01T08:08:11-07:00Alister Cordinerhttp://code.activestate.com/recipes/users/4174503/http://code.activestate.com/recipes/579065-longest-common-subsequence-of-3-strings/ <p style="color: grey"> Python recipe 579065 by <a href="/recipes/users/4174503/">Alister Cordiner</a> (<a href="/recipes/tags/diff/">diff</a>, <a href="/recipes/tags/lcs/">lcs</a>). </p> <p>Script to find the longest common subsequence (LCS) of 3+ strings using dynamic programming.</p> Guess a number 2 (the computer attempts to guess your number) (Python) 2014-11-15T12:11:16-08:00Benoithttp://code.activestate.com/recipes/users/4191135/http://code.activestate.com/recipes/578963-guess-a-number-2-the-computer-attempts-to-guess-yo/ <p style="color: grey"> Python recipe 578963 by <a href="/recipes/users/4191135/">Benoit</a> (<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/sample/">sample</a>). Revision 2. </p> <p>Guess a number was part 1 was Human trying to guess a random number between 1 and 100 in 10 tries. This time the computer is trying to guess. Simple program for beginner to learn very simple formula as a start to an algorithm for AI. And conditional choice. The program is not perfect, as it will not force a victory when the computer guessed right, neither it can control if you cheated or not. But this is not the goal here. </p> Simple Cipher (Python) 2014-07-22T16:23:39-07:00Stephen Driffillhttp://code.activestate.com/recipes/users/4190452/http://code.activestate.com/recipes/578912-simple-cipher/ <p style="color: grey"> Python recipe 578912 by <a href="/recipes/users/4190452/">Stephen Driffill</a> (<a href="/recipes/tags/cipher/">cipher</a>, <a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/cryptography/">cryptography</a>). </p> <p>Takes a string and encodes it using a simple cipher.</p> Create on-the-fly class adapters with functools.partial (Python) 2014-05-29T18:48:28-07:00Christoph Schuelerhttp://code.activestate.com/recipes/users/4174094/http://code.activestate.com/recipes/578884-create-on-the-fly-class-adapters-with-functoolspar/ <p style="color: grey"> Python recipe 578884 by <a href="/recipes/users/4174094/">Christoph Schueler</a> (<a href="/recipes/tags/adapter/">adapter</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/partial/">partial</a>, <a href="/recipes/tags/pattern/">pattern</a>). </p> <p>functools.partial could not only applied to functions it also works with classes. This opens some interesting perspectives, like on-the-fly creation of class adapters, as the following code illustrates.</p> Coordinates of numpy array from index and shape (Python) 2012-10-23T19:51:55-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578302-coordinates-of-numpy-array-from-index-and-shape/ <p style="color: grey"> Python recipe 578302 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/coordinates/">coordinates</a>, <a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>returns the coordinates of a numpy array given the index and the shape. A first_index_et function is given as example code</p> Singleton (parameter based) (Python) 2012-04-18T06:13:25-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578103-singleton-parameter-based/ <p style="color: grey"> Python recipe 578103 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/parameters/">parameters</a>, <a href="/recipes/tags/singleton/">singleton</a>). Revision 3. </p> <p><strong>Why?</strong></p> <ul> <li>There are many recipes but nearly all cover the global singleton only.</li> <li>I need a singleton which can handle things in a specific context (environment).</li> </ul> <p><strong>The final singleton is a combination of following two recipes</strong>:</p> <ul> <li><a href="http://www.python.org/dev/peps/pep-0318/#examples" rel="nofollow">http://www.python.org/dev/peps/pep-0318/#examples</a></li> <li><a href="http://stackoverflow.com/a/9489387" rel="nofollow">http://stackoverflow.com/a/9489387</a></li> </ul> <p><strong>The basic advantages</strong>:</p> <ul> <li>Hiding the singleton code by a simple decorator</li> <li>Flexible, because you can define fully global singletons or parameter based singletons.</li> </ul> <p><strong>Latest changes</strong>:</p> <ul> <li>Although a function/method does not have parameters you can call it with parameters <strong>args</strong> and <strong>kwargs</strong> as you now see in the <strong>getInstance</strong> function.</li> <li>...</li> </ul> Simple curses based MySQL 'top' (Python) 2011-11-02T20:28:48-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/577936-simple-curses-based-mysql-top/ <p style="color: grey"> Python recipe 577936 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/mysql/">mysql</a>, <a href="/recipes/tags/time/">time</a>). Revision 3. </p> <p>This is little more than a modification of my previous recipe, however, I found it useful so I thought I would post it in the hopes that someone else would as well. There is color (BOLD white really) designation for 'Query' states vs others like 'Sleep'. </p> Determine credit card type by number (Python) 2011-08-01T13:41:09-07:00Paul Saparovhttp://code.activestate.com/recipes/users/4178836/http://code.activestate.com/recipes/577815-determine-credit-card-type-by-number/ <p style="color: grey"> Python recipe 577815 by <a href="/recipes/users/4178836/">Paul Saparov</a> (<a href="/recipes/tags/card/">card</a>, <a href="/recipes/tags/credit/">credit</a>). </p> <p>Simple function that determines cc type by number</p> An Introduction to Deferred Default Arguments (Python) 2011-08-12T23:08:30-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577789-an-introduction-to-deferred-default-arguments/ <p style="color: grey"> Python recipe 577789 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>). Revision 2. </p> <p>If you have a function that calls another, passing through an argument, you likely want the default argument of your function to match that of the called function.</p> <p>Deferred default arguments is what I call the technique of using a sentinel for the default argument of your function and having the called function translate the sentinel into its own default argument.</p> <p>You'll find a more thorough treatment after the recipe.</p> HTTPS httplib Client Connection with Certificate Validation (Python) 2011-01-18T18:30:45-08:00Marcelo Fernándezhttp://code.activestate.com/recipes/users/4173551/http://code.activestate.com/recipes/577548-https-httplib-client-connection-with-certificate-v/ <p style="color: grey"> Python recipe 577548 by <a href="/recipes/users/4173551/">Marcelo Fernández</a> (<a href="/recipes/tags/certificate/">certificate</a>, <a href="/recipes/tags/client/">client</a>, <a href="/recipes/tags/client_server/">client_server</a>, <a href="/recipes/tags/httplib/">httplib</a>, <a href="/recipes/tags/https/">https</a>, <a href="/recipes/tags/networking/">networking</a>, <a href="/recipes/tags/ssl/">ssl</a>, <a href="/recipes/tags/validation/">validation</a>). </p> <p>Despite httplib.HTTPSConnection lets the programmer specify the client's pair of certificates, it doesn't force the underlying SSL library to check the server certificate against the client keys (from the client point of view).</p> <p>This class allows to force this check, to ensure the python client is connecting to the right server.</p> Long period random number generator (Python) 2010-12-02T05:19:25-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576707-long-period-random-number-generator/ <p style="color: grey"> Python recipe 576707 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/random/">random</a>). Revision 13. </p> <p>Implements a complementary-multiply-with-carry psuedo-random-number-generator. Period is 3636507990 * 2 ** 43487 (approximately 10 ** 13101).</p> A flexible state machine class (Python) 2011-05-18T15:17:02-07:00Mike Sweeneyhttp://code.activestate.com/recipes/users/4177990/http://code.activestate.com/recipes/577701-a-flexible-state-machine-class/ <p style="color: grey"> Python recipe 577701 by <a href="/recipes/users/4177990/">Mike Sweeney</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/state_machine/">state_machine</a>). Revision 2. </p> <p>The operation of the state machine is defined by transitions. The transitions control what value is returned and which new state to switch to, given an "event" input when in a certain current "state". State machines have many applications such as games, process controls, and language parsing.</p> NBD server in python (Python) 2011-02-08T20:59:23-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/577569-nbd-server-in-python/ <p style="color: grey"> Python recipe 577569 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/analyze/">analyze</a>, <a href="/recipes/tags/block/">block</a>, <a href="/recipes/tags/device/">device</a>, <a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/io/">io</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/nbd/">nbd</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Linux Network Block Device server in Python</p> <p>This is a simplified version based on Kragen Sitaker's <a href="http://lists.canonical.org/pipermail/kragen-hacks/2004-May/000397.html" rel="nofollow">http://lists.canonical.org/pipermail/kragen-hacks/2004-May/000397.html</a></p> <p>Close is never actually called, at least not on the same connection -- linux C nbd-client -d seems to stall, perhaps it tries to open another socket?</p> <p>This code doesn't check for error conditions, failed reads/writes, past end of disk, etc.</p> <p>It prints io requests, you can analyze filesystem and user program io patterns.</p> Maclaurin's_series_exponential(x) (Python) 2010-07-07T12:52:28-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577293-maclaurins_series_exponentialx/ <p style="color: grey"> Python recipe 577293 by <a href="/recipes/users/4155345/">Fouad Teniou</a> (<a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p> <p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices .</p> <p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p> <p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series. </p> <p>The natural exponential function e^x has a constant base e and a variable x and Maclaurin's series to approximate the natural exponential converge on the interval ]-inf , + inf [.</p> Flatten a list (Python) 2010-06-18T03:45:20-07:00Giannis Fysakishttp://code.activestate.com/recipes/users/4174072/http://code.activestate.com/recipes/577250-flatten-a-list/ <p style="color: grey"> Python recipe 577250 by <a href="/recipes/users/4174072/">Giannis Fysakis</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>). Revision 3. </p> <p>Flatten a list </p> Solving the Black-Scholes PDE with laplace inversion:Revised (Python) 2010-04-06T13:26:02-07:00Fernando Nieuwveldthttp://code.activestate.com/recipes/users/4172088/http://code.activestate.com/recipes/577142-solving-the-black-scholes-pde-with-laplace-inversi/ <p style="color: grey"> Python recipe 577142 by <a href="/recipes/users/4172088/">Fernando Nieuwveldt</a> (<a href="/recipes/tags/black/">black</a>, <a href="/recipes/tags/laplace/">laplace</a>, <a href="/recipes/tags/scholes/">scholes</a>, <a href="/recipes/tags/talbot/">talbot</a>). Revision 2. </p> <p>I originally posted this code in <a href="http://code.activestate.com/recipes/577132/">Recipe 577132</a> and this is a repost of that recipe with corrections since there was an error in the original recipe. Added here is an error analysis to show the effectiveness of the Laplace inversion method for pricing European options. One can test the accuracy of this method to the finite difference schemes. The laplace transform of Black-Scholes PDE was taken and the result was inverted using the Talbot method for numerical inversion. For a derivation of the laplace transform of the Black-Scholes PDE, see for instance <a href="http://www.wilmott.com/pdfs/020310_skachkov.pdf." rel="nofollow">www.wilmott.com/pdfs/020310_skachkov.pdf.</a></p> Random URL (JavaScript) 2010-03-25T16:07:54-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577143-random-url/ <p style="color: grey"> JavaScript recipe 577143 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/url/">url</a>). </p> <p>Save this code as randomURL.html. Everytime you open the file w/ a browser it will try to visit a random Internet page. Unfortunately most random trials does not take you a browser renderable webpage. I am open to ideas on how to make certain it finds a good webpage everytime.</p> Plasma fractal using Python Image Library (Python) 2010-03-14T21:33:10-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577113-plasma-fractal-using-python-image-library/ <p style="color: grey"> Python recipe 577113 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/math/">math</a>). </p> <p>Plasma fractal using Python Image Library.</p>