Top-rated recipes tagged "meta:requires=random"http://code.activestate.com/recipes/tags/meta:requires=random/top/2013-09-22T21:27:24-07:00ActiveState Code RecipesDecorator for BindingConstants at compile time (Python) 2010-11-16T08:36:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time/ <p style="color: grey"> Python recipe 277940 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 9. </p> <p>Decorator for automatic code optimization. If a global is known at compile time, replace it with a constant. Fold tuples of constants into a single constant. Fold constant attribute lookups into a single constant.</p> Bloom Filter (Python) 2011-06-04T17:44:21-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577684-bloom-filter/ <p style="color: grey"> Python recipe 577684 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/big_table/">big_table</a>, <a href="/recipes/tags/bloom_filter/">bloom_filter</a>, <a href="/recipes/tags/sets/">sets</a>, <a href="/recipes/tags/spelling_checker/">spelling_checker</a>, <a href="/recipes/tags/spell_checker/">spell_checker</a>). Revision 18. </p> <p>Space efficient, probabilistic set membership tester. Has no False Negatives but allows a rare False Positive.</p> Persistent dict with multiple standard file formats (Python) 2011-09-06T20:01:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/ <p style="color: grey"> Python recipe 576642 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/dbm/">dbm</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/persistent/">persistent</a>, <a href="/recipes/tags/shelve/">shelve</a>). Revision 10. </p> <p>dbdict: a dbm based on a dict subclass.</p> <p>On open, loads full file into memory. On close, writes full dict to disk (atomically). Supported output file formats: csv, json, and pickle. Input file format automatically discovered.</p> <p>Usable by the shelve module for fast access.</p> PyDbLite, a small in-memory database engine (Python) 2011-07-18T19:36:04-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/496770-pydblite-a-small-in-memory-database-engine/ <p style="color: grey"> Python recipe 496770 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/database/">database</a>). Revision 4. </p> <p>A small, fast, in-memory database management program</p> <p>The database object supports the iterator protocol, so that requests can be expressed with list comprehensions or generator expressions instead of SQL. The equivalent of :</p> <pre class="prettyprint"><code>cursor.execute("SELECT name FROM table WHERE age=30") rows = cursor.fetchall() </code></pre> <p>is :</p> <pre class="prettyprint"><code>rows = table(age=30) </code></pre> <p>The module stores data in a cPickled file. Records are indexed by a unique record identifier, that can be used for direct access. Since operations are processed in memory they are extremely fast, nearly as fast as SQLite in the few tests I made, and MUCH faster than other pure-Python modules such as Gadfly or KirbyBase. An index can be created on a field to even speed up selections</p> <p>Concurrency control is supported by a version number set for each record</p> <p>Complete documentation is <a href="http://www.pydblite.net">here</a></p> Binary floating point summation accurate to full precision (Python) 2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/ <p style="color: grey"> Python recipe 393090 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p> Language detection using character trigrams (Python) 2004-11-07T01:08:26-08:00Douglas Bagnallhttp://code.activestate.com/recipes/users/1629020/http://code.activestate.com/recipes/326576-language-detection-using-character-trigrams/ <p style="color: grey"> Python recipe 326576 by <a href="/recipes/users/1629020/">Douglas Bagnall</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>The Trigram class can be used to compare blocks of text based on their local structure, which is a good indicator of the language used. It could also be used within a language to discover and compare the characteristic footprints of various registers or authors. As all n-gram implementations should, it has a method to make up nonsense words.</p> Public Key Encryption (RSA) (Python) 2012-05-12T23:34:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577737-public-key-encryption-rsa/ <p style="color: grey"> Python recipe 577737 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/inverse/">inverse</a>, <a href="/recipes/tags/multiplicative/">multiplicative</a>, <a href="/recipes/tags/primality_testing/">primality_testing</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/publickey/">publickey</a>, <a href="/recipes/tags/rsa/">rsa</a>). Revision 2. </p> <p>Simple code to create and use public/private keypairs. Accompanied by a rudimentary encoder.</p> Finding the convex hull of a set of 2D points (Python) 2001-08-17T00:50:23-07:00Dinu Ghermanhttp://code.activestate.com/recipes/users/124101/http://code.activestate.com/recipes/66527-finding-the-convex-hull-of-a-set-of-2d-points/ <p style="color: grey"> Python recipe 66527 by <a href="/recipes/users/124101/">Dinu Gherman</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>This simple code calculates the convex hull of a set of 2D points and generates EPS files to visualise them. The algorithm was taken from a textbook on Computional Geometry.</p> Left-handed password generator (Python) 2011-10-31T12:51:14-07:00Alexhttp://code.activestate.com/recipes/users/4179771/http://code.activestate.com/recipes/577930-left-handed-password-generator/ <p style="color: grey"> Python recipe 577930 by <a href="/recipes/users/4179771/">Alex</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/lefthand/">lefthand</a>, <a href="/recipes/tags/lefthanded/">lefthanded</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/passwords/">passwords</a>). Revision 2. </p> <p>Generate passwords that can easily be typed with only the left hand, very simple script</p> Efficient Running Median using an Indexable Skiplist (Python) 2010-02-07T14:40:03-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576930-efficient-running-median-using-an-indexable-skipli/ <p style="color: grey"> Python recipe 576930 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/advanced_data_structure/">advanced_data_structure</a>, <a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/new_algorithm/">new_algorithm</a>, <a href="/recipes/tags/skiplist/">skiplist</a>, <a href="/recipes/tags/sliding_window/">sliding_window</a>). Revision 10. </p> <p>Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Also gives fast indexed access to value. Running time per median update is proportional to the log of the window size.</p> Friendly Readable ID Strings (Python) 2007-08-10T14:27:52-07:00Robin Parmarhttp://code.activestate.com/recipes/users/99666/http://code.activestate.com/recipes/526619-friendly-readable-id-strings/ <p style="color: grey"> Python recipe 526619 by <a href="/recipes/users/99666/">Robin Parmar</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </p> <p>One often needs unique ID strings to tag processes, threads, files or anything else that might be created in quantity. Traditionally these are created based on PIDs or similar system values. But it is not easy to visually recognise these strings, which makes debugging more difficult than it need be. This recipe creates readable and pronounceable ID strings that are much easier to work with.</p> Chomsky random text generator (Python) 2005-09-13T18:46:01-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/440546-chomsky-random-text-generator/ <p style="color: grey"> Python recipe 440546 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/text/">text</a>). Revision 2. </p> <p>Creates believable Chomsky style obfuscated prose.</p> My first application server (Python) 2009-02-23T11:53:57-08:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/392879-my-first-application-server/ <p style="color: grey"> Python recipe 392879 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/web/">web</a>). Revision 8. </p> <p>For those who want to start dynamic web programming, but don't know what to choose among the many Python web frameworks, this program might be a good starting point</p> <p>ScriptServer is a minimalist application server, handling both GET and POST requests, including multipart/form-data for file uploads, HTTP redirections, and with an in-memory session management. It can run Python scripts and template files using the standard string substitution format</p> <p>The scripts are run in the same process as the server, avoiding the CGI overhead. The environment variables are provided in the namespace where the script runs</p> <p>To start the server, run </p> <pre class="prettyprint"><code>python ScriptServer.py </code></pre> <p>In your web browser, enter <a href="http://localhost" rel="nofollow">http://localhost</a>, this will show you a listing of the directory. Add the scripts in the same directory as ScriptServer</p> Encode multipart form data for uploading files via POST (Python) 2013-09-22T21:27:24-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578668-encode-multipart-form-data-for-uploading-files-via/ <p style="color: grey"> Python recipe 578668 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/form/">form</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/multipart/">multipart</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>This function lets you encode form fields <em>and</em> files in multipart/form-data format for uploading files via HTTP POST.</p> Simple Back-propagation Neural Network in Python source code (Python) 2012-05-30T17:09:49-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578148-simple-back-propagation-neural-network-in-python-s/ <p style="color: grey"> Python recipe 578148 by <a href="/recipes/users/4182015/">David Adler</a> (<a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/back_propagation/">back_propagation</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/neural_network/">neural_network</a>, <a href="/recipes/tags/propagation/">propagation</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a slightly different version of this <a href="http://arctrix.com/nas/python/bpnn.py" rel="nofollow">http://arctrix.com/nas/python/bpnn.py</a></p> A simple Matrix class (Python) 2012-05-14T13:34:31-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/578131-a-simple-matrix-class/ <p style="color: grey"> Python recipe 578131 by <a href="/recipes/users/4169530/">Anand B Pillai</a> (<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>). Revision 3. </p> <p>A simple class in Python representing a Matrix with basic operations, operator overloading and class factory methods to make Matrices from different sources.</p> "More Sex is Safer Sex" Paradox (Python) 2010-09-22T02:56:37-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/577403-more-sex-is-safer-sex-paradox/ <p style="color: grey"> Python recipe 577403 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/simulation/">simulation</a>). </p> <p>In his book, "More Sex Is Safer Sex: The Unconventional Wisdom of Economics, " Steven Landsburg argues that if sexual conservatives took more sexual partners it would improve everyone's chances of finding low-risk partners, thereby reducing the spread of STDs for all. </p> <p>See <a href="http://www.nytimes.com/2007/07/08/books/chapters/0708-1st-land.html?_r=1&amp;pagewanted=print" rel="nofollow">http://www.nytimes.com/2007/07/08/books/chapters/0708-1st-land.html?_r=1&amp;pagewanted=print</a> .</p> <p>This recipe attempts to simulate the scenario which Landsburg describes and test his claim that "More sex is safer sex."</p> Decorator for BindingConstants at compile time (Python) 2009-09-15T00:34:37-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576904-decorator-for-bindingconstants-at-compile-time/ <p style="color: grey"> Python recipe 576904 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/optimisation/">optimisation</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/programs/">programs</a>). </p> <p>Decorator for automatic code optimization. If a global is known at compile time, replace it with a constant. Fold tuples of constants into a single constant. Fold constant attribute lookups into a single constant.</p> <p>This is only an update of <a href="http://code.activestate.com/recipes/277940/">Recipe 277940</a>, making it compatible with Python 3. All credit must go to the original author, Raymond Hettinger.</p> Miller-Rabin Primality Test (Python) 2007-04-07T16:11:50-07:00Dite Ditehttp://code.activestate.com/recipes/users/4047667/http://code.activestate.com/recipes/511459-miller-rabin-primality-test/ <p style="color: grey"> Python recipe 511459 by <a href="/recipes/users/4047667/">Dite Dite</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Very simple implementation of Miller-Rabin Primality Test with Tkinter</p> Generate a human readable 'random' password (nicepass.py) (Python) 2005-04-26T07:13:28-07:00Pradeep Kishore Gowdahttp://code.activestate.com/recipes/users/2230395/http://code.activestate.com/recipes/410076-generate-a-human-readable-random-password-nicepass/ <p style="color: grey"> Python recipe 410076 by <a href="/recipes/users/2230395/">Pradeep Kishore Gowda</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 4. </p> <p>Password will be generated in the form 'word'+digits+'word' eg.,nice137pace instead of a difficult-to-remember - K8Yn9muL</p>