Popular recipes tagged "meta:loc=85"http://code.activestate.com/recipes/tags/meta:loc=85/2014-06-13T14:29:36-07:00ActiveState Code RecipesSimple model to predict UK Interest Rates (Python)
2014-06-13T14:29:36-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578891-simple-model-to-predict-uk-interest-rates/
<p style="color: grey">
Python
recipe 578891
by <a href="/recipes/users/4166679/">alexander baker</a>
.
</p>
<p>A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model. #
Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with.
The dynamics of the Vasicek model are describe below.</p>
<p>In this model, the parameters are constants, and the random motion is generated by the Q measure Brownian motion .
An important property of the Vasicek model is that the interest rate is mean reverting to , and the tendency to revert is controlled by .
Also, this process is a diffusion process, hence Markovian, which will lead to some nice closed form formulas.
Finally, the future value of the interest rate is normally distributed with the distribution .</p>
Levenshtein, my love (Python)
2014-01-15T09:14:30-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578810-levenshtein-my-love/
<p style="color: grey">
Python
recipe 578810
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p><em>be kind and comment, especially if you downvote</em></p>
<p><strong>levenshtein_distance()</strong> is an implementation of the iterative algorithm for the levenshtein distance (cf. <a href="http://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix%29" rel="nofollow">http://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix)</a></p>
<p><strong>levenshtein_sequence()</strong> is an attempt to retrieve one of the levenshtein paths (the one that give priority to substitution, deletion, insertion, in this order). The result is a list of tuples made of:</p>
<ol>
<li>the operation ( <code>=</code>, <code>-</code>, <code>+</code>, <code>*</code> for respectively keep, delete, insert, substitute)</li>
<li>the coordinate in the first</li>
<li>and in the second string.</li>
</ol>
<pre class="prettyprint"><code>>>> levenshtein_sequence('saturday', 'sunday')
[('=', 0, 0), ('-', 1, 0), ('-', 2, 0), ('=', 3, 1), ('*', 4, 2), ('=', 5, 3), ('=', 6, 4), ('=', 7, 5)]
>>> levenshtein_sequence('kitten', 'sitting')
[('*', 0, 0), ('=', 1, 1), ('=', 2, 2), ('=', 3, 3), ('*', 4, 4), ('=', 5, 5), ('+', 5, 6)]
</code></pre>
<p>This code is part of foreplays, in a plan I have to improve difflib with alternative SequenceMatchers \o/</p>
<p><em>/!\ tab indented, as usual.</em></p>
Credit card number check (Python)
2013-08-12T14:11:25-07:00Matt Joneshttp://code.activestate.com/recipes/users/4182764/http://code.activestate.com/recipes/578202-credit-card-number-check/
<p style="color: grey">
Python
recipe 578202
by <a href="/recipes/users/4182764/">Matt Jones</a>
(<a href="/recipes/tags/validation/">validation</a>).
Revision 2.
</p>
<p>Pass in the credit card number and the card type and get back a result with an explanation.</p>
Delayed Decorator (Python)
2011-12-20T06:37:15-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/577993-delayed-decorator/
<p style="color: grey">
Python
recipe 577993
by <a href="/recipes/users/4180313/">Peter Donis</a>
(<a href="/recipes/tags/decorators/">decorators</a>).
</p>
<p>This is a decorator wrapper class to delay decorating the base function until it is actually invoked. This can be useful when decorating ordinary functions if some decorator parameters depend on data that is only known at function invocation. It can also be used (and was written) to ensure that a decorated method of a class gets decorated once per instance instead of once per class; the use case that prompted this was the need to memoize a generator (see the <a href="http://code.activestate.com/recipes/577992-memoize-generator/">Memoized Generator</a> recipe), but the implementation is general.</p>
xml to python dictionary and back (Python)
2012-10-24T00:44:30-07:00kris kvilekvalhttp://code.activestate.com/recipes/users/4178118/http://code.activestate.com/recipes/577722-xml-to-python-dictionary-and-back/
<p style="color: grey">
Python
recipe 577722
by <a href="/recipes/users/4178118/">kris kvilekval</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/mapping/">mapping</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 2.
</p>
<p>Simple mapping of XML to python dictionary based on Perl XML::Simple</p>
Run asynchronous tasks using coroutines (Python)
2010-08-06T16:16:20-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577129-run-asynchronous-tasks-using-coroutines/
<p style="color: grey">
Python
recipe 577129
by <a href="/recipes/users/4173270/">Arnau Sanchez</a>
(<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/gobject/">gobject</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pygtk/">pygtk</a>).
Revision 20.
</p>
<p>This recipe shows a simple, transparent (and hopefully pythonic) way of running asynchronous tasks when writing a event-driven application (i.e. GUI). The aim is to allow a programmer to write time-consuming functions (usually IO-bound, but not only) with sequential-looking code, instead of scattering the logic over a bunch of callbacks. We will take advantage of the coroutines introduced in Python 2.5 (see <a href="http://www.python.org/dev/peps/pep-0342" rel="nofollow">http://www.python.org/dev/peps/pep-0342</a>). </p>
<p>The goal: wouldn't it be great if we could write something like this?</p>
<pre class="prettyprint"><code>def myjob(entry, arg1, arg2, arg3):
result1 = function_that_takes_eons_to_complete(arg1, arg2)
result2 = another_function_that_downloads_a_big_really_big_file(result1, arg3)
entry.set_text("The result is: %d" % result2)
def on_start_button___clicked(button, entry):
myjob(entry, 1, 2, 3)
...
gtk.main()
</code></pre>
<p>Indeed, but we can't! The GUI will hang until the job is done and the user will be rightfully angry. Coroutines to the rescue: the absolute minimal change we can make to this code is transforming <em>myjob</em> into a coroutine and yield every time we do blocking stuff:</p>
<pre class="prettyprint"><code>def myjob(entry, arg1, arg2, arg3):
result1 = yield some_task(arg1, arg2)
result2 = yield some_other_task(result1, arg3)
entry.set_text("The result is: %d" % result2)
def on_start__clicked(button, entry):
start_job(myjob(entry, 1, 2, 3))
</code></pre>
<p><em>some_task</em> and <em>some_other_task</em> are here the asynchronous implementation of the sequential tasks used in the first fragment, and <em>start_job</em> the wrapper around the coroutine. Note that we still have to implement non-blocking versions of the tasks, but they are usually pretty generic (wait some time, download a file, ...) and can be re-used. If you happen to have a CPU-bound function or even a IO-bound code you cannot split (<em>urllib2</em> anyone?), you can always use a generic threaded task (granted, the whole point of using co-routines should be avoiding threads, but there is no alternative here).</p>
<p>At the end, all the plumbing we need to make it work is just 1 function: <em>start_job</em> (wrapper around the job to manage the flow of the coroutine). The rest of the code -two asynchronous tasks (<em>sleep_task</em>, <em>threaded_task</em>) and a demo app- are shown solely as an example.</p>
Bible Verse Quiz - diff.py (Python)
2010-02-12T04:22:40-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577051-bible-verse-quiz-diffpy/
<p style="color: grey">
Python
recipe 577051
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/bible/">bible</a>, <a href="/recipes/tags/quiz/">quiz</a>, <a href="/recipes/tags/verse/">verse</a>, <a href="/recipes/tags/_versematch_/">_versematch_</a>).
Revision 2.
</p>
<p>Compute differences and similarities between a pair of sequences.</p>
<p>After finding the "difflib.SequenceMatcher" class unsuitable, this module
was written and re-written several times into the polished version below.</p>
Poor man's mgrid (Python)
2009-01-21T15:37:13-08:00David Lamberthttp://code.activestate.com/recipes/users/4167420/http://code.activestate.com/recipes/576625-poor-mans-mgrid/
<p style="color: grey">
Python
recipe 576625
by <a href="/recipes/users/4167420/">David Lambert</a>
(<a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/index_tricks/">index_tricks</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/mgrid/">mgrid</a>, <a href="/recipes/tags/numerical/">numerical</a>, <a href="/recipes/tags/numerical_methods/">numerical_methods</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/scipy/">scipy</a>, <a href="/recipes/tags/trick/">trick</a>).
</p>
<p>Python 3 code.
scipy.mgrid is a useful!
This short implementation comes without installing numpy.</p>
Prime look up program (Python)
2008-07-26T19:45:12-07:00Phil Huffmanhttp://code.activestate.com/recipes/users/4165783/http://code.activestate.com/recipes/576380-prime-look-up-program/
<p style="color: grey">
Python
recipe 576380
by <a href="/recipes/users/4165783/">Phil Huffman</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/primes/">primes</a>).
</p>
<p>Finds the nth prime without using a sieve algorithm. </p>
Print Tkinter canvas without a postscript printer (Python)
2008-04-28T21:32:47-07:00jesse jameshttp://code.activestate.com/recipes/users/4145727/http://code.activestate.com/recipes/572175-print-tkinter-canvas-without-a-postscript-printer/
<p style="color: grey">
Python
recipe 572175
by <a href="/recipes/users/4145727/">jesse james</a>
.
Revision 2.
</p>
<p>My printer will not accept postscript and all i needed to print was canvas rectangles. So here is something i put together... hope it helps someone else.
-works for polys, recs, and lines-</p>
Stop Application. (Tcl)
2005-10-18T23:22:28-07:00Patrick Finneganhttp://code.activestate.com/recipes/users/1220635/http://code.activestate.com/recipes/442458-stop-application/
<p style="color: grey">
Tcl
recipe 442458
by <a href="/recipes/users/1220635/">Patrick Finnegan</a>
(<a href="/recipes/tags/websphere/">websphere</a>).
</p>
<p>Stop Application.</p>
Copying and Pasting Directories (and Files) (Python)
2005-10-02T18:54:25-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/440626-copying-and-pasting-directories-and-files/
<p style="color: grey">
Python
recipe 440626
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/files/">files</a>).
</p>
<p>Here are a few basic fuctions that allow someone to copy a directory (along with its files and sub-directories) and paste it somewhere else. They ignore links and anything else that is not a directory or a file. The data returned from copy_dir() is in a format that paste_dir() can understand. A design note: what you get back from copy_dir() may be different whether or not you have a path separator at the end of the path; paste_dir() makes use of that feature (whether or not you want the first folder to be created). The fuctions can easily be modified to not include sub-directories in the returned package, and errors caused by accessing to files or directories can also be ignored with some simple editting. Also, note the asserts in the functions; these functions are designed to crash hard so that the calling code has to take care of the errors.</p>
Simple XOR keyword Encryption (Python)
2004-02-03T03:26:47-08:00Simon Peveretthttp://code.activestate.com/recipes/users/1410328/http://code.activestate.com/recipes/266586-simple-xor-keyword-encryption/
<p style="color: grey">
Python
recipe 266586
by <a href="/recipes/users/1410328/">Simon Peverett</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>A very simple keyword encryption algorithm. Produces an encrypted string the same size as the orginal. Not very strong encryption but good enough for lightweight stuff. Works on bytes so could be used for binary streams if converted to and from strings - maybe!</p>
Indexing into XML (Python)
2002-10-08T05:38:27-07:00Dave Pawsonhttp://code.activestate.com/recipes/users/127209/http://code.activestate.com/recipes/155582-indexing-into-xml/
<p style="color: grey">
Python
recipe 155582
by <a href="/recipes/users/127209/">Dave Pawson</a>
.
</p>
<p>Build an index into a directory of xml files.
Search the index. Based around the indexing code
from Alex Martelli, linux magazine, July 2002.</p>
Pinhole (Python)
2002-02-15T15:39:11-08:00Simon Fosterhttp://code.activestate.com/recipes/users/207220/http://code.activestate.com/recipes/114642-pinhole/
<p style="color: grey">
Python
recipe 114642
by <a href="/recipes/users/207220/">Simon Foster</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>Pinhole is a simple network utility that forwards a port
to the host specified. The optional newport parameter
may be used to redirect to a different port on the target
host. Uses threads gratuitously.</p>
Finding differences in patches on Solaris (Python)
2001-09-15T11:35:12-07:00Joe Frosthttp://code.activestate.com/recipes/users/130985/http://code.activestate.com/recipes/68417-finding-differences-in-patches-on-solaris/
<p style="color: grey">
Python
recipe 68417
by <a href="/recipes/users/130985/">Joe Frost</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>Uses ssh to gather patch information on two solaris boxes. It prints out the differences in patches of the two computers.</p>